Electroneum
Loading...
Searching...
No Matches
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 "parserse_base_utils.h"
32#include "portable_storage.h"
33#include "file_io_utils.h"
34
35namespace epee
36{
37 namespace serialization
38 {
39 //-----------------------------------------------------------------------------------------------------------
40 template<class t_struct>
41 bool load_t_from_json(t_struct& out, const std::string& json_buff)
42 {
44 bool rs = ps.load_from_json(json_buff);
45 if(!rs)
46 return false;
47
48 return out.load(ps);
49 }
50 //-----------------------------------------------------------------------------------------------------------
51 template<class t_struct>
52 bool load_t_from_json_file(t_struct& out, const std::string& json_file)
53 {
54 std::string f_buff;
55 if(!file_io_utils::load_file_to_string(json_file, f_buff))
56 return false;
57
58 return load_t_from_json(out, f_buff);
59 }
60 //-----------------------------------------------------------------------------------------------------------
61 template<class t_struct>
62 bool store_t_to_json(t_struct& str_in, std::string& json_buff, size_t indent = 0, bool insert_newlines = true)
63 {
65 str_in.store(ps);
66 ps.dump_as_json(json_buff, indent, insert_newlines);
67 return true;
68 }
69 //-----------------------------------------------------------------------------------------------------------
70 template<class t_struct>
71 std::string store_t_to_json(t_struct& str_in, size_t indent = 0, bool insert_newlines = true)
72 {
73 std::string json_buff;
74 store_t_to_json(str_in, json_buff, indent, insert_newlines);
75 return json_buff;
76 }
77 //-----------------------------------------------------------------------------------------------------------
78 template<class t_struct>
79 bool store_t_to_json_file(t_struct& str_in, const std::string& fpath)
80 {
81 std::string json_buff;
82 store_t_to_json(str_in, json_buff);
83 return file_io_utils::save_string_to_file(fpath, json_buff);
84 }
85 //-----------------------------------------------------------------------------------------------------------
86 template<class t_struct>
87 bool load_t_from_binary(t_struct& out, const epee::span<const uint8_t> binary_buff)
88 {
90 bool rs = ps.load_from_binary(binary_buff);
91 if(!rs)
92 return false;
93
94 return out.load(ps);
95 }
96 //-----------------------------------------------------------------------------------------------------------
97 template<class t_struct>
98 bool load_t_from_binary(t_struct& out, const std::string& binary_buff)
99 {
100 return load_t_from_binary(out, epee::strspan<uint8_t>(binary_buff));
101 }
102 //-----------------------------------------------------------------------------------------------------------
103 template<class t_struct>
104 bool load_t_from_binary_file(t_struct& out, const std::string& binary_file)
105 {
106 std::string f_buff;
107 if(!file_io_utils::load_file_to_string(binary_file, f_buff))
108 return false;
109
110 return load_t_from_binary(out, f_buff);
111 }
112 //-----------------------------------------------------------------------------------------------------------
113 template<class t_struct>
114 bool store_t_to_binary(t_struct& str_in, std::string& binary_buff, size_t indent = 0)
115 {
117 str_in.store(ps);
118 return ps.store_to_binary(binary_buff);
119 }
120 //-----------------------------------------------------------------------------------------------------------
121 template<class t_struct>
122 std::string store_t_to_binary(t_struct& str_in, size_t indent = 0)
123 {
124 std::string binary_buff;
125 store_t_to_binary(str_in, binary_buff, indent);
126 return binary_buff;
127 }
128 }
129}
bool store_to_binary(binarybuffer &target)
bool dump_as_json(std::string &targetObj, size_t indent=0, bool insert_newlines=true)
bool load_from_json(const std::string &source)
bool load_from_binary(const epee::span< const uint8_t > target)
Non-owning sequence of data. Does not deep copy.
Definition span.h:57
bool load_file_to_string(const std::string &path_to_file, std::string &target_str, size_t max_size=1000000000)
bool save_string_to_file(const std::string &path_to_file, const std::string &str)
bool load_t_from_binary(t_struct &out, const epee::span< const uint8_t > binary_buff)
bool store_t_to_json(t_struct &str_in, std::string &json_buff, size_t indent=0, bool insert_newlines=true)
bool store_t_to_json_file(t_struct &str_in, const std::string &fpath)
bool load_t_from_binary_file(t_struct &out, const std::string &binary_file)
bool load_t_from_json_file(t_struct &out, const std::string &json_file)
bool load_t_from_json(t_struct &out, const std::string &json_buff)
bool store_t_to_binary(t_struct &str_in, std::string &binary_buff, size_t indent=0)
span< const T > strspan(const std::string &s) noexcept
make a span from a std::string
Definition span.h:171