client-cpp  0.0.1-SNAPSHOT
ValueDeltaType.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 VALUEDELTATYPE_HPP_
18 #define VALUEDELTATYPE_HPP_
19 
20 #include <iomanip>
21 #include <sstream>
22 #include <vector>
23 
26 
27 namespace kaa {
28 
34 public:
40  ValueDeltaType(const IDeltaType::DeltaValue& data, const avro::Type& type)
41  : value_(data), type_(type) {}
42 
47  inline virtual const DeltaValue& getNewValue() {
48  return value_;
49  }
50 
55  virtual std::string toString() const;
56 
57 private:
59  avro::Type type_;
60 };
61 
62 inline std::string ValueDeltaType::toString() const
63 {
64  std::stringstream ss;
65 
66  try {
67  switch (type_) {
68  case avro::AVRO_RECORD: {
69  ConfigurationDeltaPtr configurationDelta = boost::any_cast<ConfigurationDeltaPtr>(value_);
70  ss << configurationDelta->toString();
71  break;
72  }
73  case avro::AVRO_BOOL: {
74  ss << std::boolalpha << boost::any_cast<bool>(value_);
75  break;
76  }
77  case avro::AVRO_INT: {
78  ss << boost::any_cast<int32_t>(value_);
79  break;
80  }
81  case avro::AVRO_LONG: {
82  ss << boost::any_cast<int64_t>(value_);
83  break;
84  }
85  case avro::AVRO_FLOAT: {
86  ss << boost::any_cast<float>(value_);
87  break;
88  }
89  case avro::AVRO_DOUBLE: {
90  ss << boost::any_cast<double>(value_);
91  break;
92  }
93  case avro::AVRO_STRING: {
94  ss << "\"" << boost::any_cast<std::string>(value_) << "\"";
95  break;
96  }
97  case avro::AVRO_ENUM: {
98  ss << boost::any_cast<std::string>(value_);
99  break;
100  }
101  case avro::AVRO_BYTES:
102  case avro::AVRO_FIXED: {
103  const std::vector<boost::uint8_t> buffer =
104  boost::any_cast<std::vector<boost::uint8_t> >(value_);
105 
106  for (auto it = buffer.begin(); it != buffer.end();) {
107  ss << std::setw(2) << std::setfill('0') << std::hex << (int)*it << std::dec;
108  if (++it != buffer.end()) {
109  ss << "-";
110  }
111  }
112  break;
113  }
114  default: throw KaaException("Unknown avro type");
115  }
116  } catch (...) {
117  ss << "unknown";
118  }
119 
120  return ss.str();
121 }
122 
123 } /* namespace kaa */
124 
125 #endif /* VALUEDELTATYPE_HPP_ */
boost::any DeltaValue
Definition: IDeltaType.hpp:36
ValueDeltaType(const IDeltaType::DeltaValue &data, const avro::Type &type)
virtual std::string toString() const
boost::shared_ptr< IConfigurationDelta > ConfigurationDeltaPtr
virtual const DeltaValue & getNewValue()