summaryrefslogtreecommitdiff
path: root/lib/element.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.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.cpp')
-rw-r--r--lib/element.cpp41
1 files changed, 21 insertions, 20 deletions
diff --git a/lib/element.cpp b/lib/element.cpp
index dfc3f57..04a638b 100644
--- a/lib/element.cpp
+++ b/lib/element.cpp
@@ -3,11 +3,10 @@
#include "element_impl.hpp"
#include <gras/element.hpp>
#include <boost/format.hpp>
-#include <boost/detail/atomic_count.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
-
-static boost::detail::atomic_count unique_id_pool(0);
+#include <boost/functional/hash.hpp>
+#include <cstdlib>
using namespace gras;
@@ -20,8 +19,25 @@ Element::Element(const std::string &name)
{
this->reset(new ElementImpl());
(*this)->name = name;
- (*this)->unique_id = ++unique_id_pool;
- (*this)->id = str(boost::format("%s(%d)") % this->name() % this->unique_id());
+ std::string extra;
+ std::string uid = name;
+ boost::hash<std::string> string_hash;
+ std::size_t h = string_hash(uid);
+ while (true)
+ {
+ try
+ {
+ this->set_uid(uid);
+ break;
+ }
+ catch(const std::invalid_argument &ex)
+ {
+ extra = str(boost::format("%04x") % short(h++));
+ uid = name+"#"+extra;
+ }
+ }
+ if (not extra.empty()) (*this)->repr = name;
+ else (*this)->repr = str(boost::format("%s (%s)") % name % extra);
if (GENESIS) std::cerr << "New element: " << to_string() << std::endl;
}
@@ -58,21 +74,6 @@ bool Element::equals(const Element &rhs)
return this->get() == rhs.get();
}
-long Element::unique_id(void) const
-{
- return (*this)->unique_id;
-}
-
-std::string Element::name(void) const
-{
- return (*this)->name;
-}
-
-std::string Element::to_string(void) const
-{
- return (*this)->id;
-}
-
void Element::adopt_element(const std::string &name, const Element &child)
{
if (child->parent) throw std::invalid_argument(str(boost::format(