115
Snapshotting
The C library requires manual snapshots before modifying data in a transaction. The
C++ bindings do all of the snapshotting automatically, to reduce the probability of
programmer error. The pmem::obj::p template wrapper class is the basic building block
for this mechanism. It is designed to work with basic types and not compound types
such as classes or PODs (Plain Old Data, structures with fields only and without any
object-oriented features). This is because it does not define operator->() and there is
no possibility to implement operator.(). The implementation of pmem::obj::p is based
on the operator=(). Each time the assignment operator is called, the value wrapped
by p will be changed, and the library needs to snapshot the old value. In addition to
snapshotting, the p<> template ensures the variable is persisted correctly, flushing data if
necessary. Listing 8-2 provides an example of using the p<> template.
Listing 8-2. Using the p<> template to persist values correctly
39
struct bad_example {
40
int some_int;
41
float some_float;
42
};
43
44
struct good_example {
45
pmem::obj::p pint;
46
pmem::obj::p pfloat;
47
};
48
49
struct root {
50
bad_example bad;
51
good_example good;
52
};
53
54
int main(int argc, char *argv[]) {
55
auto pop = pmem::obj::pool::open("/daxfs/file", "p");
56
57
auto r = pop.root();
58
Chapter 8 libpmemobj-Cpp: the adaptable language - C++ and persistent memory
Précédent

- 141/457

Suivant