summaryrefslogtreecommitdiff
path: root/lib/factory.cpp
blob: 1c634fdf30c559a6aae5c3af63b4d12d81b75fe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);
}