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_DATA_MODEL_INDEX_H
17 #define META_INTERFACE_MODEL_DATA_MODEL_INDEX_H
18 
19 #include <meta/base/meta_types.h>
20 #include <meta/base/namespace.h>
21 #include <meta/base/types.h>
22 
META_BEGIN_NAMESPACE()23 META_BEGIN_NAMESPACE()
24 
25 /**
26  * @brief Multi-dimensional index for Data Models.
27  * These indices are not meant to be stored as such because they can contain pointers to other dimensions,
28  * so either construct new index for each call or make sure all the dimensions are stored.
29  * Example:
30  *     struct Index2D {
31  *         Index2D(size_t x, size_t y) : secondDim_(x), firstDim_(y, &secondDim_) {}
32  *         operator DataModelIndex() const {
33  *             return firstDim_;
34  *         }
35  *         DataModelIndex secondDim_;
36  *         DataModelIndex firstDim_;
37  *     };
38  */
39 class DataModelIndex {
40 public:
41     /**
42      * @brief Default construction creates invalid index.
43      */
44     DataModelIndex() = default;
45     DataModelIndex(size_t index, const DataModelIndex* dim = nullptr) : index_(index), dimension_(dim) {}
46 
47     /**
48      * @brief Returns true if this is valid index.
49      */
50     bool IsValid() const
51     {
52         return index_ != -1;
53     }
54 
55     /**
56      * @brief Get the plain index value for current dimension.
57      */
58     size_t Index() const
59     {
60         return index_;
61     }
62 
63     /**
64      * @brief Get next dimension
65      */
66     DataModelIndex Dimension() const
67     {
68         return dimension_ ? *dimension_ : DataModelIndex {};
69     }
70 
71     bool operator==(const DataModelIndex& other) const
72     {
73         return index_ == other.index_ && dimension_ == other.dimension_;
74     }
75     bool operator!=(const DataModelIndex& other) const
76     {
77         return !(*this == other);
78     }
79 
80     const DataModelIndex* GetDimensionPointer() const
81     {
82         return dimension_;
83     }
84 
85 private:
86     size_t index_ = -1;
87     const DataModelIndex* dimension_ {};
88 };
89 
90 META_END_NAMESPACE()
91 
92 META_TYPE(META_NS::DataModelIndex)
93 
94 #endif
95