aboutsummaryrefslogtreecommitdiff
path: root/dynvars-test.cpp
blob: c80a7cad7ec0fb65ed91c298f294412969414953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <functional>
#include <memory>
#include <future>

#include "dynvars.hpp"

#include <boost/signals2.hpp>

using namespace ::std;
using namespace ::boost::signals2;
using namespace ::dynvars;

#define thread_local __thread
thread_local dynvar<string> greetee;

function<void ()>
make_greeter(const string& greeting) {
  return [=]() {
    cout << greeting << " " << *greetee << "!" << endl;
  };
};

int
main(int argc, char **argv) {
  greetee = "nobody";
  signal<void ()> sig;

  sig.connect(make_greeter("Hi"));

  sig();
  with<string, void>(greetee, "luser", [&](){ sig(); });
  with<string, void>(greetee, "geek",  [&](){ sig(); });
  sig();

  return EXIT_SUCCESS;
}