Skip to content

Commit 260292d

Browse files
feat: Add utility for forward declaring codecs
Add a simple macro which forward declares the full specialization for a `codec<T>`. These forward declarations add a lot of noise while being very similar and C++ doesn't really offer the tools to avoid these.
1 parent 2035d9a commit 260292d

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

sources.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dplx_target_sources(deeppack
2929
dp/fwd
3030
dp/indefinite_range
3131
dp/layout_descriptor
32+
dp/macros
3233
dp/object_def
3334
dp/tuple_def
3435

src/dplx/dp/macros.hpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
// Copyright Henrik Steffen Gaßmann 2021.
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/dp/fwd.hpp>
11+
12+
// NOLINTBEGIN(modernize-macro-to-enum)
13+
// NOLINTBEGIN(cppcoreguidelines-macro-usage)
14+
15+
#define DPLX_DP_DECLARE_CODEC_SIMPLE(_fq_type) \
16+
template <> \
17+
class dplx::dp::codec<_fq_type> \
18+
{ \
19+
using value_type = _fq_type; \
20+
\
21+
public: \
22+
static auto size_of(emit_context &ctx, _fq_type const &value) noexcept \
23+
-> std::uint64_t; \
24+
static auto encode(emit_context &ctx, _fq_type const &value) noexcept \
25+
-> result<void>; \
26+
static auto decode(parse_context &ctx, _fq_type &outValue) noexcept \
27+
-> result<void>; \
28+
}
29+
30+
// NOLINTEND(cppcoreguidelines-macro-usage)
31+
// NOLINTEND(modernize-macro-to-enum)

src/dplx/dp/macros.test.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
// Copyright Henrik Steffen Gaßmann 2021.
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/macros.hpp>
9+
10+
namespace dp_tests
11+
{
12+
13+
namespace
14+
{
15+
16+
class custom_type
17+
{
18+
};
19+
20+
} // namespace
21+
22+
} // namespace dp_tests
23+
24+
DPLX_DP_DECLARE_CODEC_SIMPLE(dp_tests::custom_type);

0 commit comments

Comments
 (0)