PDA

View Full Version : More abuses for the Template Factory Pattern. (c++)



Gleeok
06-17-2013, 01:43 AM
One thing that I get really tired of doing is defining how Objects need to serialize (read/write) themselves to file; be it xml, binary, whatever. It's also error prone if you add something new to the Serialize() method, but forget to do the same (or wrongly do it) to the Deserialize() method. Worse is that we need to duplicate this code for each file type we want. In case you're new to files, you can't just say fwrite(f, this, sizeof*this)); because 90+% of the time the members are not POD type.

The solution I came up with is very small, a single file, uses simple buffers and arrays, and is actually O(1) time. Considering what it does this is pretty good I think. (Looks at boost::serializer bloated nonsense)..

Here it is:




struct AttributeAccessorInfo
{
public:
AttributeAccessorVariableType type;
const char* name;
int offset;

AttributeAccessorInfo() : type(VAR_TYPE_NONE), name(0), offset(0) {}
AttributeAccessorInfo( AttributeAccessorVariableType type, const char* name, int offset )
: type(type), name(name), offset(offset)
{}

};


class AttributeAccessorObjectTypeBase
{
public:
typedef fc::vector<AttributeAccessorInfo> vec_type;

AttributeAccessorObjectTypeBase( const char* className ) : m_className(className) {}
virtual ~AttributeAccessorObjectTypeBase()
{}

void RegisterAttributeAccessor( const AttributeAccessorInfo& attrInfo );

NO_INLINE void SerializeObjectAttributesXml( const void* obj, XmlWriter* xml );
NO_INLINE void DeserializeObjectAttributesXml( void* obj, XmlReader* xml );

int OnReadAttributeInfoXml( void* obj, XmlReader* xml, const AttributeAccessorInfo& attr );
int OnWriteAttributeInfoXml( const void* obj, XmlWriter* xml, const AttributeAccessorInfo& attr );

protected:
static int m_typeIdGenerator;

static int GenerateTypeId()
{
int currentVal = m_typeIdGenerator++;
return currentVal;
}

const char* m_className;
vec_type m_attributes;
};


template<class T>
class AttributeAccessorObjectTypeInfo : public AttributeAccessorObjectTypeBase
{
public:
AttributeAccessorObjectTypeInfo( const char* className )
: AttributeAccessorObjectTypeBase(className)
{}

static int GetTypeId()
{
static int typeId = AttributeAccessorObjectTypeBase::GenerateTypeId();
return typeId;
}
};


class ObjectAttributeSerializerFactory
{
public:
enum : size_t
{
MaxBufferBytes = 512
};

typedef fc::fixed_tiny_vector<AttributeAccessorObjectTypeBase*, MaxBufferBytes / sizeof(AttributeAccessorObjectTypeBase*)> vec_type;
typedef fc::aligned_buffer<MaxBufferBytes, FC_ALIGNOF(AttributeAccessorObjectTypeBase*)> buffer_type;

ObjectAttributeSerializerFactory() :
m_factories(),
m_buffer(),
m_offset(0)
{
}

static ObjectAttributeSerializerFactory* GetInstance()
{
return &m_instance;
}

template <class T>
void RegisterAttribute( const AttributeAccessorInfo& attrInfo )
{
int registeredTypeId = AttributeAccessorObjectTypeInfo<T>::GetTypeId();
m_instance.m_factories[registeredTypeId]->RegisterAttributeAccessor(attrInfo);
}

template <class T>
void RegisterFactoryType( const char* className )
{
int registeredTypeId = AttributeAccessorObjectTypeInfo<T>::GetTypeId();
ASSERT((size_t)registeredTypeId <= m_factories.size());

// only register a new factory if typeId is a valid type.
if( (size_t)registeredTypeId == m_factories.size() )
{
ASSERT(m_offset <= MaxBufferBytes - sizeof(AttributeAccessorObjectTypeInfo<T>));

AttributeAccessorObjectTypeInfo<T>* p = new (m_buffer.buffer + m_offset) AttributeAccessorObjectTypeInfo<T>(className);
m_offset += sizeof(AttributeAccessorObjectTypeInfo<T>);
m_factories.push_back(p);
}
}

template <class T>
void SerializeObjectAttributesXml( const T* obj, XmlWriter* xml )
{
int registeredTypeId = AttributeAccessorObjectTypeInfo<T>::GetTypeId();
m_instance.m_factories[registeredTypeId]->SerializeObjectAttributesXml(obj, xml);
}

template <class T>
void DeserializeObjectAttributesXml( T* obj, XmlReader* xml )
{
int registeredTypeId = AttributeAccessorObjectTypeInfo<T>::GetTypeId();
m_instance.m_factories[registeredTypeId]->DeserializeObjectAttributesXml(obj, xml);
}


private:
// There's zero point in having more than one instance
// of a factory that depends on compile-time constants
// and static template typeid generation.
static ObjectAttributeSerializerFactory m_instance;

vec_type m_factories;
buffer_type m_buffer;
size_t m_offset;

};


