DetectorGraph  2.0
publisher.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_PUBLISHER_HPP_
16 #define DETECTORGRAPH_INCLUDE_PUBLISHER_HPP_
17 
18 #include "graph.hpp"
19 #include "topic.hpp"
20 #include "dgassert.hpp"
21 
22 namespace DetectorGraph
23 {
24 /**
25  * @brief Base class that implements a Publisher behavior.
26  *
27  * Detectors inherit from a number of versions of this class
28  * to implement their output set.
29  *
30  * A class `FooDetector` acquires the "Publisher of BarTopicState"
31  * behavior by inheriting `Publisher` templated to `BarTopicState`.
32  *
33  * @code
34 class FooDetector :
35  public Detector,
36  public Publisher<BarTopicState>
37 {
38  // ...
39 }
40  * @endcode
41  *
42  * This class serves a tiny purpose:
43  * - Abstracts the act of Publishing to a Topic into a type-safe method.
44  * - Caches the output Topic for this edge.
45  * - Document in a clear way the Publishing behavior of an inheriting Detector.
46  *
47  * The enable the Publishing arc, detectors must call Detector::SetupPublishing:
48  * @code
49 class FooDetector :
50  public Detector,
51  public Publisher<BarTopicState>
52 {
53  FooDetector(Graph* graph) : Detector(graph)
54  {
55  SetupPublishing<BarTopicState>(this);
56  }
57 }
58  * @endcode
59  *
60  * If you publish multiple items you will need to either qualify each `Publish`
61  * or include a `using` declaration in your header. See <a href="http://stackoverflow.com/questions/5368862/why-do-multiple-inherited-functions-with-same-name-but-different-signatures-not">StackOverflow</a>
62  * for more information
63  *
64  */
65 template<class T>
66 class Publisher
67 {
68 protected:
70 
71 public:
72 
73  Publisher() : mTopic(NULL)
74  {
75  }
76 
77  void SetGraph(Graph* aGraph)
78  {
79  mTopic = aGraph->ResolveTopic<T>();
80  }
81 
82  /**
83  * @brief Publish a new version of T to a Topic
84  */
85  void Publish(const T& data)
86  {
87  DG_ASSERT(mTopic);
88  mTopic->Publish(data);
89  }
90 };
91 
92 } // namespace DetectorGraph
93 
94 #endif // DETECTORGRAPH_INCLUDE_PUBLISHER_HPP_
void Publish(const T &arPayload)
Append data to its vector.
Definition: topic.hpp:110
Implements a graph of Topics & Detectors with Input/Output APIs.
Definition: graph.hpp:127
Manage data and its handler.
Definition: topic.hpp:84
void SetGraph(Graph *aGraph)
Definition: publisher.hpp:77
void Publish(const T &data)
Publish a new version of T to a Topic.
Definition: publisher.hpp:85
Topic< TTopicState > * ResolveTopic()
Find/add a topic in the detector graph.
Definition: graph.hpp:160
Base class that implements a Publisher behavior.
Definition: publisher.hpp:66
#define DG_ASSERT(condition)
Definition: dgassert.hpp:20