TypeShims.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 
00039 #ifndef __TYPE_SHIMS_H__
00040 #define __TYPE_SHIMS_H__
00041 
00042 #include <string>
00043 #include "../debug/Formatter.h"
00044 #include "Serialize.h"
00045 
00046 namespace oasys {
00047 
00048 //----------------------------------------------------------------------------
00049 class IntShim : public Formatter, public SerializableObject {
00050 public:
00051     IntShim(int32_t value = 0, const char* name = "int")
00052         : name_(name), value_(value) {}
00053     IntShim(const Builder&) {}
00054 
00055     // virtual from Formatter
00056     int format(char* buf, size_t sz) const {
00057         return snprintf(buf, sz, "%d", value_);
00058     }
00059     
00060     // virtual from SerializableObject
00061     void serialize(SerializeAction* a) {
00062         a->process(name_.c_str(), &value_);
00063     }
00064 
00065     int32_t value() const { return value_; }
00066     void assign(int32_t value) { value_ = value; }
00067 
00068     
00069 private:
00070     std::string name_;
00071     int32_t     value_;
00072 };
00073 
00074 //----------------------------------------------------------------------------
00075 class UIntShim : public Formatter, public SerializableObject {
00076 public:
00077     UIntShim(u_int32_t value = 0, const char* name = "u_int")
00078         : name_(name), value_(value) {}
00079     UIntShim(const Builder&) {}
00080     
00081     // virtual from Formatter
00082     int format(char* buf, size_t sz) const {
00083         return snprintf(buf, sz, "%u", value_);
00084     }
00085     
00086     // virtual from SerializableObject
00087     void serialize(SerializeAction* a) {
00088         a->process(name_.c_str(), &value_);
00089     }
00090 
00091     u_int32_t value() const { return value_; }
00092     void assign(u_int32_t value) { value_ = value; }
00093 
00094     bool operator==(const UIntShim& other) const {
00095         return value_ == other.value_;
00096     }
00097     
00098 private:
00099     std::string name_;
00100     u_int32_t   value_;
00101 };
00102 
00103 //----------------------------------------------------------------------------
00104 class StringShim : public Formatter, public SerializableObject {
00105 public:
00106     StringShim(const std::string& str = "", const char* name = "string")
00107         : name_(name), str_(str) {}
00108     StringShim(const Builder&) {}
00109     
00110     virtual ~StringShim() {}
00111     
00112     // virtual from Formatter
00113     int format(char* buf, size_t sz) const {
00114         return snprintf(buf, sz, "%s", str_.c_str());
00115     }
00116     
00117     // virtual from SerializableObject
00118     void serialize(SerializeAction* a) {
00119         a->process(name_.c_str(), &str_);
00120     }
00121 
00122     const std::string& value() const { return str_; }
00123     void assign(const std::string& str) { str_.assign(str); }
00124 
00125 private:
00126     std::string name_;
00127     std::string str_;
00128 };
00129 
00130 //----------------------------------------------------------------------------
00131 class NullStringShim : public Formatter, public SerializableObject {
00132 public:
00133     NullStringShim(const char* str, const char* name = "string") 
00134         : name_(name), str_(const_cast<char*>(str)) 
00135     {
00136         free_mem_ = (str == 0);
00137     }
00138 
00139     NullStringShim(const Builder&)
00140         : name_("string"), str_(NULL), free_mem_(false)
00141     {}
00142 
00143     ~NullStringShim() { if(free_mem_) { free(str_); } }
00144 
00145     // virtual from Formatter
00146     int format(char* buf, size_t sz) const {
00147         return snprintf(buf, sz, "%s", str_);
00148     }
00149     
00150     // virtual from SerializableObject
00151     void serialize(SerializeAction* a)
00152     {
00153         size_t len = 0;
00154         a->process(name_.c_str(), 
00155                    reinterpret_cast<u_char**>(&str_), &len,
00156                    Serialize::NULL_TERMINATED | Serialize::ALLOC_MEM);
00157         free_mem_ = true;
00158     }
00159 
00160     const char* value() const { return str_; }
00161 
00162 private:
00163     std::string name_;
00164     char* str_;
00165     bool free_mem_;
00166 };
00167 
00168 //----------------------------------------------------------------------------
00169 class ByteBufShim : public Formatter, public SerializableObject {
00170 public:
00171     ByteBufShim(char* buf, size_t size)
00172         : buf_(buf), size_(size), own_buf_(false) {}
00173 
00174     ByteBufShim(const Builder&) 
00175         : buf_(0), size_(0), own_buf_(false) {}
00176 
00177     ~ByteBufShim() {
00178         if (buf_ != 0 && own_buf_) {
00179             free(buf_);
00180         }
00181     }
00182 
00183     // virtual from Formatter
00184     int format(char* buf, size_t sz) const {
00185         return snprintf(buf, sz, "NEED TO IMPLEMENT ByteBufShim::format");
00186     }
00187 
00188     // virtual from SerializableObject
00189     void serialize(SerializeAction* a)
00190     {
00191         a->process("bytes", (u_char**)&buf_,
00192                    &size_, Serialize::ALLOC_MEM);
00193 
00194         if (a->action_code() == Serialize::UNMARSHAL) {
00195             own_buf_ = true;
00196         }
00197     }
00198  
00199     const char* value() const { return buf_; }
00200     char*       take_buf()    { own_buf_ = false; return buf_; }
00201     size_t      size()  const { return size_; }
00202 
00203 private:
00204     char*  buf_;
00205     size_t size_;    
00206     bool   own_buf_;
00207 };
00208 
00209 //----------------------------------------------------------------------------
00223 template<typename _SerializablePrefix, typename _SerializableObject>
00224 struct PrefixAdapter : public SerializableObject {
00225     PrefixAdapter(_SerializablePrefix* prefix,
00226                   _SerializableObject* obj)
00227         : prefix_(prefix),
00228           obj_(obj)
00229     {}
00230     
00231     void serialize(SerializeAction* a) 
00232     {
00233         a->process("prefix", prefix_);
00234         a->process("obj",    obj_);
00235     }
00236 
00237     _SerializablePrefix* prefix_;
00238     _SerializableObject* obj_;
00239 };
00240 
00241 template<typename _SerializablePrefix, typename _SerializableObject>
00242 PrefixAdapter<_SerializablePrefix, _SerializableObject>
00243 prefix_adapter(_SerializablePrefix* prefix,
00244                _SerializableObject* obj)
00245 {
00246     return PrefixAdapter<_SerializablePrefix,
00247                          _SerializableObject>(prefix, obj);
00248 }
00249 
00250 }; // namespace oasys
00251 
00252 #endif //__TYPE_SHIMS_H__

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