Created
June 26, 2013 00:08
-
-
Save bovine/5863647 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/interprocess/managed_mapped_file.hpp> | |
#include <boost/interprocess/managed_shared_memory.hpp> | |
#include <boost/interprocess/allocators/allocator.hpp> | |
#include <boost/interprocess/containers/string.hpp> | |
#include <boost/flyweight.hpp> | |
#include <boost/flyweight/no_tracking.hpp> | |
#include <boost/flyweight/hashed_factory.hpp> | |
using namespace boost::flyweights; | |
using namespace boost::container; | |
using namespace boost::interprocess; | |
typedef boost::interprocess::allocator<boost::mpl::_1, boost::interprocess::managed_mapped_file::segment_manager> ShmFactoryEntryAllocator; | |
typedef boost::interprocess::allocator<char, boost::interprocess::managed_mapped_file::segment_manager> ShmAllocatorChar; | |
typedef boost::interprocess::basic_string<char, std::char_traits<char>, ShmAllocatorChar> ShmString; | |
class MyShmAllocator : public ShmFactoryEntryAllocator { | |
public: | |
static boost::interprocess::managed_mapped_file::segment_manager *segmentManager; | |
MyShmAllocator() | |
: ShmFactoryEntryAllocator(segmentManager) { | |
} | |
}; | |
boost::interprocess::managed_mapped_file::segment_manager *MyShmAllocator::segmentManager = NULL; | |
// TODO: using ShmFactoryEntryAllocator does not work | |
typedef boost::flyweights::hashed_factory<boost::hash<ShmString>, std::equal_to<ShmString>, MyShmAllocator> ShmStringHashedFactory; | |
//typedef boost::flyweights::hashed_factory<boost::hash<ShmString>, std::equal_to<ShmString>, ShmFactoryEntryAllocator> ShmStringHashedFactory; | |
//typedef boost::flyweights::hashed_factory<boost::hash<ShmString>, std::equal_to<ShmString>, std::allocator<boost::mpl::_1> > ShmStringHashedFactory; | |
// TODO: need to be able to use a hashed_factory with our custom allocator. | |
typedef boost::flyweights::flyweight<ShmString, ShmStringHashedFactory> ShmFlyweightString; | |
//typedef boost::flyweights::flyweight<ShmString> ShmFlyweightString; | |
int main(int argc, char** argv) | |
{ | |
managed_mapped_file *segment = new managed_mapped_file(create_only, "memory.dat", 409600); | |
ShmFactoryEntryAllocator factoryEntryAllocator(segment->get_segment_manager()); | |
MyShmAllocator::segmentManager = segment->get_segment_manager(); | |
// create a normal string in shared-memory. | |
ShmString *ps1 = segment->construct<ShmString>("s1")("some shm normal string", factoryEntryAllocator); | |
// create a flyweight string in shared memory. | |
ShmFlyweightString *ps2 = segment->construct<ShmFlyweightString>(anonymous_instance)("some shm flyweight string", factoryEntryAllocator); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment