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  uint8_t* rawOctets() const { return octets; }
179 
180  void print(std::ostream& o) const { o << "F" << width << ":"; };
181 };
182 
183 template <class T, int W>
185 {
186  FixedWidthValue<W>* const fwv = dynamic_cast< FixedWidthValue<W>* const>(data.get());
187  if (fwv) {
188  uint8_t* octets = fwv->rawOctets();
189  T v = 0;
190  for (int i = 0; i < W-1; ++i) {
191  v |= octets[i]; v <<= 8;
192  }
193  v |= octets[W-1];
194  return v;
195  } else {
197  }
198 }
199 
200 template <class T>
201 inline T FieldValue::getIntegerValue() const
202 {
203  FixedWidthValue<1>* const fwv = dynamic_cast< FixedWidthValue<1>* const>(data.get());
204  if (fwv) {
205  uint8_t* octets = fwv->rawOctets();
206  return octets[0];
207  } else {
208  throw InvalidConversionException();
209  }
210 }
211 
212 template <class T, int W>
214  FixedWidthValue<W>* const fwv = dynamic_cast< FixedWidthValue<W>* const>(data.get());
215  if (fwv) {
216  T value;
217  uint8_t* const octets = convertIfRequired(fwv->rawOctets(), W);
218  uint8_t* const target = reinterpret_cast<uint8_t*>(&value);
219  for (size_t i = 0; i < W; ++i) target[i] = octets[i];
220  return value;
221  } else {
223  }
224 }
225 
226 template <int W> void FieldValue::getFixedWidthValue(unsigned char* value) const
227 {
228  FixedWidthValue<W>* const fwv = dynamic_cast< FixedWidthValue<W>* const>(data.get());
229  if (fwv) {
230  for (size_t i = 0; i < W; ++i) value[i] = fwv->rawOctets()[i];
231  } else {
233  }
234 }
235 
236 template <>
237 inline float FieldValue::get<float>() const {
238  return getFloatingPointValue<float, 4>();
239 }
240 
241 template <>
242 inline double FieldValue::get<double>() const {
243  return getFloatingPointValue<double, 8>();
244 }
245 
246 template <>
248  public:
249  // Implicit default constructor is fine
250  uint32_t encodedSize() const { return 0; }
251  void encode(Buffer&) {};
252  void decode(Buffer&) {};
253  bool operator==(const Data& d) const {
254  const FixedWidthValue<0>* rhs = dynamic_cast< const FixedWidthValue<0>* >(&d);
255  return rhs != 0;
256  }
257  void print(std::ostream& o) const { o << "F0"; };
258 };
259 
260 template <int lenwidth>
262  std::vector<uint8_t> octets;
263 
264  public:
266  VariableWidthValue(const std::vector<uint8_t>& data) : octets(data) {}
267  VariableWidthValue(const uint8_t* start, const uint8_t* end) : octets(start, end) {}
268  uint32_t encodedSize() const { return lenwidth + octets.size(); }
269  void encode(Buffer& buffer) {
270  buffer.putUInt<lenwidth>(octets.size());
271  if (octets.size() > 0)
272  buffer.putRawData(&octets[0], octets.size());
273  };
274  void decode(Buffer& buffer) {
275  uint32_t len = buffer.getUInt<lenwidth>();
276  octets.resize(len);
277  if (len > 0)
278  buffer.getRawData(&octets[0], len);
279  }
280  bool operator==(const Data& d) const {
281  const VariableWidthValue<lenwidth>* rhs = dynamic_cast< const VariableWidthValue<lenwidth>* >(&d);
282  if (rhs == 0) return false;
283  else return octets==rhs->octets;
284  }
285 
286  bool convertsToString() const { return true; }
287  std::string getString() const { return std::string(octets.begin(), octets.end()); }
288 
289  void print(std::ostream& o) const { o << "V" << lenwidth << ":" << octets.size() << ":"; };
290 };
291 
292 template <class T>
294  T value;
295  public:
296 
298  EncodedValue(const T& v) : value(v) {}
299 
300  T& getValue() { return value; }
301  const T& getValue() const { return value; }
302 
303  uint32_t encodedSize() const { return value.encodedSize(); }
304 
305  void encode(Buffer& buffer) {
306  value.encode(buffer);
307  };
308  void decode(Buffer& buffer) {
309  value.decode(buffer);
310  }
311  bool operator==(const Data& d) const {
312  const EncodedValue<T>* rhs = dynamic_cast< const EncodedValue<T>* >(&d);
313  if (rhs == 0) return false;
314  else return value==rhs->value;
315  }
316 
317  void print(std::ostream& o) const { o << "[" << value << "]"; };
318 };
319 
324 template <class T>
325 inline bool FieldValue::get(T& t) const
326 {
327  const EncodedValue<T>* v = dynamic_cast< EncodedValue<T>* >(data.get());
328  if (v != 0) {
329  t = v->getValue();
330  return true;
331  } else {
332  try {
333  t = get<T>();
334  return true;
335  } catch (const InvalidConversionException&) {
336  return false;
337  }
338  }
339 }
340 
341 class Str8Value : public FieldValue {
342  public:
343  QPID_COMMON_EXTERN Str8Value(const std::string& v);
344 };
345 
346 class Str16Value : public FieldValue {
347  public:
348  QPID_COMMON_EXTERN Str16Value(const std::string& v);
349 };
350 
351 class Var16Value : public FieldValue {
352  public:
353  QPID_COMMON_EXTERN Var16Value(const std::string& v, uint8_t code);
354 };
355 
356 class Var32Value : public FieldValue {
357  public:
358  QPID_COMMON_EXTERN Var32Value(const std::string& v, uint8_t code);
359 };
360 
361 class Struct32Value : public FieldValue {
362  public:
363  QPID_COMMON_EXTERN Struct32Value(const std::string& v);
364 };
365 
366 class FloatValue : public FieldValue
367 {
368  public:
370 };
371 class DoubleValue : public FieldValue
372 {
373  public:
375 };
376 
377 /*
378  * Basic integer value encodes as signed 32 bit
379  */
380 class IntegerValue : public FieldValue {
381  public:
383 };
384 
385 class TimeValue : public FieldValue {
386  public:
387  QPID_COMMON_EXTERN TimeValue(uint64_t v);
388 };
389 
390 class Integer64Value : public FieldValue {
391  public:
393 };
394 
395 class Unsigned64Value : public FieldValue {
396  public:
398 };
399 
400 class FieldTableValue : public FieldValue {
401  public:
404 };
405 
406 class ArrayValue : public FieldValue {
407  public:
409 };
410 
411 class VoidValue : public FieldValue {
412  public:
414 };
415 
416 class BoolValue : public FieldValue {
417  public:
419 };
420 
421 class Unsigned8Value : public FieldValue {
422  public:
424 };
425 
426 class Unsigned16Value : public FieldValue {
427  public:
429 };
430 
431 class Unsigned32Value : public FieldValue {
432  public:
434 };
435 
436 class Integer8Value : public FieldValue {
437  public:
439 };
440 
441 class Integer16Value : public FieldValue {
442  public:
444 };
445 
447 
448 class ListValue : public FieldValue {
449  public:
450  typedef List ValueType;
452 };
453 
454 class UuidValue : public FieldValue {
455  public:
456  QPID_COMMON_EXTERN UuidValue(const unsigned char*);
457 };
458 
459 template <class T>
461 {
462  if (vptr) {
463  const EncodedValue<T>* ev = dynamic_cast< EncodedValue<T>* >(&(vptr->getData()));
464  if (ev != 0) {
465  value = ev->getValue();
466  return true;
467  }
468  }
469  return false;
470 }
471 
472 }} // qpid::framing
473 
474 #endif

Qpid C++ API Reference
Generated on Thu Nov 15 2012 for Qpid C++ Client API by doxygen 1.8.1.2