hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
common_c_types_helpers.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_COMMON_C_TYPES_HELPERS_HPP
7 #define IROHA_COMMON_C_TYPES_HELPERS_HPP
8 
10 
11 #include <cstddef>
12 #include <cstring>
13 #include <optional>
14 #include <string_view>
15 #include <type_traits>
16 #include <vector>
17 
18 #include "common/result.hpp"
19 
20 namespace iroha {
21 
22  inline void clearCharBuffer(Iroha_CharBuffer &buf) {
23  buf.data = nullptr;
24  buf.size = 0;
25  }
26 
27  inline void toCharBuffer(Iroha_CharBuffer &buf, const std::string &string) {
28  buf.data = new char[string.length() + 1];
29  strncpy(buf.data, string.c_str(), string.length());
30  buf.data[string.length()] = 0;
31  buf.size = string.length();
32  }
33 
34  inline void toCharBuffer(Iroha_CharBuffer &buf,
35  std::optional<std::string> const &opt_string) {
36  if (opt_string) {
37  toCharBuffer(buf, opt_string.value());
38  } else {
39  clearCharBuffer(buf);
40  }
41  }
42 
43  inline std::string_view charBufferToStringView(Iroha_CharBuffer const &buf) {
44  return std::string_view{buf.data, buf.size};
45  }
46 
47  inline std::vector<std::string_view> charBufferArrayToStringViewVector(
48  Iroha_CharBufferArray const &arr) {
49  std::vector<std::string_view> result;
50  Iroha_CharBuffer const *const end = arr.data + arr.size;
51  for (Iroha_CharBuffer *ptr = arr.data; ptr < end; ++ptr) {
52  result.emplace_back(charBufferToStringView(*ptr));
53  }
54  return result;
55  }
56 
57  using namespace iroha::expected;
58 
59  struct ResultVisitor {
61  Iroha_Result result;
63  clearCharBuffer(result.data);
64  return {};
65  }
66 
67  template <typename T, typename = std::enable_if_t<not std::is_void_v<T>>>
68  Iroha_Result operator()(Value<T> const &value) const {
69  Iroha_Result result;
71  toCharBuffer(result.data, value.value);
72  return result;
73  }
74 
75  template <typename T, typename = std::enable_if_t<not std::is_void_v<T>>>
76  Iroha_Result operator()(Error<T> const &error) const {
77  Iroha_Result result;
79  toCharBuffer(result.data, error.error);
80  return result;
81  }
82  };
83 
84 } // namespace iroha
85 
86 #endif
std::string_view charBufferToStringView(Iroha_CharBuffer const &buf)
Definition: common_c_types_helpers.hpp:43
Definition: result_fwd.hpp:20
Definition: result_fwd.hpp:15
Iroha_Result_Type which
Definition: common_c_types.h:30
Iroha_Result operator()(Value< void >) const
Definition: common_c_types_helpers.hpp:60
Definition: common_c_types.h:25
std::vector< std::string_view > charBufferArrayToStringViewVector(Iroha_CharBufferArray const &arr)
Definition: common_c_types_helpers.hpp:47
char * data
Definition: common_c_types.h:14
Iroha_Result operator()(Error< T > const &error) const
Definition: common_c_types_helpers.hpp:76
Definition: common_c_types.h:18
Definition: block_query.hpp:15
Definition: common_c_types_helpers.hpp:59
Iroha_CharBuffer data
Definition: common_c_types.h:29
unsigned long long size
Definition: common_c_types.h:20
Definition: common_c_types.h:24
void clearCharBuffer(Iroha_CharBuffer &buf)
Definition: common_c_types_helpers.hpp:22
Iroha_Result operator()(Value< T > const &value) const
Definition: common_c_types_helpers.hpp:68
Definition: common_c_types.h:13
void toCharBuffer(Iroha_CharBuffer &buf, const std::string &string)
Definition: common_c_types_helpers.hpp:27
Definition: common_c_types.h:28
Definition: result_fwd.hpp:10
Iroha_CharBuffer * data
Definition: common_c_types.h:19
unsigned long long size
Definition: common_c_types.h:15