client-cpp  0.6.1
CommonValue.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 COMMONVALUE_HPP_
18 #define COMMONVALUE_HPP_
19 
20 #include "kaa/KaaDefaults.hpp"
21 
22 #ifdef KAA_USE_CONFIGURATION
23 
25 
26 #include <boost/ref.hpp>
27 #include <memory>
28 #include <cstdint>
29 #include <iomanip>
30 #include <sstream>
31 
32 namespace kaa {
33 
34 template <typename T, CommonValueType CVT>
35 class CommonValue : public ICommonValue {
36 public:
37  typedef T value_type;
38  typedef T * value_ptr;
39  typedef T & value_ref;
40  typedef const T & value_cref;
41 
42  typedef std::shared_ptr<T> shared_ptr;
43 
44  CommonValue(value_cref value, std::size_t len = 0);
45  CommonValue(const CommonValue<T, CVT> &r);
46  ~CommonValue();
47 
48  const boost::any getValue() const { return boost::cref(value_).get(); }
49  avro::GenericDatum toAvro() const;
50  std::string toString() const;
51 private:
52  T value_;
53  std::size_t valLength_;
54 };
55 
56 template <>
57 CommonValue<std::uint8_t *, CommonValueType::COMMON_BYTES>::~CommonValue()
58 {
59  delete[] value_;
60 }
61 
62 template <typename T, CommonValueType CVT>
63 CommonValue<T, CVT>::CommonValue(value_cref value, std::size_t len) : ICommonValue(CVT), value_(value), valLength_(len)
64 {
65 
66 }
67 
68 template <typename T, CommonValueType CVT>
69 CommonValue<T, CVT>::CommonValue(const CommonValue<T, CVT> &r) : ICommonValue(CVT), value_(r.value_), valLength_(r.valLength_)
70 {
71 }
72 
73 template <typename T, CommonValueType CVT>
74 CommonValue<T, CVT>::~CommonValue()
75 {
76 
77 }
78 
79 template <typename T, CommonValueType CVT>
80 std::string CommonValue<T, CVT>::toString() const
81 {
82  std::stringstream ss;
83  ss << value_;
84  return ss.str();
85 }
86 
87 template <>
88 std::string CommonValue<std::string, CommonValueType::COMMON_STRING>::toString() const
89 {
90  std::stringstream ss;
91  ss << "\"" << value_ << "\"";
92  return ss.str();
93 }
94 
95 template <>
96 std::string CommonValue<std::vector<std::uint8_t>, CommonValueType::COMMON_BYTES>::toString() const
97 {
98  std::stringstream ss;
99  for (auto it = value_.begin(); it != value_.end();) {
100  ss << std::setw(2) << std::setfill('0') << std::hex << (int)*it << std::dec;
101  if (++it != value_.end()) {
102  ss << "-";
103  }
104  }
105  return ss.str();
106 }
107 
108 template <typename T, CommonValueType CVT>
109 avro::GenericDatum CommonValue<T, CVT>::toAvro() const
110 {
111  avro::GenericDatum datum(value_);
112  return datum;
113 }
114 
115 template <>
116 CommonValue<std::uint8_t *, CommonValueType::COMMON_BYTES>::CommonValue(value_cref value, std::size_t len)
117  : ICommonValue(CommonValueType::COMMON_BYTES)
118  , value_(new std::uint8_t[len])
119  , valLength_(len)
120 {
121  std::copy(value, value + len, value_);
122 }
123 
124 template <>
125 CommonValue<std::uint8_t *, CommonValueType::COMMON_BYTES>::CommonValue(const CommonValue<std::uint8_t *, CommonValueType::COMMON_BYTES> &r)
126  : ICommonValue(CommonValueType::COMMON_BYTES)
127  , value_(new std::uint8_t[r.valLength_])
128  , valLength_(r.valLength_)
129 {
130  std::copy(r.value_, r.value_ + valLength_, value_);
131 }
132 
133 template <>
134 std::string CommonValue<std::uint8_t *, CommonValueType::COMMON_BYTES>::toString() const
135 {
136  std::stringstream ss;
137  for (std::size_t i = 0; i < valLength_; ) {
138  ss << std::setw(2) << std::setfill('0') << std::hex << (int)value_[i] << std::dec;
139  if (++i != valLength_) {
140  ss << "-";
141  }
142  }
143  return ss.str();
144 }
145 
146 } // namespace kaa
147 
148 #endif
149 
150 #endif /* COMMONVALUE_HPP_ */