/home/aconway/svn/qpid/cpp/src/qpid/framing/Value.h

00001 /*
00002  *
00003  * Licensed to the Apache Software Foundation (ASF) under one
00004  * or more contributor license agreements.  See the NOTICE file
00005  * distributed with this work for additional information
00006  * regarding copyright ownership.  The ASF licenses this file
00007  * to you under the Apache License, Version 2.0 (the
00008  * "License"); you may not use this file except in compliance
00009  * with the License.  You may obtain a copy of the License at
00010  * 
00011  *   http://www.apache.org/licenses/LICENSE-2.0
00012  * 
00013  * Unless required by applicable law or agreed to in writing,
00014  * software distributed under the License is distributed on an
00015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00016  * KIND, either express or implied.  See the License for the
00017  * specific language governing permissions and limitations
00018  * under the License.
00019  *
00020  */
00021 #include <iostream>
00022 #include <vector>
00023 #include "amqp_types.h"
00024 #include "FieldTable.h"
00025 
00026 #ifndef _Value_
00027 #define _Value_
00028 
00029 namespace qpid {
00030 namespace framing {
00031 
00032 class Buffer;
00033 
00038 struct Decimal {
00039     uint32_t value;
00040     uint8_t decimals;
00041 
00042     Decimal(uint32_t value_=0, uint8_t decimals_=0) : value(value_), decimals(decimals_) {}
00043     bool operator==(const Decimal& d) const {
00044         return decimals == d.decimals && value == d.value;
00045     }
00046     bool operator!=(const Decimal& d) const { return !(*this == d); }
00047 };
00048 
00049 std::ostream& operator<<(std::ostream& out, const Decimal& d);
00050 
00054 class Value {
00055   public:
00056     virtual ~Value();
00057     virtual uint32_t size() const = 0;
00058     virtual char getType() const = 0;
00059     virtual void encode(Buffer& buffer) = 0;
00060     virtual void decode(Buffer& buffer) = 0;
00061     virtual bool operator==(const Value&) const = 0;
00062     bool operator!=(const Value& v) const { return !(*this == v); }
00063     virtual void print(std::ostream& out) const = 0;
00064 
00066     static std::auto_ptr<Value> decode_value(Buffer& buffer);
00067 };
00068 
00069 std::ostream& operator<<(std::ostream& out, const Value& d);
00070 
00071 
00075 template <class T>
00076 class ValueOps : public Value
00077 {
00078   protected:
00079     T value;
00080   public:
00081     ValueOps() {}
00082     ValueOps(const T& v) : value(v) {}
00083     const T& getValue() const { return value; }
00084     T& getValue() { return value; }
00085 
00086     virtual bool operator==(const Value& v) const {
00087         const ValueOps<T>* vo = dynamic_cast<const ValueOps<T>*>(&v);
00088         if (vo == 0) return false;
00089         else return value == vo->value;
00090     }
00091 
00092     void print(std::ostream& out) const { out << value; }
00093 };
00094 
00095 
00096 class StringValue : public ValueOps<std::string> {
00097   public:
00098     StringValue(const std::string& v) : ValueOps<std::string>(v) {}
00099     StringValue() {}
00100     virtual uint32_t size() const { return 4 + value.length(); }
00101     virtual char getType() const { return 'S'; }
00102     virtual void encode(Buffer& buffer);
00103     virtual void decode(Buffer& buffer);
00104 };
00105 
00106 class IntegerValue : public ValueOps<int> {
00107   public:
00108     IntegerValue(int v) : ValueOps<int>(v) {}
00109     IntegerValue(){}
00110     virtual uint32_t size() const { return 4; }
00111     virtual char getType() const { return 'I'; }
00112     virtual void encode(Buffer& buffer);
00113     virtual void decode(Buffer& buffer);
00114 };
00115 
00116 class TimeValue : public ValueOps<uint64_t> {
00117   public:
00118     TimeValue(uint64_t v) : ValueOps<uint64_t>(v){}
00119     TimeValue(){}
00120     virtual uint32_t size() const { return 8; }
00121     virtual char getType() const { return 'T'; }
00122     virtual void encode(Buffer& buffer);
00123     virtual void decode(Buffer& buffer);
00124 };
00125 
00126 class DecimalValue : public ValueOps<Decimal> {
00127   public:
00128     DecimalValue(const Decimal& d) : ValueOps<Decimal>(d) {} 
00129     DecimalValue(uint32_t value_=0, uint8_t decimals_=0) :
00130         ValueOps<Decimal>(Decimal(value_, decimals_)){}
00131     virtual uint32_t size() const { return 5; }
00132     virtual char getType() const { return 'D'; }
00133     virtual void encode(Buffer& buffer);
00134     virtual void decode(Buffer& buffer);
00135 };
00136 
00137 
00138 class FieldTableValue : public ValueOps<FieldTable> {
00139   public:
00140     FieldTableValue(const FieldTable& v) : ValueOps<FieldTable>(v){}
00141     FieldTableValue(){}
00142     virtual uint32_t size() const { return 4 + value.size(); }
00143     virtual char getType() const { return 'F'; }
00144     virtual void encode(Buffer& buffer);
00145     virtual void decode(Buffer& buffer);
00146 };
00147 
00148 class EmptyValue : public Value {
00149   public:
00150     ~EmptyValue();
00151     virtual uint32_t size() const { return 0; }
00152     virtual char getType() const { return 0; }
00153     virtual void encode(Buffer& ) {}
00154     virtual void decode(Buffer& ) {}
00155     virtual bool operator==(const Value& v) const {
00156         return dynamic_cast<const EmptyValue*>(&v);
00157     }
00158     virtual void print(std::ostream& out) const;
00159 };
00160 
00161 //non-standard types, introduced in java client for JMS compliance
00162 class BinaryValue : public StringValue {
00163   public:
00164     BinaryValue(const std::string& v) : StringValue(v) {}
00165     BinaryValue() {}
00166     virtual char getType() const { return 'x'; }
00167 };
00168 
00169 }} // qpid::framing
00170 
00171 #endif

Generated on Tue Apr 17 14:22:03 2007 for Qpid by  doxygen 1.4.7