client-cpp  0.6.3
AddedItemsDeltaType.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ADDEDITEMSDELTATYPE_HPP_
18 #define ADDEDITEMSDELTATYPE_HPP_
19 
20 #include "kaa/KaaDefaults.hpp"
21 
22 #ifdef KAA_USE_CONFIGURATION
23 
24 #include <list>
25 
26 #include <boost/any.hpp>
27 
29 
30 namespace kaa {
31 
35 class AddedItemsDeltaType : public EmptyDeltaType {
36 public:
40  AddedItemsDeltaType() {}
41 
45  AddedItemsDeltaType(const AddedItemsDeltaType& type)
46  : addedItemsStorage_(type.addedItemsStorage_) {}
47 
52  AddedItemsDeltaType(const AddedItems& items)
53  : addedItemsStorage_(items) {}
54 
59  virtual const AddedItems& getAddedItems() {
60  return addedItemsStorage_;
61  }
62 
67  virtual std::string toString() const;
68 
72  void addItem(const IDeltaType::DeltaValue& item) {
73  addedItemsStorage_.push_back(item);
74  }
75 
76 
77 private:
78  std::list<IDeltaType::DeltaValue> addedItemsStorage_;
79 };
80 
81 inline std::string AddedItemsDeltaType::toString() const
82 {
83  std::stringstream ss;
84  bool toRecord = false;
85  bool isKnownType = true;
86 
87  ss << "[ ";
88 
89  if (!addedItemsStorage_.empty()) {
90  try {
91  boost::any_cast<ConfigurationDeltaPtr>(addedItemsStorage_.front());
92  toRecord = true;
93  } catch (...) {
94  try {
95  boost::any_cast<DeltaTypePtr>(addedItemsStorage_.front());
96  } catch (...) {
97  isKnownType = false;
98  }
99  }
100 
101  if (isKnownType) {
102  for (auto it = addedItemsStorage_.begin(); it != addedItemsStorage_.end();) {
103  if (toRecord) {
104  ConfigurationDeltaPtr configurationDelta =
105  boost::any_cast<ConfigurationDeltaPtr>(*it);
106  ss << configurationDelta->toString();
107  } else {
108  DeltaTypePtr deltaType = boost::any_cast<DeltaTypePtr>(*it);
109  ss << deltaType->toString();
110  }
111 
112  if (++it != addedItemsStorage_.end()) {
113  ss << ", ";
114  }
115  }
116  } else {
117  ss << "unknown type";
118  }
119  }
120 
121  ss << " ]";
122  return ss.str();
123 }
124 
125 } /* namespace kaa */
126 
127 #endif
128 
129 #endif /* ADDEDITEMSDELTATYPE_HPP_ */