Skip to content

Instantly share code, notes, and snippets.

@rokups
Created October 11, 2018 07:50
Show Gist options
  • Save rokups/6a98dbecbbc159afccc9e915118e3b77 to your computer and use it in GitHub Desktop.
Save rokups/6a98dbecbbc159afccc9e915118e3b77 to your computer and use it in GitHub Desktop.
/// Template implementation of the object factory.
template <class T> class ObjectFactoryImpl : public ObjectFactory
{
public:
/// Construct.
explicit ObjectFactoryImpl(Context* context) :
ObjectFactory(context)
{
typeInfo_ = T::GetTypeInfoStatic();
allocator_ = AllocatorInitialize(sizeof(T));
}
~ObjectFactoryImpl() override
{
AllocatorUninitialize(allocator_);
allocator_ = nullptr;
}
/// Create an object of the specific type.
SharedPtr<Object> CreateObject() override
{
auto* newObject = static_cast<T*>(AllocatorReserve(allocator_));
new(newObject) T(context_);
newObject->SetDeleter([this, newObject](RefCounted* refCounted) {
newObject->~T();
AllocatorFree(allocator_, newObject);
});
return SharedPtr<Object>(newObject);
}
private:
AllocatorBlock* allocator_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment