OpenStructure
Loading...
Searching...
No Matches
container_serialization.hh
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2// This file is part of the OpenStructure project <www.openstructure.org>
3//
4// Copyright (C) 2008-2020 by the OpenStructure authors
5//
6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License as published by the Free
8// Software Foundation; either version 3.0 of the License, or (at your option)
9// any later version.
10// This library is distributed in the hope that it will be useful, but WITHOUT
11// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13// details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with this library; if not, write to the Free Software Foundation, Inc.,
17// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18//------------------------------------------------------------------------------
19#ifndef OST_CONTAINER_SERIALIZATION_HH
20#define OST_CONTAINER_SERIALIZATION_HH
21/*
22 Author: Marco Biasini
23 */
24#include <ost/stdint.hh>
25#include <vector>
26#include <map>
27
28#include <boost/type_traits/is_pod.hpp>
29namespace ost { namespace io {
30
31template <typename T>
32void Serialize(BinaryDataSink& sink, const std::vector<T>& value) {
33 sink << uint32_t(value.size());
34 if (boost::is_pod<T>::value) {
35 sink.Stream().write(reinterpret_cast<const char*>(&value.front()),
36 sizeof(T)*value.size());
37 } else {
38 typename std::vector<T>::const_iterator i=value.begin();
39 for (; i!=value.end(); ++i) {
40 sink << *i;
41 }
42 }
43}
44
45template <typename T>
46void Serialize(BinaryDataSource& source, std::vector<T>& value) {
47 uint32_t vector_size;
48 source >> vector_size;
49 value.resize(vector_size);
50 if (boost::is_pod<T>::value) {
51 source.Stream().read(reinterpret_cast<char*>(&value.front()),
52 sizeof(T)*vector_size);
53 } else {
54 typename std::vector<T>::iterator i=value.begin();
55 for (; i!=value.end(); ++i) {
56 source >> *i;
57 }
58 }
59}
60
61template <typename K, typename V>
62void Serialize(BinaryDataSink& sink, const std::map<K, V>& value) {
63 sink << uint32_t(value.size());
64 typename std::map<K, V>::const_iterator i=value.begin();
65 for (; i!=value.end(); ++i) {
66 sink << i->first;
67 sink << i->second;
68 }
69}
70
71template <typename K, typename V>
72void Serialize(BinaryDataSource& source, std::map<K, V>& value) {
73 uint32_t vector_size;
74 source >> vector_size;
75 for (uint32_t i=0; i<vector_size; ++i) {
76 K k;
77 source >> k;
78 std::pair<typename std::map<K, V>::iterator, bool> p;
79 p=value.insert(std::make_pair(k, V()));
80 source >> p.first->second;
81 }
82}
83
84}}
85
86#endif
void Serialize(BinaryDataSink &sink, const T &value)
Definition base.dox:1
unsigned int uint32_t
Definition stdint_msc.hh:80