serialization.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Boost.MultiIndex example of serialization of a MRU list.
  2. *
  3. * Copyright 2003-2008 Joaquin M Lopez Munoz.
  4. * Distributed under the Boost Software License, Version 1.0.
  5. * (See accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * See http://www.boost.org/libs/multi_index for library home page.
  9. */
  10. #if !defined(NDEBUG)
  11. #define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
  12. #define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
  13. #endif
  14. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  15. #include <algorithm>
  16. #include <boost/archive/text_oarchive.hpp>
  17. #include <boost/archive/text_iarchive.hpp>
  18. #include <boost/multi_index_container.hpp>
  19. #include <boost/multi_index/hashed_index.hpp>
  20. #include <boost/multi_index/identity.hpp>
  21. #include <boost/multi_index/sequenced_index.hpp>
  22. #include <fstream>
  23. #include <iostream>
  24. #include <iterator>
  25. #include <sstream>
  26. #include <string>
  27. using namespace boost::multi_index;
  28. /* An MRU (most recently used) list keeps record of the last n
  29. * inserted items, listing first the newer ones. Care has to be
  30. * taken when a duplicate item is inserted: instead of letting it
  31. * appear twice, the MRU list relocates it to the first position.
  32. */
  33. template <typename Item>
  34. class mru_list
  35. {
  36. typedef multi_index_container<
  37. Item,
  38. indexed_by<
  39. sequenced<>,
  40. hashed_unique<identity<Item> >
  41. >
  42. > item_list;
  43. public:
  44. typedef Item item_type;
  45. typedef typename item_list::iterator iterator;
  46. mru_list(std::size_t max_num_items_):max_num_items(max_num_items_){}
  47. void insert(const item_type& item)
  48. {
  49. std::pair<iterator,bool> p=il.push_front(item);
  50. if(!p.second){ /* duplicate item */
  51. il.relocate(il.begin(),p.first); /* put in front */
  52. }
  53. else if(il.size()>max_num_items){ /* keep the length <= max_num_items */
  54. il.pop_back();
  55. }
  56. }
  57. iterator begin(){return il.begin();}
  58. iterator end(){return il.end();}
  59. /* Utilities to save and load the MRU list, internally
  60. * based on Boost.Serialization.
  61. */
  62. void save_to_file(const char* file_name)const
  63. {
  64. std::ofstream ofs(file_name);
  65. boost::archive::text_oarchive oa(ofs);
  66. oa<<boost::serialization::make_nvp("mru",*this);
  67. }
  68. void load_from_file(const char* file_name)
  69. {
  70. std::ifstream ifs(file_name);
  71. if(ifs){
  72. boost::archive::text_iarchive ia(ifs);
  73. ia>>boost::serialization::make_nvp("mru",*this);
  74. }
  75. }
  76. private:
  77. item_list il;
  78. std::size_t max_num_items;
  79. /* serialization support */
  80. friend class boost::serialization::access;
  81. template<class Archive>
  82. void serialize(Archive& ar,const unsigned int)
  83. {
  84. ar&BOOST_SERIALIZATION_NVP(il);
  85. ar&BOOST_SERIALIZATION_NVP(max_num_items);
  86. }
  87. };
  88. int main()
  89. {
  90. const char* mru_store="mru_store";
  91. /* Construct a MRU limited to 10 items and retrieve its
  92. * previous contents.
  93. */
  94. mru_list<std::string> mru(10);
  95. mru.load_from_file(mru_store);
  96. /* main loop */
  97. for(;;){
  98. std::cout<<"enter a term: ";
  99. std::string line;
  100. std::getline(std::cin,line);
  101. if(line.empty())break;
  102. std::string term;
  103. std::istringstream iss(line);
  104. iss>>term;
  105. if(term.empty())break;
  106. mru.insert(term);
  107. std::cout<<"most recently entered terms:"<<std::endl;
  108. std::copy(
  109. mru.begin(),mru.end(),
  110. std::ostream_iterator<std::string>(std::cout,"\n"));
  111. }
  112. /* persist the MRU list */
  113. mru.save_to_file(mru_store);
  114. return 0;
  115. }