8 #ifndef CPPCMS_ARCHIVE_TRAITS_H 9 #define CPPCMS_ARCHIVE_TRAITS_H 11 #include <cppcms/serialization_classes.h> 12 #include <cppcms/json.h> 14 #include <booster/shared_ptr.h> 15 #include <booster/intrusive_ptr.h> 16 #include <booster/hold_ptr.h> 17 #include <booster/clone_ptr.h> 18 #include <booster/copy_ptr.h> 20 #include <booster/traits/enable_if.h> 21 #include <booster/traits/is_base_of.h> 23 #include <booster/auto_ptr_inc.h> 37 struct archive_traits<S,typename
booster::enable_if<booster::is_base_of<serializable_base,S> >::type > {
38 static void save(serializable_base
const &d,archive &a)
42 static void load(serializable_base &d,archive &a)
49 struct archive_traits<T const> {
50 static void save(T
const &o,archive &a)
54 static void load(T &o,archive &a)
61 struct archive_traits<std::string> {
62 static void save(std::string
const &o,archive &a)
64 a.write_chunk(o.c_str(),o.size());
66 static void load(std::string &o,archive &a)
68 std::string res = a.read_chunk_as_string();
80 struct const_pod_data {
85 inline archive &
operator<<(archive &a,const_pod_data
const &d)
92 inline archive &
operator&(archive &a,pod_data
const &d)
94 if(a.mode()==archive::load_from_archive)
97 a.write_chunk(d.pointer,d.len);
100 inline archive &
operator<<(archive &a,pod_data
const &d)
105 inline archive &
operator>>(archive &a,pod_data
const &d)
114 details::pod_data as_pod(T &v)
116 details::pod_data d = { &v ,
sizeof(v) };
121 details::const_pod_data as_pod(T
const &v)
123 details::const_pod_data d = { &v ,
sizeof(v) };
130 #define CPPCMS_TRIVIAL_ARCHIVE(Type) \ 133 struct archive_traits<std::vector<Type> > { \ 134 typedef std::vector<Type> vec; \ 135 static void save(vec const &v,archive &a) \ 139 v.size()*sizeof(Type); \ 142 a.write_chunk(p,len); \ 144 static void load(vec &v,archive &a) \ 146 size_t n=a.next_chunk_size() / \ 153 a.read_chunk(p,n*sizeof(Type)); \ 158 struct archive_traits<Type[n]> { \ 159 static void save(Type const d[n],archive &a) \ 161 a.write_chunk(&d[0],sizeof(Type)*n); \ 163 static void load(Type d[n],archive &a) \ 165 a.read_chunk(&d[0],sizeof(Type)*n); \ 169 struct archive_traits<Type const [n]> { \ 170 static void save(Type const d[n],archive &a) \ 172 a.write_chunk(&d[0],sizeof(Type)*n); \ 177 struct archive_traits<Type> { \ 178 static void save(Type const d,archive &a) \ 180 a.write_chunk(&d,sizeof(d)); \ 182 static void load(Type &d,archive &a) \ 184 a.read_chunk(&d,sizeof(d)); \ 189 CPPCMS_TRIVIAL_ARCHIVE(
char)
190 CPPCMS_TRIVIAL_ARCHIVE(
signed char)
191 CPPCMS_TRIVIAL_ARCHIVE(
unsigned char)
192 CPPCMS_TRIVIAL_ARCHIVE(
signed short)
193 CPPCMS_TRIVIAL_ARCHIVE(
unsigned short)
194 CPPCMS_TRIVIAL_ARCHIVE(
signed int)
195 CPPCMS_TRIVIAL_ARCHIVE(
unsigned int)
196 CPPCMS_TRIVIAL_ARCHIVE(
signed long)
197 CPPCMS_TRIVIAL_ARCHIVE(
unsigned long)
198 CPPCMS_TRIVIAL_ARCHIVE(
signed long long)
199 CPPCMS_TRIVIAL_ARCHIVE(
unsigned long long)
200 CPPCMS_TRIVIAL_ARCHIVE(
wchar_t)
201 CPPCMS_TRIVIAL_ARCHIVE(
float)
202 CPPCMS_TRIVIAL_ARCHIVE(
double)
203 CPPCMS_TRIVIAL_ARCHIVE(
long double)
208 template<
typename T,
int size>
209 struct archive_traits<T [size]>
211 static void save(T
const d[size],archive &a)
213 for(
int i=0;i<size;i++)
216 static void load(T d[size],archive &a)
218 for(
int i=0;i<size;i++)
223 template<
typename F,
typename S>
224 struct archive_traits<std::pair<F,S> >
226 static void save(std::pair<F,S>
const &d,archive &a)
231 static void load(std::pair<F,S> &d,archive &a)
239 struct archive_traits<json::value> {
240 static void save(json::value
const &v,archive &a)
242 std::ostringstream ss;
244 std::string s=ss.str();
245 a.write_chunk(s.c_str(),s.size());
247 static void load(json::value &v,archive &a)
249 std::istringstream ss;
250 ss.str(a.read_chunk_as_string());
251 if(!v.load(ss,
true)) {
252 throw archive_error(
"Invalid json");
260 void archive_save_container(T
const &v,archive &a)
262 typename T::const_iterator it;
266 for(it=v.begin();it!=v.end();++it) {
272 void archive_load_container(T &v,archive &a)
277 std::insert_iterator<T> it(v,v.begin());
278 typedef typename T::value_type value_type;
279 for(
size_t i=0;i<n;i++) {
291 #define CPPCMS_CONTAINER_ARCHIVE2(Type) \ 293 template<typename V1,typename V2> \ 294 struct archive_traits<Type<V1,V2> > \ 296 typedef Type<V1,V2> cont; \ 297 static void save(cont const &d,archive &a) \ 299 details::archive_save_container<cont>(d,a); \ 301 static void load(cont &v,archive &a) \ 304 archive_traits<size_t>::load(n,a); \ 306 typedef std::pair<V1,V2> pair_type; \ 307 for(size_t i=0;i<n;i++) { \ 309 archive_traits<pair_type>::load(tmp,a); \ 316 #define CPPCMS_CONTAINER_ARCHIVE(Type) \ 318 template<typename V> \ 319 struct archive_traits<Type<V> > \ 321 static void save(Type<V> const &d,archive &a) \ 323 details::archive_save_container<Type<V> >(d,a); \ 325 static void load(Type<V> &d,archive &a) \ 327 details::archive_load_container<Type<V> >(d,a); \ 332 CPPCMS_CONTAINER_ARCHIVE(std::vector)
333 CPPCMS_CONTAINER_ARCHIVE(std::list)
334 CPPCMS_CONTAINER_ARCHIVE2(std::map)
335 CPPCMS_CONTAINER_ARCHIVE2(std::multimap)
336 CPPCMS_CONTAINER_ARCHIVE(std::set)
337 CPPCMS_CONTAINER_ARCHIVE(std::multiset)
339 #define CPPCMS_ARCHIVE_SMART_POINTER(SmartPtr) \ 341 template<typename V> \ 342 struct archive_traits<SmartPtr<V> > { \ 343 typedef SmartPtr<V> pointer; \ 344 static void save(pointer const &d,archive &a) \ 346 char empty = d.get() == 0; \ 347 a.write_chunk(&empty,1); \ 349 archive_traits<V>::save(*d,a); \ 352 static void load(pointer &d,archive &a) \ 355 a.read_chunk(&empty,1); \ 361 archive_traits<V>::load(*d,a); \ 368 #define CPPCMS_ARCHIVE_INTRUSIVE_POINTER(SmartPtr) \ 370 template<typename V> \ 371 struct archive_traits<SmartPtr<V> > { \ 372 typedef SmartPtr<V> pointer; \ 373 static void save(pointer const &d,archive &a) \ 375 char empty = d.get() == 0; \ 376 a.write_chunk(&empty,1); \ 378 archive_traits<V>::save(*d,a); \ 381 static void load(pointer &d,archive &a) \ 384 a.read_chunk(&empty,1); \ 390 archive_traits<V>::load(*d,a); \ 400 CPPCMS_ARCHIVE_SMART_POINTER(
booster::hold_ptr)
401 CPPCMS_ARCHIVE_SMART_POINTER(
booster::copy_ptr)
402 CPPCMS_ARCHIVE_SMART_POINTER(
booster::clone_ptr)
403 CPPCMS_ARCHIVE_SMART_POINTER(std::auto_ptr)
404 CPPCMS_ARCHIVE_INTRUSIVE_POINTER(
booster::intrusive_ptr)
value_type
Definition: formatting.h:85
This is the namespace where all CppCMS functionality is placed.
Definition: application.h:19
archive & operator>>(archive &a, Archivable &object)
Definition: serialization_classes.h:186
archive & operator&(archive &a, Archivable &object)
Definition: serialization_classes.h:163
static void load(Object &d, archive &a)
void write_chunk(void const *begin, size_t len)
static void save(Object const &d, archive &a)
void read_chunk(void *begin, size_t len)
archive & operator<<(archive &a, Archivable const &object)
Definition: serialization_classes.h:176
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23