summaryrefslogtreecommitdiff
path: root/i/marvin/src/utils/callback.tcc
diff options
context:
space:
mode:
Diffstat (limited to 'i/marvin/src/utils/callback.tcc')
-rw-r--r--i/marvin/src/utils/callback.tcc136
1 files changed, 136 insertions, 0 deletions
diff --git a/i/marvin/src/utils/callback.tcc b/i/marvin/src/utils/callback.tcc
new file mode 100644
index 0000000..d10df75
--- /dev/null
+++ b/i/marvin/src/utils/callback.tcc
@@ -0,0 +1,136 @@
+// callback.tcc
+// robert - programme du robot 2005. {{{
+//
+// Copyright (C) 2004 Nicolas Schodet
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// Contact :
+// Web: http://perso.efrei.fr/~schodet/
+// Email: <contact@ni.fr.eu.org>
+// }}}
+
+#include <algorithm>
+
+/// Default constructor. Make an empty callback.
+template<typename R>
+Callback<R>::Callback (void)
+ : holder_ (0)
+{
+}
+
+/// Constructor.
+template<typename R>
+template<typename T>
+Callback<R>::Callback (T callback)
+ : holder_ (new Holder<T> (callback))
+{
+}
+
+/// Copy constructor.
+template<typename R>
+Callback<R>::Callback (const Callback<R> &other)
+ : holder_ (other.holder_ ? other.holder_->clone () : 0)
+{
+}
+
+/// Destructor.
+template<typename R>
+Callback<R>::~Callback (void)
+{
+ delete holder_;
+}
+
+/// Assignement operator.
+template<typename R>
+Callback<R> &
+Callback<R>::operator= (const Callback<R> &rhs)
+{
+ Callback<R> (rhs).swap (*this);
+ return *this;
+}
+
+/// Call the contained callback.
+template<typename R>
+R
+Callback<R>::operator () (void)
+{
+ if (!holder_)
+ throw bad_callback ();
+ else
+ return (*holder_) ();
+}
+
+/// Swap the callback content with another callback.
+template<typename R>
+void
+Callback<R>::swap (Callback<R> &rhs)
+{
+ std::swap (holder_, rhs.holder_);
+}
+
+/// Change the contained callback.
+template<typename R>
+template<typename T>
+Callback<R> &
+Callback<R>::operator= (T rhs)
+{
+ Callback<R> (rhs).swap (*this);
+ return *this;
+}
+
+/// Test if empty.
+template<typename R>
+bool
+Callback<R>::empty (void) const
+{
+ return !holder_;
+}
+
+/// Test if not empty.
+template<typename R>
+Callback<R>::operator bool (void) const
+{
+ return !empty ();
+}
+
+template<typename R>
+Callback<R>::AbstractHolder::~AbstractHolder (void)
+{
+}
+
+template<typename R>
+template<typename T>
+Callback<R>::Holder<T>::Holder (T callback)
+ : callback_ (callback)
+{
+}
+
+template<typename R>
+template<typename T>
+R
+Callback<R>::Holder<T>::operator () (void)
+{
+ return callback_ ();
+}
+
+template<typename R>
+template<typename T>
+typename Callback<R>::AbstractHolder *
+Callback<R>::Holder<T>::clone (void)
+{
+ return new Holder<T> (callback_);
+}
+