DetectorGraph  2.0
subscriptiondispatcherscontainer-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_SUBSCRIPTIONDISPATCHERCONTAINER_LITE_HPP_
16 #define DETECTORGRAPH_INCLUDE_SUBSCRIPTIONDISPATCHERCONTAINER_LITE_HPP_
17 
19 #include "dgassert.hpp"
20 #include "detectorgraphliteconfig.hpp"
21 #include "dgstdincludes.hpp"
22 
23 namespace DetectorGraph
24 {
25 
26 /**
27  * @brief _Internal_ - Manages a fixed number of SubscriptionDispatchers.
28  *
29  * This class is responsible for creating and owning SubscriptionDispatcher
30  * for a particular detector. A detector that subscribes to two Topics will
31  * have two SubscriptionDispatchers.
32  *
33  * This is a STL-less & heap-less implementation of the same as:
34  * subscriptiondispatcherscontainer-stl.hpp
35  */
37 {
38 public:
39 
40  SubscriptionDispatchersContainer() : mNumInDispatchers()
41  {
42  // Since we use a raw buffer to store dispatchers and index it by
43  // increments of sizeof(SubscriptionDispatcher<T>) we must ensure
44  // that all instances are aligned.
45  DG_ASSERT(sizeof(SubscriptionDispatcher<TopicState>) % sizeof(uint32_t) == 0);
46  }
47 
48  template<class TTopicState>
50  {
51  // Purely a paranoid/documentation assert. Dispatchers do not depend on
52  // the size of TTopicState and thus we can have a array of multiple
53  // dispatchers of different types.
55 
56  // The below will fail if one of your Detectors has more "in" edges
57  // than kMaxNumberOfInEdges. That value should be the max number of
58  // subscriptions by any detector.
59  // Bump that config value when necessary.
60  DG_ASSERT(mNumInDispatchers < DetectorGraphConfig::kMaxNumberOfInEdges);
61 
62  // Computes position to create dispatchers on.
63  uint8_t* dispatcherStorage = &(mDispatchersStorage[mNumInDispatchers * sizeof(SubscriptionDispatcher<TopicState>)]);
64  SubscriptionDispatcherInterface* dispatcher = new(dispatcherStorage) SubscriptionDispatcher<TTopicState>(topic, subscriber);
65 
66  mInDispatchers[mNumInDispatchers++] = dispatcher;
67  }
68 
69  SubscriptionDispatcherInterface* const (& GetDispatchers() const)[DetectorGraphConfig::kMaxNumberOfInEdges]
70  {
71  return mInDispatchers;
72  }
73 
74  const size_t GetSize() const
75  {
76  return mNumInDispatchers;
77  }
78 
80  {
81  // This is for being pedantic and future-proof.
82  // Right now this could be compiled or flagged out.
83  for (unsigned i = 0; i != GetSize(); ++i)
84  {
86  }
87  }
88 private:
89  uint8_t mDispatchersStorage[DetectorGraphConfig::kMaxNumberOfInEdges * sizeof(SubscriptionDispatcher<TopicState>)];
90  SubscriptionDispatcherInterface* mInDispatchers[DetectorGraphConfig::kMaxNumberOfInEdges];
91  size_t mNumInDispatchers;
92 };
93 
94 } // namespace DetectorGraph
95 
96 #endif // DETECTORGRAPH_INCLUDE_SUBSCRIPTIONDISPATCHERCONTAINER_LITE_HPP_
Internal - Manages a fixed number of SubscriptionDispatchers.
SubscriptionDispatcherInterface *const (& GetDispatchers() const)[DetectorGraphConfig::kMaxNumberOfInEdges]
Manage data and its handler.
Definition: topic.hpp:84
Internal - Implements the data-out edge from a topic to one of its subscriber.
Internal - Provide interface for a SubscriptionDispatcher
void CreateDispatcher(Topic< TTopicState > *topic, SubscriberInterface< TTopicState > *subscriber)
A Pure interface that declares the Subscriber behavior.
#define DG_ASSERT(condition)
Definition: dgassert.hpp:20