Skip to content

Commit 9d5015b

Browse files
Merge pull request #15 from deeplex/dev/status-code-string-ref
feat: Add codecs for `system_error2` `string_ref` and `cncr::uuid`
2 parents ca7b0a0 + 7fdd5cf commit 9d5015b

10 files changed

+270
-3
lines changed

.clang-tidy

+2
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ CheckOptions:
3030
value: true
3131
- key: cppcoreguidelines-macro-usage.AllowedRegexp
3232
value: "DPLX_.+_WORKAROUND(_TESTED_AT)?"
33+
- key: readability-function-cognitive-complexity.IgnoreMacros
34+
value: true

.github/workflows/cpp-ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
uses: lukka/run-vcpkg@v10
4141
with:
4242
vcpkgDirectory: ${{ github.workspace }}/build/vcpkg
43-
vcpkgGitCommitId: 1271068e139c1fc30bae405c0bca0e379e155bd2
43+
vcpkgGitCommitId: 501db0f17ef6df184fcdbfbe0f87cde2313b6ab1
4444
prependedCacheKey: compiler=${{ matrix.compiler }}
4545
#appendedCacheKey: r00
4646

@@ -100,7 +100,7 @@ jobs:
100100
uses: lukka/run-vcpkg@v10
101101
with:
102102
vcpkgDirectory: ${{ github.workspace }}/../vcpkg
103-
vcpkgGitCommitId: 1271068e139c1fc30bae405c0bca0e379e155bd2
103+
vcpkgGitCommitId: 501db0f17ef6df184fcdbfbe0f87cde2313b6ab1
104104
prependedCacheKey: compiler=clang-15
105105
#appendedCacheKey: r00
106106

sources.cmake

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ dplx_target_sources(deeppack
1212
dp/codecs/std-chrono
1313
dp/codecs/std-filesystem
1414
dp/codecs/std-string
15+
dp/codecs/system_error2
16+
dp/codecs/uuid
1517

1618
dp/items/skip_item
1719
)

src/dplx/dp/codecs/system_error2.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
// Copyright 2023 Henrik Steffen Gaßmann
3+
//
4+
// Distributed under the Boost Software License, Version 1.0.
5+
// (See accompanying file LICENSE or copy at
6+
// https://www.boost.org/LICENSE_1_0.txt)
7+
8+
#include "dplx/dp/codecs/system_error2.hpp"
9+
10+
#include <dplx/dp/api.hpp>
11+
#include <dplx/dp/items/emit_core.hpp>
12+
#include <dplx/dp/items/item_size_of_core.hpp>
13+
14+
namespace dplx::dp
15+
{
16+
17+
auto codec<SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref>::size_of(
18+
emit_context &ctx, value_type const &value) noexcept -> std::uint64_t
19+
{
20+
return dp::item_size_of_u8string(ctx, value.size());
21+
}
22+
auto codec<SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref>::encode(
23+
emit_context &ctx, value_type const &value) noexcept -> result<void>
24+
{
25+
return dp::emit_u8string(ctx, value.data(), value.size());
26+
}
27+
28+
} // namespace dplx::dp

