Skip to content

Commit

Permalink
Merge pull request intel#344 from elbeno/simplify-builder-meta
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevalenty authored Aug 20, 2023
2 parents 78215ff + 94746ac commit 99f4b3f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 45 deletions.
18 changes: 12 additions & 6 deletions include/cib/builder_meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ namespace cib {
/**
* Describe a builder to cib.
*
* @tparam BuilderType
* @tparam Builder
* The initial builder type cib should use when creating a builder.
* This is only the initial type, the BuilderType::add(...) function
* This is only the initial type, the Builder::add(...) function
* may return a different type and cib will track that correctly.
*
* @tparam InterfaceType
* @tparam Interface
* The type-erased interface services built with this builder
* will implement. For example, cib::callback allows many other
* callables to get executed when its service gets invoked. The
Expand All @@ -19,8 +19,14 @@ namespace cib {
*
* @example cib::callback_meta
*/
template <typename BuilderType, typename InterfaceType> struct builder_meta {
auto builder() -> BuilderType;
auto interface() -> InterfaceType;
template <typename Builder, typename Interface> struct builder_meta {
using builder_t = Builder;
using interface_t = Interface;
};

template <typename BuilderMeta>
using builder_t = typename BuilderMeta::builder_t;

template <typename BuilderMeta>
using interface_t = typename BuilderMeta::interface_t;
} // namespace cib
4 changes: 2 additions & 2 deletions include/cib/built.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <cib/detail/builder_traits.hpp>
#include <cib/builder_meta.hpp>

namespace cib {
/**
Expand All @@ -11,5 +11,5 @@ namespace cib {
*
* @see cib::builder_meta
*/
template <typename ServiceMeta> traits::interface_t<ServiceMeta> service;
template <typename ServiceMeta> interface_t<ServiceMeta> service;
} // namespace cib
1 change: 0 additions & 1 deletion include/cib/config.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <cib/builder_meta.hpp>
#include <cib/detail/builder_traits.hpp>
#include <cib/detail/compiler.hpp>
#include <cib/detail/components.hpp>
#include <cib/detail/conditional.hpp>
Expand Down
21 changes: 0 additions & 21 deletions include/cib/detail/builder_traits.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions include/cib/detail/extend.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <cib/detail/builder_traits.hpp>
#include <cib/builder_meta.hpp>
#include <cib/detail/compiler.hpp>
#include <cib/detail/config_item.hpp>
#include <cib/tuple.hpp>
Expand All @@ -9,7 +9,7 @@ namespace cib::detail {
template <typename ServiceType, typename... Args>
struct extend : public config_item {
using service_type = ServiceType;
constexpr static auto builder = cib::traits::builder_v<service_type>;
constexpr static auto builder = cib::builder_t<service_type>{};
cib::tuple<Args...> args_tuple;

CIB_CONSTEVAL explicit extend(Args const &...args) : args_tuple{args...} {}
Expand Down
15 changes: 8 additions & 7 deletions test/cib/builder_meta.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#include <cib/cib.hpp>
#include <cib/builder_meta.hpp>

#include <catch2/catch_test_macros.hpp>

#include <type_traits>

struct TestBuilderTag {};
struct TestInterfaceTag {};
namespace {
struct TestBuilderTag;
struct TestInterfaceTag;

struct test_builder_meta
: public cib::builder_meta<TestBuilderTag, TestInterfaceTag> {};
} // namespace

TEST_CASE(
"builder_meta builder and interface type traits return correct values") {
REQUIRE(std::is_same_v<TestBuilderTag,
cib::traits::builder_t<test_builder_meta>>);
REQUIRE(std::is_same_v<TestInterfaceTag,
cib::traits::interface_t<test_builder_meta>>);
REQUIRE(std::is_same_v<TestBuilderTag, cib::builder_t<test_builder_meta>>);
REQUIRE(
std::is_same_v<TestInterfaceTag, cib::interface_t<test_builder_meta>>);
}
12 changes: 6 additions & 6 deletions test/cib/callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ template <typename BuilderValue> constexpr static auto build() {

template <typename BuilderMeta, typename BuiltCallback>
constexpr static bool built_is_convertable_to_interface(BuiltCallback) {
using interface_type = cib::traits::interface_t<BuilderMeta>;
using interface_type = cib::interface_t<BuilderMeta>;
return std::is_convertible_v<BuiltCallback, interface_type>;
}

struct EmptyCallbackNoArgs {
using meta = cib::callback_meta<>;
constexpr static auto value = cib::traits::builder_t<meta>{};
constexpr static auto value = cib::builder_t<meta>{};
};

TEST_CASE("empty callback with no args", "[callback]") {
Expand All @@ -35,7 +35,7 @@ template <int Id> static bool is_callback_invoked = false;
struct CallbackNoArgsWithSingleExtension {
using meta = cib::callback_meta<>;
constexpr static auto value = []() {
auto const builder = cib::traits::builder_t<meta>{};
auto const builder = cib::builder_t<meta>{};
return builder.add([]() { is_callback_invoked<0> = true; });
}();
};
Expand Down Expand Up @@ -66,7 +66,7 @@ struct CallbackNoArgsWithMultipleExtensions {
};

constexpr static auto value = []() {
auto const builder = cib::traits::builder_t<meta>{};
auto const builder = cib::builder_t<meta>{};

return builder.add([]() { is_callback_invoked<0> = true; })
.add(extension_one)
Expand Down Expand Up @@ -97,7 +97,7 @@ TEST_CASE("callback with no args with multiple extensions", "[callback]") {

struct CallbackWithArgsWithNoExtensions {
using meta = cib::callback_meta<int, bool>;
constexpr static auto value = cib::traits::builder_t<meta>{};
constexpr static auto value = cib::builder_t<meta>{};
};

TEST_CASE("callback with args with no extensions", "[callback]") {
Expand Down Expand Up @@ -128,7 +128,7 @@ struct CallbackWithArgsWithMultipleExtensions {
};

constexpr static auto value = []() {
auto const builder = cib::traits::builder_t<meta>{};
auto const builder = cib::builder_t<meta>{};

return builder
.add([](int a, bool b) {
Expand Down

0 comments on commit 99f4b3f

Please sign in to comment.