Skip to content

Commit 9576c5d

Browse files
committed
fix: review issues
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
1 parent c30e4a7 commit 9576c5d

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

include/scale/detail/custom_decomposing.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
* struct Point {
1818
* int x, y;
1919
*
20-
* SCALE_CUSTOM_DECOMPOSING(Point, x, y);
20+
* SCALE_CUSTOM_DECOMPOSITION(Point, x, y);
2121
* };
2222
* @endcode
2323
*/
24-
#define SCALE_CUSTOM_DECOMPOSING(Self, ...) \
24+
#define SCALE_CUSTOM_DECOMPOSITION(Self, ...) \
2525
private: \
2626
decltype(auto) _custom_decompose_and_apply(auto &&f) { \
2727
return std::forward<decltype(f)>(f)(__VA_ARGS__); \

include/scale/detail/fixed_width_integer.hpp

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,47 @@ namespace scale {
2020

2121
namespace scale {
2222

23+
/**
24+
* @brief Concept for big fixed-width integer types.
25+
*
26+
* This concept checks if the given type is one of the predefined large
27+
* integer types.
28+
*/
2329
template <typename T>
2430
concept BigFixedWidthInteger =
2531
std::is_same_v<std::remove_cvref_t<T>, uint128_t>
2632
or std::is_same_v<std::remove_cvref_t<T>, uint256_t>
2733
or std::is_same_v<std::remove_cvref_t<T>, uint512_t>
2834
or std::is_same_v<std::remove_cvref_t<T>, uint1024_t>;
2935

36+
/**
37+
* @brief Traits for determining the size of fixed-width integer types.
38+
*
39+
* This primary template handles standard integral types.
40+
*/
3041
template <typename T, typename = void>
3142
struct FixedWidthIntegerTraits;
3243

33-
// Для целочисленных типов
44+
/**
45+
* @brief Specialization for built-in integral types.
46+
*/
3447
template <std::integral T>
3548
struct FixedWidthIntegerTraits<T> {
3649
static constexpr size_t bytes = sizeof(T);
3750
static constexpr size_t bits = bytes * 8;
3851
};
3952

53+
/**
54+
* @brief Specialization for Boost multiprecision integer types.
55+
*
56+
* Computes the number of bytes required to store the value,
57+
* excluding any metadata or auxiliary data.
58+
*/
4059
template <typename T>
4160
struct FixedWidthIntegerTraits<T, std::enable_if_t<std::is_class_v<T>>> {
4261
private:
62+
// Compute count significant bytes needed to store value,
63+
// except any metadata and auxiliaries
4364
static constexpr size_t compute_bytes() {
4465
T temp{};
4566
return temp.backend().size() * sizeof(temp.backend().limbs()[0]);
@@ -51,12 +72,27 @@ namespace scale {
5172
};
5273

5374
namespace detail {
75+
/**
76+
* @brief Converts a value from one type to another using static_cast.
77+
* @tparam To Target type.
78+
* @tparam From Source type.
79+
* @param value Value to convert.
80+
* @return Converted value.
81+
*/
5482
template <typename To, typename From>
5583
requires std::is_convertible_v<From, To>
5684
To convert_to(From value) {
5785
return static_cast<To>(value);
5886
}
5987

88+
/**
89+
* @brief Specialized conversion for Boost multiprecision numbers.
90+
* @tparam To Target type.
91+
* @tparam From Boost multiprecision number type.
92+
* @param value Value to convert.
93+
* @return Converted value.
94+
* @throws std::system_error if conversion results in data loss.
95+
*/
6096
template <typename To, typename From>
6197
requires boost::multiprecision::is_number<From>::value
6298
To convert_to(const From &value) {
@@ -72,10 +108,10 @@ namespace scale {
72108
} // namespace detail
73109

74110
/**
75-
* encodeInteger encodes any integer type to little-endian representation
76-
* @tparam T integer type
77-
* @param value integer value
78-
* @param stream output stream
111+
* @brief Encodes an integer to little-endian representation.
112+
* @tparam T Integer type.
113+
* @param value Integer value to encode.
114+
* @param stream Output stream where encoded data is written.
79115
*/
80116
template <typename T>
81117
requires std::is_integral_v<std::remove_cvref_t<T>>
@@ -84,13 +120,18 @@ namespace scale {
84120
constexpr size_t size = sizeof(I);
85121
constexpr size_t bits = size * 8;
86122
boost::endian::endian_buffer<boost::endian::order::little, I, bits> buf{};
87-
buf = value; // cannot initialize, only assign
123+
buf = value; // Assign value to endian buffer
88124
for (size_t i = 0; i < size; ++i) {
89-
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
90-
stream << buf.data()[i];
125+
stream << buf.data()[i]; // Write each byte to the stream
91126
}
92127
}
93128

129+
/**
130+
* @brief Encodes large fixed-width integers to little-endian format.
131+
* @tparam S Type of the SCALE encoder stream.
132+
* @param v Integer value to encode.
133+
* @param s Encoder stream.
134+
*/
94135
template <typename S>
95136
requires std::derived_from<std::remove_cvref_t<S>, ScaleEncoderStream>
96137
void encodeInteger(const BigFixedWidthInteger auto &v, S &s) {
@@ -101,20 +142,31 @@ namespace scale {
101142
}
102143
}
103144

145+
/**
146+
* @brief Decodes an integer from little-endian representation.
147+
* @tparam T Integer type.
148+
* @param value Reference to store the decoded integer.
149+
* @param stream Input stream from which data is read.
150+
*/
104151
template <typename T>
105152
requires std::is_integral_v<std::remove_cvref_t<T>>
106153
void decodeInteger(T &value, ScaleDecoderStream &stream) {
107154
using I = std::remove_cvref_t<T>;
108155
constexpr size_t size = sizeof(I);
109156
constexpr size_t bits = size * 8;
110157
boost::endian::endian_buffer<boost::endian::order::little, I, bits> buf{};
111-
buf = value; // cannot initialize, only assign
158+
buf = value; // Assign initial value
112159
for (size_t i = 0; i < size; ++i) {
113-
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
114-
stream >> buf.data()[i];
160+
stream >> buf.data()[i]; // Read each byte from the stream
115161
}
116162
}
117163

164+
/**
165+
* @brief Decodes large fixed-width integers from little-endian format.
166+
* @tparam S Type of the SCALE decoder stream.
167+
* @param v Reference to store the decoded integer.
168+
* @param s Decoder stream.
169+
*/
118170
template <typename S>
119171
requires std::derived_from<std::remove_cvref_t<S>, ScaleDecoderStream>
120172
void decodeInteger(BigFixedWidthInteger auto &v, S &s) {

test/scale_custom_decomposing_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct CustomDecomposableObject {
2525

2626
bool operator==(const CustomDecomposableObject &other) const = default;
2727

28-
SCALE_CUSTOM_DECOMPOSING(CustomDecomposableObject, b, c, d);
28+
SCALE_CUSTOM_DECOMPOSITION(CustomDecomposableObject, b, c, d);
2929
};
3030

3131
TEST(CustomDecomposable, encode) {

0 commit comments

Comments
 (0)