Anklang 0.3.0-460-gc4ef46ba
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
callback.cc
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#include "callback.hh"
3#include "internal.hh"
4#include <string.h>
5
6#define CDEBUG(...) Ase::debug ("callback", __VA_ARGS__)
7
8namespace Ase {
9
10// == RtCall::Callable ==
12{
13 memset (mem_, 0, sizeof (mem_));
14}
15
16RtCall::RtCall (const RtCall &other)
17{
18 if (other.callable())
19 memcpy (mem_, other.mem_, sizeof (other.mem_));
20}
21
22const RtCall::Callable*
23RtCall::callable () const
24{
25 if (mem_[0] == 0 && mem_[1] == 0 && mem_[2] == 0 && mem_[3] == 0)
26 return nullptr;
27 return reinterpret_cast<const Callable*> (unalias_ptr (&mem_[0]));
28}
29
30RtCall::RtCall (void (*f) ())
31{
32 struct Wrapper : Callable {
33 Wrapper (void (*f) ()) : f_ (f) {}
34 void call() const override { return f_ (); }
35 void (*f_) ();
36 };
37 static_assert (sizeof (mem_) >= sizeof (Wrapper));
38 Wrapper *w = new (mem_) Wrapper { f };
39 ASE_ASSERT_RETURN (w == (void*) mem_);
40}
41
42void
44{
45 const Callable *wcallable = callable();
46 if (wcallable)
47 wcallable->call();
48}
49
50void RtCall::Callable::call () const {}
51
52// == JobQueue ==
53JobQueue::JobQueue (const Caller &caller) :
54 caller_ (caller)
55{}
56
57} // Ase
58
59#include <assert.h>
60
61namespace {
62using namespace Ase;
63
64TEST_INTEGRITY (callback_list_test);
65static void
66callback_list_test()
67{
69 String r;
70 const size_t aid = cbl->add ([&r] (const char *delim) { r += delim; r += "a"; });
71 const size_t bid = cbl->add ([&r,&cbl,aid] (const char *delim) { r += delim; r += "b"; cbl->del (aid); });
72 const size_t cid = cbl->add ([&r] (const char *delim) { r += delim; r += "c"; });
73 (*cbl) ("+");
74 assert (r == "+a+b+c");
75 (*cbl) ("|");
76 assert (r == "+a+b+c|b|c");
77 cbl->del (bid);
78 (*cbl) ("*");
79 assert (r == "+a+b+c|b|c*c");
80 cbl->del (cid);
81 (*cbl) ("-");
82 assert (r == "+a+b+c|b|c*c");
83}
84
85} // Anon
assert
Reentrant callback list with configurable arguments.
Definition callback.hh:13
#define ASE_ASSERT_RETURN(expr,...)
Return from the current function if expr evaluates to false and issue an assertion warning.
Definition cxxaux.hh:82
#define TEST_INTEGRITY(FUNC)
Register func as an integrity test.
Definition internal.hh:77
memcpy
memset
The Anklang C++ API namespace.
Definition api.hh:9
Wrap simple callback pointers, without using malloc (obstruction free).
Definition callback.hh:91
void invoke()
Invoke the wrapped function call.
Definition callback.cc:43
~RtCall()
Clear function pointers.
Definition callback.cc:11
RtCall(T &o, void(T::*f)())
Wrap a simple void func() object member function call.
Definition callback.hh:125