Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET Apache Qpid Documentation
FieldValue.h
Go to the documentation of this file.
1 #ifndef _framing_FieldValue_h
2 #define _framing_FieldValue_h
3 /*
4  *
5  * Licensed to the Apache Software Foundation (ASF) under one
6  * or more contributor license agreements. See the NOTICE file
7  * distributed with this work for additional information
8  * regarding copyright ownership. The ASF licenses this file
9  * to you under the Apache License, Version 2.0 (the
10  * "License"); you may not use this file except in compliance
11  * with the License. You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing,
16  * software distributed under the License is distributed on an
17  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18  * KIND, either express or implied. See the License for the
19  * specific language governing permissions and limitations
20  * under the License.
21  *
22  */
23 
24 #include "qpid/Exception.h"
26 #include "qpid/framing/Buffer.h"
29 
30 #include <iostream>
31 #include <memory>
32 #include <vector>
33 
34 #include <assert.h>
35 
36 namespace qpid {
37 namespace framing {
38 
45 
53 };
54 
55 class List;
56 
63  public:
64  /*
65  * Abstract type for content of different types
66  */
67  class Data {
68  public:
69  virtual ~Data() {};
70  virtual uint32_t encodedSize() const = 0;
71  virtual void encode(Buffer& buffer) = 0;
72  virtual void decode(Buffer& buffer) = 0;
73  virtual bool operator==(const Data&) const = 0;
74 
75  virtual bool convertsToInt() const { return false; }
76  virtual bool convertsToString() const { return false; }
77  virtual int64_t getInt() const { throw InvalidConversionException();}
78  virtual std::string getString() const { throw InvalidConversionException(); }
79 
80  virtual void print(std::ostream& out) const = 0;
81  };
82 
83  FieldValue(): data(0) {};
84  // Default assignment operator is fine
85  void setType(uint8_t type);
86  QPID_COMMON_EXTERN uint8_t getType() const;
87  Data& getData() { return *data; }
88  uint32_t encodedSize() const { return 1 + data->encodedSize(); };
89  bool empty() const { return data.get() == 0; }
90  void encode(Buffer& buffer);
91  void decode(Buffer& buffer);
92  QPID_COMMON_EXTERN bool operator==(const FieldValue&) const;
93  QPID_COMMON_INLINE_EXTERN bool operator!=(const FieldValue& v) const { return !(*this == v); }
94 
95  QPID_COMMON_EXTERN void print(std::ostream& out) const;
96 
97  template <typename T> bool convertsTo() const { return false; }
98  template <typename T> T get() const { throw InvalidConversionException(); }
99 
100  template <class T, int W> T getIntegerValue() const;
101  template <class T> T getIntegerValue() const;
102  template <class T, int W> T getFloatingPointValue() const;
103  template <int W> void getFixedWidthValue(unsigned char*) const;
104  template <class T> bool get(T&) const;
105 
106  protected:
107  FieldValue(uint8_t t, Data* d): typeOctet(t), data(d) {}
108 
109  QPID_COMMON_EXTERN static uint8_t* convertIfRequired(uint8_t* const octets, int width);
110 
111  private:
112  uint8_t typeOctet;
113  std::auto_ptr<Data> data;
114 
115 };
116 
117 template <>
118 inline bool FieldValue::convertsTo<int>() const { return data->convertsToInt(); }
119 
120 template <>
121 inline bool FieldValue::convertsTo<int64_t>() const { return data->convertsToInt(); }
122 
123 template <>
124 inline bool FieldValue::convertsTo<std::string>() const { return data->convertsToString(); }
125 
126 template <>
127 inline int FieldValue::get<int>() const { return static_cast<int>(data->getInt()); }
128 
129 template <>
130 inline int64_t FieldValue::get<int64_t>() const { return data->getInt(); }
131 
132 template <>
133 inline std::string FieldValue::get<std::string>() const { return data->getString(); }
134 
135 inline std::ostream& operator<<(std::ostream& out, const FieldValue& v) {
136  v.print(out);
137  return out;
138 }
139 
140 template <int width>
142  uint8_t octets[width];
143 
144  public:
146  FixedWidthValue(const uint8_t (&data)[width]) : octets(data) {}
147  FixedWidthValue(const uint8_t* const data)
148  {
149  for (int i = 0; i < width; i++) octets[i] = data[i];
150  }
151  FixedWidthValue(uint64_t v)
152  {
153  for (int i = width; i > 1; --i) {
154  octets[i-1] = (uint8_t) (0xFF & v); v >>= 8;
155  }
156  octets[0] = (uint8_t) (0xFF & v);
157  }
158  uint32_t encodedSize() const { return width; }
159  void encode(Buffer& buffer) { buffer.putRawData(octets, width); }
160  void decode(Buffer& buffer) { buffer.getRawData(octets, width); }
161  bool operator==(const Data& d) const {
162  const FixedWidthValue<width>* rhs = dynamic_cast< const FixedWidthValue<width>* >(&d);
163  if (rhs == 0) return false;
164  else return std::equal(&octets[0], &octets[width], &rhs->octets[0]);
165  }
166 
167  bool convertsToInt() const { return true; }
168  int64_t getInt() const
169  {
170  int64_t v = 0;
171  for (int i = 0; i < width-1; ++i) {
172  v |= octets[i]; v <<= 8;
173  }
174  v |= octets[width-1];
175  return v;
176  }
177  uint8_t* rawOctets() { return octets; }
178  const uint8_t* rawOctets() const { return octets; }
179 
180  void print(std::ostream& o) const { o << "F" << width << ":"; };
181 };
182 
183 class UuidData : public FixedWidthValue<16> {
184  public:
185  UuidData();
186  UuidData(const unsigned char* bytes);
187  bool convertsToString() const;
188  std::string getString() const;
189 };
190 
191 template <class T, int W>
193 {
194  FixedWidthValue<W>* const fwv = dynamic_cast< FixedWidthValue<W>* const>(data.get());
195  if (fwv) {
196  uint8_t* octets = fwv->rawOctets();
197  T v = 0;
198  for (int i = 0; i < W-1; ++i) {
199  v |= octets[i]; v <<= 8;
200  }
201  v |= octets[W-1];
202  return v;
203  } else {
205  }
206 }
207 
208 template <class T>
210 {
211  FixedWidthValue<1>* const fwv = dynamic_cast< FixedWidthValue<1>* const>(data.get());
212  if (fwv) {
213  uint8_t* octets = fwv->rawOctets();
214  return octets[0];
215  } else {
217  }
218 }
219 
220 template <class T, int W>
222  FixedWidthValue<W>* const fwv = dynamic_cast< FixedWidthValue<W>* const>(data.get());
223  if (fwv) {
224  T value;
225  uint8_t* const octets = convertIfRequired(fwv->rawOctets(), W);
226  uint8_t* const target = reinterpret_cast<uint8_t*>(&value);
227  for (size_t i = 0; i < W; ++i) target[i] = octets[i];
228  return value;
229  } else {
231  }
232 }
233 
234 template <int W> void FieldValue::getFixedWidthValue(unsigned char* value) const
235 {
236  FixedWidthValue<W>* const fwv = dynamic_cast< FixedWidthValue<W>* const>(data.get());
237  if (fwv) {
238  for (size_t i = 0; i < W; ++i) value[i] = fwv->rawOctets()[i];
239  } else {
241  }
242 }
243 
244 template <>
245 inline float FieldValue::get<float>() const {
246  return getFloatingPointValue<float, 4>();
247 }
248 
249 template <>
250 inline double FieldValue::get<double>() const {
251  return getFloatingPointValue<double, 8>();
252 }
253 
254 template <>
256  public:
257  // Implicit default constructor is fine
258  uint32_t encodedSize() const { return 0; }
259  void encode(Buffer&) {};
260  void decode(Buffer&) {};
261  bool operator==(const Data& d) const {
262  const FixedWidthValue<0>* rhs = dynamic_cast< const FixedWidthValue<0>* >(&d);
263  return rhs != 0;
264  }
265  void print(std::ostream& o) const { o << "F0"; };
266 };
267 
268 template <int lenwidth>
270  std::vector<uint8_t> octets;
271 
272  public:
274  VariableWidthValue(const std::vector<uint8_t>& data) : octets(data) {}
275  VariableWidthValue(const uint8_t* start, const uint8_t* end) : octets(start, end) {}
276  uint32_t encodedSize() const { return lenwidth + octets.size(); }
277  void encode(Buffer& buffer) {
278  buffer.putUInt<lenwidth>(octets.size());
279  if (octets.size() > 0)
280  buffer.putRawData(&octets[0], octets.size());
281  };
282  void decode(Buffer& buffer) {
283  uint32_t len = buffer.getUInt<lenwidth>();
284  buffer.checkAvailable(len);
285  octets.resize(len);
286  if (len > 0)
287  buffer.getRawData(&octets[0], len);
288  }
289  bool operator==(const Data& d) const {
290  const VariableWidthValue<lenwidth>* rhs = dynamic_cast< const VariableWidthValue<lenwidth>* >(&d);
291  if (rhs == 0) return false;
292  else return octets==rhs->octets;
293  }
294 
295  bool convertsToString() const { return true; }
296  std::string getString() const { return std::string(octets.begin(), octets.end()); }
297 
298  void print(std::ostream& o) const { o << "V" << lenwidth << ":" << octets.size() << ":"; };
299 };
300 
301 template <class T>
303  T value;
304  public:
305 
307  EncodedValue(const T& v) : value(v) {}
308 
309  T& getValue() { return value; }
310  const T& getValue() const { return value; }
311 
312  uint32_t encodedSize() const { return value.encodedSize(); }
313 
314  void encode(Buffer& buffer) {
315  value.encode(buffer);
316  };
317  void decode(Buffer& buffer) {
318  value.decode(buffer);
319  }
320  bool operator==(const Data& d) const {
321  const EncodedValue<T>* rhs = dynamic_cast< const EncodedValue<T>* >(&d);
322  if (rhs == 0) return false;
323  else return value==rhs->value;
324  }
325 
326  void print(std::ostream& o) const { o << "[" << value << "]"; };
327 };
328 
333 template <class T>
334 inline bool FieldValue::get(T& t) const
335 {
336  const EncodedValue<T>* v = dynamic_cast< EncodedValue<T>* >(data.get());
337  if (v != 0) {
338  t = v->getValue();
339  return true;
340  } else {
341  try {
342  t = get<T>();
343  return true;
344  } catch (const InvalidConversionException&) {
345  return false;
346  }
347  }
348 }
349 
350 class Str8Value : public FieldValue {
351  public:
352  QPID_COMMON_EXTERN Str8Value(const std::string& v);
353 };
354 
355 class Str16Value : public FieldValue {
356  public:
357  QPID_COMMON_EXTERN Str16Value(const std::string& v);
358 };
359 
360 class Var16Value : public FieldValue {
361  public:
362  QPID_COMMON_EXTERN Var16Value(const std::string& v, uint8_t code);
363 };
364 
365 class Var32Value : public FieldValue {
366  public:
367  QPID_COMMON_EXTERN Var32Value(const std::string& v, uint8_t code);
368  };
369 
370 class Struct32Value : public FieldValue {
371  public:
372  QPID_COMMON_EXTERN Struct32Value(const std::string& v);
373 };
374 
375 class FloatValue : public FieldValue
376 {
377  public:
379 };
380 class DoubleValue : public FieldValue
381 {
382  public:
384 };
385 
386 /*
387  * Basic integer value encodes as signed 32 bit
388  */
389 class IntegerValue : public FieldValue {
390  public:
392 };
393 
394 class TimeValue : public FieldValue {
395  public:
396  QPID_COMMON_EXTERN TimeValue(uint64_t v);
397 };
398 
399 class Integer64Value : public FieldValue {
400  public:
402 };
403 
404 class Unsigned64Value : public FieldValue {
405  public:
407 };
408 
409 class FieldTableValue : public FieldValue {
410  public:
413 };
414 
415 class ArrayValue : public FieldValue {
416  public:
418 };
419 
420 class VoidValue : public FieldValue {
421  public:
423 };
424 
425 class BoolValue : public FieldValue {
426  public:
428 };
429 
430 class Unsigned8Value : public FieldValue {
431  public:
433 };
434 
435 class Unsigned16Value : public FieldValue {
436  public:
438 };
439 
440 class Unsigned32Value : public FieldValue {
441  public:
443 };
444 
445 class Integer8Value : public FieldValue {
446  public:
448 };
449 
450 class Integer16Value : public FieldValue {
451  public:
453 };
454 
456 
457 class ListValue : public FieldValue {
458  public:
459  typedef List ValueType;
461 };
462 
463 class UuidValue : public FieldValue {
464  public:
466  QPID_COMMON_EXTERN UuidValue(const unsigned char*);
467 };
468 
469 template <class T>
471 {
472  if (vptr) {
473  const EncodedValue<T>* ev = dynamic_cast< EncodedValue<T>* >(&(vptr->getData()));
474  if (ev != 0) {
475  value = ev->getValue();
476  return true;
477  }
478  }
479  return false;
480 }
481 
482 }} // qpid::framing
483 
484 #endif

Qpid C++ API Reference
Generated on Fri Aug 29 2014 for Qpid C++ Client API by doxygen 1.8.3.1