MeshLib
 
Loading...
Searching...
No Matches
MRObjectFactory.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include <memory>
5#include <string>
6
7namespace MR
8{
9
12
14MRMESH_API std::shared_ptr<Object> createObject( const std::string & className );
15
17#define MR_ADD_CLASS_FACTORY( className ) \
18 static MR::ObjectFactory<className> className##_Factory_{ #className };
19
20using ObjectMakerFunc = std::shared_ptr<Object>();
21
23{
24public:
25 MRMESH_API ObjectFactoryBase( std::string className, ObjectMakerFunc * creator );
27
28private:
29 std::string className_;
30};
31
32template<typename T>
34{
35public:
36 static_assert( std::is_base_of_v<Object, T>, "MR::Object is not base of T" );
37
38 ObjectFactory( std::string className )
39 : ObjectFactoryBase( std::move( className ),
40 []() { return std::static_pointer_cast<Object>( std::make_shared<T>() ); } )
41 { }
42};
43
45
46} // namespace MR
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
Definition MRObjectFactory.h:23
MRMESH_API ~ObjectFactoryBase()
MRMESH_API ObjectFactoryBase(std::string className, ObjectMakerFunc *creator)
Definition MRObjectFactory.h:34
ObjectFactory(std::string className)
Definition MRObjectFactory.h:38
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:7
std::shared_ptr< Object >() ObjectMakerFunc
Definition MRObjectFactory.h:20