OSVR-Core
GraphFactoryRegistration.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_GraphFactoryRegistration_h_GUID_12D5835A_12E1_4A94_758D_853F0A98C2A5
26 #define INCLUDED_GraphFactoryRegistration_h_GUID_12D5835A_12E1_4A94_758D_853F0A98C2A5
27 
28 // Internal Includes
29 #include <osvr/Util/UniquePtr.h>
30 
31 // Library/third-party includes
32 #include <boost/noncopyable.hpp>
33 
34 // Standard includes
35 #include <string>
36 #include <iosfwd>
37 #include <functional>
38 #include <unordered_map>
39 
40 class GraphOutputFactory : boost::noncopyable {
41  public:
42  GraphOutputFactory() = default;
43 
45  osvr::unique_ptr<GraphOutputInterface> create(std::ostream &os,
46  std::string const &type) {
47  return m_factories.at(type)(os);
48  }
49  typedef std::function<osvr::unique_ptr<GraphOutputInterface>(
50  std::ostream &os)> GraphFactory;
51  void registerGraphFactory(std::string const &type, GraphFactory factory) {
52  m_factories[type] = factory;
53  }
54 
55  private:
56  std::unordered_map<std::string, GraphFactory> m_factories;
57 };
58 
59 template <typename T> class GraphFactoryRegistration {
60  public:
62  std::string const &type) {
63  genericFactory.registerGraphFactory(type, [](std::ostream &os) {
64  return osvr::unique_ptr<GraphOutputInterface>(new T(os));
65  });
66  }
67 };
68 
69 #endif // INCLUDED_GraphFactoryRegistration_h_GUID_12D5835A_12E1_4A94_758D_853F0A98C2A5
Definition: GraphFactoryRegistration.h:59
osvr::unique_ptr< GraphOutputInterface > create(std::ostream &os, std::string const &type)
Factory method.
Definition: GraphFactoryRegistration.h:45
Header to bring unique_ptr into the osvr namespace.
Definition: GraphFactoryRegistration.h:40