Nui
event.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <memory>
5 #include <vector>
6 
7 namespace Nui
8 {
9  struct EventImpl
10  {
11  virtual bool call(std::size_t eventId) = 0;
12  virtual bool valid() const = 0;
13  virtual ~EventImpl() = default;
14  };
15 
17  {
18  TwoFunctorEventImpl(std::function<bool(std::size_t eventId)> action, std::function<bool()> valid)
19  : action_{std::move(action)}
20  , valid_{std::move(valid)}
21  {}
22 
23  bool call(std::size_t eventId) override
24  {
25  auto result = action_(eventId);
26  return result;
27  }
28 
29  bool valid() const override
30  {
31  return valid_();
32  }
33 
34  private:
35  std::function<bool(std::size_t eventId)> action_;
36  std::function<bool()> valid_;
37  };
38 
39  class Event
40  {
41  public:
43  std::function<bool(std::size_t eventId)> action,
44  std::function<bool()> valid =
45  [] {
46  return true;
47  })
48  : impl_{std::make_unique<TwoFunctorEventImpl>(std::move(action), std::move(valid))}
49  {}
50  Event(Event const&) = delete;
51  Event(Event&&) = default;
52  Event& operator=(Event const&) = delete;
53  Event& operator=(Event&&) = default;
54 
55  operator bool() const
56  {
57  return impl_->valid();
58  }
59  bool operator()(std::size_t eventId) const
60  {
61  return impl_->call(eventId);
62  }
63 
64  private:
65  std::unique_ptr<EventImpl> impl_;
66  };
67 }
Definition: event.hpp:40
Event & operator=(Event const &)=delete
Event(Event &&)=default
Event(Event const &)=delete
bool operator()(std::size_t eventId) const
Definition: event.hpp:59
Event(std::function< bool(std::size_t eventId)> action, std::function< bool()> valid=[] { return true;})
Definition: event.hpp:42
Event & operator=(Event &&)=default
Definition: file_dialog.hpp:6
Definition: event.hpp:10
virtual ~EventImpl()=default
virtual bool valid() const =0
virtual bool call(std::size_t eventId)=0
Definition: event.hpp:17
bool valid() const override
Definition: event.hpp:29
bool call(std::size_t eventId) override
Definition: event.hpp:23
TwoFunctorEventImpl(std::function< bool(std::size_t eventId)> action, std::function< bool()> valid)
Definition: event.hpp:18