DetectorGraph  2.0
futurepublisher.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_FUTUREPUBLISHER_HPP_
16 #define DETECTORGRAPH_INCLUDE_FUTUREPUBLISHER_HPP_
17 
18 #include "graph.hpp"
19 #include "dgassert.hpp"
20 
21 namespace DetectorGraph
22 {
23 
24 /**
25  * @brief Publish data to the graph for future evaluation.
26  *
27  * Detectors inherit from a @ref FuturePublisher<T> to implement the
28  * behavior of being a Publisher of T for a future/next evaluation.
29  * This differs from a normal Publisher in a very fundamental way, it
30  * enables a detector to publish something to an upstream topic - thus
31  * constructing a feedback loop.
32  * Normally, publishing to an upstream topic would create a directed
33  * cycle in the graph thus making it impossible to be topologically
34  * sorted.
35  *
36  * Using FuturePublisher a detector can implement a feedback loop
37  * while still making sure the topological nature of graph evaluation
38  * is preserved.
39  *
40  * A class `FooDetector` acquires the "FuturePublisher of BarTopicState"
41  * behavior by inheriting `FuturePublisher` templated to `BarTopicState`.
42  *
43  * This is a template class and so it must be kept minimal to prevent code
44  * bloat. This class serves a tiny purpose:
45  * - Document in a clear way the "FuturePublishing" behavior of an inheriting
46  * Detector.
47  * - Caches a pointer to the @ref Graph
48  * - Abstracts the act of Publishing to the Graph into a type-safe method.
49  *
50  * The only requirement to use the @ref FuturePublisher functionality is to
51  * call Detector::SetupFuturePublishing from the inheriting Detector's constructor:
52  *
53  * Below is an [example](@ref counterwithreset.cpp):
54  * @snippet counterwithreset.cpp Reset Detector
55  *
56  * When implementing feedback loops in a graph one should also consider
57  * DetectorGraph::Lag as in some cases it's more general and extensible.
58  *
59  */
60 template<class T>
62 {
63 public:
64 
66  {
67  }
68 
69  void SetGraph(Graph* aGraph)
70  {
71  mGraph = aGraph;
72  }
73 
74  /**
75  * @brief Publish a new version of T to the Graph for future evaluation.
76  */
77  void PublishOnFutureEvaluation(const T& aData)
78  {
80  mGraph->PushData<T>(aData);
81  }
82 
83 protected:
85 };
86 
87 } // namespace DetectorGraph
88 
89 #endif // DETECTORGRAPH_INCLUDE_FUTUREPUBLISHER_HPP_
Implements a graph of Topics & Detectors with Input/Output APIs.
Definition: graph.hpp:127
Publish data to the graph for future evaluation.
void PushData(const TTopicState &aTopicState)
Push data to a specific topic in the graph.
Definition: graph.hpp:187
void PublishOnFutureEvaluation(const T &aData)
Publish a new version of T to the Graph for future evaluation.
#define DG_ASSERT(condition)
Definition: dgassert.hpp:20