Files
LifeProject/LifeCore/Property.h
T
2025-05-13 02:46:21 +03:00

183 lines
3.6 KiB
C++

#ifndef Property_h__
#define Property_h__
#include "delegate/Delegate.h"
#include "IProperty.h"
#include "Utils.h"
#include <assert.h>
namespace LifeCore
{
#define DEFINE_PROPERTY(___name, ___type) \
Core::Property<##___type##>*## ___name;
#define ALLOC_PROPERTY(_name, _type) \
new Core::Property<##_type##>(STR(#_name));
template<typename T>
class Property : public IProperty
{
public:
Property()
{
}
void Set(const void* src, size_t size){
//size must be equal
assert(sizeof(T) == size);
memcpy(&value, src, size);
}
const void* GetPtr(){
return &value;
}
bool Serialize(LifeCore::IMemoryStream* stm)
{
StringSerialize(name, stm);
stm->Write(size_t(sizeof(value)));
stm->Write(&value, sizeof(value));
return true;
}
bool Deserialize(LifeCore::IMemoryStream* stm)
{
name = StringDeserialize(stm);
size_t data_len;
stm->Read(&data_len, sizeof(data_len));
stm->Read(&value, data_len);
return true;
}
const Property<T>& operator=(const T& other)
{
value = other;
OnValueChanged();
return *this;
}
const Property<T>& operator=(const Property<T>& other)
{
value = other.value;
OnValueChanged();
return *this;
}
const T& operator()()
{
return value;
}
String ToString() { return STR(typeid(T).name()); };
void FromString(const String& str){}
const String& GetName()
{
return name;
}
private:
String name;
T value;
};
bool Property<String>::Serialize(LifeCore::IMemoryStream* stm)
{
StringSerialize(name, stm);
StringSerialize(value, stm);
return true;
}
bool Property<String>::Deserialize(LifeCore::IMemoryStream* stm)
{
name = StringDeserialize(stm);
value = StringDeserialize(stm);
return true;
}
void Property<String>::FromString(const String& str)
{
*this = str;
}
void Property<int8>::FromString(const String& str)
{
*this = StringToInt8(str);
}
void Property<int16>::FromString(const String& str)
{
*this = StringToInt16(str);
}
void Property<int32>::FromString(const String& str)
{
*this = StringToInt32(str);
}
void Property<int64>::FromString(const String& str)
{
*this = StringToInt64(str);
}
void Property<uint8>::FromString(const String& str)
{
*this = StringToUInt8(str);
}
void Property<uint16>::FromString(const String& str)
{
*this = StringToUInt16(str);
}
void Property<uint32>::FromString(const String& str)
{
*this = StringToUInt32(str);
}
void Property<uint64>::FromString(const String& str)
{
*this = StringToUInt64(str);
}
void Property<float>::FromString(const String& str)
{
*this = StringToFloat(str);
}
void Property<double>::FromString(const String& str)
{
*this = StringToDouble(str);
}
String Property<String>::ToString()
{
return value;
}
String Property<int8>::ToString()
{
return Int8ToString(value);
}
String Property<int16>::ToString()
{
return Int16ToString(value);
}
String Property<int32>::ToString()
{
return Int32ToString(value);
}
String Property<int64>::ToString()
{
return Int64ToString(value);
}
String Property<uint8>::ToString()
{
return UInt8ToString(value);
}
String Property<uint16>::ToString()
{
return UInt16ToString(value);
}
String Property<uint32>::ToString()
{
return UInt32ToString(value);
}
String Property<uint64>::ToString()
{
return UInt64ToString(value);
}
String Property<bool>::ToString()
{
return BoolToString(value);
}
String Property<float>::ToString()
{
return FloatToString(value);
}
String Property<double>::ToString()
{
return DoubleToString(value);
}
}
#endif // Property_h__