Created
September 6, 2012 14:35
-
-
Save dprokoptsev/3656916 to your computer and use it in GitHub Desktop.
mms usage sample
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
// Declare a type | |
template<class StoragePolicy> | |
struct My { | |
int i; | |
mms::string<StoragePolicy> str; | |
mms::map<StoragePolicy, mms::string<StoragePolicy>, int> map; | |
// Field introspection | |
template<class Action> | |
void traverseFields(Action a) const { a(i)(str)(map); } | |
}; | |
// Create and populate a value | |
My<mms::Standalone> my; | |
my.i = 22; | |
my.str = "a string"; | |
my.map.insert(std::make_pair("ten", 10)); | |
my.map.insert(std::make_pair("eleven", 11)); | |
my.map.insert(std::make_pair("twelve", 12)); | |
// Serialize it | |
std::ofstream out("filename"); | |
size_t pos = mms::write(out, my); | |
// mmap(2) it | |
const char* ptr = mmap(...); | |
// Just cast the address and you get a pointer to mmapped type | |
const My<mms::Mmapped>* pmy = | |
reinterpret_cast<const My<mms::Mmapped>*>(ptr + pos); | |
// Read mmapped value (probably in another program, | |
// on another machine, etc...) | |
// Mmapped containers resemble STL-like interface | |
std::cout << pmy->i << std::endl; | |
std::cout << pmy->str << std::endl; | |
std::cout << pmy->map.size() << std::endl; | |
std::cout << pmy->map["twelve"] << std::endl; | |
BOOST_FOREACH(const auto& kv, pmy->map) | |
std::cout << "map[" << kv.first << "] = " << kv.second << std::endl; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment