doc_pmr.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. //[doc_pmr_ShoppingList_hpp
  11. //ShoppingList.hpp
  12. #include <boost/container/pmr/vector.hpp>
  13. #include <boost/container/pmr/string.hpp>
  14. class ShoppingList
  15. {
  16. // A vector of strings using polymorphic allocators. Every element
  17. // of the vector will use the same allocator as the vector itself.
  18. boost::container::pmr::vector_of
  19. <boost::container::pmr::string>::type m_strvec;
  20. //Alternatively in compilers that support template aliases:
  21. // boost::container::pmr::vector<boost::container::pmr::string> m_strvec;
  22. public:
  23. // This makes uses_allocator<ShoppingList, memory_resource*>::value true
  24. typedef boost::container::pmr::memory_resource* allocator_type;
  25. // If the allocator is not specified, "m_strvec" uses pmr::get_default_resource().
  26. explicit ShoppingList(allocator_type alloc = 0)
  27. : m_strvec(alloc) {}
  28. // Copy constructor. As allocator is not specified,
  29. // "m_strvec" uses pmr::get_default_resource().
  30. ShoppingList(const ShoppingList& other)
  31. : m_strvec(other.m_strvec) {}
  32. // Copy construct using the given memory_resource.
  33. ShoppingList(const ShoppingList& other, allocator_type a)
  34. : m_strvec(other.m_strvec, a) {}
  35. allocator_type get_allocator() const
  36. { return m_strvec.get_allocator().resource(); }
  37. void add_item(const char *item)
  38. { m_strvec.emplace_back(item); }
  39. //...
  40. };
  41. //]]
  42. //[doc_pmr_main_cpp
  43. //=#include "ShoppingList.hpp"
  44. #include <cassert>
  45. #include <boost/container/pmr/list.hpp>
  46. #include <boost/container/pmr/monotonic_buffer_resource.hpp>
  47. void processShoppingList(const ShoppingList&)
  48. { /**/ }
  49. int main()
  50. {
  51. using namespace boost::container;
  52. //All memory needed by folder and its contained objects will
  53. //be allocated from the default memory resource (usually new/delete)
  54. pmr::list_of<ShoppingList>::type folder; // Default allocator resource
  55. //Alternatively in compilers that support template aliases:
  56. // boost::container::pmr::list<ShoppingList> folder;
  57. {
  58. char buffer[1024];
  59. pmr::monotonic_buffer_resource buf_rsrc(&buffer, 1024);
  60. //All memory needed by temporaryShoppingList will be allocated
  61. //from the local buffer (speeds up "processShoppingList")
  62. ShoppingList temporaryShoppingList(&buf_rsrc);
  63. assert(&buf_rsrc == temporaryShoppingList.get_allocator());
  64. //list nodes, and strings "salt" and "pepper" will be allocated
  65. //in the stack thanks to "monotonic_buffer_resource".
  66. temporaryShoppingList.add_item("salt");
  67. temporaryShoppingList.add_item("pepper");
  68. //...
  69. //All modifications and additions to "temporaryShoppingList"
  70. //will use memory from "buffer" until it's exhausted.
  71. processShoppingList(temporaryShoppingList);
  72. //Processing done, now insert it in "folder",
  73. //which uses the default memory resource
  74. folder.push_back(temporaryShoppingList);
  75. assert(pmr::get_default_resource() == folder.back().get_allocator());
  76. //temporaryShoppingList, buf_rsrc, and buffer go out of scope
  77. }
  78. return 0;
  79. }
  80. //]