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