Skip to content

Commit

Permalink
feat: Add utility for forward declaring codecs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BurningEnlightenment committed Mar 7, 2023
1 parent 2035d9a commit 260292d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dplx_target_sources(deeppack
dp/fwd
dp/indefinite_range
dp/layout_descriptor
dp/macros
dp/object_def
dp/tuple_def

Expand Down
31 changes: 31 additions & 0 deletions src/dplx/dp/macros.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

// Copyright Henrik Steffen Gaßmann 2021.
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE or copy at
// https://www.boost.org/LICENSE_1_0.txt)

#pragma once

#include <dplx/dp/fwd.hpp>

// NOLINTBEGIN(modernize-macro-to-enum)
// NOLINTBEGIN(cppcoreguidelines-macro-usage)

#define DPLX_DP_DECLARE_CODEC_SIMPLE(_fq_type) \
template <> \
class dplx::dp::codec<_fq_type> \
{ \
using value_type = _fq_type; \
\
public: \
static auto size_of(emit_context &ctx, _fq_type const &value) noexcept \
-> std::uint64_t; \
static auto encode(emit_context &ctx, _fq_type const &value) noexcept \
-> result<void>; \
static auto decode(parse_context &ctx, _fq_type &outValue) noexcept \
-> result<void>; \
}

// NOLINTEND(cppcoreguidelines-macro-usage)
// NOLINTEND(modernize-macro-to-enum)
24 changes: 24 additions & 0 deletions src/dplx/dp/macros.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

// Copyright Henrik Steffen Gaßmann 2021.
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE or copy at
// https://www.boost.org/LICENSE_1_0.txt)

#include <dplx/dp/macros.hpp>

namespace dp_tests
{

namespace
{

class custom_type
{
};

} // namespace

} // namespace dp_tests

DPLX_DP_DECLARE_CODEC_SIMPLE(dp_tests::custom_type);

0 comments on commit 260292d

Please sign in to comment.