| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#ifndef DMLC_ANY_H_ |
| 7 |
#define DMLC_ANY_H_ |
| 8 |
|
| 9 |
|
| 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 |
|
| 21 |
class any; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
template <typename T> |
| 34 |
inline T &get(any &src); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
template <typename T> |
| 47 |
inline const T &get(const any &src); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
template <typename T> |
| 60 |
inline const T &unsafe_get(const any &src); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
template <typename T> |
| 73 |
inline T &unsafe_get(any &src); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
class any { |
| 91 |
public: |
| 92 |
|
| 93 |
inline any() = default; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
inline any(any &&other); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
inline any(const any &other); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
template <typename T> |
| 110 |
inline any(T &&other); |
| 111 |
|
| 112 |
inline ~any(); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
inline any &operator=(any &&other); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
inline any &operator=(const any &other); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
template <typename T> |
| 132 |
inline any &operator=(T &&other); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
inline bool empty() const; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
inline void clear(); |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
inline void swap(any &other); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
inline const std::type_info &type() const; |
| 150 |
|
| 151 |
template <typename T, typename... Args> |
| 152 |
inline void construct(Args &&...args); |
| 153 |
|
| 154 |
private: |
| 155 |
|
| 156 |
|
| 157 |
template <typename T> |
| 158 |
class TypeOnHeap; |
| 159 |
template <typename T> |
| 160 |
class TypeOnStack; |
| 161 |
template <typename T> |
| 162 |
class TypeInfo; |
| 163 |
|
| 164 |
static const size_t kStack = sizeof(void *) * 3; |
| 165 |
static const size_t kAlign = sizeof(void *); |
| 166 |
|
| 167 |
union Data { |
| 168 |
|
| 169 |
std::aligned_storage<kStack, kAlign>::type stack; |
| 170 |
|
| 171 |
void *pheap; |
| 172 |
}; |
| 173 |
|
| 174 |
struct Type { |
| 175 |
|
| 176 |
void (*destroy)(Data *data); |
| 177 |
|
| 178 |
void (*create_from_data)(Data *dst, const Data &src); |
| 179 |
|
| 180 |
const std::type_info *ptype_info; |
| 181 |
}; |
| 182 |
|
| 183 |
template <typename T> |
| 184 |
struct data_on_stack { |
| 185 |
static const bool value = alignof(T) <= kAlign && sizeof(T) <= kStack; |
| 186 |
}; |
| 187 |
|
| 188 |
template <typename T> |
| 189 |
friend T &get(any &src); |
| 190 |
template <typename T> |
| 191 |
friend const T &get(const any &src); |
| 192 |
template <typename T> |
| 193 |
friend T &unsafe_get(any &src); |
| 194 |
template <typename T> |
| 195 |
friend const T &unsafe_get(const any &src); |
| 196 |
|
| 197 |
inline void construct(any &&other); |
| 198 |
|
| 199 |
inline void construct(const any &other); |
| 200 |
|
| 201 |
template <typename T> |
| 202 |
inline void check_type() const; |
| 203 |
template <typename T> |
| 204 |
inline void check_type_by_name() const; |
| 205 |
|
| 206 |
const Type *type_{nullptr}; |
| 207 |
|
| 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) { |
| 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) { |
| 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) { |
| 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 |
|
| 406 |
Type type_; |
| 407 |
|
| 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 |
|
| 419 |
|
| 420 |
} |
| 421 |
|
| 422 |
#endif |