Commit a7d8a39c authored by Dominik Charousset's avatar Dominik Charousset

Add type_id_list::concat

parent 37a96d85
......@@ -19,8 +19,6 @@
#include <cstdint>
#include <type_traits>
#include "caf/detail/type_traits.hpp"
#pragma once
namespace caf {
......@@ -29,31 +27,31 @@ namespace caf {
enum class byte : uint8_t {};
template <class IntegerType,
class = detail::enable_if_tt<std::is_integral<IntegerType>>>
class = std::enable_if_t<std::is_integral<IntegerType>::value>>
constexpr IntegerType to_integer(byte x) noexcept {
return static_cast<IntegerType>(x);
}
template <class IntegerType,
class E = detail::enable_if_tt<std::is_integral<IntegerType>>>
class E = std::enable_if_t<std::is_integral<IntegerType>::value>>
constexpr byte& operator<<=(byte& x, IntegerType shift) noexcept {
return x = static_cast<byte>(to_integer<uint8_t>(x) << shift);
}
template <class IntegerType,
class E = detail::enable_if_tt<std::is_integral<IntegerType>>>
class E = std::enable_if_t<std::is_integral<IntegerType>::value>>
constexpr byte operator<<(byte x, IntegerType shift) noexcept {
return static_cast<byte>(to_integer<uint8_t>(x) << shift);
}
template <class IntegerType,
class E = detail::enable_if_tt<std::is_integral<IntegerType>>>
class E = std::enable_if_t<std::is_integral<IntegerType>::value>>
constexpr byte& operator>>=(byte& x, IntegerType shift) noexcept {
return x = static_cast<byte>(to_integer<uint8_t>(x) >> shift);
}
template <class IntegerType,
class E = detail::enable_if_tt<std::is_integral<IntegerType>>>
class E = std::enable_if_t<std::is_integral<IntegerType>::value>>
constexpr byte operator>>(byte x, IntegerType shift) noexcept {
return static_cast<byte>(static_cast<unsigned char>(x) >> shift);
}
......
......@@ -26,8 +26,6 @@
#include "caf/detail/type_pair.hpp"
#include "caf/fwd.hpp"
#include "caf/none.hpp"
#include "caf/type_id.hpp"
#include "caf/type_id_list.hpp"
#include "caf/unit.hpp"
namespace caf::detail {
......@@ -50,21 +48,6 @@ struct strip_param<param<T>> {
using type = T;
};
template <class List>
struct to_type_id_list_helper;
template <class... Ts>
struct to_type_id_list_helper<type_list<Ts...>> {
static constexpr type_id_list get() {
return make_type_id_list<typename strip_param<Ts>::type...>();
}
};
template <class List>
constexpr type_id_list to_type_id_list() {
return to_type_id_list_helper<List>::get();
}
/// Denotes the empty list.
using empty_type_list = type_list<>;
......
......@@ -144,7 +144,6 @@ class string_view;
class tracing_data;
class tracing_data_factory;
class type_id_list;
class type_id_list_builder;
class uri;
class uri_builder;
class uuid;
......
......@@ -25,6 +25,7 @@
#include "caf/detail/comparable.hpp"
#include "caf/detail/core_export.hpp"
#include "caf/span.hpp"
#include "caf/type_id.hpp"
namespace caf {
......@@ -92,6 +93,17 @@ public:
/// type-erased tuple for the element types stored in this list.
size_t data_size() const noexcept;
/// Concatenates all `lists` into a single type ID list.
static type_id_list concat(span<type_id_list> lists);
/// Concatenates all `lists` into a single type ID list.
template <class... Ts>
static type_id_list
concat(type_id_list list1, type_id_list list2, Ts... lists) {
type_id_list arr[] = {list1, list2, lists...};
return concat(arr);
}
private:
pointer data_;
};
......@@ -132,4 +144,19 @@ type_id_list make_argument_type_id_list() {
return argument_type_id_list_factory<F>::make();
}
template <class List>
struct to_type_id_list_helper;
template <class... Ts>
struct to_type_id_list_helper<type_list<Ts...>> {
static constexpr type_id_list get() {
return make_type_id_list<typename strip_param<Ts>::type...>();
}
};
template <class List>
constexpr type_id_list to_type_id_list() {
return to_type_id_list_helper<List>::get();
}
} // namespace caf::detail
......@@ -19,6 +19,7 @@
#include "caf/type_id_list.hpp"
#include "caf/detail/meta_object.hpp"
#include "caf/detail/type_id_list_builder.hpp"
namespace caf {
......@@ -49,4 +50,16 @@ std::string to_string(type_id_list xs) {
return result;
}
type_id_list type_id_list::concat(span<type_id_list> lists) {
auto total_size = size_t{0};
for (auto ls : lists)
total_size += ls.size();
detail::type_id_list_builder builder;
builder.reserve(total_size);
for (auto ls : lists)
for (auto id : ls)
builder.push_back(id);
return builder.move_to_list();
}
} // namespace caf
......@@ -59,3 +59,35 @@ CAF_TEST(type ID lists are convertible to strings) {
auto xs = make_type_id_list<uint16_t, bool, float, long double>();
CAF_CHECK_EQUAL(to_string(xs), "[uint16_t, bool, float, ldouble]");
}
CAF_TEST(type ID lists are concatenable) {
// 1 + 0
CHECK_EQ((make_type_id_list<int8_t>()),
type_id_list::concat(make_type_id_list<int8_t>(),
make_type_id_list<>()));
CHECK_EQ((make_type_id_list<int8_t>()),
type_id_list::concat(make_type_id_list<>(),
make_type_id_list<int8_t>()));
// 1 + 1
CHECK_EQ((make_type_id_list<int8_t, int16_t>()),
type_id_list::concat(make_type_id_list<int8_t>(),
make_type_id_list<int16_t>()));
// 2 + 0
CHECK_EQ((make_type_id_list<int8_t, int16_t>()),
type_id_list::concat(make_type_id_list<int8_t, int16_t>(),
make_type_id_list<>()));
CHECK_EQ((make_type_id_list<int8_t, int16_t>()),
type_id_list::concat(make_type_id_list<>(),
make_type_id_list<int8_t, int16_t>()));
// 2 + 1
CHECK_EQ((make_type_id_list<int8_t, int16_t, int32_t>()),
type_id_list::concat(make_type_id_list<int8_t, int16_t>(),
make_type_id_list<int32_t>()));
CHECK_EQ((make_type_id_list<int8_t, int16_t, int32_t>()),
type_id_list::concat(make_type_id_list<int8_t>(),
make_type_id_list<int16_t, int32_t>()));
// 2 + 2
CHECK_EQ((make_type_id_list<int8_t, int16_t, int32_t, int64_t>()),
type_id_list::concat(make_type_id_list<int8_t, int16_t>(),
make_type_id_list<int32_t, int64_t>()));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment