client-cpp  0.0.1-SNAPSHOT
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 <list>
21 
22 #include <boost/any.hpp>
23 
25 
26 namespace kaa {
27 
32 public:
37 
42  : addedItemsStorage_(type.addedItemsStorage_) {}
43 
49  : addedItemsStorage_(items) {}
50 
55  virtual const AddedItems& getAddedItems() {
56  return addedItemsStorage_;
57  }
58 
63  virtual std::string toString() const;
64 
68  void addItem(const IDeltaType::DeltaValue& item) {
69  addedItemsStorage_.push_back(item);
70  }
71 
72 
73 private:
74  std::list<IDeltaType::DeltaValue> addedItemsStorage_;
75 };
76 
77 inline std::string AddedItemsDeltaType::toString() const
78 {
79  std::stringstream ss;
80  bool toRecord = false;
81  bool isKnownType = true;
82 
83  ss << "[ ";
84 
85  if (!addedItemsStorage_.empty()) {
86  try {
87  boost::any_cast<ConfigurationDeltaPtr>(addedItemsStorage_.front());
88  toRecord = true;
89  } catch (...) {
90  try {
91  boost::any_cast<DeltaTypePtr>(addedItemsStorage_.front());
92  } catch (...) {
93  isKnownType = false;
94  }
95  }
96 
97  if (isKnownType) {
98  for (auto it = addedItemsStorage_.begin(); it != addedItemsStorage_.end();) {
99  if (toRecord) {
100  ConfigurationDeltaPtr configurationDelta =
101  boost::any_cast<ConfigurationDeltaPtr>(*it);
102  ss << configurationDelta->toString();
103  } else {
104  DeltaTypePtr deltaType = boost::any_cast<DeltaTypePtr>(*it);
105  ss << deltaType->toString();
106  }
107 
108  if (++it != addedItemsStorage_.end()) {
109  ss << ", ";
110  }
111  }
112  } else {
113  ss << "unknown type";
114  }
115  }
116 
117  ss << " ]";
118  return ss.str();
119 }
120 
121 } /* namespace kaa */
122 
123 #endif /* ADDEDITEMSDELTATYPE_HPP_ */
boost::any DeltaValue
Definition: IDeltaType.hpp:36
std::list< DeltaValue > AddedItems
Definition: IDeltaType.hpp:37
virtual std::string toString() const
AddedItemsDeltaType(const AddedItemsDeltaType &type)
boost::shared_ptr< IConfigurationDelta > ConfigurationDeltaPtr
boost::shared_ptr< IDeltaType > DeltaTypePtr
Definition: IDeltaType.hpp:28
AddedItemsDeltaType(const AddedItems &items)
virtual const AddedItems & getAddedItems()
void addItem(const IDeltaType::DeltaValue &item)