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

Fix/inline injector #116

Merged
merged 4 commits into from
Jul 13, 2024
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
48 changes: 47 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,50 @@ Checks: '-*,
-hicpp-vararg,
-hicpp-member-init,
misc-*,
-misc-non-private-member-variables-in-classes,
-misc-non-private-member-variables-in-classes'

# Disabled Options Explanation:
# CppCoreGuidelines:
# cppcoreguidelines-pro-bounds-constant-array-index: requires use of guideline support library
# cppcoreguidelines-pro-type-vararg: Mostly triggers for functions we didn't write in acceptable
# situations.
# cppcoreguidelines-pro-type-member-init: Even though this rule is useful, it cannot be uphold in
# some cases with generated constructors and would break a lot of code.
# cppcoreguidelines-non-private-member-variables-in-classes: This rule cannot be followed because
# of testing requirements to make some members protected.

# Llvm:
# llvm-header-guard: pragma once allowed
# llvm-include-order: Clang format sorts the includes, should maybe fixed in clang-format file.

# Modernize:
# modernize-use-nodiscard: wants nodiscard for every function.
# modernize-avoid-c-arrays: duplicate rule.
# modernize-use-using: Cannot apply to SDK.

# Performance:
# performance-noexcept-move-constructor: This is cannot be generally applied and is
# potentially dangerous if applied wrongly.
# Performance improvement from this is questionable and less relevant.

# Readability:
# readability-redundant-member-init: Initializing all members of a class even if unnecessary is
# not bad style
# readability-uppercase-literal-suffix
# readability-else-after-return: Rule cannot be followed
# readability-named-parameter: Breaks builds with unused variables
# readability-redundant-access-specifiers: Redundant access specifiers (public, private) can
# actually increase readability by introducing visual barriers
# readability-magic-numbers: cppcoreguidelines has a rule for it, no duplicate needed.

# Hicpp:
# hicpp-uppercase-literal-suffix
# hicpp-noexcept-move: This cannot be generally applied and is potentially dangerous if applied
# wrongly. Performance improvement from this is questionable and less relevant.
# hicpp-vararg: Mostly triggers for functions we didn't write in acceptable situations.
# hicpp-member-init: Even though this rule is useful, it cannot be uphold in some cases with
# generated constructors and would break a lot of code.
# hicpp-avoid-c-arrays: duplicate rule.

# Misc:
# misc-non-private-member-variables-in-classes: Public members should be allowed in structs
67 changes: 67 additions & 0 deletions nui/include/nui/event_system/event.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

#include <functional>
#include <memory>
#include <vector>

namespace Nui
{
struct EventImpl
{
virtual bool call(std::size_t eventId) = 0;
virtual bool valid() const = 0;
virtual ~EventImpl() = default;
};

struct TwoFunctorEventImpl : public EventImpl
{
TwoFunctorEventImpl(std::function<bool(std::size_t eventId)> action, std::function<bool()> valid)
: action_{std::move(action)}
, valid_{std::move(valid)}
{}

bool call(std::size_t eventId) override
{
auto result = action_(eventId);
return result;
}

bool valid() const override
{
return valid_();
}

private:
std::function<bool(std::size_t eventId)> action_;
std::function<bool()> valid_;
};

class Event
{
public:
Event(
std::function<bool(std::size_t eventId)> action,
std::function<bool()> valid =
[] {
return true;
})
: impl_{std::make_unique<TwoFunctorEventImpl>(std::move(action), std::move(valid))}
{}
Event(Event const&) = delete;
Event(Event&&) = default;
Event& operator=(Event const&) = delete;
Event& operator=(Event&&) = default;

operator bool() const
{
return impl_->valid();
}
bool operator()(std::size_t eventId) const
{
return impl_->call(eventId);
}

private:
std::unique_ptr<EventImpl> impl_;
};
}
85 changes: 85 additions & 0 deletions nui/include/nui/event_system/event_context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#pragma once

#include <nui/frontend/event_system/event_registry.hpp>

#include <memory>

namespace Nui
{
class EventEngine
{
public:
EventEngine() = default;
EventEngine(const EventEngine&) = default;
EventEngine(EventEngine&&) = default;
EventEngine& operator=(const EventEngine&) = default;
EventEngine& operator=(EventEngine&&) = default;
virtual ~EventEngine() = default;

virtual EventRegistry& eventRegistry() = 0;
};

class DefaultEventEngine : public EventEngine
{
public:
EventRegistry& eventRegistry() override
{
return eventRegistry_;
}

private:
EventRegistry eventRegistry_;
};

/**
* @brief This object can be copied with low cost.
*/
class EventContext
{
public:
using EventIdType = EventRegistry::EventIdType;

EventContext()
: impl_{std::make_shared<DefaultEventEngine>()}
{}
EventContext(EventContext const&) = default;
EventContext(EventContext&&) = default;
EventContext& operator=(EventContext const&) = default;
EventContext& operator=(EventContext&&) = default;
~EventContext() = default;

EventIdType registerEvent(Event event)
{
return impl_->eventRegistry().registerEvent(std::move(event));
}
auto activateEvent(EventIdType id)
{
return impl_->eventRegistry().activateEvent(id);
}
void executeActiveEventsImmediately()
{
impl_->eventRegistry().executeActiveEvents();
}
void executeEvent(EventIdType id)
{
impl_->eventRegistry().executeEvent(id);
}
EventIdType registerAfterEffect(Event event)
{
return impl_->eventRegistry().registerAfterEffect(std::move(event));
}
void cleanInvalidEvents()
{
impl_->eventRegistry().cleanInvalidEvents();
}
void reset()
{
impl_->eventRegistry().clear();
}

private:
std::shared_ptr<EventEngine> impl_;
};

extern thread_local EventContext globalEventContext;
}
98 changes: 98 additions & 0 deletions nui/include/nui/event_system/event_registry.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#pragma once

