client-cpp  0.6.3
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 "kaa/KaaDefaults.hpp"
21 
22 #ifdef KAA_USE_CONFIGURATION
23 
24 #include <iomanip>
25 #include <sstream>
26 #include <vector>
27 #include <cstdint>
28 
31 
32 namespace kaa {
33 
38 class ValueDeltaType: public EmptyDeltaType {
39 public:
45  ValueDeltaType(const IDeltaType::DeltaValue& data, const avro::Type& type)
46  : value_(data), type_(type) {}
47 
52  inline virtual const DeltaValue& getNewValue() {
53  return value_;
54  }
55 
60  virtual std::string toString() const;
61 
62 private:
63  IDeltaType::DeltaValue value_;
64  avro::Type type_;
65 };
66 
67 inline std::string ValueDeltaType::toString() const
68 {
69  std::stringstream ss;
70 
71  try {
72  switch (type_) {
73  case avro::AVRO_RECORD: {
74  ConfigurationDeltaPtr configurationDelta = boost::any_cast<ConfigurationDeltaPtr>(value_);
75  ss << configurationDelta->toString();
76  break;
77  }
78  case avro::AVRO_BOOL: {
79  ss << std::boolalpha << boost::any_cast<bool>(value_);
80  break;
81  }
82  case avro::AVRO_INT: {
83  ss << boost::any_cast<std::int32_t>(value_);
84  break;
85  }
86  case avro::AVRO_LONG: {
87  ss << boost::any_cast<std::int64_t>(value_);
88  break;
89  }
90  case avro::AVRO_FLOAT: {
91  ss << boost::any_cast<float>(value_);
92  break;
93  }
94  case avro::AVRO_DOUBLE: {
95  ss << boost::any_cast<double>(value_);
96  break;
97  }
98  case avro::AVRO_STRING: {
99  ss << "\"" << boost::any_cast<std::string>(value_) << "\"";
100  break;
101  }
102  case avro::AVRO_ENUM: {
103  ss << boost::any_cast<std::string>(value_);
104  break;
105  }
106  case avro::AVRO_BYTES:
107  case avro::AVRO_FIXED: {
108  const std::vector<std::uint8_t> buffer =
109  boost::any_cast<std::vector<std::uint8_t> >(value_);
110 
111  for (auto it = buffer.begin(); it != buffer.end();) {
112  ss << std::setw(2) << std::setfill('0') << std::hex << (int)*it << std::dec;
113  if (++it != buffer.end()) {
114  ss << "-";
115  }
116  }
117  break;
118  }
119  default: throw KaaException("Unknown avro type");
120  }
121  } catch (...) {
122  ss << "unknown";
123  }
124 
125  return ss.str();
126 }
127 
128 } /* namespace kaa */
129 
130 #endif
131 
132 #endif /* VALUEDELTATYPE_HPP_ */