#ifndef GUIDMAP_H #define GUIDMAP_H #include #include #include #include class GUIDMap { public: class NotFound : public std::exception { }; protected: GUIDMap(int16_t s) : seed(int32_t(s) << 16) { } virtual ~GUIDMap() { } mutable boost::shared_mutex lock; int32_t seed; int16_t key; }; template class GUIDMapImpl : public GUIDMap { public: typedef std::map Storage; GUIDMapImpl(int16_t s) : GUIDMap(s) { } ~GUIDMapImpl() { BOOST_FOREACH(typename Storage::value_type v, storage) { ValueDeleter(v.second); } } int32_t store(const Value & value) { boost::unique_lock l(lock); int32_t newKey = (seed + ++key); while (storage.find(newKey) != storage.end()) { newKey = (seed + ++key); } storage.insert(std::pair(newKey, value)); return newKey; } void remove(int32_t key) { boost::unique_lock l(lock); storage.erase(key); } Value get(int32_t key) const { boost::shared_lock l(lock); typename Storage::const_iterator i = storage.find(key); if (i != storage.end()) { return i->second; } throw NotFound(); } bool contains(int32_t key) const { boost::shared_lock l(lock); return (storage.find(key) != storage.end()); } private: Storage storage; }; #endif