src/dplx/dp/codecs/system_error2.hpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
// Copyright 2023 Henrik Steffen Gaßmann
3+
//
4+
// Distributed under the Boost Software License, Version 1.0.
5+
// (See accompanying file LICENSE or copy at
6+
// https://www.boost.org/LICENSE_1_0.txt)
7+
8+
#pragma once
9+
10+
#include <concepts>
11+
12+
#include <status-code/status_code_domain.hpp>
13+
14+
#include <dplx/dp/fwd.hpp>
15+
16+
namespace dplx::dp
17+
{
18+
19+
template <>
20+
class codec<SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref>
21+
{
22+
using value_type = SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref;
23+
24+
public:
25+
static auto size_of(emit_context &ctx, value_type const &value) noexcept
26+
-> std::uint64_t;
27+
static auto encode(emit_context &ctx, value_type const &value) noexcept
28+
-> result<void>;
29+
};
30+
template <>
31+
class codec<SYSTEM_ERROR2_NAMESPACE::status_code_domain::
32+
atomic_refcounted_string_ref>
33+
: public codec<SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref>
34+
{
35+
};
36+
template <std::derived_from<
37+
SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref> StringRef>
38+
class codec<StringRef>
39+
: public codec<SYSTEM_ERROR2_NAMESPACE::status_code_domain::string_ref>
40+
{
41+
};
42+
43+
} // namespace dplx::dp
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
// Copyright 2023 Henrik Steffen Gaßmann
3+
//
4+
// Distributed under the Boost Software License, Version 1.0.
5+
// (See accompanying file LICENSE or copy at
6+
// https://www.boost.org/LICENSE_1_0.txt)
7+
8+
#include "dplx/dp/codecs/system_error2.hpp"
9+
10+
#include <catch2/catch_test_macros.hpp>
11+
12+
#include <dplx/dp/api.hpp>
13+
14+
#include "blob_matcher.hpp"
15+
#include "item_sample_ct.hpp"
16+
#include "test_input_stream.hpp"
17+
#include "test_output_stream.hpp"
18+
#include "test_utils.hpp"
19+
20+
namespace dp_tests
21+
{
22+
23+
using string_ref = dp::system_error::status_code_domain::string_ref;
24+
25+
TEST_CASE("system_error2::status_code_domain::string_ref should be encodable")
26+
{
27+
item_sample_ct<std::string_view> const sample{
28+
"some", 5, {0x64, u8's', u8'o', u8'm', u8'e'}
29+
};
30+
string_ref value(sample.value.data(), sample.value.size());
31+
32+
SECTION("to a stream")
33+
{
34+
simple_test_output_stream outputStream(sample.encoded_length);
35+
36+
REQUIRE(dp::encode(outputStream, value));
37+
38+
CHECK_BLOB_EQ(outputStream.written(), sample.encoded_bytes());
39+
}
40+
SECTION("with a size_of operator")
41+
{
42+
CHECK(dp::encoded_size_of(value) == sample.encoded_length);
43+
}
44+
}
45+
46+
} // namespace dp_tests

src/dplx/dp/codecs/uuid.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
// Copyright 2023 Henrik Steffen Gaßmann
3+
//
4+
// Distributed under the Boost Software License, Version 1.0.
5+
// (See accompanying file LICENSE or copy at
6+
// https://www.boost.org/LICENSE_1_0.txt)
7+
8+
#include "dplx/dp/codecs/uuid.hpp"
9+
10+
#include <dplx/dp/items.hpp>
11+
12+
namespace dplx::dp
13+
{
14+
15+
auto dplx::dp::codec<cncr::uuid>::decode(parse_context &ctx,
16+
cncr::uuid &value) noexcept
17+
-> result<void>
18+
{
19+
constexpr auto stateSize = cncr::uuid::state_size;
20+
DPLX_TRY(dp::expect_item_head(ctx, type_code::binary, stateSize));
21+
22+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
23+
cncr::blob<std::byte, stateSize, stateSize> raw;
24+
DPLX_TRY(ctx.in.bulk_read(static_cast<std::byte *>(raw.values), stateSize));
25+
26+
value = cncr::uuid(raw.values);
27+
return oc::success();
28+
}
29+
30+
auto codec<cncr::uuid>::encode(emit_context &ctx,
31+
cncr::uuid const value) noexcept -> result<void>
32+
{
33+
auto const canonical = value.canonical();
34+
return dp::emit_binary(ctx,
35+
static_cast<std::byte const *>(canonical.values),
36+
sizeof(canonical.values));
37+
}
38+
39+
auto codec<cncr::uuid>::size_of(emit_context &ctx, cncr::uuid) noexcept
40+
-> std::uint64_t
41+
{
42+
return dp::item_size_of_binary(ctx, cncr::uuid::state_size);
43+
}
44+
45+
} // namespace dplx::dp

