TypeShims.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 
00018 #ifndef __TYPE_SHIMS_H__
00019 #define __TYPE_SHIMS_H__
00020 
00021 #include <string>
00022 #include "../debug/Formatter.h"
00023 #include "Serialize.h"
00024 
00025 namespace oasys {
00026 
00027 //----------------------------------------------------------------------------
00028 class IntShim : public Formatter, public SerializableObject {
00029 public:
00030     IntShim(int32_t value = 0, const char* name = "int")
00031         : name_(name), value_(value) {}
00032     IntShim(const Builder&) {}
00033 
00034     // virtual from Formatter
00035     int format(char* buf, size_t sz) const {
00036         return snprintf(buf, sz, "%d", value_);
00037     }
00038     
00039     // virtual from SerializableObject
00040     void serialize(SerializeAction* a) {
00041         a->process(name_.c_str(), &value_);
00042     }
00043 
00044     int32_t value() const { return value_; }
00045     void assign(int32_t value) { value_ = value; }
00046 
00047     
00048 private:
00049     std::string name_;
00050     int32_t     value_;
00051 };
00052 
00053 //----------------------------------------------------------------------------
00054 class UIntShim : public Formatter, public SerializableObject {
00055 public:
00056     UIntShim(u_int32_t value = 0, const char* name = "u_int")
00057         : name_(name), value_(value) {}
00058     UIntShim(const Builder&) {}
00059     
00060     // virtual from Formatter
00061     int format(char* buf, size_t sz) const {
00062         return snprintf(buf, sz, "%u", value_);
00063     }
00064     
00065     // virtual from SerializableObject
00066     void serialize(SerializeAction* a) {
00067         a->process(name_.c_str(), &value_);
00068     }
00069 
00070     u_int32_t value() const { return value_; }
00071     void assign(u_int32_t value) { value_ = value; }
00072 
00073     bool operator==(const UIntShim& other) const {
00074         return value_ == other.value_;
00075     }
00076     
00077 private:
00078     std::string name_;
00079     u_int32_t   value_;
00080 };
00081 
00082 //----------------------------------------------------------------------------
00083 class StringShim : public Formatter, public SerializableObject {
00084 public:
00085     StringShim(const std::string& str = "", const char* name = "string")
00086         : name_(name), str_(str) {}
00087     StringShim(const Builder&) {}
00088     
00089     virtual ~StringShim() {}
00090     
00091     // virtual from Formatter
00092     int format(char* buf, size_t sz) const {
00093         return snprintf(buf, sz, "%s", str_.c_str());
00094     }
00095     
00096     // virtual from SerializableObject
00097     void serialize(SerializeAction* a) {
00098         a->process(name_.c_str(), &str_);
00099     }
00100 
00101     const std::string& value() const { return str_; }
00102     void assign(const std::string& str) { str_.assign(str); }
00103 
00104 private:
00105     std::string name_;
00106     std::string str_;
00107 };
00108 
00109 //----------------------------------------------------------------------------
00110 class NullStringShim : public Formatter, public SerializableObject {
00111 public:
00112     NullStringShim(const char* str, const char* name = "string") 
00113         : name_(name), str_(const_cast<char*>(str)) 
00114     {
00115         free_mem_ = (str == 0);
00116     }
00117 
00118     NullStringShim(const Builder&)
00119         : name_("string"), str_(NULL), free_mem_(false)
00120     {}
00121 
00122     ~NullStringShim() { if(free_mem_) { free(str_); } }
00123 
00124     // virtual from Formatter
00125     int format(char* buf, size_t sz) const {
00126         return snprintf(buf, sz, "%s", str_);
00127     }
00128     
00129     // virtual from SerializableObject
00130     void serialize(SerializeAction* a)
00131     {
00132         u_int32_t len = 0;
00133         a->process(name_.c_str(), &str_, &len,
00134                    Serialize::NULL_TERMINATED | Serialize::ALLOC_MEM);
00135         free_mem_ = true;
00136     }
00137 
00138     const char* value() const { return str_; }
00139 
00140 private:
00141     std::string name_;
00142     char* str_;
00143     bool free_mem_;
00144 };
00145 
00146 //----------------------------------------------------------------------------
00147 class ByteBufShim : public Formatter, public SerializableObject {
00148 public:
00149     ByteBufShim(char* buf, size_t size)
00150         : buf_(buf), size_(size), own_buf_(false) {}
00151 
00152     ByteBufShim(const Builder&) 
00153         : buf_(0), size_(0), own_buf_(false) {}
00154 
00155     ~ByteBufShim() {
00156         if (buf_ != 0 && own_buf_) {
00157             free(buf_);
00158         }
00159     }
00160 
00161     // virtual from Formatter
00162     int format(char* buf, size_t sz) const {
00163         return snprintf(buf, sz, "NEED TO IMPLEMENT ByteBufShim::format");
00164     }
00165 
00166     // virtual from SerializableObject
00167     void serialize(SerializeAction* a)
00168     {
00169         a->process("bytes", &buf_, &size_, Serialize::ALLOC_MEM);
00170 
00171         if (a->action_code() == Serialize::UNMARSHAL) {
00172             own_buf_ = true;
00173         }
00174     }
00175  
00176     const char* value() const { return buf_; }
00177     char*       take_buf()    { own_buf_ = false; return buf_; }
00178     u_int32_t   size()  const { return size_; }
00179 
00180 private:
00181     char*     buf_;
00182     u_int32_t size_;    
00183     bool      own_buf_;
00184 };
00185 
00186 //----------------------------------------------------------------------------
00200 template<typename _SerializablePrefix, typename _SerializableObject>
00201 struct PrefixAdapter : public SerializableObject {
00202     PrefixAdapter(_SerializablePrefix* prefix,
00203                   _SerializableObject* obj)
00204         : prefix_(prefix),
00205           obj_(obj)
00206     {}
00207     
00208     void serialize(SerializeAction* a) 
00209     {
00210         a->process("prefix", prefix_);
00211         a->process("obj",    obj_);
00212     }
00213 
00214     _SerializablePrefix* prefix_;
00215     _SerializableObject* obj_;
00216 };
00217 
00218 template<typename _SerializablePrefix, typename _SerializableObject>
00219 PrefixAdapter<_SerializablePrefix, _SerializableObject>
00220 prefix_adapter(_SerializablePrefix* prefix,
00221                _SerializableObject* obj)
00222 {
00223     return PrefixAdapter<_SerializablePrefix,
00224                          _SerializableObject>(prefix, obj);
00225 }
00226 
00227 }; // namespace oasys
00228 
00229 #endif //__TYPE_SHIMS_H__

Generated on Thu Jun 7 16:56:53 2007 for DTN Reference Implementation by  doxygen 1.5.1