summaryrefslogtreecommitdiff
path: root/lib/element_uid.cpp
blob: 49daad9bfec647267d399056dd7bd860bb0e370a (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
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.

#include "element_impl.hpp"
#include <gras/element.hpp>
#include <boost/thread/mutex.hpp>
#include <stdexcept>
#include <set>

using namespace gras;

static boost::mutex uid_mutex;
static std::set<std::string> &get_uid_set(void)
{
    static std::set<std::string> uid_set;
    return uid_set;
}

static void free_uid(std::string *s)
{
    boost::mutex::scoped_lock l(uid_mutex);
    get_uid_set().erase(*s);
    delete s;
}

static std::string *alloc_uid(const std::string &uid)
{
    boost::mutex::scoped_lock l(uid_mutex);
    if (get_uid_set().count(uid) > 0)
    {
        throw std::invalid_argument("Element::set_uid - uid already in use: " + uid);
    }
    get_uid_set().insert(uid);
    return new std::string(uid);
}

void Element::set_uid(const std::string &uid)
{
    (*this)->uid.reset(alloc_uid(uid), &free_uid);
}

std::string Element::get_uid(void) const
{
    boost::mutex::scoped_lock l(uid_mutex);
    return *((*this)->uid);
}