VISR  0.12.0
Versatile Interactive Scene Renderer
object_factory.hpp
Go to the documentation of this file.
1 /* Copyright Institute of Sound and Vibration Research - All rights reserved */
2 
3 #ifndef VISR_OBJECTMODEL_OBJECT_FACTORY_HPP_INCLUDED
4 #define VISR_OBJECTMODEL_OBJECT_FACTORY_HPP_INCLUDED
5 
6 #include "object.hpp"
7 #include "object_type.hpp"
8 #include "object_parser.hpp" // experimental support for dispatching to the correct parser.
9 
10 #include "export_symbols.hpp"
11 
12 #include <boost/function.hpp>
13 
14 #include <map>
15 #include <memory>
16 
17 namespace visr
18 {
19 namespace objectmodel
20 {
21 
22 class VISR_OBJECTMODEL_LIBRARY_SYMBOL ObjectFactory
23 {
24 public:
25  static std::unique_ptr<Object> create( ObjectTypeId typeId, ObjectId objectId );
26 
27  static const ObjectParser & parser( ObjectTypeId typeId );
28 
29  template< class ObjectType, class ParserType >
30  static void registerObjectType( ObjectTypeId typeId );
31 
32 private:
33  struct Creator
34  {
35  using CreateFunction = boost::function< Object* ( ObjectId id )>;
36 
37  explicit Creator( CreateFunction fcn, ObjectParser * parser );
38 
39  Object* create( ObjectId id ) const;
40 
41  ObjectParser const & parser() const;
42  private:
43  CreateFunction mCreateFunction;
44 
45  std::shared_ptr<ObjectParser> mParser;
46  };
47 
48  template< class ObjectType, class ParserType >
49  class TCreator: public Creator
50  {
51  public:
52  TCreator( )
53  : Creator( &TCreator<ObjectType,ParserType>::construct, new ParserType() )
54  {
55  }
56 
57  static Object* construct( ObjectId objectId )
58  {
59  return new ObjectType( objectId );
60  }
61  };
62 
63  using CreatorTable = std::map<ObjectTypeId, Creator >;
64 
65  static CreatorTable & creatorTable();
66 };
67 
68 template< class ObjectType, class ParserType >
70 {
71  creatorTable().insert( std::make_pair( typeId, TCreator<ObjectType, ParserType>() ) );
72 }
73 
74 } // namespace objectmodel
75 } // namespace visr
76 
77 #endif // #ifndef VISR_OBJECTMODEL_OBJECT_FACTORY_HPP_INCLUDED
Definition: object_factory.hpp:22
unsigned int ObjectId
Definition: object.hpp:23
static void registerObjectType(ObjectTypeId typeId)
Definition: object_factory.hpp:69
ObjectTypeId
Definition: object_type.hpp:26
Definition: options.cpp:10
Definition: object_parser.hpp:21
Definition: object.hpp:36