1/*
2 * Copyright (c) 2010-2017 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 <framework/global.h>
27#include <algorithm>
28#include <type_traits>
29#include <typeinfo>
30
31namespace stdext {
32
33// disable memory alignment
34#pragma pack(push,1)
35
36template<typename T>
37struct 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
41class packed_any {
42public:
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) { }
73 ~packed_any()
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
91template<typename T>
92typename std::enable_if<can_pack_in_any<T>::value, T>::type
93packed_any_cast(const packed_any& operand) {
94 VALIDATE(operand.scalar);
95 union {
96 T v;
97 packed_any::placeholder* content;
98 };
99 content = operand.content;
100 return v;
101}
102
103template<typename T>
104typename std::enable_if<!can_pack_in_any<T>::value, T>::type
105packed_any_cast(const packed_any& operand) {
106 VALIDATE(operand.type() == typeid(T));
107 return static_cast<packed_any::holder<T>*>(operand.content)->held;
108}
109
110template<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
118