Debian Codegraph @

contrib/epee/include/storages/portable_storage_base.h

raw symbols SCIP
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 <string>
33 #include <vector>
34 #include <deque>
35 #include <map>
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_DOUBLE 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>
82 struct array_entry_t
83 {
84 array_entry_t():m_it(m_array.end()){}
85 array_entry_t(const array_entry_t& other):m_array(other.m_array), m_it(m_array.end()){}
86
87 array_entry_t& operator=(const array_entry_t& other)
88 {
89 m_array = other.m_array;
90 m_it = m_array.end();
91 return *this;
92 }
93
94 const t_entry_type* get_first_val() const
95 {
96 m_it = m_array.begin();
97 return get_next_val();
98 }
99
100 t_entry_type* get_first_val()
101 {
102 m_it = m_array.begin();
103 return get_next_val();
104 }
105
106
107 const t_entry_type* get_next_val() const
108 {
109 if(m_it == m_array.end())
110 return nullptr;
111 return &(*(m_it++));
112 }
113
114 t_entry_type* get_next_val()
115 {
116 if(m_it == m_array.end())
117 return nullptr;
118 return (t_entry_type*)&(*(m_it++));//fuckoff
119 }
120
121 t_entry_type& insert_first_val(t_entry_type&& v)
122 {
123 m_array.clear();
124 m_it = m_array.end();
125 return insert_next_value(std::move(v));
126 }
127
128 t_entry_type& insert_next_value(t_entry_type&& v)
129 {
130 m_array.push_back(std::move(v));
131 return m_array.back();
132 }
133
134 void reserve(size_t n)
135 {
136 entry_container<t_entry_type>::reserve(m_array, n);
137 }
138
139 typename entry_container<t_entry_type>::type m_array;
140 mutable typename entry_container<t_entry_type>::type::const_iterator m_it;
141 };
142
143
144 typedef boost::make_recursive_variant<
145 array_entry_t<section>,
146 array_entry_t<uint64_t>,
147 array_entry_t<uint32_t>,
148 array_entry_t<uint16_t>,
149 array_entry_t<uint8_t>,
150 array_entry_t<int64_t>,
151 array_entry_t<int32_t>,
152 array_entry_t<int16_t>,
153 array_entry_t<int8_t>,
154 array_entry_t<double>,
155 array_entry_t<bool>,
156 array_entry_t<std::string>,
157 array_entry_t<section>,
158 array_entry_t<boost::recursive_variant_>
159 >::type array_entry;
160
161 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;
162
163 typedef std::string binarybuffer;//it's ok
164
165 /************************************************************************/
166 /* */
167 /************************************************************************/
168 struct section
169 {
170 std::map<std::string, storage_entry> m_entries;
171 };
172
173 //handle-like aliases
174 typedef section* hsection;
175 typedef array_entry* harray;
176 }
177 }