src/dplx/dp/codecs/uuid.hpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
// Copyright 2023 Henrik Steffen Gaßmann
3+
//
4+
// Distributed under the Boost Software License, Version 1.0.
5+
// (See accompanying file LICENSE or copy at
6+
// https://www.boost.org/LICENSE_1_0.txt)
7+
8+
#pragma once
9+
10+
#include <dplx/cncr/uuid.hpp>
11+
12+
#include <dplx/dp/fwd.hpp>
13+
14+
namespace dplx::dp
15+
{
16+
17+
template <>
18+
class codec<cncr::uuid>
19+
{
20+
public:
21+
static auto decode(parse_context &ctx, cncr::uuid &value) noexcept
22+
-> result<void>;
23+
static auto encode(emit_context &ctx, cncr::uuid value) noexcept
24+
-> result<void>;
25+
static auto size_of(emit_context &ctx, cncr::uuid value) noexcept
26+
-> std::uint64_t;
27+
};
28+
29+
} // namespace dplx::dp

src/dplx/dp/codecs/uuid.test.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
// Copyright 2023 Henrik Steffen Gaßmann
3+
//
4+
// Distributed under the Boost Software License, Version 1.0.
5+
// (See accompanying file LICENSE or copy at
6+
// https://www.boost.org/LICENSE_1_0.txt)
7+
8+
#include "dplx/dp/codecs/uuid.hpp"
9+
10+
#include <catch2/catch_test_macros.hpp>
11+
#include <catch2/generators/catch_generators.hpp>
12+
13+
#include <dplx/dp/api.hpp>
14+
15+
#include "blob_matcher.hpp"
16+
#include "item_sample_ct.hpp"
17+
#include "range_generator.hpp"
18+
#include "test_input_stream.hpp"
19+
#include "test_output_stream.hpp"
20+
#include "test_utils.hpp"
21+
22+
namespace dp_tests
23+
{
24+
25+
namespace
26+
{
27+
28+
using namespace cncr::uuid_literals;
29+
30+
constexpr item_sample_ct<cncr::uuid, 20U> uuid_samples[] = {
31+
{"00000000-0000-0000-0000-000000000000"_uuid,
32+
17, {0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
34+
{"00000000-0000-4000-bfff-ffffffffffff"_uuid,
35+
17, {0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0xbf, 0xff,
36+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
37+
{"ffffffff-ffff-ffff-ffff-ffffffffffff"_uuid,
38+
17, {0x50, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
39+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
40+
};
41+
42+
} // namespace
43+
44+
TEST_CASE("cncr::uuid has a codec")
45+
{
46+
auto const sample = GENERATE(borrowed_range(uuid_samples));
47+
INFO(sample);
48+
49+
SECTION("with encode")
50+
{
51+
simple_test_output_stream outputStream(sample.encoded_length);
52+
53+
REQUIRE(dp::encode(outputStream, sample.value));
54+
55+
CHECK_BLOB_EQ(outputStream.written(), sample.encoded_bytes());
56+
}
57+
SECTION("with size_of")
58+
{
59+
CHECK(dp::encoded_size_of(sample.value) == sample.encoded_length);
60+
}
61+
SECTION("with decode")
62+
{
63+
simple_test_input_stream inputStream(sample.encoded_bytes());
64+
65+
cncr::uuid value{};
66+
REQUIRE(dp::decode(inputStream, value));
67+
68+
CHECK(value == sample.value);
69+
}
70+
}
71+
72+
} // namespace dp_tests

vcpkg-configuration.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg-configuration.schema.json",
33
"default-registry": {
44
"kind": "builtin",
5-
"baseline": "1271068e139c1fc30bae405c0bca0e379e155bd2"
5+
"baseline": "501db0f17ef6df184fcdbfbe0f87cde2313b6ab1"
66
},
77
"registries": [
88
{

0 commit comments

Comments
 (0)