ARB
rootAsWin.h
Go to the documentation of this file.
1 // ============================================================ //
2 // //
3 // File : rootAsWin.h //
4 // Purpose : //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in July 2013 //
7 // Institute of Microbiology (Technical University Munich) //
8 // http://www.arb-home.de/ //
9 // //
10 // ============================================================ //
11 
12 #ifndef ROOTASWIN_H
13 #define ROOTASWIN_H
14 
15 #ifndef CB_H
16 #include <cb.h>
17 #endif
18 #ifndef AW_WINDOW_HXX
19 #include <aw_window.hxx>
20 #endif
21 
23  // Allows to use any RootCallback where a WindowCallback is expected.
24 
25  RootCallback rcb;
26  void call(AW_window *aww) const { rcb(aww->get_root()); }
27  RootAsWindowCallback(const RootCallback& rcb_) : rcb(rcb_) {}
28 
29  static void window_cb(AW_window *aww, const RootAsWindowCallback *rawcb) { rawcb->call(aww); }
30  static void delete_raw_cb(RootAsWindowCallback *raw_cb) { delete raw_cb; }
31 
32  template<typename T>
33  static RETURN_TYPE_IF(true,void)
34  call_root_as_win(AW_window *aww, void (*root_cb)(AW_root*, T), T t)
35  {
36  root_cb(aww->get_root(), t);
37  }
38 
39  template<typename T>
40  static RETURN_TYPE_IF(CONST_VARIANT_REQUIRED(T),void)
41  call_rootWithConstParam_as_win(AW_window *aww, void (*root_cb)(AW_root*, CONST_PARAM_T(T)), T t)
42  {
43  root_cb(aww->get_root(), t);
44  }
45 
46  static void call_root_as_win(AW_window *aww, void (*root_cb)(AW_root*)) { root_cb(aww->get_root()); }
47 
48 public:
49 
50  static WindowCallback reuse(const RootCallback& rcb) {
51  // It's not recommended to use this widely, mainly this is a helper for refactoring.
52  // Normally you should better adapt your callback-type.
53  //
54  // Use it in cases where a user-provided callback has to be used
55  // both as RootCallback and as WindowCallback.
56 
57  return makeWindowCallback(window_cb, delete_raw_cb, new RootAsWindowCallback(rcb));
58  }
59 
60  template<typename T>
61  static RETURN_TYPE_IF(true,WindowCallback)
62  simple(void (*root_cb)(AW_root*, T), T t)
63  {
64  // usable for simple root callbacks with only one argument
65  // (automatically creates one general wrapper function for each type T)
66  return makeWindowCallback(call_root_as_win, root_cb, t);
67  }
68  template<typename T>
69  static RETURN_TYPE_IF(CONST_VARIANT_REQUIRED(T),WindowCallback)
70  simple(void (*root_cb)(AW_root*, CONST_PARAM_T(T)), T t)
71  {
72  // like above, but instanciates (if required) an additional variant,
73  // where root_cb guarantees not to modify the mutable T.
74  return makeWindowCallback(call_rootWithConstParam_as_win, root_cb, t);
75  }
76 
77  static WindowCallback simple(void (*root_cb)(AW_root*)) {
78  // usable for simple root callbacks with no argument
79  return makeWindowCallback(call_root_as_win, root_cb);
80  }
81 };
82 
83 #else
84 #error rootAsWin.h included twice
85 #endif // ROOTASWIN_H
#define CONST_VARIANT_REQUIRED(T)
Definition: cbtypes.h:297
static WindowCallback reuse(const RootCallback &rcb)
Definition: rootAsWin.h:50
static CONST_PARAM_T(T))
AW_root * get_root()
Definition: aw_window.hxx:362
static WindowCallback simple(void(*root_cb)(AW_root *))
Definition: rootAsWin.h:77