Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit 2bc7289

Browse files
authored
Merge pull request #1032 from karczex/pool_example
Add example for using pool as a class member
2 parents 4cb0a28 + 58a11eb commit 2bc7289

4 files changed

Lines changed: 106 additions & 1 deletion

File tree

examples/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ add_example(concurrent_hash_map_string concurrent_hash_map/concurrent_hash_map_s
224224

225225
add_example(pool pool/pool.cpp)
226226

227+
add_example(pool_as_class_member pool/pool_as_class_member.cpp)
228+
227229
add_example(mutex mutex/mutex.cpp)
228230

229231
add_example(make_persistent make_persistent/make_persistent.cpp)

examples/pool/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-License-Identifier: BSD-3-Clause
2-
# Copyright 2020, Intel Corporation
2+
# Copyright 2020-2021, Intel Corporation
33

44
cmake_minimum_required(VERSION 3.4)
55
project(pool CXX)
@@ -21,6 +21,10 @@ endif()
2121

2222
link_directories(${LIBPMEMOBJ++_LIBRARY_DIRS})
2323

24+
add_executable(pool_as_class_member pool_as_class_member.cpp)
25+
target_include_directories(pool_as_class_member PUBLIC ${LIBPMEMOBJ++_INCLUDE_DIRS} . ..)
26+
target_link_libraries(pool_as_class_member ${LIBPMEMOBJ++_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
27+
2428
add_executable(pool pool.cpp)
2529
target_include_directories(pool PUBLIC ${LIBPMEMOBJ++_INCLUDE_DIRS} . ..)
2630
target_link_libraries(pool ${LIBPMEMOBJ++_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
/* Copyright 2021, Intel Corporation */
3+
4+
/*
5+
* pool_as_class_member.cpp -- Example shows how to manage pool using RAII
6+
* idiom.
7+
*/
8+
9+
#include <iostream>
10+
//! [pool_class_member_example]
11+
#include <libpmemobj++/p.hpp>
12+
#include <libpmemobj++/persistent_ptr.hpp>
13+
#include <libpmemobj++/pool.hpp>
14+
15+
using namespace pmem::obj;
16+
17+
class Foo {
18+
private:
19+
/* pool root structure */
20+
struct persistent_data {
21+
p<int> some_variable;
22+
};
23+
24+
using pool_type = pmem::obj::pool<persistent_data>;
25+
/* pool object */
26+
pool_type pop;
27+
28+
const char *layout = "pool_layout";
29+
30+
public:
31+
Foo(const char *poolfile_path)
32+
{
33+
/* Create a pmemobj pool. */
34+
if (pool_type::check(poolfile_path, layout) == 1) {
35+
pop = pool_type::open(poolfile_path, layout);
36+
} else {
37+
std::cerr << "Cannot open pool" << std::endl
38+
<< "Trying to create a new one " << std::endl;
39+
pop = pool_type::create("poolfile", layout,
40+
PMEMOBJ_MIN_POOL);
41+
}
42+
}
43+
44+
Foo() = delete;
45+
46+
~Foo()
47+
{
48+
/* Close a pmemobj pool */
49+
pop.close();
50+
}
51+
52+
void
53+
set(int variable)
54+
{
55+
auto root_obj = pop.root();
56+
root_obj->some_variable = variable;
57+
pop.persist(root_obj->some_variable);
58+
}
59+
60+
void
61+
increment()
62+
{
63+
set(pop.root()->some_variable + 1);
64+
}
65+
66+
void
67+
print()
68+
{
69+
std::cout << pop.root()->some_variable << std::endl;
70+
}
71+
};
72+
73+
void
74+
pool_example()
75+
{
76+
auto foo = Foo("poolfile");
77+
foo.print();
78+
foo.set(42);
79+
foo.print();
80+
foo.increment();
81+
foo.print();
82+
}
83+
//! [pool_class_member_example]
84+
85+
int
86+
main()
87+
{
88+
try {
89+
pool_example();
90+
} catch (const std::exception &e) {
91+
std::cerr << "Exception " << e.what() << std::endl;
92+
return -1;
93+
}
94+
95+
return 0;
96+
}

include/libpmemobj++/pool.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class persistent_ptr;
4343
* where providing pool template argument is undesirable. The typical usage
4444
* example would be:
4545
* @snippet pool/pool.cpp pool_base_example
46+
*
47+
* The exmple of using pool with RAII idiom:
48+
* @snippet pool/pool_as_class_member.cpp pool_class_member_example
4649
*/
4750
class pool_base {
4851
public:

0 commit comments

Comments
 (0)