Serialize.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_SERIALIZE_H_
00039 #define _OASYS_SERIALIZE_H_
00040 
00047 #include <string>
00048 #include <vector>
00049 #include <sys/types.h>
00050 #include "../compat/inttypes.h"
00051 
00052 namespace oasys {
00053 
00054 class Serialize;
00055 class SerializeAction;
00056 class SerializableObject;
00057 
00062 class Serialize {
00063 public:
00067     typedef enum {
00068         MARSHAL = 1,    
00069         UNMARSHAL,      
00070         INFO            
00071     } action_t;
00072     
00077     typedef enum {
00078         CONTEXT_UNKNOWN = 1,    
00079         CONTEXT_NETWORK,        
00080         CONTEXT_LOCAL           
00081     } context_t;
00082 
00084     enum {
00085         USE_CRC = 1 << 0,
00086     };
00087 
00089     enum {
00090         INCLUDE_NAME    = 1 << 0, 
00091         DOT_SEPARATED   = 1 << 1, 
00092     };
00093 
00095     enum {
00096         ALLOC_MEM       = 1<<0, 
00097         NULL_TERMINATED = 1<<1, 
00098     };
00099 };
00100 
00112 class Builder {
00113 public:
00114     Builder() {}
00115     Builder(const Builder&) {}
00116 
00117     static Builder& builder() {
00118         return static_builder_;
00119     }
00120 
00121 protected:
00122     static Builder static_builder_;
00123 };
00124 
00128 class SerializableObject {
00129 public:
00130     virtual ~SerializableObject() {}
00131 
00136     virtual void serialize(SerializeAction* a) = 0;
00137 };
00138 
00142 typedef std::vector<SerializableObject*> SerializableObjectVector;
00143 
00155 class SerializeAction : public Serialize {
00156 public:
00157     
00165     SerializeAction(action_t action, context_t context, int options = 0);
00166 
00172     virtual int action(SerializableObject* object);
00173 
00177     virtual void begin_action();
00178 
00182     virtual void end_action();
00183 
00187     action_t action_code() { return action_; }
00188     
00192     context_t context() { return context_; }
00193 
00197     bool error() { return error_; }
00198 
00199     /***********************************************************************
00200      *
00201      * Processor functions, one for each type.
00202      *
00203      ***********************************************************************/
00204 
00213     virtual void process(const char* name, SerializableObject* object)
00214     {
00215         (void)name;
00216         object->serialize(this);
00217     }
00218     
00222     virtual void process(const char* name, u_int32_t* i) = 0;
00223 
00227     virtual void process(const char* name, u_int16_t* i) = 0;
00228 
00232     virtual void process(const char* name, u_int8_t* i) = 0;
00233 
00237     virtual void process(const char* name, bool* b) = 0;
00238 
00246     virtual void process(const char* name, u_char* bp, size_t len) = 0;
00247 
00260     virtual void process(const char* name, u_char** bp,
00261                          size_t* lenp, int flags) = 0;
00262 
00266     virtual void process(const char* name, std::string* s) = 0;
00267 
00269 
00272     virtual void process(const char* name, int32_t* i)
00273     {
00274         process(name, (u_int32_t*)i);
00275     }
00276     
00277     virtual void process(const char* name, int16_t* i)
00278     {
00279         process(name, (u_int16_t*)i);
00280     }
00281 
00282     virtual void process(const char* name, int8_t* i)
00283     {
00284         process(name, (u_int8_t*)i);
00285     }
00286 
00287     virtual void process(const char* name, char* bp, size_t len)
00288     {
00289         process(name, (u_char*)bp, len);
00290     }
00291 
00293     
00295     void logpath(const char* log) { log_ = log; }
00296     
00300     virtual ~SerializeAction();
00301 
00302 protected:
00303     action_t  action_;  
00304     context_t context_; 
00305 
00306     int       options_; 
00307     const char* log_;   
00308     
00312     void signal_error() { error_ = true; }
00313     
00314 private:
00315     bool      error_;   
00316 
00317     SerializeAction();  
00318 };
00319 
00320 } // namespace oasys
00321 
00322 #endif /* _OASYS_SERIALIZE_H_ */

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1