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>
209 inline T FieldValue::getIntegerValue() const
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 {
216  throw InvalidConversionException();
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  octets.resize(len);
285  if (len > 0)
286  buffer.getRawData(&octets[0], len);
287  }
288  bool operator==(const Data& d) const {
289  const VariableWidthValue<lenwidth>* rhs = dynamic_cast< const VariableWidthValue<lenwidth>* >(&d);
290  if (rhs == 0) return false;
291  else return octets==rhs->octets;
292  }
293 
294  bool convertsToString() const { return true; }
295  std::string getString() const { return std::string(octets.begin(), octets.end()); }
296 
297  void print(std::ostream& o) const { o << "V" << lenwidth << ":" << octets.size() << ":"; };
298 };
299 
300 template <class T>
302  T value;
303  public:
304 
306  EncodedValue(const T& v) : value(v) {}
307 
308  T& getValue() { return value; }
309  const T& getValue() const { return value; }
310 
311  uint32_t encodedSize() const { return value.encodedSize(); }
312 
313  void encode(Buffer& buffer) {
314  value.encode(buffer);
315  };
316  void decode(Buffer& buffer) {
317  value.decode(buffer);
318  }
319  bool operator==(const Data& d) const {
320  const EncodedValue<T>* rhs = dynamic_cast< const EncodedValue<T>* >(&d);
321  if (rhs == 0) return false;
322  else return value==rhs->value;
323  }
324 
325  void print(std::ostream& o) const { o << "[" << value << "]"; };
326 };
327 
332 template <class T>
333 inline bool FieldValue::get(T& t) const
334 {
335  const EncodedValue<T>* v = dynamic_cast< EncodedValue<T>* >(data.get());
336  if (v != 0) {
337  t = v->getValue();
338  return true;
339  } else {
340  try {
341  t = get<T>();
342  return true;
343  } catch (const InvalidConversionException&) {
344  return false;
345  }
346  }
347 }
348 
349 class Str8Value : public FieldValue {
350  public:
351  QPID_COMMON_EXTERN Str8Value(const std::string& v);
352 };
353 
354 class Str16Value : public FieldValue {
355  public:
356  QPID_COMMON_EXTERN Str16Value(const std::string& v);
357 };
358 
359 class Var16Value : public FieldValue {
360  public:
361  QPID_COMMON_EXTERN Var16Value(const std::string& v, uint8_t code);
362 };
363 
364 class Var32Value : public FieldValue {
365  public:
366  QPID_COMMON_EXTERN Var32Value(const std::string& v, uint8_t code);
367  };
368 
369 class Struct32Value : public FieldValue {
370  public:
371  QPID_COMMON_EXTERN Struct32Value(const std::string& v);
372 };
373 
374 class FloatValue : public FieldValue
375 {
376  public:
378 };
379 class DoubleValue : public FieldValue
380 {
381  public:
383 };
384 
385 /*
386  * Basic integer value encodes as signed 32 bit
387  */
388 class IntegerValue : public FieldValue {
389  public:
391 };
392 
393 class TimeValue : public FieldValue {
394  public:
395  QPID_COMMON_EXTERN TimeValue(uint64_t v);
396 };
397 
398 class Integer64Value : public FieldValue {
399  public:
401 };
402 
403 class Unsigned64Value : public FieldValue {
404  public:
406 };
407 
408 class FieldTableValue : public FieldValue {
409  public:
412 };
413 
414 class ArrayValue : public FieldValue {
415  public:
417 };
418 
419 class VoidValue : public FieldValue {
420  public:
422 };
423 
424 class BoolValue : public FieldValue {
425  public:
427 };
428 
429 class Unsigned8Value : public FieldValue {
430  public:
432 };
433 
434 class Unsigned16Value : public FieldValue {
435  public:
437 };
438 
439 class Unsigned32Value : public FieldValue {
440  public:
442 };
443 
444 class Integer8Value : public FieldValue {
445  public:
447 };
448 
449 class Integer16Value : public FieldValue {
450  public:
452 };
453 
455 
456 class ListValue : public FieldValue {
457  public:
458  typedef List ValueType;
460 };
461 
462 class UuidValue : public FieldValue {
463  public:
465  QPID_COMMON_EXTERN UuidValue(const unsigned char*);
466 };
467 
468 template <class T>
470 {
471  if (vptr) {
472  const EncodedValue<T>* ev = dynamic_cast< EncodedValue<T>* >(&(vptr->getData()));
473  if (ev != 0) {
474  value = ev->getValue();
475  return true;
476  }
477  }
478  return false;
479 }
480 
481 }} // qpid::framing
482 
483 #endif

Qpid C++ API Reference
Generated on Thu Feb 14 2013 for Qpid C++ Client API by doxygen 1.8.3