summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJosh Blum2013-07-07 11:50:03 -0700
committerJosh Blum2013-07-07 11:50:03 -0700
commitc6373f53f4690d667e553ed7258e13c3b45aa323 (patch)
tree202bf0d31140c5567cad54dea697ad1dcbba6432 /lib
parente2a237e121edfb53378ae57d8faa71491d0f29f1 (diff)
downloadsandhi-c6373f53f4690d667e553ed7258e13c3b45aa323.tar.gz
sandhi-c6373f53f4690d667e553ed7258e13c3b45aa323.tar.bz2
sandhi-c6373f53f4690d667e553ed7258e13c3b45aa323.zip
gras: created element factory
Diffstat (limited to 'lib')
-rw-r--r--lib/CMakeLists.txt1
-rw-r--r--lib/callable.cpp1
-rw-r--r--lib/factory.cpp49
3 files changed, 51 insertions, 0 deletions
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 38216b3..93ef63c 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -44,6 +44,7 @@ list(APPEND GRAS_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/callable.cpp
${CMAKE_CURRENT_SOURCE_DIR}/element.cpp
${CMAKE_CURRENT_SOURCE_DIR}/element_uid.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/factory.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sbuffer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/circular_buffer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/buffer_queue_circ.cpp
diff --git a/lib/callable.cpp b/lib/callable.cpp
index af27399..a7bc7da 100644
--- a/lib/callable.cpp
+++ b/lib/callable.cpp
@@ -42,6 +42,7 @@ void Callable::unregister_call(const std::string &name)
void Callable::_register_call(const std::string &name, void *entry)
{
CallableRegistry *cr = reinterpret_cast<CallableRegistry *>(_call_registry.get());
+ if (cr->count(name) != 0) throw std::invalid_argument("Callable - method already registered for name: " + name);
(*cr)[name].reset(reinterpret_cast<CallableRegistryEntry *>(entry));
}
diff --git a/lib/factory.cpp b/lib/factory.cpp
new file mode 100644
index 0000000..1c634fd
--- /dev/null
+++ b/lib/factory.cpp
@@ -0,0 +1,49 @@
+// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
+
+#include <gras/factory.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/thread/mutex.hpp>
+#include <stdexcept>
+#include <map>
+
+using namespace gras;
+
+FactoryRegistryEntry::FactoryRegistryEntry(void)
+{
+ //NOP
+}
+
+FactoryRegistryEntry::~FactoryRegistryEntry(void)
+{
+ //NOP
+}
+
+typedef std::map<std::string, boost::shared_ptr<FactoryRegistryEntry> > FactoryRegistryType;
+
+static FactoryRegistryType &get_factory_registry(void)
+{
+ static FactoryRegistryType r;
+ return r;
+}
+
+static boost::mutex mutex;
+
+void Factory::_register_make(const std::string &name, void *entry)
+{
+ boost::mutex::scoped_lock l(mutex);
+ if (get_factory_registry().count(name) != 0)
+ {
+ throw std::invalid_argument("Factory - function already registered for name: " + name);
+ }
+ get_factory_registry()[name].reset(reinterpret_cast<FactoryRegistryEntry *>(entry));
+}
+
+Element *Factory::_make(const std::string &name, const PMCC &args)
+{
+ boost::mutex::scoped_lock l(mutex);
+ if (get_factory_registry().count(name) == 0)
+ {
+ throw std::invalid_argument("Factory - no function registered for name: " + name);
+ }
+ return get_factory_registry()[name]->make(args);
+}