#define REGISTER_ATTRIBUTE_FACTORY_TYPE(class_) \
ObjectAttributeSerializerFactory::GetInstance()->RegisterFactoryType<class_>(#class_)

#define PUSH_ATTRIBUTE_NODE(class_, nodeName) \
ObjectAttributeSerializerFactory::GetInstance()->RegisterAttribute<class_> \
(AttributeAccessorInfo(VAR_TYPE_PUSH_NODE, nodeName, 0))

#define POP_ATTRIBUTE_NODE(class_) \
ObjectAttributeSerializerFactory::GetInstance()->RegisterAttribute<class_> \
(AttributeAccessorInfo(VAR_TYPE_POP_NODE, "", 0))

#define REGISTER_ATTRIBUTE(class_, type, name, var) \
ObjectAttributeSerializerFactory::GetInstance()->RegisterAttribute<class_> \
(AttributeAccessorInfo(type, name, offsetof(class_, var)))

#define SERIALIZE_OBJECT_ATTRIBUTES_XML(obj, xml) \
ObjectAttributeSerializerFactory::GetInstance()->SerializeObjectAttributesXml(obj, xml)

#define DESERIALIZE_OBJECT_ATTRIBUTES_XML(obj, xml) \
ObjectAttributeSerializerFactory::GetInstance()->DeserializeObjectAttributesXml(obj, xml)


Now any classes we want to write to file we add a static "Register" method that looks like this:



void CharacterClass::RegisterObject()
{
REGISTER_ATTRIBUTE_FACTORY_TYPE(CharacterClass);
REGISTER_ATTRIBUTE(CharacterClass, VAR_TYPE_STRING, "name", name);
REGISTER_ATTRIBUTE(CharacterClass, VAR_TYPE_STRING, "script", script);
REGISTER_ATTRIBUTE(CharacterClass, VAR_TYPE_STRING, "description", description);
//etc..
}


And that's how babies are made.

...yeah, I guess I haven't actually done anything worthwhile this week. :\

jeffythedragonslayer
01-27-2024, 04:26 AM
The bloated nonsense, for comparison: https://www.boost.org/doc/libs/1_84_0/libs/serialization/doc/index.html

Gleeok
01-29-2024, 10:22 PM
Hah, I forgot about that (it got deleted) one. I always get a kick out of programming when things go sideways.

Anyway, the project got majorly setback in 2014-15 and now it's in limbo. I just haven't got around to it, but I plan on releasing some tools and assets when I finish digging them up. The PSP unpacking tool was pretty nice: all the sfx/maps/tiles/sprites/etc from the PSP games are easy.

Also R.I.P. Imzogelmo.

jeffythedragonslayer
01-30-2024, 09:01 AM
And that's how babies are made.

...yeah, I guess I haven't actually done anything worthwhile this week. :\

That's OK because some people are dying to know:

https://www.purezc.net/forums/index.php?showtopic=78601