summaryrefslogtreecommitdiff
path: root/lib/element_uid.cpp
diff options
context:
space:
mode:
authorJosh Blum2013-06-01 22:46:29 -0700
committerJosh Blum2013-06-01 22:46:29 -0700
commitd15ca88acfbd71c5d4f8ab3dabe0f4fbde205985 (patch)
treee0ba8437cf65503fa05197e774024da4f9b54230 /lib/element_uid.cpp
parent40af24eb55d2d43f51d7ada30566d5203f0fef8c (diff)
downloadsandhi-d15ca88acfbd71c5d4f8ab3dabe0f4fbde205985.tar.gz
sandhi-d15ca88acfbd71c5d4f8ab3dabe0f4fbde205985.tar.bz2
sandhi-d15ca88acfbd71c5d4f8ab3dabe0f4fbde205985.zip
gras: created uid API - replaces name and unique_id
Diffstat (limited to 'lib/element_uid.cpp')
-rw-r--r--lib/element_uid.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/element_uid.cpp b/lib/element_uid.cpp
new file mode 100644
index 0000000..89035b4
--- /dev/null
+++ b/lib/element_uid.cpp
@@ -0,0 +1,46 @@
+// 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(void *p)
+{
+ boost::mutex::scoped_lock l(uid_mutex);
+ std::string *uid = reinterpret_cast<std::string *>(p);
+ get_uid_set().erase(*uid);
+ delete uid;
+}
+
+void Element::set_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);
+ }
+ (*this)->uid.reset(new std::string(uid), &free_uid);
+ get_uid_set().insert(uid);
+}
+
+std::string Element::get_uid(void) const
+{
+ boost::mutex::scoped_lock l(uid_mutex);
+ return *((*this)->uid);
+}
+
+std::string Element::to_string(void) const
+{
+ return (*this)->repr;
+}