summaryrefslogtreecommitdiff
path: root/libadhocutil/factory.h
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-09-14 20:31:30 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2015-09-14 20:31:30 +0100
commit13ef5b22effca60ac26bce71d4c67011d4c533aa (patch)
tree8820f1c1b2a02cb757bac5e854d914a5c9f2772c /libadhocutil/factory.h
parentCreate a unique namespace for plugin registry functions (diff)
downloadlibadhocutil-13ef5b22effca60ac26bce71d4c67011d4c533aa.tar.bz2
libadhocutil-13ef5b22effca60ac26bce71d4c67011d4c533aa.tar.xz
libadhocutil-13ef5b22effca60ac26bce71d4c67011d4c533aa.zip
Add basic factory support
Diffstat (limited to 'libadhocutil/factory.h')
-rw-r--r--libadhocutil/factory.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/libadhocutil/factory.h b/libadhocutil/factory.h
new file mode 100644
index 0000000..2493525
--- /dev/null
+++ b/libadhocutil/factory.h
@@ -0,0 +1,36 @@
+#ifndef ADHOCUTIL_FACTORY_H
+#define ADHOCUTIL_FACTORY_H
+
+#include "plugins.h"
+
+namespace AdHoc {
+ template <typename Base, typename ... Params>
+ class Factory {
+ public:
+ virtual ~Factory() = 0;
+
+ virtual Base * create(const Params & ...) const = 0;
+
+ template <typename Impl, typename _ = Factory<Base, Params...>>
+ class For : public _
+ {
+ public:
+ Base * create(const Params & ... p) const override
+ {
+ return new Impl(p...);
+ }
+ };
+
+ static const Factory * get(const std::string & name);
+ static Base * create(const std::string & name, const Params & ... p);
+ };
+}
+
+#define NAMEDFACTORY(Name, Implementation, BaseFactory) \
+ NAMEDPLUGIN(Name, BaseFactory::For<Implementation>, BaseFactory)
+
+#define FACTORY(Implementation, BaseFactory) \
+ NAMEDFACTORY(#Implementation, Implementation, BaseFactory)
+
+#endif
+