sequencer
callable.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sequencer/assert.hpp>
5 
6 namespace sequencer
7 {
8  template < class ReturnType >
9  class callable_t
10  {
11  template < class Impl >
12  static ReturnType call( const type_erased_storage_t& data )
13  {
14  return data.template get< Impl >()();
15  }
16 
17  public:
18  callable_t() noexcept = default;
19 
20  template < class T,
21  std::enable_if_t< !std::is_constructible< callable_t, T >::value >* = nullptr >
22  // NOLINTNEXTLINE(bugprone-forwarding-reference-overload)
23  callable_t( T&& value )
24  : call_( &call< std::decay_t< T > > ), impl_( std::forward< T >( value ) )
25  {
26  }
27 
28  template < class T,
29  std::enable_if_t< !std::is_constructible< callable_t, T >::value >* = nullptr >
30  callable_t& operator=( T&& value )
31  {
32  // NOLINTNEXTLINE(cppcoreguidelines-c-copy-assignment-signature)
33  return *this = callable_t( std::forward< T >( value ) );
34  }
35 
36  explicit operator bool() const noexcept
37  {
38  return bool( impl_ );
39  }
40 
41  ReturnType operator()() const
42  {
43  SEQUENCER_ASSERT( call_ )
44  SEQUENCER_ASSERT( impl_ )
45  return call_( impl_ );
46  }
47 
48  private:
49  using call_t = ReturnType ( * )( const type_erased_storage_t& );
50  call_t call_{nullptr};
51  type_erased_storage_t impl_{};
52  };
53 } // namespace sequencer
ReturnType operator()() const
Definition: callable.hpp:41
Definition: callable.hpp:9
Definition: beat_duration.hpp:106
#define SEQUENCER_ASSERT(cond)
Definition: assert.hpp:8
Definition: type_erased_storage.hpp:11
callable_t() noexcept=default
callable_t & operator=(T &&value)
Definition: callable.hpp:30
callable_t(T &&value)
Definition: callable.hpp:23