Serialize.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_SERIALIZE_H_
00018 #define _OASYS_SERIALIZE_H_
00019 
00026 #include <string>
00027 #include <vector>
00028 #include <sys/types.h>
00029 #include "../compat/inttypes.h"
00030 
00031 namespace oasys {
00032 
00033 class Serialize;
00034 class SerializeAction;
00035 class SerializableObject;
00036 
00041 class Serialize {
00042 public:
00046     typedef enum {
00047         MARSHAL = 1,    
00048         UNMARSHAL,      
00049         INFO            
00050     } action_t;
00051     
00056     typedef enum {
00057         CONTEXT_UNKNOWN = 1,    
00058         CONTEXT_NETWORK,        
00059         CONTEXT_LOCAL           
00060     } context_t;
00061 
00063     enum {
00064         USE_CRC = 1 << 0,
00065     };
00066 
00068     enum {
00069         ALLOC_MEM       = 1<<0, 
00070         NULL_TERMINATED = 1<<1, 
00071     };
00072 };
00073 
00085 class Builder {
00086 public:
00087     Builder() {}
00088     Builder(const Builder&) {}
00089 
00090     static Builder& builder() {
00091         return static_builder_;
00092     }
00093 
00094 protected:
00095     static Builder static_builder_;
00096 };
00097 
00101 class SerializableObject {
00102 public:
00103     virtual ~SerializableObject() {}
00104 
00109     virtual void serialize(SerializeAction* a) = 0;
00110 };
00111 
00115 typedef std::vector<SerializableObject*> SerializableObjectVector;
00116 
00128 class SerializeAction : public Serialize {
00129 public:
00130     
00138     SerializeAction(action_t action, context_t context, int options = 0);
00139 
00145     virtual int action(SerializableObject* object);
00146 
00150     virtual void begin_action();
00151 
00155     virtual void end_action();
00156 
00160     void reset_error() { error_ = false; }
00161 
00165     action_t action_code() { return action_; }
00166     
00170     context_t context() { return context_; }
00171 
00175     bool error() { return error_; }
00176 
00177     /***********************************************************************
00178      *
00179      * Processor functions, one for each type.
00180      *
00181      ***********************************************************************/
00182 
00191     virtual void process(const char* name, SerializableObject* object)
00192     {
00193         (void)name;
00194         object->serialize(this);
00195     }
00196 
00200     virtual void process(const char* name, u_int64_t* i) = 0;
00201     
00205     virtual void process(const char* name, u_int32_t* i) = 0;
00206 
00210     virtual void process(const char* name, u_int16_t* i) = 0;
00211 
00215     virtual void process(const char* name, u_int8_t* i) = 0;
00216 
00220     virtual void process(const char* name, bool* b) = 0;
00221 
00229     virtual void process(const char* name, u_char* bp, u_int32_t len) = 0;
00230 
00243     virtual void process(const char* name, u_char** bp,
00244                          u_int32_t* lenp, int flags) = 0;
00245 
00249     virtual void process(const char* name, std::string* s) = 0;
00250 
00252 
00255     virtual void process(const char* name, int64_t* i)
00256     {
00257         process(name, (u_int64_t*)i);
00258     }
00259     
00260     virtual void process(const char* name, int32_t* i)
00261     {
00262         process(name, (u_int32_t*)i);
00263     }
00264     
00265     virtual void process(const char* name, int16_t* i)
00266     {
00267         process(name, (u_int16_t*)i);
00268     }
00269 
00270     virtual void process(const char* name, int8_t* i)
00271     {
00272         process(name, (u_int8_t*)i);
00273     }
00274 
00275     virtual void process(const char* name, char* bp, u_int32_t len)
00276     {
00277         process(name, (u_char*)bp, len);
00278     }
00279 
00280     virtual void process(const char* name, char** bp,
00281                          u_int32_t* lenp, int flags)
00282     {
00283         process(name, (u_char**)bp, lenp, flags);
00284     }
00285 
00286     
00288 
00290     void logpath(const char* log) { log_ = log; }
00291     
00295     virtual ~SerializeAction();
00296 
00297 protected:
00298     action_t  action_;  
00299     context_t context_; 
00300 
00301     int       options_; 
00302     const char* log_;   
00303     
00307     void signal_error() { error_ = true; }
00308     
00309 private:
00310     bool      error_;   
00311 
00312     SerializeAction();  
00313 };
00314 
00315 } // namespace oasys
00316 
00317 #endif /* _OASYS_SERIALIZE_H_ */

Generated on Sat Sep 8 08:43:33 2007 for DTN Reference Implementation by  doxygen 1.5.3