Skip to content

Commit 98d4396

Browse files
committed
xxx_encoder options
1 parent a63366e commit 98d4396

File tree

8 files changed

+217
-70
lines changed

8 files changed

+217
-70
lines changed

include/jsoncons_ext/bson/bson_options.hpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,14 @@ class bson_options_common
1818
{
1919
friend class bson_options;
2020

21-
int max_nesting_depth_;
21+
int max_nesting_depth_{1024};
2222
protected:
23+
bson_options_common() = default;
24+
bson_options_common(const bson_options_common&) = default;
2325
virtual ~bson_options_common() = default;
2426

25-
bson_options_common()
26-
: max_nesting_depth_(1024)
27-
{
28-
}
29-
30-
bson_options_common(const bson_options_common&) = default;
3127
bson_options_common& operator=(const bson_options_common&) = default;
32-
bson_options_common(bson_options_common&&) = default;
33-
bson_options_common& operator=(bson_options_common&&) = default;
28+
3429
public:
3530
int max_nesting_depth() const
3631
{
@@ -42,25 +37,31 @@ class bson_decode_options : public virtual bson_options_common
4237
{
4338
friend class bson_options;
4439
public:
45-
bson_decode_options()
46-
{
47-
}
40+
bson_decode_options() = default;
41+
bson_decode_options(const bson_decode_options& other) = default;
42+
protected:
43+
bson_decode_options& operator=(const bson_decode_options& other) = default;
4844
};
4945

5046
class bson_encode_options : public virtual bson_options_common
5147
{
5248
friend class bson_options;
5349
public:
54-
bson_encode_options()
55-
{
56-
}
50+
bson_encode_options() = default;
51+
bson_encode_options(const bson_encode_options& other) = default;
52+
protected:
53+
bson_encode_options& operator=(const bson_encode_options& other) = default;
5754
};
5855

5956
class bson_options final : public bson_decode_options, public bson_encode_options
6057
{
6158
public:
6259
using bson_options_common::max_nesting_depth;
6360

61+
bson_options() = default;
62+
bson_options(const bson_options& other) = default;
63+
bson_options& operator=(const bson_options& other) = default;
64+
6465
bson_options& max_nesting_depth(int value)
6566
{
6667
this->max_nesting_depth_ = value;

include/jsoncons_ext/cbor/cbor_options.hpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,14 @@ class cbor_options_common
2020
{
2121
friend class cbor_options;
2222

23-
int max_nesting_depth_;
23+
int max_nesting_depth_{1024};
2424
protected:
25+
cbor_options_common() = default;
26+
cbor_options_common(const cbor_options_common&) = default;
2527
virtual ~cbor_options_common() = default;
2628

27-
cbor_options_common()
28-
: max_nesting_depth_(1024)
29-
{
30-
}
31-
32-
cbor_options_common(const cbor_options_common&) = default;
3329
cbor_options_common& operator=(const cbor_options_common&) = default;
34-
cbor_options_common(cbor_options_common&&) = default;
35-
cbor_options_common& operator=(cbor_options_common&&) = default;
30+
3631
public:
3732
int max_nesting_depth() const
3833
{
@@ -44,24 +39,24 @@ class cbor_decode_options : public virtual cbor_options_common
4439
{
4540
friend class cbor_options;
4641
public:
47-
cbor_decode_options()
48-
{
49-
}
42+
cbor_decode_options() = default;
43+
cbor_decode_options(const cbor_decode_options& other) = default;
44+
protected:
45+
cbor_decode_options& operator=(const cbor_decode_options& other) = default;
5046
};
5147

5248
class cbor_encode_options : public virtual cbor_options_common
5349
{
5450
friend class cbor_options;
5551

56-
bool use_stringref_;
57-
bool use_typed_arrays_;
52+
bool use_stringref_{false};
53+
bool use_typed_arrays_{false};
54+
public:
55+
cbor_encode_options() = default;
56+
cbor_encode_options(const cbor_encode_options& other) = default;
57+
protected:
58+
cbor_encode_options& operator=(const cbor_encode_options& other) = default;
5859
public:
59-
cbor_encode_options()
60-
: use_stringref_(false),
61-
use_typed_arrays_(false)
62-
{
63-
}
64-
6560
bool pack_strings() const
6661
{
6762
return use_stringref_;
@@ -80,6 +75,10 @@ class cbor_options final : public cbor_decode_options, public cbor_encode_option
8075
using cbor_encode_options::pack_strings;
8176
using cbor_encode_options::use_typed_arrays;
8277

78+
cbor_options() = default;
79+
cbor_options(const cbor_options& other) = default;
80+
cbor_options& operator=(const cbor_options& other) = default;
81+
8382
cbor_options& max_nesting_depth(int value)
8483
{
8584
this->max_nesting_depth_ = value;

include/jsoncons_ext/msgpack/msgpack_options.hpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,14 @@ class msgpack_options_common
1818
{
1919
friend class msgpack_options;
2020

21-
int max_nesting_depth_;
21+
int max_nesting_depth_{1024};
2222
protected:
23-
virtual ~msgpack_options_common() = default;
23+
msgpack_options_common() = default;
24+
msgpack_options_common(const msgpack_options_common&) = default;
2425

25-
msgpack_options_common()
26-
: max_nesting_depth_(1024)
27-
{
28-
}
26+
virtual ~msgpack_options_common() = default;
2927

30-
msgpack_options_common(const msgpack_options_common&) = default;
3128
msgpack_options_common& operator=(const msgpack_options_common&) = default;
32-
msgpack_options_common(msgpack_options_common&&) = default;
33-
msgpack_options_common& operator=(msgpack_options_common&&) = default;
3429
public:
3530
int max_nesting_depth() const
3631
{
@@ -42,25 +37,31 @@ class msgpack_decode_options : public virtual msgpack_options_common
4237
{
4338
friend class msgpack_options;
4439
public:
45-
msgpack_decode_options()
46-
{
47-
}
40+
msgpack_decode_options() = default;
41+
msgpack_decode_options(const msgpack_decode_options& other) = default;
42+
protected:
43+
msgpack_decode_options& operator=(const msgpack_decode_options& other) = default;
4844
};
4945

5046
class msgpack_encode_options : public virtual msgpack_options_common
5147
{
5248
friend class msgpack_options;
5349
public:
54-
msgpack_encode_options()
55-
{
56-
}
50+
msgpack_encode_options() = default;
51+
msgpack_encode_options(const msgpack_encode_options& other) = default;
52+
protected:
53+
msgpack_encode_options& operator=(const msgpack_encode_options& other) = default;
5754
};
5855

5956
class msgpack_options final : public msgpack_decode_options, public msgpack_encode_options
6057
{
6158
public:
6259
using msgpack_options_common::max_nesting_depth;
6360

61+
msgpack_options() = default;
62+
msgpack_options(const msgpack_options& other) = default;
63+
msgpack_options& operator=(const msgpack_options& other) = default;
64+
6465
msgpack_options& max_nesting_depth(int value)
6566
{
6667
this->max_nesting_depth_ = value;

include/jsoncons_ext/ubjson/ubjson_options.hpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,11 @@ class ubjson_options_common
1818
{
1919
friend class ubjson_options;
2020

21-
int max_nesting_depth_;
21+
int max_nesting_depth_{1024};
2222
protected:
23-
virtual ~ubjson_options_common() = default;
24-
25-
ubjson_options_common()
26-
: max_nesting_depth_(1024)
27-
{
28-
}
29-
23+
ubjson_options_common() = default;
3024
ubjson_options_common(const ubjson_options_common&) = default;
31-
ubjson_options_common& operator=(const ubjson_options_common&) = default;
32-
ubjson_options_common(ubjson_options_common&&) = default;
33-
ubjson_options_common& operator=(ubjson_options_common&&) = default;
25+
virtual ~ubjson_options_common() = default;
3426
public:
3527
int max_nesting_depth() const
3628
{
@@ -43,12 +35,11 @@ class ubjson_decode_options : public virtual ubjson_options_common
4335
friend class ubjson_options;
4436
std::size_t max_items_{1 << 24};
4537
public:
46-
ubjson_decode_options()
47-
{
48-
}
49-
50-
~ubjson_decode_options() = default;
51-
38+
ubjson_decode_options() = default;
39+
ubjson_decode_options(const ubjson_decode_options& other) = default;
40+
protected:
41+
ubjson_decode_options& operator=(const ubjson_decode_options& other) = default;
42+
public:
5243
std::size_t max_items() const
5344
{
5445
return max_items_;
@@ -59,15 +50,22 @@ class ubjson_encode_options : public virtual ubjson_options_common
5950
{
6051
friend class ubjson_options;
6152
public:
62-
ubjson_encode_options()
63-
{
64-
}
53+
ubjson_encode_options() = default;
54+
ubjson_encode_options(const ubjson_encode_options& other) = default;
55+
protected:
56+
ubjson_encode_options& operator=(const ubjson_encode_options& other) = default;
6557
};
6658

6759
class ubjson_options final : public ubjson_decode_options, public ubjson_encode_options
6860
{
6961
public:
7062
using ubjson_options_common::max_nesting_depth;
63+
using ubjson_decode_options::max_items;
64+
65+
ubjson_options() = default;
66+
ubjson_options(const ubjson_options& other) = default;
67+
68+
ubjson_options& operator=(const ubjson_options& other) = default;
7169

7270
ubjson_options& max_nesting_depth(int value)
7371
{

test/bson/src/bson_encoder_tests.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,37 @@ namespace {
3838
}
3939
}
4040

41+
TEST_CASE("bson_options tests")
42+
{
43+
auto options = bson::bson_options{}
44+
.max_nesting_depth(2000);
45+
46+
SECTION("copy constructor")
47+
{
48+
bson::bson_options other(options);
49+
CHECK(options.max_nesting_depth() == other.max_nesting_depth());
50+
}
51+
52+
SECTION("assignment")
53+
{
54+
bson::bson_options other;
55+
other = options;
56+
CHECK(options.max_nesting_depth() == other.max_nesting_depth());
57+
}
58+
59+
SECTION("bson_decode_options copy constructor")
60+
{
61+
bson::bson_decode_options other(options);
62+
CHECK(options.max_nesting_depth() == other.max_nesting_depth());
63+
}
64+
65+
SECTION("bson_encode_options copy constructor")
66+
{
67+
bson::bson_encode_options other(options);
68+
CHECK(options.max_nesting_depth() == other.max_nesting_depth());
69+
}
70+
}
71+
4172
TEST_CASE("serialize to bson")
4273
{
4374
SECTION("array")

test/cbor/src/cbor_encoder_tests.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,45 @@
2222

2323
using namespace jsoncons;
2424

25+
TEST_CASE("cbor_options tests")
26+
{
27+
auto options = cbor::cbor_options{}
28+
.max_nesting_depth(2000)
29+
.pack_strings(true)
30+
.use_typed_arrays(true);
31+
32+
SECTION("copy constructor")
33+
{
34+
cbor::cbor_options other(options);
35+
CHECK(options.max_nesting_depth() == other.max_nesting_depth());
36+
CHECK(options.pack_strings() == other.pack_strings());
37+
CHECK(options.use_typed_arrays() == other.use_typed_arrays());
38+
}
39+
40+
SECTION("assignment")
41+
{
42+
cbor::cbor_options other;
43+
other = options;
44+
CHECK(options.max_nesting_depth() == other.max_nesting_depth());
45+
CHECK(options.pack_strings() == other.pack_strings());
46+
CHECK(options.use_typed_arrays() == other.use_typed_arrays());
47+
}
48+
49+
SECTION("cbor_decode_options copy constructor")
50+
{
51+
cbor::cbor_decode_options other(options);
52+
CHECK(options.max_nesting_depth() == other.max_nesting_depth());
53+
}
54+
55+
SECTION("cbor_encode_options copy constructor")
56+
{
57+
cbor::cbor_encode_options other(options);
58+
CHECK(options.max_nesting_depth() == other.max_nesting_depth());
59+
CHECK(options.pack_strings() == other.pack_strings());
60+
CHECK(options.use_typed_arrays() == other.use_typed_arrays());
61+
}
62+
}
63+
2564
TEST_CASE("cbor encode multi dim array test")
2665
{
2766
std::vector<uint8_t> v;

test/msgpack/src/msgpack_encoder_tests.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,37 @@
1818

1919
using namespace jsoncons;
2020

21+
TEST_CASE("msgpack_options tests")
22+
{
23+
auto options = msgpack::msgpack_options{}
24+
.max_nesting_depth(2000);
25+
26+
SECTION("copy constructor")
27+
{
28+
msgpack::msgpack_options other(options);
29+
CHECK(options.max_nesting_depth() == other.max_nesting_depth());
30+
}
31+
32+
SECTION("assignment")
33+
{
34+
msgpack::msgpack_options other;
35+
other = options;
36+
CHECK(options.max_nesting_depth() == other.max_nesting_depth());
37+
}
38+
39+
SECTION("msgpack_decode_options copy constructor")
40+
{
41+
msgpack::msgpack_decode_options other(options);
42+
CHECK(options.max_nesting_depth() == other.max_nesting_depth());
43+
}
44+
45+
SECTION("msgpack_encode_options copy constructor")
46+
{
47+
msgpack::msgpack_encode_options other(options);
48+
CHECK(options.max_nesting_depth() == other.max_nesting_depth());
49+
}
50+
}
51+
2152
TEST_CASE("serialize array to msgpack")
2253
{
2354
std::vector<uint8_t> v;

0 commit comments

Comments
 (0)