48 #ifndef Sawyer_Attribute_H
49 #define Sawyer_Attribute_H
51 #include <boost/any.hpp>
52 #include <boost/foreach.hpp>
53 #include <boost/lexical_cast.hpp>
54 #include <Sawyer/Exception.h>
55 #include <Sawyer/Map.h>
56 #include <Sawyer/Optional.h>
57 #include <Sawyer/Sawyer.h>
58 #include <Sawyer/Synchronization.h>
135 namespace Attribute {
162 SAWYER_EXPORT
Id id(
const std::string &
name);
170 SAWYER_EXPORT
const std::string&
name(
Id);
181 : Exception::NotFound(attrName +
" does not exist in object") {}
191 : Exception::
AlreadyExists(attrName +
" is already a declared attribute (id=" +
192 boost::lexical_cast<std::string>(
id) +
")") {}
207 template<
class SyncTag = SAWYER_THREAD_TAG>
215 mutable typename Sync::Mutex mutex_;
228 typename Sync::LockGuard lock(other.mutex_);
229 values_ = other.values_;
236 typename Sync::LockGuard2 lock(mutex_, other.mutex_);
237 values_ = other.values_;
247 typename Sync::LockGuard lock(mutex_);
248 return values_.
exists(
id);
258 typename Sync::LockGuard lock(mutex_);
266 typename Sync::LockGuard lock(mutex_);
279 typename Sync::LockGuard lock(mutex_);
280 values_.
insert(
id, boost::any(value));
292 typename Sync::LockGuard lock(mutex_);
293 if (!values_.
exists(
id)) {
294 values_.
insert(
id, boost::any(value));
309 typename Sync::LockGuard lock(mutex_);
310 AttrMap::ConstNodeIterator found = values_.
find(
id);
311 if (found == values_.
nodes().end()) {
314 throw DoesNotExist(
"attribute id " + boost::lexical_cast<std::string>(
id) +
" [not declared]");
332 typename Sync::LockGuard lock(mutex_);
346 typename Sync::LockGuard lock(mutex_);
347 AttrMap::ConstNodeIterator found = values_.
find(
id);
348 if (found == values_.
nodes().end())
350 return boost::any_cast<T>(found->value());
360 typename Sync::LockGuard lock(mutex_);
361 AttrMap::ConstNodeIterator found = values_.
find(
id);
362 if (found == values_.
nodes().end())
364 return boost::any_cast<T>(found->value());
371 typename Sync::LockGuard lock(mutex_);
372 return values_.
size();
379 typename Sync::LockGuard lock(mutex_);
380 std::vector<Id> retval;
381 retval.reserve(values_.
size());
382 BOOST_FOREACH (
Id id, values_.
keys())
383 retval.push_back(
id);
Holds a value or nothing.
API and storage for attributes.
AlreadyExists(const std::string &attrName, Id id)
Constructor taking an attribute name or description.
NodeIterator find(const Key &key)
Find a node by key.
Storage(const Storage &other)
Copy constructor.
Error for non-existing values.
Storage()
Default constructor.
T getAttribute(Id id) const
Get an attribute that is known to exist.
T attributeOrElse(Id id, const T &dflt) const
Return an attribute or a specified value.
void clearAttributes()
Erase all attributes.
void checkBoost()
Check for valid boost version or abort.
Optional< Value > getOptional(const Key &key) const
Lookup and return a value or nothing.
Exception thrown when redeclaring an existing attribute.
T attributeOrDefault(Id id) const
Return an attribute or a default-constructed value.
Map & insert(const Key &key, const Value &value)
Insert or update a key/value pair.
size_t size() const
Number of nodes, keys, or values in this container.
boost::iterator_range< NodeIterator > nodes()
Iterators for container nodes.
std::vector< Id > attributeIds() const
Returns ID numbers for all IDs stored in this container.
bool setAttributeMaybe(Id id, const T &value)
Store an attribute if not already present.
size_t Id
Attribute identification.
Value orDefault() const
Obtain a value or a default.
Error for existing values.
const Value & orElse(const Value &dflt) const
Obtain value or something else.
Name space for the entire library.
boost::bad_any_cast WrongQueryType
Exception thrown when wrong data type is queried.
Id declare(const std::string &name)
Register a new attribute key.
Traits for thread synchronization.
void setAttribute(Id id, const T &value)
Store an attribute.
Sawyer::Optional< T > optionalAttribute(Id id) const
Return the attribute as an optional value.
size_t nAttributes() const
Number of attributes stored.
Id id(const std::string &name)
Returns the ID for an attribute name.
DoesNotExist(const std::string &attrName)
Constructor taking an attribute name or description.
Exception for non-existing values.
bool attributeExists(Id id) const
Check attribute existence.
bool exists(const Key &key) const
Determine if a key exists.
const Id INVALID_ID
Invalid attribute ID.
Storage & operator=(const Storage &other)
Assignment operator.
boost::iterator_range< ConstKeyIterator > keys()
Iterators for container keys.
Map & erase(const Key &key)
Remove a node with specified key.
Map & clear()
Remove all nodes.
const std::string & name(Id)
Returns the name for an attribute ID.
void eraseAttribute(Id id)
Erase an attribute.