Skip to content

Commit 9195604

Browse files
committed
Replace boost optional to std optional
1 parent d464fa1 commit 9195604

7 files changed

Lines changed: 38 additions & 38 deletions

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ It allows encoding and decoding following data types:
99
* bool values
1010
* pairs of types represented by ```std::pair<T1, T2>```
1111
* compact integers represented by CompactInteger type
12-
* optional values represented by ```boost::optional<T>```
13-
* as special case of optional values ```boost::optional<bool>``` is encoded using one byte following specification.
12+
* optional values represented by ```std::optional<T>```
13+
* as special case of optional values ```std::optional<bool>``` is encoded using one byte following specification.
1414
* collections of items represented by ```std::vector<T>```
1515
* variants represented by ```boost::variant<T...>```
1616

@@ -26,8 +26,8 @@ auto * raw_str = "zxczxczx";
2626
bool b = true;
2727
CompactInteger ci = 123456789;
2828
boost::variant<uint8_t, uint32_t, CompactInteger> vint = CompactInteger(12345);
29-
boost::optional<std::string> opt_str = "asdfghjkl";
30-
boost::optional<bool> opt_bool = false;
29+
std::optional<std::string> opt_str = "asdfghjkl";
30+
std::optional<bool> opt_bool = false;
3131
std::pair<uint8_t, uint32_t> pair{1u, 2u};
3232
std::vector<uint32_t> coll_ui32 = {1u, 2u, 3u, 4u};
3333
std::vector<std::string> coll_str = {"asd", "fgh", "jkl"};
@@ -58,8 +58,8 @@ std::string str;
5858
bool b = true;
5959
CompactInteger ci;
6060
boost::variant<uint8_t, uint32_t, CompactInteger> vint;
61-
boost::optional<std::string> opt_str;
62-
boost::optional<bool> opt_bool;
61+
std::optional<std::string> opt_str;
62+
std::optional<bool> opt_bool;
6363
std::pair<uint8_t, uint32_t> pair{};
6464
std::vector<uint32_t> coll_ui32;
6565
std::vector<std::string> coll_str;

include/scale/scale_decoder_stream.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#define SCALE_CORE_SCALE_SCALE_DECODER_STREAM_HPP
88

99
#include <array>
10+
#include <optional>
1011

1112
#include <boost/multiprecision/cpp_int.hpp>
12-
#include <boost/optional.hpp>
1313
#include <boost/variant.hpp>
1414
#include <gsl/span>
1515

