src/primitives/inherent_data.hpp¶
Namespaces¶
| Name |
|---|
| sgns |
| sgns::primitives |
Classes¶
| Name | |
|---|---|
| struct | sgns::primitives::InherentData |
Types¶
| Name | |
|---|---|
| enum class | InherentDataError { IDENTIFIER_ALREADY_EXISTS = 1, IDENTIFIER_DOES_NOT_EXIST} inherent data encode/decode error codes |
| using base::Blob< 8u > | InherentIdentifier |
Functions¶
| Name | |
|---|---|
| OUTCOME_HPP_DECLARE_ERROR_2(sgns::primitives , InherentDataError ) | |
| template <class Stream ,typename =std::enable_if_t Stream & |
operator<<(Stream & s, const InherentData & v) output InherentData object instance to stream |
| template <class Stream ,typename =std::enable_if_t Stream & |
operator>>(Stream & s, InherentData & v) decodes InherentData object instance from stream |
Types Documentation¶
enum InherentDataError¶
| Enumerator | Value | Description |
|---|---|---|
| IDENTIFIER_ALREADY_EXISTS | 1 | |
| IDENTIFIER_DOES_NOT_EXIST |
inherent data encode/decode error codes
using InherentIdentifier¶
Functions Documentation¶
function OUTCOME_HPP_DECLARE_ERROR_2¶
function operator<<¶
template <class Stream ,
typename =std::enable_if_t<Stream::is_encoder_stream>>
Stream & operator<<(
Stream & s,
const InherentData & v
)
output InherentData object instance to stream
Parameters:
- s stream reference
- v value to output
Template Parameters:
- Stream stream type
Return: reference to stream
function operator>>¶
template <class Stream ,
typename =std::enable_if_t<Stream::is_decoder_stream>>
Stream & operator>>(
Stream & s,
InherentData & v
)
decodes InherentData object instance from stream
Parameters:
- s stream reference
- v value to decode
Template Parameters:
- Stream input stream type
Return: reference to stream
Source code¶
#ifndef SUPERGENIUS_INHERENT_DATA_HPP
#define SUPERGENIUS_INHERENT_DATA_HPP
#include <map>
#include <vector>
#include <boost/iterator_adaptors.hpp>
#include <boost/optional.hpp>
#include "outcome/outcome.hpp"
#include "base/blob.hpp"
#include "base/buffer.hpp"
#include "base/outcome_throw.hpp"
#include "scale/scale.hpp"
namespace sgns::primitives {
enum class InherentDataError {
IDENTIFIER_ALREADY_EXISTS = 1,
IDENTIFIER_DOES_NOT_EXIST
};
} // namespace sgns::primitives
OUTCOME_HPP_DECLARE_ERROR_2(sgns::primitives, InherentDataError);
namespace sgns::primitives {
using InherentIdentifier = base::Blob<8u>;
struct InherentData {
template <typename T>
outcome::result<void> putData(InherentIdentifier identifier, T inherent) {
if (data.find(identifier) == data.end()) {
data[identifier] = base::Buffer(scale::encode(inherent).value());
return outcome::success();
}
return InherentDataError::IDENTIFIER_ALREADY_EXISTS;
}
template <typename T>
void replaceData(InherentIdentifier identifier, T inherent) {
data[identifier] = base::Buffer(scale::encode(inherent).value());
}
template <typename T>
outcome::result<T> getData(const InherentIdentifier &identifier) const {
auto inherent = data.find(identifier);
if (inherent != data.end()) {
auto buf = inherent->second;
return scale::decode<T>(buf);
}
return InherentDataError::IDENTIFIER_DOES_NOT_EXIST;
}
bool operator==(const InherentData &rhs) const;
bool operator!=(const InherentData &rhs) const;
std::map<InherentIdentifier, base::Buffer> data;
friend std::ostream &operator<<(std::ostream &out, const InherentData &v)
{
const auto &data = v.data;
std::vector<std::pair<InherentIdentifier, base::Buffer>> vec;
vec.reserve(data.size());
for (auto &pair : data) {
vec.emplace_back(pair);
}
out << vec.size();
for(auto it = vec.begin();it != vec.end() ; it++)
{
// // out << *it ;
}
return out ;
}
};
template <class Stream,
typename = std::enable_if_t<Stream::is_encoder_stream>>
Stream &operator<<(Stream &s, const InherentData &v) {
const auto &data = v.data;
std::vector<std::pair<InherentIdentifier, base::Buffer>> vec;
vec.reserve(data.size());
for (auto &pair : data) {
vec.emplace_back(pair);
}
s << vec;
return s;
}
template <class Stream,
typename = std::enable_if_t<Stream::is_decoder_stream>>
Stream &operator>>(Stream &s, InherentData &v) {
std::vector<std::pair<InherentIdentifier, base::Buffer>> vec;
s >> vec;
for (const auto &item : vec) {
// throw if identifier already exists
if (v.data.find(item.first) != v.data.end()) {
base::raise(InherentDataError::IDENTIFIER_ALREADY_EXISTS);
}
v.data.insert(item);
}
return s;
}
} // namespace sgns::primitives
#endif // SUPERGENIUS_INHERENT_DATA_HPP
Updated on 2026-03-04 at 13:10:44 -0800