qpid/framing/FieldValue.h Source File

Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET | Apache Qpid Documentation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

qpid/framing/FieldValue.h
Go to the documentation of this file.

    00001 #ifndef _framing_FieldValue_h
    00002 #define _framing_FieldValue_h
    00003 /*
    00004  *
    00005  * Licensed to the Apache Software Foundation (ASF) under one
    00006  * or more contributor license agreements.  See the NOTICE file
    00007  * distributed with this work for additional information
    00008  * regarding copyright ownership.  The ASF licenses this file
    00009  * to you under the Apache License, Version 2.0 (the
    00010  * "License"); you may not use this file except in compliance
    00011  * with the License.  You may obtain a copy of the License at
    00012  *
    00013  *   http://www.apache.org/licenses/LICENSE-2.0
    00014  *
    00015  * Unless required by applicable law or agreed to in writing,
    00016  * software distributed under the License is distributed on an
    00017  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    00018  * KIND, either express or implied.  See the License for the
    00019  * specific language governing permissions and limitations
    00020  * under the License.
    00021  *
    00022  */
    00023
    00024 #include "qpid/Exception.h"
    00025 #include "qpid/framing/amqp_types.h"
    00026 #include "qpid/framing/Buffer.h"
    00027 #include "qpid/framing/FieldTable.h"
    00028 #include "qpid/CommonImportExport.h"
    00029
    00030 #include <iostream>
    00031 #include <memory>
    00032 #include <vector>
    00033
    00034 #include <assert.h>
    00035
    00036 namespace qpid {
    00037 namespace framing {
    00038
    00044 class QPID_COMMON_CLASS_EXTERN FieldValueException : public qpid::Exception {};
    00045
    00051 struct QPID_COMMON_CLASS_EXTERN InvalidConversionException : public FieldValueException {
    00052     InvalidConversionException() {}
    00053 };
    00054
    00055 class List;
    00056
    00062 class QPID_COMMON_CLASS_EXTERN FieldValue {
    00063   public:
    00064     /*
    00065      * Abstract type for content of different types
    00066      */
    00067     class Data {
    00068       public:
    00069         virtual ~Data() {};
    00070         virtual uint32_t encodedSize() const = 0;
    00071         virtual void encode(Buffer& buffer) = 0;
    00072         virtual void decode(Buffer& buffer) = 0;
    00073         virtual bool operator==(const Data&) const = 0;
    00074
    00075         virtual bool convertsToInt() const { return false; }
    00076         virtual bool convertsToString() const { return false; }
    00077         virtual int64_t getInt() const { throw InvalidConversionException();}
    00078         virtual std::string getString() const { throw InvalidConversionException(); }
    00079
    00080         virtual void print(std::ostream& out) const = 0;
    00081     };
    00082
    00083     FieldValue(): data(0) {};
    00084     // Default assignment operator is fine
    00085     void setType(uint8_t type);
    00086     QPID_COMMON_EXTERN uint8_t getType() const;
    00087     Data& getData() { return *data; }
    00088     uint32_t encodedSize() const { return 1 + data->encodedSize(); };
    00089     bool empty() const { return data.get() == 0; }
    00090     void encode(Buffer& buffer);
    00091     void decode(Buffer& buffer);
    00092     QPID_COMMON_EXTERN bool operator==(const FieldValue&) const;
    00093     QPID_COMMON_INLINE_EXTERN bool operator!=(const FieldValue& v) const { return !(*this == v); }
    00094
    00095     QPID_COMMON_EXTERN void print(std::ostream& out) const;
    00096
    00097     template <typename T> bool convertsTo() const { return false; }
    00098     template <typename T> T get() const { throw InvalidConversionException(); }
    00099
    00100     template <class T, int W> T getIntegerValue() const;
    00101     template <class T> T getIntegerValue() const;
    00102     template <class T, int W> T getFloatingPointValue() const;
    00103     template <int W> void getFixedWidthValue(unsigned char*) const;
    00104     template <class T> bool get(T&) const;
    00105
    00106   protected:
    00107     FieldValue(uint8_t t, Data* d): typeOctet(t), data(d) {}
    00108
    00109     QPID_COMMON_EXTERN static uint8_t* convertIfRequired(uint8_t* const octets, int width);
    00110
    00111   private:
    00112     uint8_t typeOctet;
    00113     std::auto_ptr<Data> data;
    00114
    00115 };
    00116
    00117 template <>
    00118 inline bool FieldValue::convertsTo<int>() const { return data->convertsToInt(); }
    00119
    00120 template <>
    00121 inline bool FieldValue::convertsTo<int64_t>() const { return data->convertsToInt(); }
    00122
    00123 template <>
    00124 inline bool FieldValue::convertsTo<std::string>() const { return data->convertsToString(); }
    00125
    00126 template <>
    00127 inline int FieldValue::get<int>() const { return static_cast<int>(data->getInt()); }
    00128
    00129 template <>
    00130 inline int64_t FieldValue::get<int64_t>() const { return data->getInt(); }
    00131
    00132 template <>
    00133 inline std::string FieldValue::get<std::string>() const { return data->getString(); }
    00134
    00135 inline std::ostream& operator<<(std::ostream& out, const FieldValue& v) {
    00136     v.print(out);
    00137     return out;
    00138 }
    00139
    00140 template <int width>
    00141 class FixedWidthValue : public FieldValue::Data {
    00142     uint8_t octets[width];
    00143
    00144   public:
    00145     FixedWidthValue() {}
    00146     FixedWidthValue(const uint8_t (&data)[width]) : octets(data) {}
    00147     FixedWidthValue(const uint8_t* const data)
    00148     {
    00149         for (int i = 0; i < width; i++) octets[i] = data[i];
    00150     }
    00151     FixedWidthValue(uint64_t v)
    00152     {
    00153         for (int i = width; i > 1; --i) {
    00154             octets[i-1] = (uint8_t) (0xFF & v); v >>= 8;
    00155         }
    00156         octets[0] = (uint8_t) (0xFF & v);
    00157     }
    00158     uint32_t encodedSize() const { return width; }
    00159     void encode(Buffer& buffer) { buffer.putRawData(octets, width); }
    00160     void decode(Buffer& buffer) { buffer.getRawData(octets, width); }
    00161     bool operator==(const Data& d) const {
    00162         const FixedWidthValue<width>* rhs = dynamic_cast< const FixedWidthValue<width>* >(&d);
    00163         if (rhs == 0) return false;
    00164         else return std::equal(&octets[0], &octets[width], &rhs->octets[0]);
    00165     }
    00166
    00167     bool convertsToInt() const { return true; }
    00168     int64_t getInt() const
    00169     {
    00170         int64_t v = 0;
    00171         for (int i = 0; i < width-1; ++i) {
    00172             v |= octets[i]; v <<= 8;
    00173         }
    00174         v |= octets[width-1];
    00175         return v;
    00176     }
    00177     uint8_t* rawOctets() { return octets; }
    00178     uint8_t* rawOctets() const { return octets; }
    00179
    00180     void print(std::ostream& o) const { o << "F" << width << ":"; };
    00181 };
    00182
    00183 template <class T, int W>
    00184 inline T FieldValue::getIntegerValue() const
    00185 {
    00186     FixedWidthValue<W>* const fwv = dynamic_cast< FixedWidthValue<W>* const>(data.get());
    00187     if (fwv) {
    00188         uint8_t* octets = fwv->rawOctets();
    00189         T v = 0;
    00190         for (int i = 0; i < W-1; ++i) {
    00191             v |= octets[i]; v <<= 8;
    00192         }
    00193         v |= octets[W-1];
    00194         return v;
    00195     } else {
    00196         throw InvalidConversionException();
    00197     }
    00198 }
    00199
    00200 template <class T>
    00201 inline T FieldValue::getIntegerValue() const
    00202 {
    00203     FixedWidthValue<1>* const fwv = dynamic_cast< FixedWidthValue<1>* const>(data.get());
    00204     if (fwv) {
    00205         uint8_t* octets = fwv->rawOctets();
    00206         return octets[0];
    00207     } else {
    00208         throw InvalidConversionException();
    00209     }
    00210 }
    00211
    00212 template <class T, int W>
    00213 inline T FieldValue::getFloatingPointValue() const {
    00214     FixedWidthValue<W>* const fwv = dynamic_cast< FixedWidthValue<W>* const>(data.get());
    00215     if (fwv) {
    00216         T value;
    00217         uint8_t* const octets = convertIfRequired(fwv->rawOctets(), W);
    00218         uint8_t* const target = reinterpret_cast<uint8_t*>(&value);
    00219         for (size_t i = 0; i < W; ++i) target[i] = octets[i];
    00220         return value;
    00221     } else {
    00222         throw InvalidConversionException();
    00223     }
    00224 }
    00225
    00226 template <int W> void FieldValue::getFixedWidthValue(unsigned char* value) const
    00227 {
    00228     FixedWidthValue<W>* const fwv = dynamic_cast< FixedWidthValue<W>* const>(data.get());
    00229     if (fwv) {
    00230         for (size_t i = 0; i < W; ++i) value[i] = fwv->rawOctets()[i];
    00231     } else {
    00232         throw InvalidConversionException();
    00233     }
    00234 }
    00235
    00236 template <>
    00237 inline float FieldValue::get<float>() const {
    00238     return getFloatingPointValue<float, 4>();
    00239 }
    00240
    00241 template <>
    00242 inline double FieldValue::get<double>() const {
    00243     return getFloatingPointValue<double, 8>();
    00244 }
    00245
    00246 template <>
    00247 class FixedWidthValue<0> : public FieldValue::Data {
    00248   public:
    00249     // Implicit default constructor is fine
    00250     uint32_t encodedSize() const { return 0; }
    00251     void encode(Buffer&) {};
    00252     void decode(Buffer&) {};
    00253     bool operator==(const Data& d) const {
    00254         const FixedWidthValue<0>* rhs = dynamic_cast< const FixedWidthValue<0>* >(&d);
    00255         return rhs != 0;
    00256     }
    00257     void print(std::ostream& o) const { o << "F0"; };
    00258 };
    00259
    00260 template <int lenwidth>
    00261 class VariableWidthValue : public FieldValue::Data {
    00262     std::vector<uint8_t> octets;
    00263
    00264   public:
    00265     VariableWidthValue() {}
    00266     VariableWidthValue(const std::vector<uint8_t>& data) : octets(data) {}
    00267     VariableWidthValue(const uint8_t* start, const uint8_t* end) : octets(start, end) {}
    00268     uint32_t encodedSize() const { return lenwidth + octets.size(); }
    00269     void encode(Buffer& buffer) {
    00270         buffer.putUInt<lenwidth>(octets.size());
    00271         if (octets.size() > 0)
    00272             buffer.putRawData(&octets[0], octets.size());
    00273     };
    00274     void decode(Buffer& buffer) {
    00275         uint32_t len = buffer.getUInt<lenwidth>();
    00276         octets.resize(len);
    00277         if (len > 0)
    00278             buffer.getRawData(&octets[0], len);
    00279     }
    00280     bool operator==(const Data& d) const {
    00281         const VariableWidthValue<lenwidth>* rhs = dynamic_cast< const VariableWidthValue<lenwidth>* >(&d);
    00282         if (rhs == 0) return false;
    00283         else return octets==rhs->octets;
    00284     }
    00285
    00286     bool convertsToString() const { return true; }
    00287     std::string getString() const { return std::string(octets.begin(), octets.end()); }
    00288
    00289     void print(std::ostream& o) const { o << "V" << lenwidth << ":" << octets.size() << ":"; };
    00290 };
    00291
    00292 template <class T>
    00293 class EncodedValue : public FieldValue::Data {
    00294     T value;
    00295   public:
    00296
    00297     EncodedValue() {}
    00298     EncodedValue(const T& v) : value(v) {}
    00299
    00300     T& getValue() { return value; }
    00301     const T& getValue() const { return value; }
    00302
    00303     uint32_t encodedSize() const { return value.encodedSize(); }
    00304
    00305     void encode(Buffer& buffer) {
    00306         value.encode(buffer);
    00307     };
    00308     void decode(Buffer& buffer) {
    00309         value.decode(buffer);
    00310     }
    00311     bool operator==(const Data& d) const {
    00312         const EncodedValue<T>* rhs = dynamic_cast< const EncodedValue<T>* >(&d);
    00313         if (rhs == 0) return false;
    00314         else return value==rhs->value;
    00315     }
    00316
    00317     void print(std::ostream& o) const { o << "[" << value << "]"; };
    00318 };
    00319
    00324 template <class T>
    00325 inline bool FieldValue::get(T& t) const
    00326 {
    00327     const EncodedValue<T>* v = dynamic_cast< EncodedValue<T>* >(data.get());
    00328     if (v != 0) {
    00329         t = v->getValue();
    00330         return true;
    00331     } else {
    00332         try {
    00333             t = get<T>();
    00334             return true;
    00335         } catch (const InvalidConversionException&) {
    00336             return false;
    00337         }
    00338     }
    00339 }
    00340
    00341 class Str8Value : public FieldValue {
    00342   public:
    00343     QPID_COMMON_EXTERN Str8Value(const std::string& v);
    00344 };
    00345
    00346 class Str16Value : public FieldValue {
    00347   public:
    00348     QPID_COMMON_EXTERN Str16Value(const std::string& v);
    00349 };
    00350
    00351 class Var16Value : public FieldValue {
    00352   public:
    00353     QPID_COMMON_EXTERN Var16Value(const std::string& v, uint8_t code);
    00354 };
    00355
    00356 class Var32Value : public FieldValue {
    00357   public:
    00358     QPID_COMMON_EXTERN Var32Value(const std::string& v, uint8_t code);
    00359 };
    00360
    00361 class Struct32Value : public FieldValue {
    00362   public:
    00363     QPID_COMMON_EXTERN Struct32Value(const std::string& v);
    00364 };
    00365
    00366 class FloatValue : public FieldValue
    00367 {
    00368   public:
    00369     QPID_COMMON_EXTERN FloatValue(float f);
    00370 };
    00371 class DoubleValue : public FieldValue
    00372 {
    00373   public:
    00374     QPID_COMMON_EXTERN DoubleValue(double f);
    00375 };
    00376
    00377 /*
    00378  * Basic integer value encodes as signed 32 bit
    00379  */
    00380 class IntegerValue : public FieldValue {
    00381   public:
    00382     QPID_COMMON_EXTERN IntegerValue(int v);
    00383 };
    00384
    00385 class TimeValue : public FieldValue {
    00386   public:
    00387     QPID_COMMON_EXTERN TimeValue(uint64_t v);
    00388 };
    00389
    00390 class Integer64Value : public FieldValue {
    00391   public:
    00392     QPID_COMMON_EXTERN Integer64Value(int64_t v);
    00393 };
    00394
    00395 class Unsigned64Value : public FieldValue {
    00396   public:
    00397     QPID_COMMON_EXTERN Unsigned64Value(uint64_t v);
    00398 };
    00399
    00400 class FieldTableValue : public FieldValue {
    00401   public:
    00402     typedef FieldTable ValueType;
    00403     QPID_COMMON_EXTERN FieldTableValue(const FieldTable&);
    00404 };
    00405
    00406 class ArrayValue : public FieldValue {
    00407   public:
    00408     QPID_COMMON_EXTERN ArrayValue(const Array&);
    00409 };
    00410
    00411 class VoidValue : public FieldValue {
    00412   public:
    00413     QPID_COMMON_EXTERN VoidValue();
    00414 };
    00415
    00416 class BoolValue : public FieldValue {
    00417   public:
    00418     QPID_COMMON_EXTERN BoolValue(bool);
    00419 };
    00420
    00421 class Unsigned8Value : public FieldValue {
    00422   public:
    00423     QPID_COMMON_EXTERN Unsigned8Value(uint8_t);
    00424 };
    00425
    00426 class Unsigned16Value : public FieldValue {
    00427   public:
    00428     QPID_COMMON_EXTERN Unsigned16Value(uint16_t);
    00429 };
    00430
    00431 class Unsigned32Value : public FieldValue {
    00432   public:
    00433     QPID_COMMON_EXTERN Unsigned32Value(uint32_t);
    00434 };
    00435
    00436 class Integer8Value : public FieldValue {
    00437   public:
    00438     QPID_COMMON_EXTERN Integer8Value(int8_t);
    00439 };
    00440
    00441 class Integer16Value : public FieldValue {
    00442   public:
    00443     QPID_COMMON_EXTERN Integer16Value(int16_t);
    00444 };
    00445
    00446 typedef IntegerValue Integer32Value;
    00447
    00448 class ListValue : public FieldValue {
    00449   public:
    00450     typedef List ValueType;
    00451     QPID_COMMON_EXTERN ListValue(const List&);
    00452 };
    00453
    00454 class UuidValue : public FieldValue {
    00455   public:
    00456     QPID_COMMON_EXTERN UuidValue(const unsigned char*);
    00457 };
    00458
    00459 template <class T>
    00460 bool getEncodedValue(FieldTable::ValuePtr vptr, T& value)
    00461 {
    00462     if (vptr) {
    00463         const EncodedValue<T>* ev = dynamic_cast< EncodedValue<T>* >(&(vptr->getData()));
    00464         if (ev != 0) {
    00465             value = ev->getValue();
    00466             return true;
    00467         }
    00468     }
    00469     return false;
    00470 }
    00471
    00472 }} // qpid::framing
    00473
    00474 #endif