#ifndef BOOSTINTERPROCESS_SHAREDMAP_HPP #define BOOSTINTERPROCESS_SHAREDMAP_HPP 1 #include #include #include #include #include #include #include #include #include #include #include #include namespace Tony { template < typename KeyType, typename ValueType, typename StoredKeyType = KeyType, typename StoredValueType = ValueType > class SharedMap : private boost::noncopyable { public: SharedMap( const std::string & shmName, const std::size_t shmSize, const std::size_t initBucketCount = 10 ); ~SharedMap(); typedef std::vector< KeyType > KeyVector; typedef std::vector< ValueType > ValueVector; void setOneValue( const KeyType & key, const ValueType & value ); bool getOneValue( const KeyType & key, ValueType & value ); void setManyValues( const KeyVector & keys, const ValueVector & values ); void setManyValues( typename KeyVector::const_iterator & keysBegin, typename KeyVector::const_iterator & keysEnd, typename ValueVector::const_iterator & valuesBegin ); void getManyValues( const KeyVector & keys, ValueVector & values ); void getManyValues( typename KeyVector::const_iterator & keysBegin, typename KeyVector::const_iterator & keysEnd, typename ValueVector::iterator & valuesBegin ); bool deleteOneKey( const KeyType & key ); std::size_t deleteManyKeys( const KeyVector & keys ); void clear(); static void removeMapping( const std::string & shmName ); struct ShmStats { std::size_t nTotalBytes; std::size_t nFreeBytes; std::size_t nNamedObjs; std::size_t nUniqueObjs; std::ostream & dumpTo( std::ostream & os ); }; // end struct ShmStats void getShmStats( struct ShmStats & stats ); private: typedef std::pair< const StoredKeyType, StoredValueType > StoredPairType; typedef boost::interprocess::managed_shared_memory ShmManagerType; typedef boost::interprocess::allocator< StoredPairType, ShmManagerType::segment_manager > ShmAllocatorType; typedef boost::interprocess::interprocess_mutex ShmMutexType; typedef boost::interprocess::scoped_lock< ShmMutexType > ShmScopedLock; typedef boost::unordered_map< StoredKeyType, StoredValueType, boost::hash< StoredKeyType >, std::equal_to< StoredKeyType >, ShmAllocatorType > ShmMapType; std::string m_sShmName; ShmManagerType m_manager; ShmMutexType * m_pMutex; ShmMapType * m_pMap; }; // end class SharedMap typedef SharedMap< /* KeyType = */ std::string, /* ValueType = */ std::string, /* StoredKeyType = */ boost::interprocess::string, /* StoredValueType = */ boost::interprocess::string > SharedStringMap; } // end namespace Tony #endif // BOOSTINTERPROCESS_SHAREDMAP_HPP