Created
October 11, 2018 07:50
-
-
Save rokups/6a98dbecbbc159afccc9e915118e3b77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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