@@ -132,7 +132,7 @@ class ScaleDecoderStream {
132132
* @param v optional value reference
133133
* @return reference to stream
134134
*/
135-
template <class T> ScaleDecoderStream &operator>>(boost::optional<T> &v) {
135+
template <class T> ScaleDecoderStream &operator>>(std::optional<T> &v) {
136136
using mutableT = std::remove_const_t<T>;
137137

138138
static_assert(std::is_default_constructible_v<mutableT>);
@@ -275,9 +275,9 @@ class ScaleDecoderStream {
275275
bool decodeBool();
276276
/**
277277
* @brief special case of optional values as described in specification
278-
* @return boost::optional<bool> value
278+
* @return std::optional<bool> value
279279
*/
280-
boost::optional<bool> decodeOptionalBool();
280+
std::optional<bool> decodeOptionalBool();
281281

282282
template <size_t I, class... Ts>
283283
void decodeElementOfTuple(std::tuple<Ts...> &v) {

include/scale/scale_encoder_stream.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#define SCALE_CORE_SCALE_SCALE_ENCODER_STREAM_HPP
88

99
#include <deque>
10+
#include <optional>
1011

11-
#include <boost/optional.hpp>
1212
#include <boost/variant.hpp>
1313
#include <gsl/span>
1414

@@ -137,7 +137,7 @@ class ScaleEncoderStream {
137137
* @return reference to stream
138138
*/
139139
template <class T>
140-
ScaleEncoderStream &operator<<(const boost::optional<T> &v) {
140+
ScaleEncoderStream &operator<<(const std::optional<T> &v) {
141141
// optional bool is a special case of optional values
142142
// it should be encoded using one byte instead of two
143143
// as described in specification
@@ -273,7 +273,7 @@ class ScaleEncoderStream {
273273
ScaleEncoderStream &putByte(uint8_t v);
274274

275275
private:
276-
ScaleEncoderStream &encodeOptionalBool(const boost::optional<bool> &v);
276+
ScaleEncoderStream &encodeOptionalBool(const std::optional<bool> &v);
277277

278278
const bool drop_data_;
279279
std::deque<uint8_t> stream_;

src/scale_decoder_stream.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ CompactInteger decodeCompactInteger(ScaleDecoderStream &stream) {
8080
ScaleDecoderStream::ScaleDecoderStream(gsl::span<const uint8_t> span)
8181
: span_{span}, current_iterator_{span_.begin()}, current_index_{0} {}
8282

83-
boost::optional<bool> ScaleDecoderStream::decodeOptionalBool() {
83+
std::optional<bool> ScaleDecoderStream::decodeOptionalBool() {
8484
auto byte = nextByte();
8585
switch (static_cast<OptionalBool>(byte)) {
8686
case OptionalBool::NONE:
87-
return boost::none;
87+
return std::nullopt;
8888
case OptionalBool::OPT_FALSE:
8989
return false;
9090
case OptionalBool::OPT_TRUE:

src/scale_encoder_stream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ ScaleEncoderStream &ScaleEncoderStream::operator<<(const CompactInteger &v) {
138138
}
139139

140140
ScaleEncoderStream &
141-
ScaleEncoderStream::encodeOptionalBool(const boost::optional<bool> &v) {
141+
ScaleEncoderStream::encodeOptionalBool(const std::optional<bool> &v) {
142142
auto result = OptionalBool::OPT_TRUE;
143143

144144
if (!v.has_value()) {

test/scale_encode_counter_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#include <gtest/gtest.h>
77

88
#include <string>
9+
#include <optional>
910

10-
#include <boost/optional.hpp>
1111
#include "scale/scale_encoder_stream.hpp"
1212

1313
using scale::ScaleEncoderStream;
@@ -60,7 +60,7 @@ TEST_F(ScaleCounter, String) {
6060
* @then the resulting stream size equals to expected
6161
*/
6262
TEST_F(ScaleCounter, EmptyOptional) {
63-
boost::optional<uint32_t> var = boost::none;
63+
std::optional<uint32_t> var = std::nullopt;
6464
s << var;
6565
SIZE(1);
6666
}
@@ -71,7 +71,7 @@ TEST_F(ScaleCounter, EmptyOptional) {
7171
* @then the resulting stream size equals to expected
7272
*/
7373
TEST_F(ScaleCounter, NonEmptyOptional) {
74-
boost::optional<uint32_t> var = 10;
74+
std::optional<uint32_t> var = 10;
7575
s << var;
7676
SIZE(5);
7777
}

test/scale_optional_test.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,42 @@ TEST(Scale, encodeOptional) {
2626
// most simple case
2727
{
2828
ScaleEncoderStream s;
29-
ASSERT_NO_THROW((s << boost::optional<uint8_t>{boost::none}));
29+
ASSERT_NO_THROW((s << std::optional<uint8_t>{std::nullopt}));
3030
ASSERT_EQ(s.data(), (ByteArray{0}));
3131
}
3232

3333
// encode existing uint8_t
3434
{
3535
ScaleEncoderStream s;
36-
ASSERT_NO_THROW((s << boost::optional<uint8_t>{1}));
36+
ASSERT_NO_THROW((s << std::optional<uint8_t>{1}));
3737
ASSERT_EQ(s.data(), (ByteArray{1, 1}));
3838
}
3939

4040
// encode negative int8_t
4141
{
4242
ScaleEncoderStream s;
43-
ASSERT_NO_THROW((s << boost::optional<int8_t>{-1}));
43+
ASSERT_NO_THROW((s << std::optional<int8_t>{-1}));
4444
ASSERT_EQ(s.data(), (ByteArray{1, 255}));
4545
}
4646

4747
// encode non-existing uint16_t
4848
{
4949
ScaleEncoderStream s;
50-
ASSERT_NO_THROW((s << boost::optional<uint16_t>{boost::none}));
50+
ASSERT_NO_THROW((s << std::optional<uint16_t>{std::nullopt}));
5151
ASSERT_EQ(s.data(), (ByteArray{0}));
5252
}
5353

5454
// encode existing uint16_t
5555
{
5656
ScaleEncoderStream s;
57-
ASSERT_NO_THROW((s << boost::optional<uint16_t>{511}));
57+
ASSERT_NO_THROW((s << std::optional<uint16_t>{511}));
5858
ASSERT_EQ(s.data(), (ByteArray{1, 255, 1}));
5959
}
6060

6161
// encode existing uint32_t
6262
{
6363
ScaleEncoderStream s;
64-
ASSERT_NO_THROW((s << boost::optional<uint32_t>{67305985}));
64+
ASSERT_NO_THROW((s << std::optional<uint32_t>{67305985}));
6565
ASSERT_EQ(s.data(), (ByteArray{1, 1, 2, 3, 4}));
6666
}
6767
}
@@ -86,22 +86,22 @@ TEST(ScaleTest, DecodeOptionalSuccess) {
8686

8787
// decode nullopt uint8_t
8888
{
89-
boost::optional<uint8_t> opt;
89+
std::optional<uint8_t> opt;
9090
ASSERT_NO_THROW((stream >> opt));
9191
ASSERT_FALSE(opt.has_value());
9292
}
9393

9494
// decode optional uint8_t
9595
{
96-
boost::optional<uint8_t> opt;
96+
std::optional<uint8_t> opt;
9797
ASSERT_NO_THROW((stream >> opt));
9898
ASSERT_TRUE(opt.has_value());
9999
ASSERT_EQ(*opt, 1);
100100
}
101101

102102
// decode optional negative int8_t
103103
{
104-
boost::optional<int8_t> opt;
104+
std::optional<int8_t> opt;
105105
ASSERT_NO_THROW((stream >> opt));
106106
ASSERT_TRUE(opt.has_value());
107107
ASSERT_EQ(*opt, -1);
@@ -110,22 +110,22 @@ TEST(ScaleTest, DecodeOptionalSuccess) {
110110
// decode nullopt uint16_t
111111
// it requires 1 zero byte just like any other nullopt
112112
{
113-
boost::optional<uint16_t> opt;
113+
std::optional<uint16_t> opt;
114114
ASSERT_NO_THROW((stream >> opt));
115115
ASSERT_FALSE(opt.has_value());
116116
}
117117

118118
// decode optional uint16_t
119119
{
120-
boost::optional<uint16_t> opt;
120+
std::optional<uint16_t> opt;
121121
ASSERT_NO_THROW((stream >> opt));
122122
ASSERT_TRUE(opt.has_value());
123123
ASSERT_EQ(*opt, 511);
124124
}
125125

126126
// decode optional uint32_t
127127
{
128-
boost::optional<uint32_t> opt;
128+
std::optional<uint32_t> opt;
129129
ASSERT_NO_THROW((stream >> opt));
130130
ASSERT_TRUE(opt.has_value());
131131
ASSERT_EQ(*opt, 67305985);
@@ -142,7 +142,7 @@ TEST(ScaleTest, DecodeOptionalSuccess) {
142142
* @then expected result obtained
143143
*/
144144
TEST(ScaleTest, EncodeOptionalBoolSuccess) {
145-
std::vector<boost::optional<bool>> values = {true, false, boost::none};
145+
std::vector<std::optional<bool>> values = {true, false, std::nullopt};
146146
ScaleEncoderStream s;
147147
for (auto &&v : values) {
148148
ASSERT_NO_THROW((s << v));
@@ -154,10 +154,10 @@ TEST(ScaleTest, EncodeOptionalBoolSuccess) {
154154
* @brief helper struct for testing decode optional bool
155155
*/
156156
struct FourOptBools {
157-
boost::optional<bool> b1;
158-
boost::optional<bool> b2;
159-
boost::optional<bool> b3;
160-
boost::optional<bool> b4;
157+
std::optional<bool> b1;
158+
std::optional<bool> b2;
159+
std::optional<bool> b3;
160+
std::optional<bool> b4;
161161
};
162162

163163
template <class Stream, typename = std::enable_if_t<Stream::is_decoder_stream>>
@@ -185,10 +185,10 @@ TEST(Scale, DecodeOptionalBoolFail) {
185185
*/
186186
TEST(Scale, DecodeOptionalBoolSuccess) {
187187
auto bytes = ByteArray{0, 1, 2, 1};
188-
using optbool = boost::optional<bool>;
188+
using optbool = std::optional<bool>;
189189

190190
EXPECT_OUTCOME_TRUE(res, decode<FourOptBools>(bytes))
191-
ASSERT_EQ(res.b1, boost::none);
191+
ASSERT_EQ(res.b1, std::nullopt);
192192
ASSERT_EQ(res.b2, optbool(true));
193193
ASSERT_EQ(res.b3, optbool(false));
194194
ASSERT_EQ(res.b4 , optbool(true));

0 commit comments

Comments
 (0)