summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/utils/bind.hh
diff options
context:
space:
mode:
authorschodet2005-03-13 20:26:23 +0000
committerschodet2005-03-13 20:26:23 +0000
commitfcfd616f76345787a8eb1318e482ab1076d50039 (patch)
treeadb7200c223de2cd3839bf9c922894f48f377dae /2005/i/robert/src/utils/bind.hh
parenta5ccb9a2944290075e032a8a18102a046841e808 (diff)
Ajout de commentaires.
Ajout de l'ObjectBinder.
Diffstat (limited to '2005/i/robert/src/utils/bind.hh')
-rw-r--r--2005/i/robert/src/utils/bind.hh50
1 files changed, 43 insertions, 7 deletions
diff --git a/2005/i/robert/src/utils/bind.hh b/2005/i/robert/src/utils/bind.hh
index 2a92c78..a5630fe 100644
--- a/2005/i/robert/src/utils/bind.hh
+++ b/2005/i/robert/src/utils/bind.hh
@@ -24,17 +24,21 @@
// Email: <contact@ni.fr.eu.org>
// }}}
-namespace utils {
-
+/// Class used to bind a function taking an argument to a functor taking no
+/// argument. This is not supposed to be used directly, but using the bind
+/// template function.
template<typename R, typename F, typename A>
-class binder
+class ArgBinder
{
+ /// Original function.
F func_;
+ /// Stored argument reference.
const A &arg_;
public:
+ /// Return value.
typedef R result_type;
public:
- binder (F func, const A &arg)
+ ArgBinder (F func, const A &arg)
: func_ (func), arg_ (arg)
{ }
result_type operator () (void)
@@ -43,13 +47,45 @@ class binder
}
};
+/// Bind a function taking an argument to a functor taking no argument.
+/// Return value must be specified, other template arguments can be implied.
template<typename R, typename F, typename A>
-binder<R, F, A>
+ArgBinder<R, F, A>
bind (F func, const A &arg)
{
- return binder<R, F, A> (func, arg);
+ return ArgBinder<R, F, A> (func, arg);
}
-} // namespace utils
+/// Class used to bind a member function to a functor taking no argument.
+/// This is not supposed to be used directly, but using the bind template
+/// function.
+template<typename R, typename C>
+class ObjBinder
+{
+ /// Original member function.
+ R (C::*func_) (void);
+ /// Object pointer stored.
+ C *obj_;
+ public:
+ /// Return value.
+ typedef R result_type;
+ public:
+ ObjBinder (R (C::*func) (void), C *obj)
+ : func_ (func), obj_ (obj)
+ { }
+ result_type operator () (void)
+ {
+ (obj_->*func_) ();
+ }
+};
+
+/// Bind a member function to a functor taking no argument. All template
+/// parameters must be specified.
+template<typename R, typename C>
+ObjBinder<R, C>
+bind (R (C::*func) (void), C *obj)
+{
+ return ObjBinder<R, C> (func, obj);
+}
#endif // bind_hh