aboutsummaryrefslogtreecommitdiff
path: root/Gem/develop/include/Gem/Utils/any.h
blob: 0861e19e6f85fe42e06cad752a1ab8b873bdc7df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * (C) Copyright Christopher Diggins 2005
 * (C) Copyright Pablo Aguilar 2005
 * (C) Copyright Kevlin Henney 2001
 *
 * Copyright (C) 2010-2011 IOhannes m zmölnig. forum::für::umläute. IEM. zmoelnig@iem.at
 *       downloaded this code from http://www.codeproject.com/KB/cpp/dynamic_typing.aspx
 *         changed namespace/defines "cdiggins" to something "gem"
 *
 * Distributed under the Boost Software License, Version 1.0. (See
 * accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 */

#ifndef GEM_ANY_HPP
#define GEM_ANY_HPP

#include "Gem/ExportDef.h"

#ifdef _MSC_VER
# pragma warning( push )
# pragma warning( disable: 4275 )
#endif

#include <stdexcept>
#include <typeinfo>
#include <algorithm>
#include <string>

//#define GEM_ANY_TYPEID_HACK

namespace gem
{
struct GEM_EXTERN bad_any_cast : std::bad_cast {
  bad_any_cast(const std::type_info& src, const std::type_info& dest)
    : result(std::string("bad cast (")+src.name() + "->" + dest.name()+")")
  { }
  virtual ~bad_any_cast(void)
#if __cplusplus <= 199711L
  throw()
#endif
  { }
  virtual const char* what(void) const
#if __cplusplus > 199711L
  noexcept
#else
  throw()
#endif
  {
    return result.c_str();
  }
  const std::string result;
};

namespace any_detail
{
// function pointer table

struct fxn_ptr_table {
  const std::type_info& (*get_type)(void);
  void (*static_delete)(void**);
  void (*clone)(void* const*, void**);
  void (*move)(void* const*,void**);
};

// static functions for small value-types

template<bool is_small>
struct fxns {
  template<typename T>
  struct type {
    static const std::type_info& get_type(void)
    {
#if GEM_ANY_TYPEID_HACK
      const std::type_info&res=typeid(T);
      // the following is a dummy use of the type_info struct
      // to make the template engine work properly on OSX/10.9
      static std::string _ = res.name();
      return res;
#else
      return typeid(T);
#endif
    }
    static void static_delete(void** x)
    {
      reinterpret_cast<T*>(x)->~T();
    }
    static void clone(void* const* src, void** dest)
    {
      new(dest) T(*reinterpret_cast<T const*>(src));
    }
    static void move(void* const* src, void** dest)
    {
      reinterpret_cast<T*>(dest)->~T();
      *reinterpret_cast<T*>(dest) = *reinterpret_cast<T const*>(src);
    }
  };
};

// static functions for big value-types (bigger than a void*)

template<>
struct fxns<false> {
  template<typename T>
  struct type {
    static const std::type_info& get_type(void)
    {
#if GEM_ANY_TYPEID_HACK
      const std::type_info&res=typeid(T);
      return res;
#else
      return typeid(T);
#endif
    }
    static void static_delete(void** x)
    {
      delete(*reinterpret_cast<T**>(x));
    }
    static void clone(void* const* src, void** dest)
    {
      *dest = new T(**reinterpret_cast<T* const*>(src));
    }
    static void move(void* const* src, void** dest)
    {
      (*reinterpret_cast<T**>(dest))->~T();
      **reinterpret_cast<T**>(dest) = **reinterpret_cast<T* const*>(src);
    }
  };
};

template<typename T>
struct get_table {
  static const bool is_small = sizeof(T) <= sizeof(void*);

  static fxn_ptr_table* get(void)
  {
    static fxn_ptr_table static_table = {
      fxns<is_small>::template type<T>::get_type
      , fxns<is_small>::template type<T>::static_delete
      , fxns<is_small>::template type<T>::clone
      , fxns<is_small>::template type<T>::move
    };
    return &static_table;
  }
};

struct empty {
};
} // namespace any_detail


struct GEM_EXTERN any {
  // structors

  template <typename T>
  any(const T& x) : table(NULL), object(NULL)
  {
    table = any_detail::get_table<T>::get();
#if defined(__GNUC__) && __GNUC__ >= 6
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wplacement-new"
#endif
    if (sizeof(T) <= sizeof(void*)) {
      new(&object) T(x);
    } else {
      object = new T(x);
    }
#if defined(__GNUC__) && __GNUC__ >= 6
# pragma GCC diagnostic pop
#endif
  }

  any(void) : table(NULL), object(NULL)
  {
    table = any_detail::get_table<any_detail::empty>::get();
    object = NULL;
  }

  any(const any& x) : table(NULL), object(NULL)
  {
    table = any_detail::get_table<any_detail::empty>::get();
    assign(x);
  }

  virtual ~any(void)
  {
    table->static_delete(&object);
  }

  // assignment

  any& assign(const any& x)
  {
    // are we copying between the same type?

    if (table == x.table) {
      // if so, we can avoid reallocation

      table->move(&x.object, &object);
    } else {
      reset();
      x.table->clone(&x.object, &object);
      table = x.table;
    }
    return *this;
  }

  template <typename T>
  any& assign(const T& x)
  {
    // are we copying between the same type?

    any_detail::fxn_ptr_table* x_table = any_detail::get_table<T>::get();
    if (table == x_table) {
      // if so, we can avoid deallocating and resuse memory

#if defined(__GNUC__) && __GNUC__ >= 6
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wplacement-new"
#endif
      if (sizeof(T) <= sizeof(void*)) {
        // create copy on-top of object pointer itself
        new(&object) T(x);
      } else {
        // create copy on-top of old version
        new(object) T(x);
      }
    } else {
      reset();
      if (sizeof(T) <= sizeof(void*)) {
        // create copy on-top of object pointer itself
        new(&object) T(x);
        // update table pointer
        table = x_table;
      } else {
        object = new T(x);
        table = x_table;
      }
#if defined(__GNUC__) && __GNUC__ >= 6
# pragma GCC diagnostic pop
#endif
    }
    return *this;
  }

  // assignment operator

  template<typename T>
  any& operator=(T const& x)
  {
    return assign(x);
  }
  any& operator=(const any& x)
  {
    return assign(x);
  }

  // utility functions

  any& swap(any& x)
  {
    std::swap(table, x.table);
    std::swap(object, x.object);
    return *this;
  }

  const std::type_info& get_type(void) const
  {
    return table->get_type();
  }

  template<typename T>
  const T& cast(void) const
  {
    if (!compatible<T>()) {
      throw bad_any_cast(get_type(), typeid(T));
    }
    if (sizeof(T) <= sizeof(void*)) {
      return *reinterpret_cast<T const*>(&object);
    } else {
      return *reinterpret_cast<T const*>(object);
    }
  }

  /// Returns true if the two types are the same.
  bool compatible(const any& x) const
  {
    return get_type() == x.get_type();
  }
  /// Returns true if the two types are the same.
  template<typename T>
  bool compatible() const
  {
    return (get_type() == typeid(T));
  }

  // implicit casting is disabled by default

#ifdef ANY_IMPLICIT_CASTING
  // automatic casting operator

  template<typename T>
  operator T(void) const
  {
    return cast<T>();
  }
#endif // implicit casting


  bool empty(void) const
  {
    return table == any_detail::get_table<any_detail::empty>::get();
  }

  void reset(void)
  {
    if (empty()) {
      return;
    }
    table->static_delete(&object);
    table = any_detail::get_table<any_detail::empty>::get();
    object = NULL;
  }

  // fields

  any_detail::fxn_ptr_table* table;
  void* object;
};

// boost::any-like casting

template<typename T>
T* any_cast(any* this_)
{
  if (this_->get_type() != typeid(T)) {
    throw bad_any_cast(this_->get_type(), typeid(T));
  }
  if (sizeof(T) <= sizeof(void*)) {
    return reinterpret_cast<T*>(&this_->object);
  } else {
    return reinterpret_cast<T*>(this_->object);
  }
}

template<typename T>
T const* any_cast(any const* this_)
{
  return any_cast<T>(const_cast<any*>(this_));
}

template<typename T>
T const& any_cast(any const& this_)
{
  return *any_cast<T>(const_cast<any*>(&this_));
}
#ifdef GEM_INTERNAL
// Note: The "unsafe" versions of any_cast are not part of the
// public interface (and hence protected by GEM_INTERNAL) and may
// be removed at any time. They are required where we know what type
// is stored in the any and can't use typeid() comparison, e.g.,
// when our types may travel across different shared libraries.
template<typename T>
T* unsafe_any_cast(any* this_)
{
  if (sizeof(T) <= sizeof(void*)) {
    return reinterpret_cast<T*>(&this_->object);
  } else {
    return reinterpret_cast<T*>(this_->object);
  }
}

template<typename T>
T const* unsafe_any_cast(any const* this_)
{
  return unsafe_any_cast<T>(const_cast<any*>(this_));
}

template<typename T>
T const& unsafe_any_cast(any const& this_)
{
  return *unsafe_any_cast<T>(const_cast<any*>(&this_));
}
#endif
}

#ifdef _MSC_VER
# pragma warning( pop )
#endif

#endif // GEM_ANY_HPP