Scopira  20080306
traits.h
1 
2 /*
3  * Copyright (c) 2002-2010 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED__SCOPIRA_TOOL_TRAITS_H__
15 #define __INCLUDED__SCOPIRA_TOOL_TRAITS_H__
16 
17 #include <vector>
18 
19 #include <scopira/tool/flow.h>
20 #include <scopira/tool/util.h>
21 #include <scopira/tool/platform.h>
22 
23 namespace scopira
24 {
25  namespace tool
26  {
27  // base for traits
28  template <class T> class flowtraits_base_g;
29  // serialization via traits (default)
30  template <class T> class flowtraits_g;
31 
32  // specializtions
33  template <> class flowtraits_g<bool>;
34  template <> class flowtraits_g<unsigned char>;
35  template <> class flowtraits_g<char>;
36  template <> class flowtraits_g<short>;
37  template <> class flowtraits_g<int>;
38 #ifdef PLATFORM_32
39  template <> class flowtraits_g<long>; // no longer needed?
40 #endif
41  template <> class flowtraits_g<int64_t>;
42  template <> class flowtraits_g<float>;
43  template <> class flowtraits_g<double>;
44 
45  template <> class flowtraits_g<std::string>;
46 
47  template <class EE> class flowtraits_g<std::vector<EE> >;
48  }
49 }
50 
57 template <class T> class scopira::tool::flowtraits_base_g
58 {
59  public:
60  typedef T data_type;
61 
62  public:
63  template <class COL>
64  static bool load_collection(itflow_i &in, COL &v) {
65  typename COL::iterator ii, endii;
66  int64_t sz;
67 
68  if (!in.read_int64_t(sz))
69  return false;
70  // or should this be changed to a clear/push_back like system to support lists, etc?
71  v.resize(static_cast<size_t>(sz));
72  endii = v.end();
73  for (ii=v.begin(); ii != endii; ++ii)
74  if (!in.read_generic(*ii))
75  return false;
76 
77  return true;
78  }
79 
80  template <class COL>
81  static void save_collection(otflow_i &out, const COL &v) {
82  typename COL::const_iterator ii, endii;
83  int64_t sz;
84 
85  sz = v.size();
86 
87  out.write_int64_t(sz);
88 
89  endii = v.end();
90  for (ii=v.begin(); ii !=endii; ++ii)
91  out.write_generic(*ii);
92  }
93 };
94 
100 template <class T> class scopira::tool::flowtraits_g : public scopira::tool::flowtraits_base_g<T>
101 {
102  public:
103  static bool load(itflow_i &in, T & outv) { return outv.load(in); }
104  static void save(otflow_i &out, const T &v) { v.save(out); }
105  static void to_string(const T &v, std::string &out) { out = "binary"; }
106  static bool from_string(std::string &s, T &out) { return false; }
107 };
108 
110 template <>
112 {
113  public:
114  static bool load(itflow_i &in, bool & outv) { return in.read_bool(outv); }
115  static void save(otflow_i &out, const bool &v) { out.write_bool(v); }
116  static void to_string(const bool &v, std::string &out) { tool::int_to_string(v,out); }
117  static bool from_string(std::string &s, bool &out) { int i; bool b = tool::string_to_int(s,i); out= i!=0; return b; }
118 };
119 
121 template <>
122 class scopira::tool::flowtraits_g<unsigned char> : public scopira::tool::flowtraits_base_g<unsigned char>
123 {
124  public:
125  static bool load(itflow_i &in, unsigned char & outv) { return in.read_byte(outv)>0; }
126  static void save(otflow_i &out, const unsigned char &v) { out.write_byte(v); }
127  static void to_string(const unsigned char &v, std::string &out) { tool::int_to_string(v,out); }
128  static bool from_string(std::string &s, unsigned char &out) { int i; bool b = tool::string_to_int(s,i); out=static_cast<unsigned char>(i); return b; }
129 };
130 
132 template <>
134 {
135  public:
136  static bool load(itflow_i &in, char & outv) { return in.read_char(outv); }
137  static void save(otflow_i &out, const char &v) { out.write_char(v); }
138  static void to_string(const char &v, std::string &out) { tool::int_to_string(v,out); }
139  static bool from_string(std::string &s, char &out) { int i; bool b = tool::string_to_int(s,i); out=static_cast<char>(i); return b; }
140 };
141 
143 template <>
145 {
146  public:
147  static bool load(itflow_i &in, short & outv) { bool b;int ii; b = in.read_int(ii); outv = ii; return b; }
148  static void save(otflow_i &out, const short &v) { out.write_int(v); }
149  static void to_string(const short &v, std::string &out) { tool::int_to_string(v,out); }
150  static bool from_string(std::string &s, short &out) { int i; bool b = tool::string_to_int(s,i); out=static_cast<short>(i); return b; }
151 };
152 
154 template <>
156 {
157  public:
158  static bool load(itflow_i &in, int & outv) { return in.read_int(outv); }
159  static void save(otflow_i &out, const int &v) { out.write_int(v); }
160  static void to_string(const int &v, std::string &out) { tool::int_to_string(v,out); }
161  static bool from_string(std::string &s, int &out) { return tool::string_to_int(s,out); }
162 };
163 
166 #ifdef PLATFORM_32
167 template <>
169 {
170  public:
171  static bool load(itflow_i &in, long & outv) { return in.read_long(outv); }
172  static void save(otflow_i &out, const long &v) { out.write_long(v); }
173  static void to_string(const long &v, std::string &out) { tool::long_to_string(v,out); }
174  static bool from_string(std::string &s, long &out) { return tool::string_to_long(s,out); }
175 };
176 #endif
177 
179 template <>
181 {
182  public:
183  static bool load(itflow_i &in, int64_t & outv) { return in.read_int64_t(outv); }
184  static void save(otflow_i &out, const int64_t &v) { out.write_int64_t(v); }
185  static void to_string(const int64_t &v, std::string &out) { tool::int64_t_to_string(v,out); }
186  static bool from_string(std::string &s, int64_t &out) { return tool::string_to_int64_t(s,out); }
187 };
188 
190 template <>
192 {
193  public:
194  static bool load(itflow_i &in, float & outv) { return in.read_float(outv); }
195  static void save(otflow_i &out, const float &v) { out.write_float(v); }
196  static void to_string(const float &v, std::string &out) { tool::double_to_string(v,out); }
197  static bool from_string(std::string &s, float &out) { double i; bool b = tool::string_to_double(s,i); out=static_cast<float>(i); return b; }
198 };
199 
201 template <>
203 {
204  public:
205  static bool load(itflow_i &in, double & outv) { return in.read_double(outv); }
206  static void save(otflow_i &out, const double &v) { out.write_double(v); }
207  static void to_string(const double &v, std::string &out) { tool::double_to_string(v,out); }
208  static bool from_string(std::string &s, double &out) { return tool::string_to_double(s,out); }
209 };
210 
211 
212 // basic object specializations
213 template <>
215 {
216  public:
217  static bool load(itflow_i &in, std::string & outv) { return in.read_string(outv); }
218  static void save(otflow_i &out, const std::string &v) { out.write_string(v); }
219  static void to_string(const std::string &v, std::string &out) { out = v; }
220  static bool from_string(std::string &s, std::string &out) { out = s; return true; }
221 };
222 
223 
224 // some specializations of STL containers
225 template <class EE>
226 class scopira::tool::flowtraits_g<std::vector<EE> > : public scopira::tool::flowtraits_base_g<std::vector<EE> >
227 {
228  public:
229  static bool load(itflow_i &in, std::vector<EE> & outv) { return load_collection(in, outv); }
230  static void save(otflow_i &out, const std::vector<EE> &v) { save_collection(out, v); }
231  static void to_string(const std::vector<EE> &v, std::string &out) { out = "std::vector<EE>"; }
232  static bool from_string(std::string &s, std::vector<EE> &out) { return false; }
233 };
234 
235 //
236 // implementation of some stuff in flow.h to avoid circular refs
237 //
238 
239 template <class TT>
241 {
242  return scopira::tool::flowtraits_g<TT>::load(*this, v);
243 }
244 
245 template <class TT>
247 {
249 }
250 
251 #endif
252 
virtual bool read_int64_t(int64_t &)=0
virtual size_t read_byte(byte_t &out)
Definition: flow.h:127
bool read_generic(TT &v)
Definition: traits.h:240
virtual bool read_bool(bool &)=0
Definition: archiveflow.h:20
int64_t string_to_int64_t(const std::string &s)
std::string long_to_string(long i)
int string_to_int(const std::string &s)
Definition: flow.h:212
Definition: flow.h:281
virtual bool read_long(long &)=0
virtual void write_int(int)=0
Definition: traits.h:30
STL namespace.
std::string double_to_string(double i)
virtual void write_char(char)=0
virtual void write_bool(bool)=0
virtual void write_int64_t(int64_t)=0
virtual bool read_char(char &)=0
std::string int64_t_to_string(int64_t i)
void write_generic(const TT &v)
Definition: traits.h:246
virtual void write_long(long)=0
void int_to_string(bool val, std::string &out)
Definition: util.h:63
virtual bool read_double(double &)=0
virtual void write_float(float)=0
Definition: traits.h:28
long string_to_long(const std::string &s)
virtual bool read_string(std::string &)=0
virtual bool read_int(int &)=0
virtual bool read_float(float &)=0
virtual size_t write_byte(byte_t b)
Definition: flow.h:178
virtual void write_string(const std::string &)=0
double string_to_double(const std::string &s)