DetectorGraph  2.0
statesnapshot.cpp
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 #include "statesnapshot.hpp"
16 
17 #include "dgassert.hpp"
18 #include "dglogging.hpp"
19 
20 namespace DetectorGraph
21 {
22 
24 : mStateStore()
25 , mStateVersion(0)
26 {
27 }
28 
29 StateSnapshot::StateSnapshot(const std::list< ptr::shared_ptr<const TopicState> >& arTopicStates)
30 : mStateStore()
31 , mStateVersion(0)
32 {
33  UpdateValues(arTopicStates);
34 }
35 
36 StateSnapshot::StateSnapshot(const StateSnapshot& arPreviousState, const std::list< ptr::shared_ptr<const TopicState> >& arTopicStates)
37 {
38  // by-value copy will do a shallow & safe copy of all ptr::shared_ptrs
39  mStateStore = arPreviousState.mStateStore;
40  UpdateValues(arTopicStates);
41  mStateVersion = arPreviousState.mStateVersion + 1;
42 }
43 
44 void StateSnapshot::UpdateValues(const std::list< ptr::shared_ptr<const TopicState> >& arTopicStates)
45 {
46  // Used for detection of consecutive/repeated named topics being published on the same evaluation
48 
49  typedef std::list< ptr::shared_ptr<const TopicState> >::const_iterator TopicStatePointerIterator;
50  for (TopicStatePointerIterator tDataIt = arTopicStates.begin();
51  tDataIt != arTopicStates.end();
52  ++tDataIt)
53  {
54  const ptr::shared_ptr<const TopicState> tpTopicState = *tDataIt;
55  if (tpTopicState->GetId() != TopicState::kAnonymousTopicState)
56  {
57  /* A malformed graph could publish more than one named TopicState
58  * with the same name on the same evaluation pass. This is not
59  * supported by StateSnapshot's map (and the second would just
60  * clobber the first one). In order to detect that bad design
61  * this check looks for consecutive TopicStates with matching
62  * names. The logic here relies on the fact that arTopicStates
63  * is composed by concatenating Topics one at a time so that
64  * repeated TopicStates would come consecutively.
65  * Thus if this check fails, multiple named TopicStates were
66  * published on same eval Pass */
67  if (tpTopicState->GetId() == previousTopicStateID)
68  {
69  // LCOV_EXCL_START
70  DG_LOG("!!!!!!!!!!!! Duplicate Detected (GetId = %d, previousTopicStateID = %d, Topic %s)",
71  (int)tpTopicState->GetId(), (int)previousTopicStateID, tpTopicState->GetName());
72  // LCOV_EXCL_STOP
73  } // LCOV_EXCL_LINE
74 
75  // Uncomment line below to re-enable paranoid check for development.
76  // DG_ASSERT(tpTopicState->GetId() != previousTopicStateID);
77 
78  previousTopicStateID = tpTopicState->GetId();
79 
80  // Just copying a ptr::shared_ptr around :)
81  mStateStore[tpTopicState->GetId()] = tpTopicState;
82  }
83  }
84 }
85 
87 {
88 }
89 
90 ptr::shared_ptr<const TopicState> StateSnapshot::GetState(TopicStateIdType aId) const
91 {
92  if (mStateStore.count(aId) > 0)
93  {
94  return mStateStore.at(aId);
95  }
96 
97  return ptr::shared_ptr<const TopicState>();
98 }
99 
100 void StateSnapshot::GetTopicStates(std::list< ptr::shared_ptr<const TopicState> >& aOutTopicStateList) const
101 {
102  typedef std::map< TopicStateIdType, ptr::shared_ptr<const TopicState> >::const_iterator StateStoreIterator;
103  for (StateStoreIterator stateIt = mStateStore.begin();
104  stateIt != mStateStore.end();
105  ++stateIt)
106  {
107  aOutTopicStateList.push_back(stateIt->second);
108  }
109 }
110 
112 {
113  return mStateStore.size();
114 }
115 
116 unsigned int StateSnapshot::GetStateVersion() const
117 {
118  return mStateVersion;
119 }
120 
121 }
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.
void DG_LOG(const char *aLogString,...)
Definition: dglogging.cpp:22
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