00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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
00056 int format(char* buf, size_t sz) const {
00057 return snprintf(buf, sz, "%d", value_);
00058 }
00059
00060
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
00082 int format(char* buf, size_t sz) const {
00083 return snprintf(buf, sz, "%u", value_);
00084 }
00085
00086
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
00113 int format(char* buf, size_t sz) const {
00114 return snprintf(buf, sz, "%s", str_.c_str());
00115 }
00116
00117
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
00146 int format(char* buf, size_t sz) const {
00147 return snprintf(buf, sz, "%s", str_);
00148 }
00149
00150
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
00184 int format(char* buf, size_t sz) const {
00185 return snprintf(buf, sz, "NEED TO IMPLEMENT ByteBufShim::format");
00186 }
00187
00188
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 };
00251
00252 #endif //__TYPE_SHIMS_H__