Electroneum
portable_storage_base.h
Go to the documentation of this file.
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 
28 
29 #pragma once
30 
31 #include <boost/variant.hpp>
32 #include <boost/any.hpp>
33 #include <string>
34 #include <vector>
35 #include <deque>
36 
37 #define PORTABLE_STORAGE_SIGNATUREA 0x01011101
38 #define PORTABLE_STORAGE_SIGNATUREB 0x01020101 // bender's nightmare
39 #define PORTABLE_STORAGE_FORMAT_VER 1
40 
41 #define PORTABLE_RAW_SIZE_MARK_MASK 0x03
42 #define PORTABLE_RAW_SIZE_MARK_BYTE 0
43 #define PORTABLE_RAW_SIZE_MARK_WORD 1
44 #define PORTABLE_RAW_SIZE_MARK_DWORD 2
45 #define PORTABLE_RAW_SIZE_MARK_INT64 3
46 
47 #ifndef MAX_STRING_LEN_POSSIBLE
48 #define MAX_STRING_LEN_POSSIBLE 2000000000 //do not let string be so big
49 #endif
50 
51 //data types
52 #define SERIALIZE_TYPE_INT64 1
53 #define SERIALIZE_TYPE_INT32 2
54 #define SERIALIZE_TYPE_INT16 3
55 #define SERIALIZE_TYPE_INT8 4
56 #define SERIALIZE_TYPE_UINT64 5
57 #define SERIALIZE_TYPE_UINT32 6
58 #define SERIALIZE_TYPE_UINT16 7
59 #define SERIALIZE_TYPE_UINT8 8
60 #define SERIALIZE_TYPE_DUOBLE 9
61 #define SERIALIZE_TYPE_STRING 10
62 #define SERIALIZE_TYPE_BOOL 11
63 #define SERIALIZE_TYPE_OBJECT 12
64 #define SERIALIZE_TYPE_ARRAY 13
65 
66 #define SERIALIZE_FLAG_ARRAY 0x80
67 
68 
69 namespace epee
70 {
71  namespace serialization
72  {
73  struct section;
74 
75  template<typename T> struct entry_container { typedef std::vector<T> type; static void reserve(type &t, size_t n) { t.reserve(n); } };
76  template<> struct entry_container<bool> { typedef std::deque<bool> type; static void reserve(type &t, size_t n) {} };
77 
78  /************************************************************************/
79  /* */
80  /************************************************************************/
81  template<class t_entry_type>
83  {
85  array_entry_t(const array_entry_t& other):m_array(other.m_array), m_it(m_array.end()){}
86 
87  const t_entry_type* get_first_val() const
88  {
89  m_it = m_array.begin();
90  return get_next_val();
91  }
92 
93  t_entry_type* get_first_val()
94  {
95  m_it = m_array.begin();
96  return get_next_val();
97  }
98 
99 
100  const t_entry_type* get_next_val() const
101  {
102  if(m_it == m_array.end())
103  return nullptr;
104  return &(*(m_it++));
105  }
106 
107  t_entry_type* get_next_val()
108  {
109  if(m_it == m_array.end())
110  return nullptr;
111  return (t_entry_type*)&(*(m_it++));//fuckoff
112  }
113 
114  t_entry_type& insert_first_val(const t_entry_type& v)
115  {
116  m_array.clear();
117  m_it = m_array.end();
118  return insert_next_value(v);
119  }
120 
121  t_entry_type& insert_next_value(const t_entry_type& v)
122  {
123  m_array.push_back(v);
124  return m_array.back();
125  }
126 
127  void reserve(size_t n)
128  {
130  }
131 
134  };
135 
136 
137  typedef boost::make_recursive_variant<
152  >::type array_entry;
153 
154  typedef boost::variant<uint64_t, uint32_t, uint16_t, uint8_t, int64_t, int32_t, int16_t, int8_t, double, bool, std::string, section, array_entry> storage_entry;
155 
156  typedef std::string binarybuffer;//it's ok
157 
158  /************************************************************************/
159  /* */
160  /************************************************************************/
161  struct section
162  {
163  std::map<std::string, storage_entry> m_entries;
164  };
165 
166  //handle-like aliases
167  typedef section* hsection;
168  typedef array_entry* harray;
169  }
170 }
::std::string string
Definition: gtest-port.h:1097
t_entry_type & insert_first_val(const t_entry_type &v)
void reserve(size_t n)
const t_entry_type * get_next_val() const
boost::variant< uint64_t, uint32_t, uint16_t, uint8_t, int64_t, int32_t, int16_t, int8_t, double, bool, std::string, section, array_entry > storage_entry
t_entry_type * get_first_val()
boost::make_recursive_variant< array_entry_t< section >, array_entry_t< uint64_t >, array_entry_t< uint32_t >, array_entry_t< uint16_t >, array_entry_t< uint8_t >, array_entry_t< int64_t >, array_entry_t< int32_t >, array_entry_t< int16_t >, array_entry_t< int8_t >, array_entry_t< double >, array_entry_t< bool >, array_entry_t< std::string >, array_entry_t< section >, array_entry_t< boost::recursive_variant_ > >::type array_entry
entry_container< t_entry_type >::type::const_iterator m_it
entry_container< t_entry_type >::type m_array
array_entry_t()
static void reserve(type &t, size_t n)
t_entry_type & insert_next_value(const t_entry_type &v)
array_entry_t(const array_entry_t &other)
const t_entry_type * get_first_val() const
int bool
Definition: stdbool.h:36
std::map< std::string, storage_entry > m_entries
t_entry_type * get_next_val()