emitter: uses type_id_v rather than a family

This commit is contained in:
Michele Caini
2019-12-12 22:17:28 +01:00
parent d1cdeb4a2d
commit 88467a87a4
7 changed files with 82 additions and 22 deletions

5
TODO
View File

@@ -26,9 +26,8 @@
* Mission: get rid of named types
- make it possible to use custom generators (eg for plugins)
* dispatcher, emitter, registry
- generators as arguments to family::type overloads
- generators to constructors
- bind identifiers by name (as for meta)
* dispatcher/emitter: id shouldn't be part of the class (upcoming feature discard will create a hole otherwise)
* type_id_enabled and fallback on old-fashioned families otherwise
- reintroduce old-fashion family and add a new family-like thing with generators
- families should be defined as out-of-class to guarantee the same identifiers for the same types
- update doc: family, dispatcher, emitter, registry, meta, across boundaries

View File

@@ -10,8 +10,7 @@
#include <vector>
#include <list>
#include "../config/config.h"
#include "../core/attribute.h"
#include "../core/family.hpp"
#include "../core/type_info.hpp"
namespace entt {
@@ -40,15 +39,11 @@ namespace entt {
*/
template<typename Derived>
class emitter {
struct ENTT_API emitter_event_family;
template<typename Type>
using event_family = family<Type, emitter_event_family>;
struct basic_pool {
virtual ~basic_pool() = default;
virtual bool empty() const ENTT_NOEXCEPT = 0;
virtual void clear() ENTT_NOEXCEPT = 0;
virtual ENTT_ID_TYPE id() const ENTT_NOEXCEPT = 0;
};
template<typename Event>
@@ -112,6 +107,10 @@ class emitter {
on_list.remove_if([](auto &&element) { return element.first; });
}
ENTT_ID_TYPE id() const ENTT_NOEXCEPT override {
return type_id_v<Event>;
}
private:
bool publishing{false};
container_type once_list{};
@@ -120,17 +119,19 @@ class emitter {
template<typename Event>
const pool_handler<Event> & assure() const {
const auto etype = event_family<std::decay_t<Event>>::type();
static const auto index = std::distance(pools.cbegin(), std::find_if(pools.cbegin(), pools.cend(), [](auto &&curr) {
return curr && curr->id() == type_id_v<Event>;
}));
if(!(etype < pools.size())) {
pools.resize(etype+1);
if(!(index < pools.size())) {
pools.resize(index+1);
}
if(!pools[etype]) {
pools[etype] = std::make_unique<pool_handler<Event>>();
if(!pools[index]) {
pools[index] = std::make_unique<pool_handler<Event>>();
}
return static_cast<const pool_handler<Event> &>(*pools[etype]);
return static_cast<const pool_handler<Event> &>(*pools[index]);
}
template<typename Event>

View File

@@ -2,8 +2,6 @@
#include <entt/signal/emitter.hpp>
#include "types.h"
template struct entt::family<event, test_emitter::emitter_event_family>;
ENTT_API void emit(int value, test_emitter &emitter) {
emitter.publish<message>(value);
}

View File

@@ -1,18 +1,17 @@
#ifndef ENTT_LIB_EMITTER_TYPES_H
#define ENTT_LIB_EMITTER_TYPES_H
#include <entt/core/attribute.h>
#include <entt/signal/emitter.hpp>
struct ENTT_API test_emitter
struct test_emitter
: entt::emitter<test_emitter>
{};
struct ENTT_API message {
struct message {
int payload;
};
struct ENTT_API event {
struct event {
int payload;
};

View File

@@ -0,0 +1,26 @@
#define CR_HOST
#include <cr.h>
#include <gtest/gtest.h>
#include <entt/signal/emitter.hpp>
#include "types.h"
TEST(Lib, Emitter) {
test_emitter emitter;
int value{};
emitter.once<event>([&](event ev, test_emitter &) { value = ev.payload; });
emitter.once<message>([&](message msg, test_emitter &) { value = msg.payload; });
emitter.publish<event>(3);
ASSERT_EQ(value, 3);
cr_plugin ctx;
ctx.userdata = &emitter;
cr_plugin_load(ctx, PLUGIN);
cr_plugin_update(ctx);
ASSERT_EQ(value, 42);
cr_plugin_close(ctx);
}

View File

@@ -0,0 +1,19 @@
#include <cr.h>
#include <entt/signal/emitter.hpp>
#include "types.h"
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
switch (operation) {
case CR_STEP:
static_cast<test_emitter *>(ctx->userdata)->publish<message>(42);
static_cast<test_emitter *>(ctx->userdata)->publish<message>(3);
break;
case CR_LOAD:
case CR_UNLOAD:
case CR_CLOSE:
// nothing to do here, this is only a test.
break;
}
return 0;
}

View File

@@ -0,0 +1,18 @@
#ifndef ENTT_PLUGIN_EMITTER_TYPES_H
#define ENTT_PLUGIN_EMITTER_TYPES_H
#include <entt/signal/emitter.hpp>
struct test_emitter
: entt::emitter<test_emitter>
{};
struct message {
int payload;
};
struct event {
int payload;
};
#endif // ENTT_PLUGIN_EMITTER_TYPES_H