DetectorGraph  2.0
topicstate.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_TOPICSTATE_HPP_
16 #define DETECTORGRAPH_INCLUDE_TOPICSTATE_HPP_
17 
18 #if !defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE)
19 // FULL_BEGIN
20 #include <typeinfo>
21 // FULL_END
22 #endif
23 
24 namespace DetectorGraph
25 {
26 
27 typedef int TopicStateIdType;
28 
29 /**
30  * @brief Base struct for topic data types
31  *
32  * This should be seen like a POD data container.
33  * A simple example could be:
34  * @code
35 struct AccelerometerData : public TopicState {
36  int32_t x;
37  int32_t y;
38  int32_t z;
39 };
40  * @endcode
41  *
42  * Ideally all TopicStates are self-explanatory and self-contained;
43  * a subscriber shouldn't need anything else to act on it.
44  * The rationale for calling it 'struct' instead of 'class':
45  * - This is supposed to be a POD-like data container
46  * - All methods here are only to support the data-carrying
47  * functionality of TopicStates.
48  * - This struct and its subclasses should not have any
49  * private members.
50  *
51  */
52 struct TopicState
53 {
54  TopicState() { }
55  virtual ~TopicState() { }
56 
57 #if !defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE)
58 // FULL_BEGIN
59  /**
60  * @brief Get type name of itself
61  *
62  * This method is a convenience method that can be used for
63  * graph debugging. It uses RTTI to generate a descriptive
64  * string that identifies the child of TopicState. This strings
65  * are compiler-specific but mostly based on the actual name of
66  * struct that inherits from TopicState.
67  */
68  const char * GetName() const
69  {
70  return typeid(*this).name(); // LCOV_EXCL_LINE
71  }
72 
73  /**
74  * @brief Default @ref GetId() return value.
75  */
76  enum { kAnonymousTopicState = -1 };
77 
78  /**
79  * @brief Returns the TopicStateId for this TopicState - to be implemented.
80  *
81  * If not implemented on a derived class it returns -1 (not implemented)
82  * meaning this TopicState is an anonymous TopicState (only used within the
83  * graph). This refers to a public number-space defined by the application
84  * for types that are intended to be provided to (or from) the outside (of
85  * the graph).
86  *
87  * For a full discussion/example of how to use _Named TopicStates_ see
88  * [Trivial Vending Machine Example](@ref trivialvendingmachine.cpp)
89  */
90  virtual TopicStateIdType GetId() const
91  {
92  return (TopicStateIdType)kAnonymousTopicState;
93  }
94 
95  /**
96  * @brief Convenience templated static method to retrieve the ID for a
97  * Type when an instance is not available.
98  */
99  template<class TTopic>
100  static TopicStateIdType GetId()
101  {
102  static const TTopic dummy = TTopic();
103  return dummy.GetId();
104  } // LCOV_EXCL_LINE
105 
106 // FULL_END
107 #endif
108 
109 };
110 
111 } // namespace DetectorGraph
112 
113 #endif // DETECTORGRAPH_INCLUDE_TOPICSTATE_HPP_
virtual TopicStateIdType GetId() const
Returns the TopicStateId for this TopicState - to be implemented.
Definition: topicstate.hpp:90
const char * GetName() const
Get type name of itself.
Definition: topicstate.hpp:68
static TopicStateIdType GetId()
Convenience templated static method to retrieve the ID for a Type when an instance is not available...
Definition: topicstate.hpp:100
Base struct for topic data types.
Definition: topicstate.hpp:52
int TopicStateIdType
Definition: topicstate.hpp:27