Monero
Loading...
Searching...
No Matches
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 <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
69namespace 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
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 {
137 }
138
141 };
142
143
144 typedef boost::make_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;
176 }
177}
section * hsection
Definition portable_storage_base.h:174
std::string binarybuffer
Definition portable_storage_base.h:163
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
Definition portable_storage_base.h:161
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
Definition portable_storage_base.h:159
array_entry * harray
Definition portable_storage_base.h:175
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
Definition binary_utils.h:36
Definition portable_storage_base.h:83
t_entry_type * get_next_val()
Definition portable_storage_base.h:114
void reserve(size_t n)
Definition portable_storage_base.h:134
array_entry_t()
Definition portable_storage_base.h:84
t_entry_type * get_first_val()
Definition portable_storage_base.h:100
entry_container< t_entry_type >::type::const_iterator m_it
Definition portable_storage_base.h:140
const t_entry_type * get_first_val() const
Definition portable_storage_base.h:94
t_entry_type & insert_first_val(t_entry_type &&v)
Definition portable_storage_base.h:121
array_entry_t & operator=(const array_entry_t &other)
Definition portable_storage_base.h:87
t_entry_type & insert_next_value(t_entry_type &&v)
Definition portable_storage_base.h:128
const t_entry_type * get_next_val() const
Definition portable_storage_base.h:107
entry_container< t_entry_type >::type m_array
Definition portable_storage_base.h:139
array_entry_t(const array_entry_t &other)
Definition portable_storage_base.h:85
std::deque< bool > type
Definition portable_storage_base.h:76
static void reserve(type &t, size_t n)
Definition portable_storage_base.h:76
Definition portable_storage_base.h:75
static void reserve(type &t, size_t n)
Definition portable_storage_base.h:75
std::vector< T > type
Definition portable_storage_base.h:75
Definition portable_storage_base.h:169
std::map< std::string, storage_entry > m_entries
Definition portable_storage_base.h:170