From d15ca88acfbd71c5d4f8ab3dabe0f4fbde205985 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Sat, 1 Jun 2013 22:46:29 -0700 Subject: gras: created uid API - replaces name and unique_id --- lib/element.cpp | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'lib/element.cpp') 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 #include -#include #include #include - -static boost::detail::atomic_count unique_id_pool(0); +#include +#include 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 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( -- cgit