Monero
portable_storage_template_helper.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 #pragma once
28 
29 #include <string>
30 
31 #include "byte_slice.h"
32 #include "parserse_base_utils.h"
33 #include "portable_storage.h"
34 #include "file_io_utils.h"
35 #include "span.h"
36 
37 namespace epee
38 {
39  class byte_stream;
40 
41  namespace serialization
42  {
43  //-----------------------------------------------------------------------------------------------------------
44  template<class t_struct>
45  bool load_t_from_json(t_struct& out, const std::string& json_buff)
46  {
48  bool rs = ps.load_from_json(json_buff);
49  if(!rs)
50  return false;
51 
52  return out.load(ps);
53  }
54  //-----------------------------------------------------------------------------------------------------------
55  template<class t_struct>
56  bool load_t_from_json_file(t_struct& out, const std::string& json_file)
57  {
58  std::string f_buff;
59  if(!file_io_utils::load_file_to_string(json_file, f_buff))
60  return false;
61 
62  return load_t_from_json(out, f_buff);
63  }
64  //-----------------------------------------------------------------------------------------------------------
65  template<class t_struct>
66  bool store_t_to_json(t_struct& str_in, std::string& json_buff, size_t indent = 0, bool insert_newlines = true)
67  {
69  str_in.store(ps);
70  ps.dump_as_json(json_buff, indent, insert_newlines);
71  return true;
72  }
73  //-----------------------------------------------------------------------------------------------------------
74  template<class t_struct>
75  std::string store_t_to_json(t_struct& str_in, size_t indent = 0, bool insert_newlines = true)
76  {
77  std::string json_buff;
78  store_t_to_json(str_in, json_buff, indent, insert_newlines);
79  return json_buff;
80  }
81  //-----------------------------------------------------------------------------------------------------------
82  template<class t_struct>
83  bool store_t_to_json_file(t_struct& str_in, const std::string& fpath)
84  {
85  std::string json_buff;
86  store_t_to_json(str_in, json_buff);
87  return file_io_utils::save_string_to_file(fpath, json_buff);
88  }
89  //-----------------------------------------------------------------------------------------------------------
90  template<class t_struct>
91  bool load_t_from_binary(t_struct& out, const epee::span<const uint8_t> binary_buff, const epee::serialization::portable_storage::limits_t *limits = NULL)
92  {
94  bool rs = ps.load_from_binary(binary_buff, limits);
95  if(!rs)
96  return false;
97 
98  return out.load(ps);
99  }
100  //-----------------------------------------------------------------------------------------------------------
101  template<class t_struct>
102  bool load_t_from_binary(t_struct& out, const std::string& binary_buff)
103  {
104  return load_t_from_binary(out, epee::strspan<uint8_t>(binary_buff));
105  }
106  //-----------------------------------------------------------------------------------------------------------
107  template<class t_struct>
108  bool load_t_from_binary_file(t_struct& out, const std::string& binary_file)
109  {
110  std::string f_buff;
111  if(!file_io_utils::load_file_to_string(binary_file, f_buff))
112  return false;
113 
114  return load_t_from_binary(out, f_buff);
115  }
116  //-----------------------------------------------------------------------------------------------------------
117  template<class t_struct>
118  bool store_t_to_binary(t_struct& str_in, byte_slice& binary_buff, size_t initial_buffer_size = 8192)
119  {
121  str_in.store(ps);
122  return ps.store_to_binary(binary_buff, initial_buffer_size);
123  }
124  //-----------------------------------------------------------------------------------------------------------
125  template<class t_struct>
126  byte_slice store_t_to_binary(t_struct& str_in, size_t initial_buffer_size = 8192)
127  {
128  byte_slice binary_buff;
129  store_t_to_binary(str_in, binary_buff, initial_buffer_size);
130  return binary_buff;
131  }
132  //-----------------------------------------------------------------------------------------------------------
133  template<class t_struct>
134  bool store_t_to_binary(t_struct& str_in, byte_stream& binary_buff)
135  {
137  str_in.store(ps);
138  return ps.store_to_binary(binary_buff);
139  }
140 
141  }
142 }
Definition: binary_utils.h:36
bool store_t_to_binary(t_struct &str_in, byte_slice &binary_buff, size_t initial_buffer_size=8192)
Definition: portable_storage_template_helper.h:118
bool load_t_from_binary_file(t_struct &out, const std::string &binary_file)
Definition: portable_storage_template_helper.h:108
::std::string string
Definition: gtest-port.h:1097
A partial drop-in replacement for std::ostream.
Definition: byte_stream.h:57
bool load_file_to_string(const std::string &path_to_file, std::string &target_str, size_t max_size=1000000000)
Definition: file_io_utils.cpp:105
Non-owning sequence of data. Does not deep copy.
Definition: span.h:54
Definition: portable_storage.h:45
bool store_t_to_json(t_struct &str_in, std::string &json_buff, size_t indent=0, bool insert_newlines=true)
Definition: portable_storage_template_helper.h:66
bool dump_as_json(std::string &targetObj, size_t indent=0, bool insert_newlines=true)
Definition: portable_storage.cpp:70
bool load_t_from_json(t_struct &out, const std::string &json_buff)
Definition: portable_storage_template_helper.h:45
bool store_t_to_json_file(t_struct &str_in, const std::string &fpath)
Definition: portable_storage_template_helper.h:83
bool load_from_json(const std::string &source)
Definition: portable_storage.cpp:80
Definition: byte_slice.h:68
bool save_string_to_file(const std::string &path_to_file, const std::string &str)
Definition: file_io_utils.cpp:71
TODO: (mj-xmr) This will be reduced in an another PR.
Definition: byte_slice.h:39
epee::serialization::portable_storage ps
Definition: load_from_binary.cpp:40
bool load_t_from_json_file(t_struct &out, const std::string &json_file)
Definition: portable_storage_template_helper.h:56
bool load_t_from_binary(t_struct &out, const epee::span< const uint8_t > binary_buff, const epee::serialization::portable_storage::limits_t *limits=NULL)
Definition: portable_storage_template_helper.h:91
Definition: portable_storage.h:52
bool store_to_binary(byte_slice &target, std::size_t initial_buffer_size=8192)
Definition: portable_storage.cpp:46
bool load_from_binary(const epee::span< const uint8_t > target, const limits_t *limits=nullptr)
Definition: portable_storage.cpp:87
indent
Definition: transfer.py:36