GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
draco_index_type.h
Go to the documentation of this file.
1// Copyright 2016 The Draco Authors.
2//
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// This files provides a basic framework for strongly typed indices that are
16// used within the Draco library. The motivation of using strongly typed indices
17// is to prevent bugs caused by mixing up incompatible indices, such as indexing
18// mesh faces with point indices and vice versa.
19//
20// Usage:
21// Define strongly typed index using macro:
22//
23// DEFINE_NEW_DRACO_INDEX_TYPE(value_type, name)
24//
25// where |value_type| is the data type of the index value (such as int32_t)
26// and |name| is a unique typename of the new index.
27//
28// E.g., we can define new index types as:
29//
30// DEFINE_NEW_DRACO_INDEX_TYPE(int, PointIndex)
31// DEFINE_NEW_DRACO_INDEX_TYPE(int, FaceIndex)
32//
33// The new types can then be used in the similar way as the regular weakly
34// typed indices (such as int32, int64, ...), but they cannot be
35// accidentally misassigned. E.g.:
36//
37// PointIndex point_index(10);
38// FaceIndex face_index;
39// face_index = point_index; // Compile error!
40//
41// One can still cast one type to another explicitly by accessing the index
42// value directly using the .value() method:
43//
44// face_index = FaceIndex(point_index.value()); // Compiles OK.
45//
46// Strongly typed indices support most of the common binary and unary
47// operators and support for additional operators can be added if
48// necessary.
49
50#ifndef DRACO_CORE_DRACO_INDEX_TYPE_H_
51#define DRACO_CORE_DRACO_INDEX_TYPE_H_
52
53#include <ostream>
54
56
57namespace draco {
58
59#define DEFINE_NEW_DRACO_INDEX_TYPE(value_type, name) \
60 struct name##_tag_type_ {}; \
61 typedef IndexType<value_type, name##_tag_type_> name;
62
63template <class ValueTypeT, class TagT>
64class IndexType {
65 public:
67 typedef ValueTypeT ValueType;
68
69 constexpr IndexType() : value_(ValueTypeT()) {}
70 constexpr explicit IndexType(ValueTypeT value) : value_(value) {}
71
72 constexpr ValueTypeT value() const { return value_; }
73
74 constexpr bool operator==(const IndexType &i) const {
75 return value_ == i.value_;
76 }
77 constexpr bool operator==(const ValueTypeT &val) const {
78 return value_ == val;
79 }
80 constexpr bool operator!=(const IndexType &i) const {
81 return value_ != i.value_;
82 }
83 constexpr bool operator!=(const ValueTypeT &val) const {
84 return value_ != val;
85 }
86 constexpr bool operator<(const IndexType &i) const {
87 return value_ < i.value_;
88 }
89 constexpr bool operator<(const ValueTypeT &val) const { return value_ < val; }
90 constexpr bool operator>(const IndexType &i) const {
91 return value_ > i.value_;
92 }
93 constexpr bool operator>(const ValueTypeT &val) const { return value_ > val; }
94 constexpr bool operator>=(const IndexType &i) const {
95 return value_ >= i.value_;
96 }
97 constexpr bool operator>=(const ValueTypeT &val) const {
98 return value_ >= val;
99 }
100
102 ++value_;
103 return *this;
104 }
106 const ThisIndexType ret(value_);
107 ++value_;
108 return ret;
109 }
110
112 --value_;
113 return *this;
114 }
116 const ThisIndexType ret(value_);
117 --value_;
118 return ret;
119 }
120
121 constexpr ThisIndexType operator+(const IndexType &i) const {
122 return ThisIndexType(value_ + i.value_);
123 }
124 constexpr ThisIndexType operator+(const ValueTypeT &val) const {
125 return ThisIndexType(value_ + val);
126 }
127 constexpr ThisIndexType operator-(const IndexType &i) const {
128 return ThisIndexType(value_ - i.value_);
129 }
130 constexpr ThisIndexType operator-(const ValueTypeT &val) const {
131 return ThisIndexType(value_ - val);
132 }
133
135 value_ += i.value_;
136 return *this;
137 }
138 inline ThisIndexType operator+=(const ValueTypeT &val) {
139 value_ += val;
140 return *this;
141 }
143 value_ -= i.value_;
144 return *this;
145 }
146 inline ThisIndexType operator-=(const ValueTypeT &val) {
147 value_ -= val;
148 return *this;
149 }
151 value_ = i.value_;
152 return *this;
153 }
154 inline ThisIndexType &operator=(const ValueTypeT &val) {
155 value_ = val;
156 return *this;
157 }
158
159 private:
160 ValueTypeT value_;
161};
162
163// Stream operator << provided for logging purposes.
164template <class ValueTypeT, class TagT>
165std::ostream &operator<<(std::ostream &os, IndexType<ValueTypeT, TagT> index) {
166 return os << index.value();
167}
168
169} // namespace draco
170
171// Specialize std::hash for the strongly indexed types.
172namespace std {
173
174template <class ValueTypeT, class TagT>
175struct hash<draco::IndexType<ValueTypeT, TagT>> {
177 return static_cast<size_t>(i.value());
178 }
179};
180
181} // namespace std
182
183#endif // DRACO_CORE_DRACO_INDEX_TYPE_H_
Definition draco_index_type.h:64
constexpr bool operator<(const IndexType &i) const
Definition draco_index_type.h:86
ValueTypeT ValueType
Definition draco_index_type.h:67
constexpr ValueTypeT value() const
Definition draco_index_type.h:72
ThisIndexType & operator+=(const IndexType &i)
Definition draco_index_type.h:134
ThisIndexType & operator-=(const IndexType &i)
Definition draco_index_type.h:142
ThisIndexType operator+=(const ValueTypeT &val)
Definition draco_index_type.h:138
constexpr ThisIndexType operator+(const ValueTypeT &val) const
Definition draco_index_type.h:124
ThisIndexType & operator=(const ValueTypeT &val)
Definition draco_index_type.h:154
ThisIndexType operator++(int)
Definition draco_index_type.h:105
IndexType< ValueTypeT, TagT > ThisIndexType
Definition draco_index_type.h:66
constexpr ThisIndexType operator+(const IndexType &i) const
Definition draco_index_type.h:121
ThisIndexType & operator=(const ThisIndexType &i)
Definition draco_index_type.h:150
ThisIndexType & operator++()
Definition draco_index_type.h:101
constexpr bool operator>(const IndexType &i) const
Definition draco_index_type.h:90
constexpr bool operator>=(const ValueTypeT &val) const
Definition draco_index_type.h:97
constexpr ThisIndexType operator-(const IndexType &i) const
Definition draco_index_type.h:127
constexpr bool operator>=(const IndexType &i) const
Definition draco_index_type.h:94
constexpr bool operator>(const ValueTypeT &val) const
Definition draco_index_type.h:93
ThisIndexType operator-=(const ValueTypeT &val)
Definition draco_index_type.h:146
constexpr bool operator==(const ValueTypeT &val) const
Definition draco_index_type.h:77
constexpr IndexType()
Definition draco_index_type.h:69
constexpr bool operator!=(const ValueTypeT &val) const
Definition draco_index_type.h:83
constexpr IndexType(ValueTypeT value)
Definition draco_index_type.h:70
ThisIndexType & operator--()
Definition draco_index_type.h:111
constexpr bool operator==(const IndexType &i) const
Definition draco_index_type.h:74
constexpr bool operator<(const ValueTypeT &val) const
Definition draco_index_type.h:89
ThisIndexType operator--(int)
Definition draco_index_type.h:115
constexpr bool operator!=(const IndexType &i) const
Definition draco_index_type.h:80
constexpr ThisIndexType operator-(const ValueTypeT &val) const
Definition draco_index_type.h:130
ret($return)
Definition mesh_login_result.php:5
Definition attribute_octahedron_transform.cc:21
std::ostream & operator<<(std::ostream &os, IndexType< ValueTypeT, TagT > index)
Definition draco_index_type.h:165
STL namespace.
size_t operator()(const draco::IndexType< ValueTypeT, TagT > &i) const
Definition draco_index_type.h:176