MeshLib Documentation
Loading...
Searching...
No Matches
MRObjectFactory.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include "MRPch/MRBindingMacros.h"
5#include <memory>
6#include <string>
7
8namespace MR
9{
10
13
15MRMESH_API std::shared_ptr<Object> createObject( const std::string & className );
16
18#define MR_ADD_CLASS_FACTORY( className ) \
19 static MR::ObjectFactory<className> className##_Factory_{ #className };
20
21using ObjectMakerFunc = std::shared_ptr<Object>();
22
24{
25public:
26 MR_BIND_IGNORE MRMESH_API ObjectFactoryBase( std::string className, ObjectMakerFunc * creator );
28
29private:
30 std::string className_;
31};
32
33template<typename T>
35{
36public:
37 static_assert( std::is_base_of_v<Object, T>, "MR::Object is not base of T" );
38
39 ObjectFactory( std::string className )
40 : ObjectFactoryBase( std::move( className ),
41 []() { return std::static_pointer_cast<Object>( std::make_shared<T>() ); } )
42 { }
43};
44
46
47} // namespace MR
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
Definition MRObjectFactory.h:24
MRMESH_API ~ObjectFactoryBase()
MR_BIND_IGNORE MRMESH_API ObjectFactoryBase(std::string className, ObjectMakerFunc *creator)
Definition MRObjectFactory.h:35
ObjectFactory(std::string className)
Definition MRObjectFactory.h:39
MRMESH_API std::shared_ptr< Object > createObject(const std::string &className)
the function to create new object instance by registered class name
Definition MRCameraOrientationPlugin.h:8
std::shared_ptr< Object >() ObjectMakerFunc
Definition MRObjectFactory.h:21