2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __ONERT_UTIL_INDEX_H__
18 #define __ONERT_UTIL_INDEX_H__
31 * @brief A wrapper class for unsigned integral Index
32 * NOTE : Max value of the underlying type is used as the invalid value
34 * @tparam T Underlying type. Must be unsigned integral type otherwise its behavior is undefined.
35 * @tparam DummyTag Dummy type to distinguish types with a same underlying type. Using an opaque
36 * type is recommended.
38 template <typename T, typename DummyTag> class Index
41 static const T UNDEFINED = std::numeric_limits<T>::max();
45 * @brief Construct a new Index object
47 explicit Index(void) : _index{UNDEFINED} {}
49 * @brief Construct a new Index object with a value in the underlying type
51 * @param o Value in the underlying type
53 explicit Index(const T o) : _index{o} {}
55 * @brief Copy Constructor
57 * @param o Object to be copied
59 Index(const Index &o) = default;
62 * @brief Assign a value in the underlying time
64 * @param o Value in the underlying type
65 * @return Index& Reference of this pointer
67 Index &operator=(const T o)
74 * @brief Copy assignment operator
76 * @param o Object to be copied
77 * @return Index& Reference of this pointer
79 Index &operator=(const Index &o) = default;
82 * @brief Equality operator
84 * @param o The other value in the underlying type to compare
85 * @return true if underlying value is the same, false otherwise
87 bool operator==(T o) const { return _index == o; }
89 * @brief Equality operator
91 * @param o The other object to compare
92 * @return true if underlying value is the same, false otherwise
94 bool operator==(const Index &o) const { return _index == o._index; }
96 * @brief Inquality operator
98 * @param o The other value in the underlying type to compare
99 * @return true if underlying value is different, false otherwise
101 bool operator!=(T o) const { return !(*this == o); }
103 * @brief Inquality operator
105 * @param o The other object to compare
106 * @return true if underlying value is different, false otherwise
108 bool operator!=(const Index &o) const { return !(*this == o); }
111 * @brief Post increment operator
113 * @return Index Index before increment
115 Index operator++(int)
123 * @brief Check whether the value is valid or not
125 * @return true if valid, false otherwise
127 bool valid() const { return _index != UNDEFINED; }
129 * @brief Check whether the value is undefined
131 * @return true if undefined, false otherwise
133 bool undefined() const { return _index == UNDEFINED; }
135 * @brief Return underlying value
137 * @return T Underlying value
139 T value() const { return _index; }
141 friend std::ostream &operator<<(std::ostream &o, const Index &t)
144 return o << std::string("undefined");
146 return o << t.value();
159 template <typename T, typename Tag> struct hash<::onert::util::Index<T, Tag>>
161 size_t operator()(const ::onert::util::Index<T, Tag> &index) const noexcept
163 return hash<T>()(index.value());
169 #endif // __ONERT_UTIL_INDEX_H__