DetectorGraph  2.0
topicregistry-stl.hpp
Go to the documentation of this file.
1 // Copyright 2017 Nest Labs, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DETECTORGRAPH_INCLUDE_TOPICREGISTRY_STL_HPP_
16 #define DETECTORGRAPH_INCLUDE_TOPICREGISTRY_STL_HPP_
17 
18 #include "topic.hpp"
19 #include "topicstate.hpp"
20 
21 #include <map>
22 #include <typeinfo>
23 
24 namespace DetectorGraph
25 {
26 /**
27  * @brief _Internal_ - A Registry of available Topics
28  *
29  * Graphs use a TopicRegistry to register and resolve (i.e. retrieve) Topics
30  * using a Type-aware API.
31  * This uses RTTI to retrieve an 'index' for each class.
32  *
33  * Right now using 'name' because it's convenient for debugging..
34  * But could use other stuff too.
35  */
36 class TopicRegistry
37 {
38  /* TODO(DGRAPH-18): Use std::type_index (from <typeindex>) as the
39  * registry's index when on C++11. */
40  std::map<const char*, BaseTopic*> registry;
41 public:
42 
43  /**
44  * @brief _Internal_ - Retrieves a Topic pointer for a given TopicState.
45  */
46  template<class TTopicState>
48  {
49 #if __cplusplus >= 201103L
50  static_assert(std::is_base_of<TopicState, TTopicState>::value,
51  "Trying to Resolve non-Topic type.");
52 #endif
53  const char* typeKey = typeid(TTopicState).name();
54  if (registry.count(typeKey) != 0)
55  {
56  return static_cast<Topic<TTopicState>*>(registry[typeKey]);
57  }
58  return NULL;
59  }
60 
61  /**
62  * @brief _Internal_ - Registers a Topic pointer for a given TopicState.
63  */
64  template<class TTopicState>
66  {
67 #if __cplusplus >= 201103L
68  static_assert(std::is_base_of<TopicState, TTopicState>::value,
69  "Trying to Register non-Topic type.");
70 #endif
71  registry[typeid(TTopicState).name()] = obj;
72  }
73 };
74 
75 } // namespace DetectorGraph
76 
77 #endif // DETECTORGRAPH_INCLUDE_TOPICREGISTRY_STL_HPP_
Manage data and its handler.
Definition: topic.hpp:84
void Register(Topic< TTopicState > *obj)
Internal - Registers a Topic pointer for a given TopicState.
Topic< TTopicState > * Resolve()
Internal - Retrieves a Topic pointer for a given TopicState.