summaryrefslogtreecommitdiff
path: root/include/gras/callable.hpp
diff options
context:
space:
mode:
authorJosh Blum2013-07-05 14:08:12 -0700
committerJosh Blum2013-07-05 14:08:12 -0700
commitbc8165bcc3a2703d8fd3d17925b6bcb55ddff6ef (patch)
treec90c0829a173381398bf6a28c8e5780f76de586f /include/gras/callable.hpp
parent1298bcd1e421ccfefafa78c6d33760818902f399 (diff)
downloadsandhi-bc8165bcc3a2703d8fd3d17925b6bcb55ddff6ef.tar.gz
sandhi-bc8165bcc3a2703d8fd3d17925b6bcb55ddff6ef.tar.bz2
sandhi-bc8165bcc3a2703d8fd3d17925b6bcb55ddff6ef.zip
gras: save callable work before we tear up more
Diffstat (limited to 'include/gras/callable.hpp')
-rw-r--r--include/gras/callable.hpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/include/gras/callable.hpp b/include/gras/callable.hpp
new file mode 100644
index 0000000..07c6034
--- /dev/null
+++ b/include/gras/callable.hpp
@@ -0,0 +1,89 @@
+// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
+
+#ifndef INCLUDED_GRAS_CALLABLE_HPP
+#define INCLUDED_GRAS_CALLABLE_HPP
+
+#include <gras/gras.hpp>
+#include <PMC/PMC.hpp>
+#include <string>
+#include <vector>
+
+namespace gras
+{
+
+/*!
+ * The callable interface allows subclasses to export public methods,
+ * but without actually exporting traditional library symbols.
+ *
+ * Callable handles the template magic so you don't have to:
+ * - registering subclass methods is simple and easy on the user.
+ * - users call registered methods with natural code aesthetics.
+ *
+ * Register a method (in the constructor of MyClass):
+ * this->register_call("set_foo", &MyClass::set_foo);
+ *
+ * Call a method on a instance of MyClass:
+ * my_class->call("set_foo", new_foo_val);
+ */
+struct GRAS_API Callable
+{
+ //! Default constructor
+ Callable(void);
+
+ //! Destructor (virtual for subclasses)
+ virtual ~Callable(void);
+
+ /*******************************************************************
+ * Register API - don't look here, template magic, not helpful
+ ******************************************************************/
+ template <typename ClassType, typename ReturnType>
+ void register_call(const std::string &key, ReturnType(ClassType::*fcn)(void));
+
+ template <typename ClassType, typename ReturnType, typename Arg0>
+ void register_call(const std::string &key, ReturnType(ClassType::*fcn)(const Arg0 &));
+
+ template <typename ClassType, typename ReturnType, typename Arg0, typename Arg1>
+ void register_call(const std::string &key, ReturnType(ClassType::*fcn)(const Arg0 &, const Arg1 &));
+
+ template <typename ClassType>
+ void register_call(const std::string &key, void(ClassType::*fcn)(void));
+
+ template <typename ClassType, typename Arg0>
+ void register_call(const std::string &key, void(ClassType::*fcn)(const Arg0 &));
+
+ template <typename ClassType, typename Arg0, typename Arg1>
+ void register_call(const std::string &key, void(ClassType::*fcn)(const Arg0 &, const Arg1 &));
+
+ /*******************************************************************
+ * Call API - don't look here, template magic, not helpful
+ ******************************************************************/
+ template <typename ReturnType>
+ ReturnType call(const std::string &key);
+
+ template <typename ReturnType, typename Arg0>
+ ReturnType call(const std::string &key, const Arg0 &);
+
+ template <typename ReturnType, typename Arg0, typename Arg1>
+ ReturnType call(const std::string &key, const Arg0 &, const Arg1 &);
+
+ void call(const std::string &key);
+
+ template <typename Arg0>
+ void call(const std::string &key, const Arg0 &);
+
+ template <typename Arg0, typename Arg1>
+ void call(const std::string &key, const Arg0 &, const Arg1 &);
+
+ /*******************************************************************
+ * Private registration hooks
+ ******************************************************************/
+ void _register_call(const std::string &, void *);
+ virtual PMCC _handle_call(const std::string &, const std::vector<PMCC> &);
+ std::map<std::string, void *> _call_registry;
+};
+
+} //namespace gras
+
+#include <gras/detail/callable.hpp>
+
+#endif /*INCLUDED_GRAS_CALLABLE_HPP*/