#include <nui/data_structures/selectables_registry.hpp>
#include <nui/frontend/event_system/event.hpp>
#include <nui/utility/visit_overloaded.hpp>

#include <functional>
#include <limits>
#include <map>

namespace Nui
{
class EventRegistry
{
public:
using RegistryType = SelectablesRegistry<Event>;
using EventIdType = SelectablesRegistry<Event>::IdType;
constexpr static EventIdType invalidEventId = std::numeric_limits<EventIdType>::max();

public:
EventRegistry() = default;
EventRegistry(const EventRegistry&) = delete;
EventRegistry(EventRegistry&&) = default;
EventRegistry& operator=(const EventRegistry&) = delete;
EventRegistry& operator=(EventRegistry&&) = default;
~EventRegistry() = default;

EventIdType registerEvent(Event event)
{
return registry_.append(std::move(event));
}

/**
* @brief Returns a pointer to the selected event (only valid until the next activation or event execution). May
* return nullptr when the event id was not found.
*
* @param id
* @return auto*
*/
RegistryType::SelectionResult activateEvent(EventIdType id)
{
return registry_.select(id);
}

EventIdType registerAfterEffect(Event event)
{
return afterEffects_.append(std::move(event));
}

void executeEvent(EventIdType id)
{
registry_.deselect(id, [](SelectablesRegistry<Event>::ItemWithId const& itemWithId) -> bool {
if (!itemWithId.item)
return false;
return itemWithId.item.value()(itemWithId.id);
});
}

void executeActiveEvents()
{
registry_.deselectAll([](SelectablesRegistry<Event>::ItemWithId const& itemWithId) -> bool {
if (!itemWithId.item)
return false;
return itemWithId.item.value()(itemWithId.id);
});
afterEffects_.deselectAll([](SelectablesRegistry<Event>::ItemWithId const& itemWithId) -> bool {
if (!itemWithId.item)
return false;
return itemWithId.item.value()(itemWithId.id);
});
}

void cleanInvalidEvents()
{
std::vector<EventIdType> invalidIds;
for (auto const& itemWithId : registry_.rawRange())
{
if (!itemWithId.item)
continue;
if (!static_cast<bool>(itemWithId.item.value()))
invalidIds.push_back(itemWithId.id);
}

for (auto const& id : invalidIds)
registry_.erase(id);
}

void clear()
{
registry_.clear();
afterEffects_.clear();
}

private:
RegistryType registry_;
RegistryType afterEffects_;
};
}
75 changes: 75 additions & 0 deletions nui/include/nui/event_system/listen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#pragma once

#include <nui/event_system/event_context.hpp>
#include <nui/event_system/observed_value.hpp>

#include <utility>
#include <memory>
#include <functional>

namespace Nui
{
template <typename ValueT>
void listen(EventContext& eventContext, Observed<ValueT> const& obs, std::function<bool(ValueT const&)> onEvent)
{
const auto eventId = eventContext.registerEvent(Event{
[obs = Detail::CopyableObservedWrap{obs}, onEvent = std::move(onEvent)](auto) {
return onEvent(obs.value());
},
[]() {
return true;
}});
obs.attachEvent(eventId);
}

template <typename ValueT>
void listen(EventContext& eventContext, Observed<ValueT> const& obs, std::function<void(ValueT const&)> onEvent)
{
return listen(eventContext, obs, [onEvent = std::move(onEvent)](ValueT const& value) {
onEvent(value);
return true;
});
}

template <typename ValueT, typename FunctionT>
void listen(EventContext& eventContext, Observed<ValueT> const& obs, FunctionT onEvent)
{
return listen(eventContext, obs, std::function(std::move(onEvent)));
}

template <typename ValueT>
void listen(
EventContext& eventContext,
std::shared_ptr<Observed<ValueT>> const& obs,
std::function<bool(ValueT const&)> onEvent)
{
const auto eventId = eventContext.registerEvent(Event{
[weak = std::weak_ptr<Observed<ValueT>>{obs}, onEvent = std::move(onEvent)](auto) {
if (auto obs = weak.lock(); obs)
return onEvent(obs->value());
return false;
},
[weak = std::weak_ptr<Observed<ValueT>>{obs}]() {
return !weak.expired();
}});
obs->attachEvent(eventId);
}

template <typename ValueT>
void listen(
EventContext& eventContext,
std::shared_ptr<Observed<ValueT>> const& obs,
std::function<void(ValueT const&)> onEvent)
{
return listen(eventContext, obs, [onEvent = std::move(onEvent)](ValueT const& value) {
onEvent(value);
return true;
});
}

template <typename ValueT, typename FunctionT>
void listen(EventContext& eventContext, std::shared_ptr<Observed<ValueT>> const& obs, FunctionT onEvent)
{
return listen(eventContext, obs, std::function(std::move(onEvent)));
}
}
Loading
Loading