MCPcopy Create free account
hub / github.com/IfcOpenShell/IfcOpenShell / VariantArray

Class VariantArray

src/ifcparse/variantarray.h:117–326  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

115
116template<typename... Types>
117class VariantArray {
118public:
119 using TypesTuple = ::impl::MapTypes_t<Types...>;
120
121 VariantArray(size_t size)
122 : size_and_indices_(size ? new uint8_t[size + 1] : nullptr)
123 , storage_(size ? new StorageType[size] : nullptr)
124 {
125 if (size) {
126 size_and_indices_[0] = (uint8_t)size;
127 memset(size_and_indices_ + 1, 0, sizeof(uint8_t) * size);
128 for (size_t i = 0; i < size; ++i) {
129 // type 0 needs to be default constructable
130 set(i, typename std::tuple_element<0, std::tuple<Types...>>::type{});
131 }
132 }
133 }
134
135 VariantArray(VariantArray&& other) noexcept
136 : size_and_indices_(other.size_and_indices_)
137 , storage_(other.storage_)
138 {
139 other.size_and_indices_ = nullptr;
140 other.storage_ = nullptr;
141 }
142
143 VariantArray& operator=(VariantArray&& other) noexcept {
144 if (this != &other) {
145 free_();
146
147 size_and_indices_ = other.size_and_indices_;
148 storage_ = other.storage_;
149
150 other.size_and_indices_ = nullptr;
151 other.storage_ = nullptr;
152 }
153 return *this;
154 }
155
156 VariantArray(const VariantArray&) = delete;
157 VariantArray(const VariantArray&&) = delete;
158 VariantArray& operator= (const VariantArray&) = delete;
159
160 template<typename T, typename = std::enable_if_t<!std::is_same_v<std::decay_t<T>, VariantArray>>>
161 void set(std::size_t index, T&& value) {
162 using U = std::decay_t<T>;
163 static_assert(::impl::TypeIndex_v<U, Types...> < sizeof...(Types), "Type not supported by variant");
164 if (index >= size()) {
165 throw std::out_of_range("Index out of range");
166 }
167
168 destroy_at_index(index);
169
170 size_and_indices_[index + 1] = ::impl::TypeIndex_v<U, Types...>;
171 using V = typename std::tuple_element<::impl::TypeIndex_v<U, Types...>, ::impl::MapTypes_t<Types... >>::type;
172 // std::wcout << "setting " << index << " to " << typeid(V).name() << " (" << ::impl::TypeIndex_v<U, Types...> << ")" << std::endl;
173 if constexpr (::impl::is_unique_ptr<V>::value) {
174 new(&storage_[index]) V(new U(value));

Callers

nothing calls this directly

Calls 3

to_stringFunction · 0.85
sizeFunction · 0.70
nameMethod · 0.45

Tested by

no test coverage detected