VISR  0.11.8
Versatile Interactive Scene Renderer
communication_protocol_factory.hpp
Go to the documentation of this file.
1 /* Copyright Institute of Sound and Vibration Research - All rights reserved */
2 
3 #ifndef VISR_COMMUNICATION_PROTOCOL_FACTORY_HPP_INCLUDED
4 #define VISR_COMMUNICATION_PROTOCOL_FACTORY_HPP_INCLUDED
5 
6 #include <memory>
7 
10 #include "export_symbols.hpp"
11 #include "parameter_type.hpp"
12 
13 #include <functional>
14 #include <map>
15 #include <memory>
16 #include <string>
17 
18 namespace visr
19 {
20 
21 // Forward declarations
22 class ParameterBase;
23 class ParameterConfigBase;
24 
29 class VISR_CORE_LIBRARY_SYMBOL CommunicationProtocolFactory
30 {
31 public:
36 
47  /*VISR_CORE_LIBRARY_SYMBOL*/ static std::unique_ptr<CommunicationProtocolBase> createProtocol( CommunicationProtocolType const & protocolType,
48  ParameterType const & paramType,
49  ParameterConfigBase const & config );
50 
58  /*VISR_CORE_LIBRARY_SYMBOL*/ static std::unique_ptr<CommunicationProtocolBase::Input> createInput( CommunicationProtocolType const & protocolType );
59 
67  /*VISR_CORE_LIBRARY_SYMBOL*/ static std::unique_ptr<CommunicationProtocolBase::Output> createOutput( CommunicationProtocolType const & protocolType );
68 
72  /*VISR_CORE_LIBRARY_SYMBOL*/ static std::size_t numberOfProtocols() noexcept;
73 
78  /*VISR_CORE_LIBRARY_SYMBOL*/ static CommunicationProtocolType typeFromName( char const * name );
79 
86  /*VISR_CORE_LIBRARY_SYMBOL*/ static std::string typeToName( CommunicationProtocolType type );
87 
92  /*VISR_CORE_LIBRARY_SYMBOL*/ static bool typeExists( CommunicationProtocolType type ) noexcept;
93 
102  template< class ConcreteCommunicationProtocol >
103  static void registerCommunicationProtocol( CommunicationProtocolType const & protocolType, char const * name );
104 
113  template< class ConcreteCommunicationProtocol >
114  static void registerCommunicationProtocol();
115 
116 private:
120  struct VISR_CORE_LIBRARY_SYMBOL Creator
121  {
125  using CreateFunction = std::function< std::unique_ptr<CommunicationProtocolBase>( ParameterType const &, ParameterConfigBase const & ) >;
126 
130  using InputCreateFunction = std::function< std::unique_ptr<CommunicationProtocolBase::Input>() >;
131 
135  using OutputCreateFunction = std::function< std::unique_ptr<CommunicationProtocolBase::Output>() >;
136 
144  /*VISR_CORE_LIBRARY_SYMBOL*/ explicit Creator( CreateFunction fcn,
145  InputCreateFunction inputCreator,
146  OutputCreateFunction outputCreator,
147  char const * name );
148 
149  ~Creator();
150 
155  /*VISR_CORE_LIBRARY_SYMBOL*/ std::unique_ptr<CommunicationProtocolBase> createProtocol( ParameterType const & paramType, ParameterConfigBase const & config ) const;
156  /*VISR_CORE_LIBRARY_SYMBOL*/ std::unique_ptr<CommunicationProtocolBase::Input> createInput() const;
157  /*VISR_CORE_LIBRARY_SYMBOL*/ std::unique_ptr<CommunicationProtocolBase::Output> createOutput() const;
158 
159  /*VISR_CORE_LIBRARY_SYMBOL*/ std::string const & name() const;
160  private:
161 
162  CreateFunction mCreateFunction;
163  InputCreateFunction mInputCreateFunction;
164  OutputCreateFunction mOutputCreateFunction;
165  std::string mName;
166  };
167 
173  /*VISR_CORE_LIBRARY_SYMBOL*/ static void registerCommunicationProtocol( CommunicationProtocolType type, Creator&& creator );
174 
175  template< class ConcreteCommunicationProtocolType >
176  class TCreator: public Creator
177  {
178  public:
179  TCreator( char const * name)
180  : Creator( &TCreator<ConcreteCommunicationProtocolType>::construct,
181  &TCreator<ConcreteCommunicationProtocolType>::constructInput,
182  &TCreator<ConcreteCommunicationProtocolType>::constructOutput,
183  name )
184  {
185  }
186 
187  static std::unique_ptr<CommunicationProtocolBase> construct( ParameterType const & paramType,
188  ParameterConfigBase const & config )
189  {
190  return std::unique_ptr<CommunicationProtocolBase>( new ConcreteCommunicationProtocolType(paramType, config ));
191  }
192 
193  static std::unique_ptr<CommunicationProtocolBase::Input> constructInput()
194  {
195  return std::unique_ptr<CommunicationProtocolBase::Input>( new typename ConcreteCommunicationProtocolType::InputBase() );
196  }
197  static std::unique_ptr<typename CommunicationProtocolBase::Output> constructOutput()
198  {
199  return std::unique_ptr<CommunicationProtocolBase::Output>( new typename ConcreteCommunicationProtocolType::OutputBase() );
200  }
201  };
202 
203  using KeyType = CommunicationProtocolType;
204 
205  using CreatorTable = std::map< KeyType, Creator >;
206 
207  static CreatorTable & creatorTable();
208 };
209 
210 template< class ConcreteCommunicationProtocolType >
212 {
213  registerCommunicationProtocol( protocolType, TCreator<ConcreteCommunicationProtocolType>(name) );
214 }
215 
216 template< class ConcreteCommunicationProtocolType >
218 {
219  registerCommunicationProtocol<ConcreteCommunicationProtocolType>(
222 }
223 
224 } // namespace visr
225 
226 #endif // #ifndef VISR_COMMUNICATION_PROTOCOL_FACTORY_HPP_INCLUDED
static void registerCommunicationProtocol()
Definition: communication_protocol_factory.hpp:217
Definition: communication_protocol_type.hpp:29
uint64_t ParameterType
Definition: parameter_type.hpp:13
std::uint64_t CommunicationProtocolType
Definition: communication_protocol_type.hpp:14
Definition: options.cpp:10
Definition: parameter_config_base.hpp:22
Definition: communication_protocol_factory.hpp:29