/usr/share/cruisecontrol-bin-2.6.1/projects/qpid-trunk/cpp/src/qpid/amqp_0_10/Array.h

00001 #ifndef QPID_AMQP_0_10_ARRAY_H
00002 #define QPID_AMQP_0_10_ARRAY_H
00003 
00004 /*
00005  *
00006  * Licensed to the Apache Software Foundation (ASF) under one
00007  * or more contributor license agreements.  See the NOTICE file
00008  * distributed with this work for additional information
00009  * regarding copyright ownership.  The ASF licenses this file
00010  * to you under the Apache License, Version 2.0 (the
00011  * "License"); you may not use this file except in compliance
00012  * with the License.  You may obtain a copy of the License at
00013  * 
00014  *   http://www.apache.org/licenses/LICENSE-2.0
00015  * 
00016  * Unless required by applicable law or agreed to in writing,
00017  * software distributed under the License is distributed on an
00018  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00019  * KIND, either express or implied.  See the License for the
00020  * specific language governing permissions and limitations
00021  * under the License.
00022  *
00023  */
00024 
00025 #include "qpid/amqp_0_10/TypeForCode.h"
00026 #include "qpid/amqp_0_10/CodeForType.h"
00027 #include "qpid/amqp_0_10/UnknownType.h"
00028 #include "qpid/amqp_0_10/exceptions.h"
00029 #include "qpid/amqp_0_10/Codec.h"
00030 #include <vector>
00031 #include <ostream>
00032 
00033 namespace qpid {
00034 namespace amqp_0_10 {
00035 
00036 template <class T> class  ArrayDomain : public std::vector<T>  {
00037   public:
00038     template <class S> void serialize(S& s) { s.split(*this); s(this->begin(), this->end()); }
00039 
00040     template <class S> void encode(S& s) const {
00041         s(contentSize())(CodeForType<T>::value)(uint32_t(this->size()));
00042     }
00043 
00044     void encode(Codec::Size& s) const  { s.raw(0, contentSize() + 4/*size*/); }
00045     
00046     template <class S> void decode(S& s) {
00047         uint32_t size; uint8_t type; uint32_t count;
00048         s(size);
00049         s.setLimit(size);
00050         s(type);
00051         if (type != CodeForType<T>::value)
00052             throw InvalidArgumentException(QPID_MSG("Array domain expected type " << CodeForType<T>::value << " but found " << type));
00053         s(count);
00054         this->resize(count);
00055     }
00056 
00057   private:
00058     uint32_t contentSize() const {
00059         return Codec::size(this->begin(), this->end()) + sizeof(uint32_t) /*count*/ + sizeof(uint8_t) /*type*/;
00060     }
00061 };
00062 
00063 template <class T>
00064 std::ostream& operator<<(std::ostream& o, const ArrayDomain<T>& ad) {
00065     std::ostream_iterator<T> i(o, " ");
00066     o << "Array<" << typeName(CodeForType<T>::value) << ">[";
00067     std::copy(ad.begin(), ad.end(), i);
00068     o << "]";
00069     return o;
00070 }
00071 
00075 template<> class ArrayDomain<UnknownType> : public std::vector<UnknownType> { 
00076   public:
00077     ArrayDomain(uint8_t type_=0) : type(type_) {}
00078     
00079     template <class S> void serialize(S& s) { s.split(*this); s(this->begin(), this->end()); }
00080 
00081     template <class S> void encode(S& s) const {
00082         s(contentSize())(type)(uint32_t(this->size()));
00083     }
00084 
00085     void encode(Codec::Size& s) const  { s.raw(0, contentSize() + 4/*size*/); }
00086 
00087     template <class S> void decode(S& s) {
00088         uint32_t size; uint32_t count;
00089         s(size);
00090         s.setLimit(size);
00091         s(type)(count);
00092         this->clear();
00093         this->resize(count, UnknownType(type));
00094     }
00095 
00096     uint8_t getType() const { return type; }
00097     
00098   private:
00099     uint32_t contentSize() const {
00100         return Codec::size(this->begin(), this->end()) + sizeof(uint32_t) /*count*/ + sizeof(uint8_t) /*type*/;
00101     }
00102     uint8_t type;
00103 };
00104 
00105 std::ostream& operator<<(std::ostream& o, const Array& a);
00106 
00107 // FIXME aconway 2008-04-08: hack to supress encoding of
00108 // command-fragments and in-doubt as there is a problem with the spec
00109 // (command-fragments does not have a one byte type code.)
00110 namespace session { class CommandFragment; }
00111 namespace dtx { class Xid; }
00112 
00113 template <> struct ArrayDomain<session::CommandFragment> : public Void {};
00114 template <> struct ArrayDomain<dtx::Xid> : public Void {};
00115 inline std::ostream& operator<<(std::ostream& o, const ArrayDomain<session::CommandFragment>&) { return o; }
00116 inline std::ostream& operator<<(std::ostream& o, const ArrayDomain<dtx::Xid>&) { return o; }
00117 
00118 }} // namespace qpid::amqp_0_10
00119 
00120 #endif  

Generated on Thu Apr 10 11:08:17 2008 for Qpid by  doxygen 1.4.7