VISR  0.11.8
Versatile Interactive Scene Renderer
parameter_factory.hpp
Go to the documentation of this file.
1 /* Copyright Institute of Sound and Vibration Research - All rights reserved */
2 
3 #ifndef VISR_PARAMETER_FACTORY_HPP_INCLUDED
4 #define VISR_PARAMETER_FACTORY_HPP_INCLUDED
5 
6 #include "export_symbols.hpp"
7 #include "parameter_type.hpp"
8 
9 #include <functional>
10 #include <map>
11 #include <memory>
12 #include <string>
13 
14 namespace visr
15 {
16 
17 // Forward declarations
18 class ParameterBase;
19 class ParameterConfigBase;
20 template<class ConcreteType, class ParameterConfig, ParameterType> class TypedParameterBase;
21 
22 class VISR_CORE_LIBRARY_SYMBOL ParameterFactory
23 {
24 public:
25  static std::unique_ptr<ParameterBase> create(ParameterType const & type, ParameterConfigBase const & config);
26 
30  static std::size_t numberOfParameterTypes() noexcept;
31 
36  static bool typeExists( ParameterType type ) noexcept;
37 
38  template< class ConcreteParameterType >
39  static void registerParameterType( ParameterType const & type );
40 
45  template< class TypedParameterType >
46  static void registerParameterType();
47 
52  template< class ConcreteParameterType >
53  class Registrar
54  {
55  public:
56  explicit Registrar( ParameterType type )
57  {
58  creatorTable().insert( std::make_pair( type, TCreator<ConcreteParameterType>() ) );
59  }
60  };
61 
62 private:
63  struct Creator
64  {
65  using CreateFunction = std::function< ParameterBase* ( ParameterConfigBase const & config ) >;
66 
67  VISR_CORE_LIBRARY_SYMBOL explicit Creator( CreateFunction fcn );
68 
69  VISR_CORE_LIBRARY_SYMBOL std::unique_ptr<ParameterBase> create( ParameterConfigBase const & config ) const;
70  private:
71  CreateFunction mCreateFunction;
72  };
73 
74  template< class ConcreteParameterType >
75  class TCreator: public Creator
76  {
77  public:
78  TCreator( )
79  : Creator( &TCreator<ConcreteParameterType>::construct )
80  {
81  }
82 
83  static ParameterBase* construct( ParameterConfigBase const & config )
84  {
85  ParameterBase* obj = new ConcreteParameterType( config );
86  return obj;
87  }
88  };
89 
90  using CreatorTable = std::map<ParameterType, Creator >;
91 
92  static CreatorTable & creatorTable();
93 };
94 
95 template< class ConcreteParameterType >
97 {
98  creatorTable().insert( std::make_pair( type, TCreator<ConcreteParameterType>() ) );
99 }
100 
101 template< class TypedParameterType >
103 {
104  registerParameterType<TypedParameterType>( TypedParameterType::staticType() );
105 }
106 
107 
108 // The macro does not work for multiple uses in the same .cpp file
109 // (multiple definitions of 'maker'), stringization of names difficult
110 // because of template brackets and namespace names.
111 // #define REGISTER_PARAMETER( type, id ) namespace { static ParameterFactory::Registrar< type > maker( id ); }
112 
113 } // namespace visr
114 
115 #endif // #ifndef VISR_PARAMETER_FACTORY_HPP_INCLUDED
static void registerParameterType()
Definition: parameter_factory.hpp:102
uint64_t ParameterType
Definition: parameter_type.hpp:13
Definition: parameter_factory.hpp:22
Definition: options.cpp:10
Definition: parameter_factory.hpp:20
Definition: parameter_config_base.hpp:22
Registrar(ParameterType type)
Definition: parameter_factory.hpp:56
Definition: parameter_base.hpp:18
Definition: parameter_factory.hpp:53