MarshalSerialize.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #ifndef _OASYS_MARSHAL_SERIALIZE_H_
00018 #define _OASYS_MARSHAL_SERIALIZE_H_
00019 
00020 #include "Serialize.h"
00021 #include "BufferedSerializeAction.h"
00022 #include "../util/ExpandableBuffer.h"
00023 #include "../util/CRC32.h"
00024 
00025 namespace oasys {
00026 
00028 
00032 class Marshal : public BufferedSerializeAction {
00033 public:
00037     Marshal(context_t context, u_char* buf, size_t length, int options = 0);
00038 
00042     Marshal(context_t context, ExpandableBuffer* buf, int options = 0);
00043 
00050     int action(const SerializableObject* const_object)
00051     {
00052         SerializableObject* object = (SerializableObject*)const_object;
00053         return SerializeAction::action(object);
00054     }
00055 
00056     void process(const char* name, SerializableObject* const_object)
00057     {
00058         SerializableObject* object = (SerializableObject*)const_object;
00059         return SerializeAction::process(name, object);
00060     }
00061 
00062     // Virtual functions inherited from SerializeAction
00063     void end_action();
00064 
00065     void process(const char* name, u_int64_t* i);
00066     void process(const char* name, u_int32_t* i);
00067     void process(const char* name, u_int16_t* i);
00068     void process(const char* name, u_int8_t* i);
00069     void process(const char* name, bool* b);
00070     void process(const char* name, u_char* bp, u_int32_t len);
00071     void process(const char* name, u_char** bp, u_int32_t* lenp, int flags);
00072     void process(const char* name, std::string* s);
00073 
00074 private:
00075     bool add_crc_;
00076 };
00077 
00079 
00086 class Unmarshal : public BufferedSerializeAction {
00087 public:
00091     Unmarshal(context_t context, const u_char* buf, size_t length,
00092               int options = 0);
00093 
00094     // Virtual functions inherited from SerializeAction
00095     void begin_action();
00096 
00097     void process(const char* name, u_int64_t* i);
00098     void process(const char* name, u_int32_t* i);
00099     void process(const char* name, u_int16_t* i);
00100     void process(const char* name, u_int8_t* i);
00101     void process(const char* name, bool* b);
00102     void process(const char* name, u_char* bp, u_int32_t len);
00103     void process(const char* name, u_char** bp, u_int32_t* lenp, int flags);
00104     void process(const char* name, std::string* s); 
00105 
00106 private:
00107     bool has_crc_;
00108 };
00109 
00111 
00115 class MarshalSize : public SerializeAction {
00116 public:
00120     MarshalSize(context_t context, int options = 0)
00121         : SerializeAction(Serialize::INFO, context, options), size_(0)
00122     {
00123     }
00124 
00128     int action(const SerializableObject* const_object)
00129     {
00130         SerializableObject* object = (SerializableObject*)const_object;
00131         return SerializeAction::action(object);
00132     }
00133     
00134     void process(const char* name, SerializableObject* const_object)
00135     {
00136         SerializableObject* object = (SerializableObject*)const_object;
00137         return SerializeAction::process(name, object);
00138     }
00139 
00141     u_int32_t size() { return size_; }
00142 
00146     static u_int32_t get_size(u_int64_t*)           { return 8; }
00147     static u_int32_t get_size(u_int32_t*)           { return 4; }
00148     static u_int32_t get_size(u_int16_t*)           { return 2; }
00149     static u_int32_t get_size(u_int8_t*)            { return 1; }
00150     static u_int32_t get_size(bool*)                { return 1; }
00151     static u_int32_t get_size(u_char*, u_int32_t len)  { return len; }
00152     static u_int32_t get_size(std::string* s)       { return s->length() + 4; }
00154     
00157     void begin_action();
00158     void process(const char* name, u_int64_t* i);
00159     void process(const char* name, u_int32_t* i);
00160     void process(const char* name, u_int16_t* i);
00161     void process(const char* name, u_int8_t* i);
00162     void process(const char* name, bool* b);
00163     void process(const char* name, u_char* bp, u_int32_t len);
00164     void process(const char* name, u_char** bp, u_int32_t* lenp, int flags);
00165     void process(const char* name, std::string* s);
00167 
00168 private:
00169     u_int32_t size_;
00170 };
00171 
00173 
00176 class MarshalCRC : public SerializeAction {
00177 public:
00178     MarshalCRC(context_t context)
00179         : SerializeAction(Serialize::INFO, context) {}
00180     
00181     u_int32_t crc() { return crc_.value(); }
00182     
00184     int action(const SerializableObject* const_object)
00185     {
00186         SerializableObject* object = (SerializableObject*)const_object;
00187         return SerializeAction::action(object);
00188     }
00189     void process(const char* name, SerializableObject* const_object)
00190     {
00191         SerializableObject* object = (SerializableObject*)const_object;
00192         return SerializeAction::process(name, object);
00193     }
00196     // virtual from SerializeAction
00197     void process(const char* name, u_int32_t* i);
00198     void process(const char* name, u_int16_t* i);
00199     void process(const char* name, u_int8_t* i);
00200     void process(const char* name, bool* b);
00201     void process(const char* name, u_char* bp, u_int32_t len);
00202     void process(const char* name, u_char** bp, u_int32_t* lenp, int flags);
00203     void process(const char* name, std::string* s);
00204 
00205 private:
00206     CRC32 crc_;
00207 };
00208 
00209 
00211 
00214 class MarshalCopy {
00215 public:
00217     static u_int32_t copy(ExpandableBuffer* buf,
00218                           const SerializableObject* src,
00219                           SerializableObject* dst);
00220 };
00221 
00222 } // namespace oasys
00223 
00224 #endif /* _OASYS_MARSHAL_SERIALIZE_H_ */

Generated on Thu Jun 7 12:54:28 2007 for DTN Reference Implementation by  doxygen 1.5.1