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
15class ShoppingList
16{
17 // A vector of strings using polymorphic allocators. Every element
18 // of the vector will use the same allocator as the vector itself.
19 boost::container::pmr::vector_of
20 <boost::container::pmr::string>::type m_strvec;
21 //Alternatively in compilers that support template aliases:
22 // boost::container::pmr::vector<boost::container::pmr::string> m_strvec;
23 public:
24
25 // This makes uses_allocator<ShoppingList, memory_resource*>::value true
26 typedef boost::container::pmr::memory_resource* allocator_type;
27
28 // If the allocator is not specified, "m_strvec" uses pmr::get_default_resource().
29 explicit ShoppingList(allocator_type alloc = 0)
30 : m_strvec(alloc) {}
31
32 // Copy constructor. As allocator is not specified,
33 // "m_strvec" uses pmr::get_default_resource().
34 ShoppingList(const ShoppingList& other)
35 : m_strvec(other.m_strvec) {}
36
37 // Copy construct using the given memory_resource.
38 ShoppingList(const ShoppingList& other, allocator_type a)
39 : m_strvec(other.m_strvec, a) {}
40
41 allocator_type get_allocator() const
42 { return m_strvec.get_allocator().resource(); }
43
44 void add_item(const char *item)
45 { m_strvec.emplace_back(args&: item); }
46
47 //...
48};
49
50//]]
51
52//[doc_pmr_main_cpp
53
54//=#include "ShoppingList.hpp"
55#include <cassert>
56#include <boost/container/pmr/list.hpp>
57#include <boost/container/pmr/monotonic_buffer_resource.hpp>
58
59void processShoppingList(const ShoppingList&)
60{ /**/ }
61
62int main()
63{
64 using namespace boost::container;
65 //All memory needed by folder and its contained objects will
66 //be allocated from the default memory resource (usually new/delete)
67 pmr::list_of<ShoppingList>::type folder; // Default allocator resource
68 //Alternatively in compilers that support template aliases:
69 // boost::container::pmr::list<ShoppingList> folder;
70 {
71 char buffer[1024];
72 pmr::monotonic_buffer_resource buf_rsrc(&buffer, 1024);
73
74 //All memory needed by temporaryShoppingList will be allocated
75 //from the local buffer (speeds up "processShoppingList")
76 ShoppingList temporaryShoppingList(&buf_rsrc);
77 assert(&buf_rsrc == temporaryShoppingList.get_allocator());
78
79 //list nodes, and strings "salt" and "pepper" will be allocated
80 //in the stack thanks to "monotonic_buffer_resource".
81 temporaryShoppingList.add_item(item: "salt");
82 temporaryShoppingList.add_item(item: "pepper");
83 //...
84
85 //All modifications and additions to "temporaryShoppingList"
86 //will use memory from "buffer" until it's exhausted.
87 processShoppingList(temporaryShoppingList);
88
89 //Processing done, now insert it in "folder",
90 //which uses the default memory resource
91 folder.push_back(x: temporaryShoppingList);
92 assert(pmr::get_default_resource() == folder.back().get_allocator());
93 //temporaryShoppingList, buf_rsrc, and buffer go out of scope
94 }
95 return 0;
96}
97
98//]
99

source code of boost/libs/container/example/doc_pmr.cpp