1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef META_INTERFACE_MODEL_IDATA_MODEL_H
17 #define META_INTERFACE_MODEL_IDATA_MODEL_H
18 
19 #include <meta/base/namespace.h>
20 #include <meta/base/types.h>
21 #include <meta/interface/interface_macros.h>
22 #include <meta/interface/intf_metadata.h>
23 #include <meta/interface/model/data_model_index.h>
24 #include <meta/interface/simple_event.h>
25 
26 META_BEGIN_NAMESPACE()
27 
28 struct IOnDataAddedInfo {
29     constexpr static BASE_NS::Uid UID { "9de720f7-8b7f-4b64-92ae-c0a3d9a13807" };
30     constexpr static char const* NAME { "OnDataAdded" };
31 };
32 
33 /**
34  * @brief Event which is triggered when data is added to the model.
35  * Note: One has to connect directly (no posting to other thread or so) to these events for the indices to be valid.
36  * @param base Base index of the added data.
37  * 1-D: the base is simply the start index where data was added and count of data entities added.
38  * N-D: the base can point to any dimension and identify count entities in that dimension.
39  *      Example: Two dimensions, let first dimension be row and second column.
40  *               Let 2dIndex(y, x) mean DataModelIndex(y, &col), where DataModelIndex col(x);
41  *               1. DataModelIndex(0) with count 1 means row was added to beginning of the model.
42  *               2. 2dIndex(0, 0) with count 5 means 5 columns were added to first row.
43  */
44 using IOnDataAdded = META_NS::SimpleEvent<IOnDataAddedInfo, void(DataModelIndex base, size_t count)>;
45 
46 struct IOnDataRemovedInfo {
47     constexpr static BASE_NS::Uid UID { "3440f5d6-b874-40e6-999c-eee3a4049b94" };
48     constexpr static char const *NAME { "OnDataRemoved" };
49 };
50 
51 /**
52  * @brief Event which is triggered when data is removed from the model.
53  * See above how indices work.
54  */
55 using IOnDataRemoved = META_NS::SimpleEvent<IOnDataRemovedInfo, void(DataModelIndex base, size_t count)>;
56 
57 struct IOnDataMovedInfo {
58     constexpr static BASE_NS::Uid UID { "7407dcc5-c903-4ceb-a337-23a1ff6f1d76" };
59     constexpr static char const *NAME { "OnDataMoved" };
60 };
61 
62 /**
63  * @brief Event which is triggered when data is removed from the model.
64  * See above how indices work.
65  */
66 using IOnDataMoved = META_NS::SimpleEvent<IOnDataMovedInfo, void(DataModelIndex from, size_t count, DataModelIndex to)>;
67 
68 /**
69  * @brief Generic data model which can be used to query data per index.
70  */
71 class IDataModel : public CORE_NS::IInterface {
72     META_INTERFACE(CORE_NS::IInterface, IDataModel, "4811e11d-39bb-46d7-8f6e-b70f43a7a9ff")
73 public:
74     /**
75      * @brief Get data at given index, null if no such index exists.
76      */
77     virtual IMetadata::ConstPtr GetModelData(const DataModelIndex& index) const = 0;
78     virtual IMetadata::Ptr GetModelData(const DataModelIndex& index) = 0;
79 
80     /**
81      * @brief Get size of given dimension at given index.
82      */
83     virtual size_t GetSize(const DataModelIndex& index) const = 0;
84 
85     /**
86      * @brief Get size of first dimension.
87      */
GetSize()88     size_t GetSize() const
89     {
90         return GetSize(DataModelIndex {});
91     }
92 
93     /**
94      * @brief Event when data is added to the model.
95      */
96     META_EVENT(IOnDataAdded, OnDataAdded)
97     /**
98      * @brief Event when data is removed from the model.
99      */
100     META_EVENT(IOnDataRemoved, OnDataRemoved)
101     /**
102      * @brief Event when data is moved in the model.
103      */
104     META_EVENT(IOnDataMoved, OnDataMoved)
105 };
106 
107 META_END_NAMESPACE()
108 
109 #endif
110