DetectorGraph  2.0
statesnapshot.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_STATESNAPSHOT_HPP_
16 #define DETECTORGRAPH_INCLUDE_STATESNAPSHOT_HPP_
17 
18 #include <map>
19 #include <list>
20 #include <stdint.h>
21 
22 #include "sharedptr.hpp"
23 #include "topicstate.hpp"
24 
25 namespace DetectorGraph
26 {
27 
28 /**
29  * @brief The collection of TopicStates that represents the graph state so far.
30  *
31  * Responsible for conveying and composing a complete state set in the form of
32  * a collection of TopicState shared_ptrs.
33  */
35 {
36 public:
37  /**
38  * @brief T=0 Constructor
39  *
40  * Builds an initial and empty StateSnapshot.
41  */
42  StateSnapshot();
43 
44  /**
45  * @brief T=0 Priming Constructor
46  *
47  * Builds a prime StateSnapshot from a TopicState list.
48  */
49  StateSnapshot(const std::list< ptr::shared_ptr<const TopicState> >& arTopicStates);
50 
51  /**
52  * @brief T>0 Constructor
53  *
54  * Builds a StateSnapshot from a previous StateSnapshot and an output list.
55  */
56  StateSnapshot(const StateSnapshot& arPreviousState, const std::list< ptr::shared_ptr<const TopicState> >& arTopicStates);
57 
58  /**
59  * @brief Destructor
60  *
61  * Note that the shared ptrs held by a snapshot are implicitly released by
62  * normal shared_ptr destructors.
63  */
65 
66  /**
67  * @brief Returns a TopicState for a given Id
68  *
69  * Or an empty shared_ptr in case no state with that Id exists.
70  */
71  ptr::shared_ptr<const TopicState> GetState(TopicStateIdType aId) const;
72 
73  /**
74  * @brief Returns the specific TopicState for a given its type
75  *
76  * Or an empty shared_ptr in case no state with that Id exists.
77  */
78  template<class T>
79  ptr::shared_ptr<const T> GetState() const
80  {
81  return ptr::static_pointer_cast<const T>(GetState(DetectorGraph::TopicState::GetId<T>()));
82  }
83 
84  /**
85  * @brief Returns the number of TopicStates in the Snapshot
86  */
87  size_t GetMapSize() const;
88 
89  /**
90  * @brief Returns the version of this snapshot.
91  *
92  * A Snapshot version is a unique positive integer that is incremented at
93  * every new Snapshot creation.
94  */
95  unsigned int GetStateVersion() const;
96 
97  /**
98  * @brief Gets TopicStates stored in this snapshot
99  *
100  * Fills aOutTopicStateList with all public TopicStates stored in this Snapshot.
101  * This can be useful for merging the contents of this snapshot into another.
102  */
103  void GetTopicStates(std::list< ptr::shared_ptr<const TopicState> >& aOutTopicStateList) const;
104 
105 private:
106  void UpdateValues(const std::list< ptr::shared_ptr<const TopicState> >& arTopicStates);
107 
108 private:
109  std::map< TopicStateIdType, ptr::shared_ptr<const TopicState> > mStateStore;
110  unsigned int mStateVersion;
111 };
112 
113 }
114 
115 #endif // DETECTORGRAPH_INCLUDE_STATESNAPSHOT_HPP_
void GetTopicStates(std::list< ptr::shared_ptr< const TopicState > > &aOutTopicStateList) const
Gets TopicStates stored in this snapshot.
StateSnapshot()
T=0 Constructor.
unsigned int GetStateVersion() const
Returns the version of this snapshot.
The collection of TopicStates that represents the graph state so far.
ptr::shared_ptr< const T > GetState() const
Returns the specific TopicState for a given its type.
size_t GetMapSize() const
Returns the number of TopicStates in the Snapshot.
int TopicStateIdType
Definition: topicstate.hpp:27