hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
blob.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_COMMON_BLOB_HPP
7 #define IROHA_COMMON_BLOB_HPP
8 
9 #include <algorithm>
10 #include <array>
11 #include <cstdint>
12 #include <stdexcept>
13 #include <string>
14 
15 #include "common/hexutils.hpp"
16 #include "common/result.hpp"
17 
18 namespace iroha {
19  using BadFormatException = std::invalid_argument;
20  using byte_t = uint8_t;
21 
30  template <size_t size_>
31  class blob_t : public std::array<byte_t, size_> {
32  public:
36  blob_t() {
37  this->fill(0);
38  }
39 
43  constexpr static size_t size() {
44  return size_;
45  }
46 
50  std::string to_string() const noexcept {
51  return std::string{this->begin(), this->end()};
52  }
53 
57  std::string to_hexstring() const noexcept {
58  return bytestringToHexstring(std::string_view{
59  reinterpret_cast<const char *>(this->data()), this->size()});
60  }
61 
62  static blob_t<size_> from_raw(const byte_t data[size_]) {
63  blob_t<size_> b;
64  std::copy(data, data + size_, b.begin());
65  return b;
66  }
67 
69  std::string_view data) {
70  if (data.size() != size_) {
71  return expected::makeError(
72  std::string{"blob_t: input string has incorrect length. Found: "}
73  + std::to_string(data.size())
74  + +", required: " + std::to_string(size_));
75  }
76  return from_raw(reinterpret_cast<const byte_t *>(data.data()));
77  }
78 
80  std::string_view hex) {
82  [](auto &&bytes) { return from_string(bytes); };
83  }
84  };
85 } // namespace iroha
86 
87 #endif // IROHA_COMMON_BLOB_HPP
static expected::Result< blob_t< size_ >, std::string > from_hexstring(std::string_view hex)
Definition: blob.hpp:79
std::invalid_argument BadFormatException
Definition: blob.hpp:19
blob_t()
Definition: blob.hpp:36
static constexpr size_t size()
Definition: blob.hpp:43
std::string bytestringToHexstring(std::string_view str)
Definition: hexutils.hpp:51
std::string to_hexstring() const noexcept
Definition: blob.hpp:57
Definition: result_fwd.hpp:27
static blob_t< size_ > from_raw(const byte_t data[size_])
Definition: blob.hpp:62
std::string to_string() const noexcept
Definition: blob.hpp:50
Definition: block_query.hpp:15
static expected::Result< blob_t< size_ >, std::string > from_string(std::string_view data)
Definition: blob.hpp:68
Definition: blob.hpp:31
iroha::expected::Result< std::string, const char * > hexstringToBytestringResult(std::string_view str)
Definition: hexutils.hpp:65
uint8_t byte_t
Definition: blob.hpp:20