Otclient  14/8/2020
packed_any.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef STDEXT_PACKEDANY_H
24 #define STDEXT_PACKEDANY_H
25 
26 #include <algorithm>
27 #include <cassert>
28 #include <type_traits>
29 #include <typeinfo>
30 
31 namespace stdext {
32 
33 // disable memory alignment
34 #pragma pack(push,1)
35 
36 template<typename T>
37 struct can_pack_in_any : std::integral_constant<bool,
38  (sizeof(T) <= sizeof(void*) && std::is_trivial<T>::value)> {};
39 
40 // improved to use less memory
41 class packed_any {
42 public:
43  struct placeholder {
44  virtual ~placeholder() { }
45  virtual const std::type_info& type() const = 0;
46  virtual placeholder* clone() const = 0;
47  };
48 
49  template<typename T>
50  struct holder : public placeholder {
51  holder(const T& value) : held(value) { }
52  const std::type_info& type() const { return typeid(T); }
53  placeholder* clone() const { return new holder(held); }
54  T held;
55  private:
56  holder& operator=(const holder &);
57  };
58 
59  placeholder* content;
60  bool scalar;
61 
62  packed_any() :
63  content(nullptr), scalar(false) { }
64  packed_any(const packed_any& other) :
65  content(!other.scalar && other.content ? other.content->clone() : other.content),
66  scalar(other.scalar) { }
67  template<typename T>
68  packed_any(const T& value, typename std::enable_if<(can_pack_in_any<T>::value)>::type* = nullptr) :
69  content(reinterpret_cast<placeholder*>(static_cast<std::size_t>(value))), scalar(true) { }
70  template<typename T>
71  packed_any(const T& value, typename std::enable_if<!(can_pack_in_any<T>::value)>::type* = nullptr) :
72  content(new holder<T>(value)), scalar(false) { }
74  { if(!scalar && content) delete content; }
75 
76  packed_any& swap(packed_any& rhs) { std::swap(content, rhs.content); std::swap(scalar, rhs.scalar); return *this; }
77 
78  template<typename T> packed_any& operator=(const T& rhs) { packed_any(rhs).swap(*this); return *this; }
79  packed_any& operator=(packed_any rhs) { rhs.swap(*this); return *this; }
80 
81  bool empty() const { return !scalar && !content; }
82  template<typename T> T cast() const;
83  const std::type_info& type() const {
84  if(!scalar)
85  return content ? content->type() : typeid(void);
86  else
87  return typeid(std::size_t);
88  }
89 };
90 
91 template<typename T>
92 typename std::enable_if<can_pack_in_any<T>::value, T>::type
93 packed_any_cast(const packed_any& operand) {
94  assert(operand.scalar);
95  union {
96  T v;
97  packed_any::placeholder* content;
98  };
99  content = operand.content;
100  return v;
101 }
102 
103 template<typename T>
104 typename std::enable_if<!can_pack_in_any<T>::value, T>::type
105 packed_any_cast(const packed_any& operand) {
106  assert(operand.type() == typeid(T));
107  return static_cast<packed_any::holder<T>*>(operand.content)->held;
108 }
109 
110 template<typename T> T packed_any::cast() const { return packed_any_cast<T>(*this); }
111 
112 // restore memory alignment
113 #pragma pack(pop)
114 
115 }
116 
117 #endif
std::swap
void swap(stdext::packed_vector< T, U > &lhs, stdext::packed_vector< T, U > &rhs)
Definition: packed_vector.h:158
stdext::packed_any::packed_any
packed_any(const T &value, typename std::enable_if<!(can_pack_in_any< T >::value)>::type *=nullptr)
Definition: packed_any.h:71
stdext::packed_any
Definition: packed_any.h:41
stdext::packed_any::operator=
packed_any & operator=(const T &rhs)
Definition: packed_any.h:78
stdext::cast
bool cast(const T &in, R &out)
Definition: cast.h:37
stdext::packed_any::~packed_any
~packed_any()
Definition: packed_any.h:73
stdext::packed_any::type
const std::type_info & type() const
Definition: packed_any.h:83
stdext::packed_any::placeholder
Definition: packed_any.h:43
stdext::packed_any_cast
std::enable_if<!can_pack_in_any< T >::value, T >::type packed_any_cast(const packed_any &operand)
Definition: packed_any.h:105
stdext::packed_any::holder
Definition: packed_any.h:50
stdext::packed_any::swap
packed_any & swap(packed_any &rhs)
Definition: packed_any.h:76
stdext::packed_any::empty
bool empty() const
Definition: packed_any.h:81
stdext::packed_any::scalar
bool scalar
Definition: packed_any.h:60
stdext::packed_any::operator=
packed_any & operator=(packed_any rhs)
Definition: packed_any.h:79
stdext::packed_any::packed_any
packed_any(const T &value, typename std::enable_if<(can_pack_in_any< T >::value)>::type *=nullptr)
Definition: packed_any.h:68
std
Definition: packed_vector.h:157
stdext::packed_any::content
placeholder * content
Definition: packed_any.h:59
stdext
Definition: any.h:30
stdext::can_pack_in_any
Definition: packed_any.h:37