Commit 9a5dbc12 authored by Dominik Charousset's avatar Dominik Charousset

Add new type ID list

parent 0b02da9f
...@@ -155,6 +155,7 @@ set(CAF_CORE_SOURCES ...@@ -155,6 +155,7 @@ set(CAF_CORE_SOURCES
src/tracing_data_factory.cpp src/tracing_data_factory.cpp
src/type_erased_tuple.cpp src/type_erased_tuple.cpp
src/type_erased_value.cpp src/type_erased_value.cpp
src/type_id_list.cpp
src/uniform_type_info_map.cpp src/uniform_type_info_map.cpp
src/uri.cpp src/uri.cpp
src/uri_builder.cpp src/uri_builder.cpp
...@@ -270,6 +271,7 @@ set(CAF_CORE_TEST_SOURCES ...@@ -270,6 +271,7 @@ set(CAF_CORE_TEST_SOURCES
test/to_string.cpp test/to_string.cpp
test/tracing_data.cpp test/tracing_data.cpp
test/type_erased_tuple.cpp test/type_erased_tuple.cpp
test/type_id_list.cpp
test/typed_response_promise.cpp test/typed_response_promise.cpp
test/typed_spawn.cpp test/typed_spawn.cpp
test/unit.cpp test/unit.cpp
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2020 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string>
#include "caf/detail/comparable.hpp"
#include "caf/detail/core_export.hpp"
#include "caf/type_id.hpp"
namespace caf {
/// A list of 16-bit type IDs, stored in a size-prefix, contiguous memory block.
class type_id_list : detail::comparable<type_id_list> {
public:
using pointer = const uint16_t*;
constexpr explicit type_id_list(pointer data) noexcept : data_(data) {
// nop
}
constexpr type_id_list(const type_id_list&) noexcept = default;
type_id_list& operator=(const type_id_list&) noexcept = default;
/// Queries whether this type list contains data, i,e, `data() != nullptr`.
constexpr operator bool() const noexcept {
return data_ != nullptr;
}
/// Returns the raw pointer to the size-prefixed list.
constexpr pointer data() const noexcept {
return data_;
}
/// Returns the number of elements in the list.
/// @pre `data() != nullptr`
constexpr size_t size() const noexcept {
return data_[0];
}
/// Returns the type ID at `index`.
/// @pre `data() != nullptr`
constexpr uint16_t operator[](size_t index) const noexcept {
return data_[index + 1];
}
/// Compares this list to `other`.
int compare(type_id_list other) const noexcept {
return memcmp(data_, other.data_, size() * sizeof(uint16_t));
}
private:
pointer data_;
};
/// @private
template <class... Ts>
struct make_type_id_list_helper {
static constexpr inline uint16_t data[]
= {static_cast<uint16_t>(sizeof...(Ts)), type_id_v<Ts>...};
};
/// Constructs a ::type_id_list from the template parameter pack `Ts`.
/// @relates type_id_list
template <class... Ts>
constexpr type_id_list make_type_id_list() {
return type_id_list{make_type_id_list_helper<Ts...>::data};
}
/// @relates type_id_list
CAF_CORE_EXPORT std::string to_string(type_id_list xs);
} // namespace caf
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2020 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/type_id_list.hpp"
#include "caf/detail/meta_object.hpp"
namespace caf {
std::string to_string(type_id_list xs) {
if (!xs || xs.size() == 0)
return "[]";
std::string result;
result += '[';
result += detail::global_meta_object(xs[0]).type_name;
for (size_t index = 1; index < xs.size(); ++index) {
result += ", ";
result += detail::global_meta_object(xs[index]).type_name;
}
result += ']';
return result;
}
} // namespace caf
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2020 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE type_id_list
#include "caf/type_id_list.hpp"
#include "caf/test/dsl.hpp"
#include "caf/init_global_meta_objects.hpp"
using namespace caf;
CAF_TEST(lists store the size at index 0) {
uint16_t data[] = {3, 1, 2, 4};
type_id_list xs{data};
CAF_CHECK_EQUAL(xs.size(), 3u);
CAF_CHECK_EQUAL(xs[0], 1u);
CAF_CHECK_EQUAL(xs[1], 2u);
CAF_CHECK_EQUAL(xs[2], 4u);
}
CAF_TEST(lists are comparable) {
uint16_t data[] = {3, 1, 2, 4};
type_id_list xs{data};
uint16_t data_copy[] = {3, 1, 2, 4};
type_id_list ys{data_copy};
CAF_CHECK_EQUAL(xs, ys);
data_copy[1] = 10;
CAF_CHECK_NOT_EQUAL(xs, ys);
CAF_CHECK_LESS(xs, ys);
}
CAF_TEST(make_type_id_list constructs a list from types) {
auto xs = make_type_id_list<uint8_t, bool, float>();
CAF_CHECK_EQUAL(xs.size(), 3u);
CAF_CHECK_EQUAL(xs[0], type_id_v<uint8_t>);
CAF_CHECK_EQUAL(xs[1], type_id_v<bool>);
CAF_CHECK_EQUAL(xs[2], type_id_v<float>);
}
CAF_TEST(type ID lists are convertible to strings) {
detail::clear_global_meta_objects();
init_global_meta_objects<builtin_type_ids>();
auto xs = make_type_id_list<uint8_t, bool, float>();
CAF_CHECK_EQUAL(to_string(xs), "[uint8_t, bool, float]");
}
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