Debian Codegraph @

src/dmlc-core/include/dmlc/any.h

raw symbols SCIP
1 /*!
2 * Copyright (c) 2016 by Contributors
3 * \file any.h
4 * \brief Container to hold any data type.
5 */
6 #ifndef DMLC_ANY_H_
7 #define DMLC_ANY_H_
8
9 // This code need c++11 to compile
10 #include <algorithm>
11 #include <cstring>
12 #include <type_traits>
13 #include <typeinfo>
14 #include <utility>
15
16 #include "./base.h"
17 #include "./logging.h"
18
19 namespace dmlc {
20 // forward declare any;
21 class any;
22
23 /*!
24 * Get a reference to content stored in the any as type T.
25 * This will cause an error if
26 * T does not match the type stored.
27 * This function is not part of std::any standard.
28 *
29 * \param src The source source any container.
30 * \return The reference of content
31 * \tparam T The type of the value to be fetched.
32 */
33 template <typename T>
34 inline T &get(any &src); // NOLINT(*)
35
36 /*!
37 * Get the const reference content stored in the any as type T.
38 * This will cause an error if
39 * T does not match the type stored.
40 * This function is not part of std::any standard.
41 *
42 * \param src The source source any container.
43 * \return The reference of content
44 * \tparam T The type of the value to be fetched.
45 */
46 template <typename T>
47 inline const T &get(const any &src);
48
49 /*!
50 * The "unsafe" versions of get. It is required when where we know
51 * what type is stored in the any and can't use typeid() comparison,
52 * e.g., when our types may travel across different shared libraries.
53 * This function is not part of std::any standard.
54 *
55 * \param src The source source any container.
56 * \return The reference of content
57 * \tparam T The type of the value to be fetched.
58 */
59 template <typename T>
60 inline const T &unsafe_get(const any &src);
61
62 /*!
63 * The "unsafe" versions of get. It is required when where we know
64 * what type is stored in the any and can't use typeid() comparison,
65 * e.g., when our types may travel across different shared libraries.
66 * This function is not part of std::any standard.
67 *
68 * \param src The source source any container.
69 * \return The reference of content
70 * \tparam T The type of the value to be fetched.
71 */
72 template <typename T>
73 inline T &unsafe_get(any &src); // NOLINT(*)
74
75 /*!
76 * \brief An any class that is compatible to std::any in c++17.
77 *
78 * \code
79 * dmlc::any a = std::string("mydear"), b = 1;
80 * // get reference out and add it
81 * dmlc::get<int>(b) += 1;
82 * // a is now string
83 * LOG(INFO) << dmlc::get<std::string>(a);
84 * // a is now 2, the string stored will be properly destructed
85 * a = std::move(b);
86 * LOG(INFO) << dmlc::get<int>(a);
87 * \endcode
88 * \sa get
89 */
90 class any {
91 public:
92 /*! \brief default constructor */
93 inline any() = default;
94 /*!
95 * \brief move constructor from another any
96 * \param other The other any to be moved
97 */
98 inline any(any &&other); // NOLINT(*)
99 /*!
100 * \brief copy constructor
101 * \param other The other any to be copied
102 */
103 inline any(const any &other); // NOLINT(*)
104 /*!
105 * \brief constructor from any types
106 * \param other The other types to be constructed into any.
107 * \tparam T The value type of other.
108 */
109 template <typename T>
110 inline any(T &&other); // NOLINT(*)
111 /*! \brief destructor */
112 inline ~any();
113 /*!
114 * \brief assign operator from other
115 * \param other The other any to be copy or moved.
116 * \return self
117 */
118 inline any &operator=(any &&other);
119 /*!
120 * \brief assign operator from other
121 * \param other The other any to be copy or moved.
122 * \return self
123 */
124 inline any &operator=(const any &other);
125 /*!
126 * \brief assign operator from any type.
127 * \param other The other any to be copy or moved.
128 * \tparam T The value type of other.
129 * \return self
130 */
131 template <typename T>
132 inline any &operator=(T &&other);
133 /*!
134 * \return whether the container is empty.
135 */
136 inline bool empty() const;
137 /*!
138 * \brief clear the content of container
139 */
140 inline void clear();
141 /*!
142 * swap current content with other
143 * \param other The other data to be swapped.
144 */
145 inline void swap(any &other); // NOLINT(*)
146 /*!
147 * \return The type_info about the stored type.
148 */
149 inline const std::type_info &type() const;
150 /*! \brief Construct value of type T inplace */
151 template <typename T, typename... Args>
152 inline void construct(Args &&...args);
153
154 private:
155 //! \cond Doxygen_Suppress
156 // declare of helper class
157 template <typename T>
158 class TypeOnHeap;
159 template <typename T>
160 class TypeOnStack;
161 template <typename T>
162 class TypeInfo;
163 // size of stack space, it takes 32 bytes for one any type.
164 static const size_t kStack = sizeof(void *) * 3;
165 static const size_t kAlign = sizeof(void *);
166 // container use dynamic storage only when space runs lager
167 union Data {
168 // stack space
169 std::aligned_storage<kStack, kAlign>::type stack;
170 // pointer to heap space
171 void *pheap;
172 };
173 // type specific information
174 struct Type {
175 // destructor function
176 void (*destroy)(Data *data);
177 // copy constructor
178 void (*create_from_data)(Data *dst, const Data &src);
179 // the type info function
180 const std::type_info *ptype_info;
181 };
182 // constant to check if data can be stored on heap.
183 template <typename T>
184 struct data_on_stack {
185 static const bool value = alignof(T) <= kAlign && sizeof(T) <= kStack;
186 };
187 // declare friend with
188 template <typename T>
189 friend T &get(any &src); // NOLINT(*)
190 template <typename T>
191 friend const T &get(const any &src);
192 template <typename T>
193 friend T &unsafe_get(any &src); // NOLINT(*)
194 template <typename T>
195 friend const T &unsafe_get(const any &src);
196 // internal construct function
197 inline void construct(any &&other);
198 // internal construct function
199 inline void construct(const any &other);
200 // internal function to check if type is correct.
201 template <typename T>
202 inline void check_type() const;
203 template <typename T>
204 inline void check_type_by_name() const;
205 // internal type specific information
206 const Type *type_{nullptr};
207 // internal data
208 Data data_;
209 };
210
211 template <typename T>
212 inline any::any(T &&other) {
213 typedef typename std::decay<T>::type DT;
214 if (std::is_same<DT, any>::value) {
215 this->construct(std::forward<T>(other));
216 } else {
217 static_assert(std::is_copy_constructible<DT>::value,
218 "Any can only hold value that is copy constructable");
219 type_ = TypeInfo<DT>::get_type();
220 if (data_on_stack<DT>::value) {
221
222 #if 6 <= __GNUC__
223
224 #endif
225 new (&(data_.stack)) DT(std::forward<T>(other));
226
227 } else {
228 data_.pheap = new DT(std::forward<T>(other));
229 }
230 }
231 }
232
233 inline any::any(any &&other) {
234 this->construct(std::move(other));
235 }
236
237 inline any::any(const any &other) {
238 this->construct(other);
239 }
240
241 inline void any::construct(any &&other) {
242 type_ = other.type_;
243 data_ = other.data_;
244 other.type_ = nullptr;
245 }
246
247 inline void any::construct(const any &other) {
248 type_ = other.type_;
249 if (type_ != nullptr) {
250 type_->create_from_data(&data_, other.data_);
251 }
252 }
253
254 template <typename T, typename... Args>
255 inline void any::construct(Args &&...args) {
256 clear();
257 typedef typename std::decay<T>::type DT;
258 type_ = TypeInfo<DT>::get_type();
259 if (data_on_stack<DT>::value) {
260
261 #if 6 <= __GNUC__
262
263 #endif
264 new (&(data_.stack)) DT(std::forward<Args>(args)...);
265
266 } else {
267 data_.pheap = new DT(std::forward<Args>(args)...);
268 }
269 }
270
271 inline any::~any() {
272 this->clear();
273 }
274
275 inline any &any::operator=(any &&other) {
276 any(std::move(other)).swap(*this);
277 return *this;
278 }
279
280 inline any &any::operator=(const any &other) {
281 any(other).swap(*this);
282 return *this;
283 }
284
285 template <typename T>
286 inline any &any::operator=(T &&other) {
287 any(std::forward<T>(other)).swap(*this);
288 return *this;
289 }
290
291 inline void any::swap(any &other) { // NOLINT(*)
292 std::swap(type_, other.type_);
293 std::swap(data_, other.data_);
294 }
295
296 inline void any::clear() {
297 if (type_ != nullptr) {
298 if (type_->destroy != nullptr) {
299 type_->destroy(&data_);
300 }
301 type_ = nullptr;
302 }
303 }
304
305 inline bool any::empty() const {
306 return type_ == nullptr;
307 }
308
309 inline const std::type_info &any::type() const {
310 if (type_ != nullptr) {
311 return *(type_->ptype_info);
312 } else {
313 return typeid(void);
314 }
315 }
316
317 template <typename T>
318 inline void any::check_type() const {
319 CHECK(type_ != nullptr) << "The any container is empty"
320 << " requested=" << typeid(T).name();
321 CHECK(*(type_->ptype_info) == typeid(T))
322 << "The stored type mismatch"
323 << " stored=" << type_->ptype_info->name() << " requested=" << typeid(T).name();
324 }
325
326 template <typename T>
327 inline void any::check_type_by_name() const {
328 CHECK(type_ != nullptr) << "The any container is empty"
329 << " requested=" << typeid(T).name();
330 CHECK(strcmp(type_->ptype_info->name(), typeid(T).name()) == 0)
331 << "The stored type name mismatch"
332 << " stored=" << type_->ptype_info->name() << " requested=" << typeid(T).name();
333 }
334
335 template <typename T>
336 inline const T &get(const any &src) {
337 src.check_type<T>();
338 return *any::TypeInfo<T>::get_ptr(&(src.data_));
339 }
340
341 template <typename T>
342 inline T &get(any &src) { // NOLINT(*)
343 src.check_type<T>();
344 return *any::TypeInfo<T>::get_ptr(&(src.data_));
345 }
346
347 template <typename T>
348 inline const T &unsafe_get(const any &src) {
349 src.check_type_by_name<T>();
350 return *any::TypeInfo<T>::get_ptr(&(src.data_));
351 }
352
353 template <typename T>
354 inline T &unsafe_get(any &src) { // NOLINT(*)
355 src.check_type_by_name<T>();
356 return *any::TypeInfo<T>::get_ptr(&(src.data_));
357 }
358
359 template <typename T>
360 class any::TypeOnHeap {
361 public:
362 inline static T *get_ptr(any::Data *data) {
363 return static_cast<T *>(data->pheap);
364 }
365 inline static const T *get_ptr(const any::Data *data) {
366 return static_cast<const T *>(data->pheap);
367 }
368 inline static void create_from_data(any::Data *dst, const any::Data &data) {
369 dst->pheap = new T(*get_ptr(&data));
370 }
371 inline static void destroy(Data *data) {
372 delete static_cast<T *>(data->pheap);
373 }
374 };
375
376 template <typename T>
377 class any::TypeOnStack {
378 public:
379 inline static T *get_ptr(any::Data *data) {
380 return reinterpret_cast<T *>(&(data->stack));
381 }
382 inline static const T *get_ptr(const any::Data *data) {
383 return reinterpret_cast<const T *>(&(data->stack));
384 }
385 inline static void create_from_data(any::Data *dst, const any::Data &data) {
386 new (&(dst->stack)) T(*get_ptr(&data));
387 }
388 inline static void destroy(Data *data) {
389 T *dptr = reinterpret_cast<T *>(&(data->stack));
390 dptr->~T();
391 }
392 };
393
394 template <typename T>
395 class any::TypeInfo :
396 public std::conditional<any::data_on_stack<T>::value, any::TypeOnStack<T>,
397 any::TypeOnHeap<T>>::type {
398 public:
399 inline static const Type *get_type() {
400 static TypeInfo<T> tp;
401 return &(tp.type_);
402 }
403
404 private:
405 // local type
406 Type type_;
407 // constructor
408 TypeInfo() {
409 if (std::is_pod<T>::value && data_on_stack<T>::value) {
410 type_.destroy = nullptr;
411 } else {
412 type_.destroy = TypeInfo<T>::destroy;
413 }
414 type_.create_from_data = TypeInfo<T>::create_from_data;
415 type_.ptype_info = &typeid(T);
416 }
417 };
418 //! \endcond
419
420 } // namespace dmlc
421
422 #endif // DMLC_ANY_H_