DetectorGraph  2.0
topicregistry-lite.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_LITE_HPP_
16 #define DETECTORGRAPH_INCLUDE_TOPICREGISTRY_LITE_HPP_
17 
18 #include "topic.hpp"
19 #include "topicstate.hpp"
20 #include "dgassert.hpp"
21 #include "dgstdincludes.hpp"
22 #include <detectorgraphliteconfig.hpp>
23 
24 namespace DetectorGraph
25 {
26 /**
27  * @brief _Internal_ - A limited-size Container for Topic<T>s
28  *
29  * Graph owns this to manage all the registered topics
30  * This Container uses a single pointer array to store
31  * BaseTopic* indexed by the topic's public ID.
32  */
34 {
35  BaseTopic* registry[DetectorGraphConfig::kMaxNumberOfTopics];
36 public:
37  TopicRegistry() : registry() {}
38  template<class TTopicState> Topic<TTopicState>* Resolve()
39  {
40 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_STATIC_ASSERTS)
41  static_assert(std::is_base_of<TopicState, TTopicState>::value,
42  "Trying to Resolve non-Topic type.");
43 #endif
44  DG_ASSERT(TopicState::GetId<TTopicState>() != TopicState::kAnonymousTopicState);
45  DG_ASSERT(TopicState::GetId<TTopicState>() < DetectorGraphConfig::kMaxNumberOfTopics);
46 
47  // The assert below will fail if a detector is added without having all
48  // its dependencies (Publishing & Subscribed Topics) added beforehand.
49  // For the Lite version of the DetectorGraph the suggested pattern is
50  // to instantiate all Topics before Detectors and then add them
51  // following the Topologic sort order.
52  DG_ASSERT(registry[TopicState::GetId<TTopicState>()] != NULL);
53  return static_cast<Topic<TTopicState>*>(registry[TopicState::GetId<TTopicState>()]);
54  }
55 
56  template<class TTopicState> void Register(Topic<TTopicState>* obj)
57  {
58 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_STATIC_ASSERTS)
59  static_assert(std::is_base_of<TopicState, TTopicState>::value,
60  "Trying to Register non-Topic type.");
61 #endif
62  DG_ASSERT(TopicState::GetId<TTopicState>() != TopicState::kAnonymousTopicState);
63 
64  // The below will fail if you have more Topics than kMaxNumberOfTopics.
65  // Bump that config value when necessary (or link both sizes when
66  // possible)
67  DG_ASSERT(TopicState::GetId<TTopicState>() < DetectorGraphConfig::kMaxNumberOfTopics);
68  DG_ASSERT(registry[TopicState::GetId<TTopicState>()] == NULL);
69  registry[TopicState::GetId<TTopicState>()] = obj;
70  }
71 };
72 
73 } // namespace DetectorGraph
74 
75 #endif // DETECTORGRAPH_INCLUDE_TOPICREGISTRY_LITE_HPP_
Internal - A limited-size Container for Topic<T>s
Manage data and its handler.
Definition: topic.hpp:84
void Register(Topic< TTopicState > *obj)
Topic< TTopicState > * Resolve()
Provide interface for a topic.
Definition: topic.hpp:46
#define DG_ASSERT(condition)
Definition: dgassert.hpp:20