Line data Source code
1 : /**
2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #ifndef XENOLITH_RENDERER_MATERIAL2D_BASE_MATERIALDATASOURCE_H_
24 : #define XENOLITH_RENDERER_MATERIAL2D_BASE_MATERIALDATASOURCE_H_
25 :
26 : #include "XLCommon.h"
27 : #include "SPSubscription.h"
28 :
29 : namespace STAPPLER_VERSIONIZED stappler::xenolith::material2d {
30 :
31 : class DataSource : public Subscription {
32 : public:
33 : using ChildsCount = ValueWrapper<size_t, class ChildsCountClassFlag>;
34 :
35 : static Id Self;
36 :
37 : using Id = Subscription::Id;
38 : using BatchCallback = Function<void(Map<Id, Value> &)>;
39 : using BatchSourceCallback = Function<void(const BatchCallback &, Id::Type first, size_t size)>;
40 :
41 : using DataCallback = Function<void(Value &&)>;
42 : using DataSourceCallback = Function<void(const DataCallback &, Id)>;
43 :
44 : using RemoveSourceCallback = Function<bool(Id, const Value &)>;
45 :
46 : virtual ~DataSource();
47 :
48 : template <class T, class... Args>
49 4 : bool init(const T &t, Args&&... args) {
50 4 : auto ret = initValue(t);
51 4 : if (ret) {
52 4 : return init(args...);
53 : }
54 0 : return false;
55 : }
56 :
57 : bool init();
58 :
59 : DataSource * getCategory(size_t n);
60 :
61 : size_t getCount(uint32_t l = 0, bool subcats = false) const;
62 : size_t getSubcatCount() const; // number of subcats
63 : size_t getItemsCount() const; // number of data items (not subcats)
64 : size_t getGlobalCount() const; // number of all data items in cat and subcats
65 :
66 : void setCategoryBounds(Id & first, size_t & count, uint32_t l = 0, bool subcats = false);
67 :
68 : bool getItemData(const DataCallback &, Id index);
69 : bool getItemData(const DataCallback &, Id index, uint32_t l, bool subcats = false);
70 : size_t getSliceData(const BatchCallback &, Id first, size_t count, uint32_t l = 0, bool subcats = false);
71 :
72 : bool removeItem(Id index, const Value &);
73 : bool removeItem(Id index, const Value &, uint32_t l, bool subcats = false);
74 :
75 : std::pair<DataSource *, bool> getItemCategory(Id itemId, uint32_t l, bool subcats = false);
76 :
77 : Id getId() const;
78 :
79 : void setSubCategories(const Vector<Rc<DataSource>> &);
80 : void setSubCategories(Vector<Rc<DataSource>> &&);
81 : const Vector<Rc<DataSource>> &getSubCategories() const;
82 :
83 : void setChildsCount(size_t count);
84 : size_t getChildsCount() const;
85 :
86 : void setData(const Value &);
87 : void setData(Value &&);
88 : const Value &getData() const;
89 :
90 : void clear();
91 : void addSubcategry(DataSource *);
92 :
93 : void setDirty();
94 :
95 : protected:
96 : struct BatchRequest;
97 : struct SliceRequest;
98 : struct Slice;
99 :
100 : void onSlice(std::vector<Slice> &, size_t &first, size_t &count, uint32_t l, bool subcats);
101 :
102 : virtual bool initValue();
103 : virtual bool initValue(const DataSourceCallback &);
104 : virtual bool initValue(const BatchSourceCallback &);
105 : virtual bool initValue(const Id &);
106 : virtual bool initValue(const ChildsCount &);
107 : virtual bool initValue(const Value &);
108 : virtual bool initValue(Value &&);
109 :
110 : virtual void onSliceRequest(const BatchCallback &, Id::Type first, size_t size);
111 :
112 : Vector<Rc<DataSource>> _subCats;
113 :
114 : Id _categoryId;
115 : size_t _count = 0;
116 : size_t _orphanCount = 0;
117 : Value _data;
118 :
119 : DataSourceCallback _sourceCallback = nullptr;
120 : BatchSourceCallback _batchCallback = nullptr;
121 : RemoveSourceCallback _removeCallback = nullptr;
122 : };
123 :
124 : }
125 :
126 : #endif /* XENOLITH_RENDERER_MATERIAL2D_BASE_MATERIALDATASOURCE_H_ */
|