MarshalSerialize.h

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 #ifndef _OASYS_MARSHAL_SERIALIZE_H_
00039 #define _OASYS_MARSHAL_SERIALIZE_H_
00040 
00041 #include "Serialize.h"
00042 #include "BufferedSerializeAction.h"
00043 #include "../util/ExpandableBuffer.h"
00044 #include "../util/CRC32.h"
00045 
00046 namespace oasys {
00047 
00049 
00053 class Marshal : public BufferedSerializeAction {
00054 public:
00058     Marshal(context_t context, u_char* buf, size_t length, int options = 0);
00059 
00063     Marshal(context_t context, ExpandableBuffer* buf, int options = 0);
00064 
00071     int action(const SerializableObject* const_object)
00072     {
00073         SerializableObject* object = (SerializableObject*)const_object;
00074         return SerializeAction::action(object);
00075     }
00076 
00077     void process(const char* name, SerializableObject* const_object)
00078     {
00079         SerializableObject* object = (SerializableObject*)const_object;
00080         return SerializeAction::process(name, object);
00081     }
00082 
00083     // Virtual functions inherited from SerializeAction
00084     void end_action();
00085 
00086     void process(const char* name, u_int32_t* i);
00087     void process(const char* name, u_int16_t* i);
00088     void process(const char* name, u_int8_t* i);
00089     void process(const char* name, bool* b);
00090     void process(const char* name, u_char* bp, size_t len);
00091     void process(const char* name, u_char** bp, size_t* lenp, int flags);
00092     void process(const char* name, std::string* s);
00093 
00094 private:
00095     bool add_crc_;
00096 };
00097 
00099 
00106 class Unmarshal : public BufferedSerializeAction {
00107 public:
00111     Unmarshal(context_t context, const u_char* buf, size_t length,
00112               int options = 0);
00113 
00114     // Virtual functions inherited from SerializeAction
00115     void begin_action();
00116 
00117     void process(const char* name, u_int32_t* i);
00118     void process(const char* name, u_int16_t* i);
00119     void process(const char* name, u_int8_t* i);
00120     void process(const char* name, bool* b);
00121     void process(const char* name, u_char* bp, size_t len);
00122     void process(const char* name, u_char** bp, size_t* lenp, int flags);
00123     void process(const char* name, std::string* s); 
00124 
00125 private:
00126     bool has_crc_;
00127 };
00128 
00130 
00134 class MarshalSize : public SerializeAction {
00135 public:
00139     MarshalSize(context_t context, int options = 0)
00140         : SerializeAction(Serialize::INFO, context, options), size_(0)
00141     {
00142     }
00143 
00147     int action(const SerializableObject* const_object)
00148     {
00149         SerializableObject* object = (SerializableObject*)const_object;
00150         return SerializeAction::action(object);
00151     }
00152     
00153     void process(const char* name, SerializableObject* const_object)
00154     {
00155         SerializableObject* object = (SerializableObject*)const_object;
00156         return SerializeAction::process(name, object);
00157     }
00158 
00160     size_t size() { return size_; }
00161 
00165     static size_t get_size(u_int32_t*)           { return 4; }
00166     static size_t get_size(u_int16_t*)           { return 2; }
00167     static size_t get_size(u_int8_t*)            { return 1; }
00168     static size_t get_size(bool*)                { return 1; }
00169     static size_t get_size(u_char*, size_t len)  { return len; }
00170     static size_t get_size(std::string* s)       { return s->length() + 4; }
00172     
00175     void begin_action();
00176     void process(const char* name, u_int32_t* i);
00177     void process(const char* name, u_int16_t* i);
00178     void process(const char* name, u_int8_t* i);
00179     void process(const char* name, bool* b);
00180     void process(const char* name, u_char* bp, size_t len);
00181     void process(const char* name, u_char** bp, size_t* lenp, int flags);
00182     void process(const char* name, std::string* s);
00184 
00185 private:
00186     size_t size_;
00187 };
00188 
00190 
00193 class MarshalCRC : public SerializeAction {
00194 public:
00195     MarshalCRC(context_t context)
00196         : SerializeAction(Serialize::INFO, context) {}
00197     
00198     u_int32_t crc() { return crc_.value(); }
00199     
00201     int action(const SerializableObject* const_object)
00202     {
00203         SerializableObject* object = (SerializableObject*)const_object;
00204         return SerializeAction::action(object);
00205     }
00206     void process(const char* name, SerializableObject* const_object)
00207     {
00208         SerializableObject* object = (SerializableObject*)const_object;
00209         return SerializeAction::process(name, object);
00210     }
00213     // virtual from SerializeAction
00214     void process(const char* name, u_int32_t* i);
00215     void process(const char* name, u_int16_t* i);
00216     void process(const char* name, u_int8_t* i);
00217     void process(const char* name, bool* b);
00218     void process(const char* name, u_char* bp, size_t len);
00219     void process(const char* name, u_char** bp, size_t* lenp, int flags);
00220     void process(const char* name, std::string* s);
00221 
00222 private:
00223     CRC32 crc_;
00224 };
00225 
00226 
00228 
00231 class MarshalCopy {
00232 public:
00234     static size_t copy(ExpandableBuffer* buf,
00235                        const SerializableObject* src,
00236                        SerializableObject* dst);
00237 };
00238 
00239 } // namespace oasys
00240 
00241 #endif /* _OASYS_MARSHAL_SERIALIZE_H_ */

Generated on Fri Dec 22 14:47:59 2006 for DTN Reference Implementation by  doxygen 1.5.1