Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BINLOG_ADAPT_CONCEPT #152

Merged
merged 3 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion .github/workflows/on_pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,33 @@ jobs:
cd ThreadSanitized
ctest -VV

build_linux_clang_cpp20:
name: Build on Linux with Clang, C++20
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Dependencies
run: |
# remove broken packages installed by default
sudo apt remove -y clang-6.0 libclang-common-6.0-dev libclang1-6.0 libllvm6.0
sudo apt install -y clang lld
- name: Configure
run: |
set -x
mkdir Clang
cd Clang
export CC=$(which clang)
export CXX=$(which clang++)
cmake -DCMAKE_CXX_STANDARD=20 ..
- name: Build
run: |
cd Clang
make -j2 VERBOSE=1
- name: Test
run: |
cd Clang
ctest -VV

build_linux_clang_tidy:
name: Build on Linux with Clang+Tidy
runs-on: ubuntu-latest
Expand All @@ -108,7 +135,7 @@ jobs:
run: |
# remove broken packages installed by default
sudo apt remove -y clang-6.0 libclang-common-6.0-dev libclang1-6.0 libllvm6.0
sudo apt install -y clang clang-tidy
sudo apt install -y clang clang-tidy lld
- name: Configure
run: |
set -x
Expand Down
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ if(POLICY CMP0069)
check_ipo_supported(RESULT BINLOG_HAS_IPO)
if(BINLOG_HAS_IPO)
message(STATUS "Use interprocedural optimization")

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# avoid error when mixing clang 10 lto and ld:
# /usr/bin/ld: libbinlog.a: error adding symbols: file format not recognized
message(STATUS "Use lld")
add_link_options("-fuse-ld=lld")
endif()
endif()
endif()

Expand Down Expand Up @@ -336,6 +343,10 @@ add_example(TscClock)
add_inttest(LoggingVariants)
endif()

if(${CMAKE_CXX_STANDARD} GREATER_EQUAL 20)
add_inttest(LoggingAdaptedConcepts)
endif()

if(Boost_FOUND)
target_compile_definitions(IntegrationTest PRIVATE BINLOG_HAS_BOOST)

Expand Down
4 changes: 4 additions & 0 deletions doc/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ User defined templates can be adapted for logging in a similar way:

[catchfile test/integration/LoggingAdaptedStructs.cpp adapt_template]

As of C++20, concepts can be also adapted for logging:

[catchfile test/integration/LoggingAdaptedConcepts.cpp concept]

For more information see the Mserialize documentation on [Adapting custom types][mserialize-act] and
[Adapting user defined recursive types for visitation][mserialize-rec].

Expand Down
37 changes: 37 additions & 0 deletions include/binlog/adapt_struct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,43 @@
MSERIALIZE_EXPAND(MSERIALIZE_MAKE_TEMPLATE_SERIALIZABLE(__VA_ARGS__)) \
/**/

#if __cplusplus >= 202002L // Otherwise __VA_OPT__ breaks
/**
* BINLOG_ADAPT_CONCEPT(Concept, members...)
*
* Make types that model `Concept` loggable, in terms of the specified members.
*
* Example:
*
* template<typename T>
* concept Stringable = requires(T a)
* {
* { a.str() } -> std::convertible_to<std::string>;
* };
*
* BINLOG_ADAPT_CONCEPT(Stringable, str)
*
* The macro has to be called in global scope (outside of any namespace).
* `Concept` is a Named Requirement - see C++20 concepts.
*
* members... is a list of data members or getters `Concept` requires.
* members... can be empty.
* See BINLOG_ADAPT_STRUCT for limitations.
*
* Calling this macro requires C++20 or greater.
*
* If type Foo models concept Bar, and Bar is adapted using this macro,
* when a Foo object is logged (that is otherwise not adapted),
* on the output, the type name will be "Bar", not "Foo".
*
* @see BINLOG_ADAPT_STRUCT
*/
#define BINLOG_ADAPT_CONCEPT(Concept, ...) \
MSERIALIZE_EXPAND(MSERIALIZE_MAKE_TEMPLATE_TAG((Concept Concept), (Concept) __VA_OPT__(,) __VA_ARGS__)) \
MSERIALIZE_EXPAND(MSERIALIZE_MAKE_TEMPLATE_SERIALIZABLE((Concept Concept), (Concept) __VA_OPT__(,) __VA_ARGS__)) \
/**/
#endif // >= C++20

/**
* Allow member list of BINLOG_ADAPT_STRUCT or BINLOG_ADAPT_TEMPLATE
* to reference private and protected members.
Expand Down
6 changes: 6 additions & 0 deletions test/integration/IntegrationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ TEST_CASE("LoggingVariants") { runReadDiff("LoggingVariants", "%m"); }

#endif

#if __cplusplus >= 202002L

TEST_CASE("LoggingAdaptedConcepts") { runReadDiff("LoggingAdaptedConcepts", "%m"); }

#endif

#ifdef BINLOG_HAS_BOOST

TEST_CASE("LoggingBoostTypes") { runReadDiff("LoggingBoostTypes", "%m"); }
Expand Down
86 changes: 86 additions & 0 deletions test/integration/LoggingAdaptedConcepts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <binlog/binlog.hpp>

#include <iostream>

// Empty

template<typename T>
concept Empty = T::is_empty::value;

#ifdef __clang__
// Since C++20, passing no arguments to a variadic macro is allowed,
// and we are testing wheter it works. clang 10 produces an incorrect warning.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#endif

BINLOG_ADAPT_CONCEPT(Empty)

#ifdef __clang__
#pragma clang diagnostic pop
#endif

struct Nothing {
using is_empty = std::true_type;
};

// Concept

//[concept
template<typename T>
concept Stringable = requires(T a)
{
{ a.str() } -> std::convertible_to<std::string>;
};

BINLOG_ADAPT_CONCEPT(Stringable, str)

struct Foo {
std::string str() const { return "bar"; }
};
//]

// Concept shadowed by BINLOG_ADAPT_STRUCT

struct Baz {
std::string str() const { return "baz"; }
int id() const { return 123; }
};

BINLOG_ADAPT_STRUCT(Baz, id)

static_assert(Stringable<Baz>);

// Concept shadowed by BINLOG_ADAPT_TEMPLATE

template <typename A>
struct Wrap
{
std::string str() const { return "wrap"; }

A a{};
};

BINLOG_ADAPT_TEMPLATE((typename A), (Wrap<A>), a)

static_assert(Stringable<Wrap<int>>);

int main()
{
BINLOG_INFO("{}", Nothing{});
// Outputs: Empty

//[concept
BINLOG_INFO("{}", Foo{});
// Outputs: Stringable{ str: bar }
//]

BINLOG_INFO("{}", Baz{});
// Outputs: Baz{ id: 123 }

BINLOG_INFO("{}", Wrap<int>{456});
// Outputs: Wrap{ a: 456 }

binlog::consume(std::cout);
return 0;
}