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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
/*
* creg - Code compoment registration system
* Type matching using class templates (only class template support partial specialization)
*/
#ifndef _TYPE_DEDUCTION_H
#define _TYPE_DEDUCTION_H
#include <memory>
#include "creg_cond.h"
namespace creg {
// Default
// If none specialization was found assume it's a class.
template<typename T, typename Enable = void>
struct DeduceType {
static_assert(std::is_same<typename std::remove_const<T>::type, typename std::remove_const<typename T::MyType>::type>::value, "class isn't creged");
static std::unique_ptr<IType> Get() { return IType::CreateObjInstanceType(T::StaticClass(), sizeof(T)); }
};
// Class case
// Covered by default case above
//WARNING: Defining this one would break any class-specialization as for std::vector & std::string below)
/*template<typename T>
struct DeduceType<T, typename std::enable_if<std::is_class<T>::value>::type> {
static std::unique_ptr<IType> Get() { return std::unique_ptr<IType>(IType::CreateObjInstanceType(T::StaticClass(), sizeof(T))); }
};*/
// Enum
template<typename T>
struct DeduceType<T, typename std::enable_if<std::is_enum<T>::value>::type> {
static std::unique_ptr<IType> Get() { return IType::CreateBasicType(crInt, sizeof(T)); }
};
// Integer+Boolean (of any size)
template<typename T>
struct DeduceType<T, typename std::enable_if<std::is_integral<T>::value>::type> {
static std::unique_ptr<IType> Get() { return IType::CreateBasicType(crInt, sizeof(T)); }
};
// Floating-Point (of any size)
template<typename T>
struct DeduceType<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
static std::unique_ptr<IType> Get() { return IType::CreateBasicType(crFloat, sizeof(T)); }
};
// Synced Integer + Float
#if defined(SYNCDEBUG) || defined(SYNCCHECK)
template<typename T>
struct DeduceType<SyncedPrimitive<T>, typename std::enable_if<std::is_integral<T>::value>::type> {
static std::unique_ptr<IType> Get() { return IType::CreateBasicType(crInt /*crSyncedInt*/, sizeof(T)); }
};
template<typename T>
struct DeduceType<SyncedPrimitive<T>, typename std::enable_if<std::is_floating_point<T>::value>::type> {
static std::unique_ptr<IType> Get() { return IType::CreateBasicType(crFloat /*crSyncedFloat*/, sizeof(T)); }
};
#endif
// helper
template<typename T>
class ObjectPointerType : public ObjectPointerBaseType
{
static_assert(std::is_same<typename std::remove_const<T>::type, typename std::remove_const<typename T::MyType>::type>::value, "class isn't creged");
public:
ObjectPointerType() : ObjectPointerBaseType(T::StaticClass(), sizeof(T*)) { }
void Serialize(ISerializer *s, void *instance) override {
void **ptr = (void**)instance;
if (s->IsWriting()) {
s->SerializeObjectPtr(ptr, (*ptr != nullptr) ? ((T*)*ptr)->GetClass() : 0);
} else {
s->SerializeObjectPtr(ptr, objClass);
}
}
};
// Pointer type
template<typename T>
struct DeduceType<T, typename std::enable_if<std::is_pointer<T>::value>::type> {
static std::unique_ptr<IType> Get() { return std::unique_ptr<IType>(new ObjectPointerType<typename std::remove_pointer<T>::type>()); }
};
// Reference type, handled as a pointer
template<typename T>
struct DeduceType<T, typename std::enable_if<std::is_reference<T>::value>::type> {
static std::unique_ptr<IType> Get() { return std::unique_ptr<IType>(new ObjectPointerType<typename std::remove_reference<T>::type>()); }
};
// C-style static array type
template<typename T, int N>
class StaticArrayType : public StaticArrayBaseType
{
public:
typedef T ArrayType[N];
StaticArrayType() : StaticArrayBaseType(DeduceType<T>::Get(), N * sizeof(T)) { }
void Serialize(ISerializer* s, void* instance) override
{
T* array = (T*) instance;
for (int a = 0; a < N; a++) {
DeduceType<T>::Get()->Serialize(s, &array[a]);
}
}
};
template<typename T, size_t ArraySize>
struct DeduceType<T[ArraySize]> {
static std::unique_ptr<IType> Get() {
return std::unique_ptr<IType>(new StaticArrayType<T, ArraySize>());
}
};
// STL static array type
template<typename ArrayT>
class stlStaticArrayType : public StaticArrayBaseType
{
public:
typedef typename ArrayT::value_type ElemT;
stlStaticArrayType() : StaticArrayBaseType(DeduceType<ElemT>::Get(), sizeof(ArrayT)) {}
void Serialize(ISerializer* s, void* instance) override
{
ArrayT& array = *(ArrayT*) instance;
for (size_t a = 0; a < array.size(); a++) {
DeduceType<ElemT>::Get()->Serialize(s, &array[a]);
}
}
};
template<typename ElemT, size_t ArraySize>
struct DeduceType<std::array<ElemT, ArraySize>> {
static std::unique_ptr<IType> Get() {
return std::unique_ptr<IType>(new stlStaticArrayType< std::array<ElemT, ArraySize> >());
}
};
template<typename VectorT>
class DynamicArrayType : public DynamicArrayBaseType
{
public:
typedef typename VectorT::value_type ElemT;
DynamicArrayType() : DynamicArrayBaseType(DeduceType<ElemT>::Get(), sizeof(VectorT)) {}
~DynamicArrayType() {}
void Serialize(ISerializer* s, void* inst) override {
VectorT& ct = *(VectorT*) inst;
if (s->IsWriting()) {
int size = (int) ct.size();
s->SerializeInt(&size, sizeof(int));
for (int a = 0; a < size; a++) {
DeduceType<ElemT>::Get()->Serialize(s, &ct[a]);
}
} else {
int size;
s->SerializeInt(&size, sizeof(int));
ct.clear();
ct.resize(size);
for (int a = 0; a < size; a++) {
DeduceType<ElemT>::Get()->Serialize(s, &ct[a]);
}
}
}
};
// Vector type (vector<ElemT>)
template<typename ElemT>
struct DeduceType<std::vector<ElemT>> {
static std::unique_ptr<IType> Get() {
return std::unique_ptr<IType>(new DynamicArrayType< std::vector<ElemT> >());
}
};
template<typename T>
class BitArrayType : public DynamicArrayBaseType
{
public:
typedef typename T::value_type ElemT;
BitArrayType() : DynamicArrayBaseType(DeduceType<ElemT>::Get(), sizeof(T)) { }
~BitArrayType() { }
void Serialize(ISerializer* s, void* inst) override {
T* ct = (T*)inst;
if (s->IsWriting()) {
int size = (int)ct->size();
s->SerializeInt(&size, sizeof(int));
for (int a = 0; a < size; a++) {
bool b = (*ct)[a];
DeduceType<ElemT>::Get()->Serialize(s, &b);
}
} else {
int size;
s->SerializeInt(&size, sizeof(int));
ct->resize(size);
for (int a = 0; a < size; a++) {
bool b;
DeduceType<ElemT>::Get()->Serialize(s, &b);
(*ct)[a] = b;
}
}
}
};
// std::vector<bool> is not a std::vector but a BitArray instead!
template<>
struct DeduceType<std::vector<bool>> {
static std::unique_ptr<IType> Get() {
return std::unique_ptr<IType>(new BitArrayType<std::vector<bool> >());
}
};
// String type
template<>
struct DeduceType<std::string> {
static std::unique_ptr<IType> Get() { return IType::CreateStringType(); }
};
// GetType allows to use parameter type deduction to get the template argument for DeduceType
template<typename T>
std::unique_ptr<IType> GetType(T& var) {
return DeduceType<T>::Get();
}
}
#endif // _TYPE_DEDUCTION_H
|