PMDK C++ bindings  1.13.0-git107.g7e59f08f
This is the C++ bindings documentation for PMDK's libpmemobj.
pmem::obj::p< T > Class Template Reference

Resides on pmem class. More...

#include <libpmemobj++/p.hpp>

Public Member Functions

 p (const T &_val) noexcept
 Value constructor. More...
 
 p ()=default
 Defaulted constructor.
 
poperator= (const p &rhs)
 Assignment operator. More...
 
template<typename Y , typename = typename std::enable_if< std::is_convertible<Y, T>::value>::type>
poperator= (const p< Y > &rhs)
 Converting assignment operator from a different p<>. More...
 
 operator T () const noexcept
 Conversion operator back to the underlying type.
 
T & get_rw ()
 Retrieves read-write reference of the object. More...
 
const T & get_ro () const noexcept
 Retrieves read-only const reference of the object. More...
 
void swap (p &other)
 Swaps two p objects of the same type. More...
 

Related Functions

(Note that these are not member functions.)

template<class T >
void swap (p< T > &a, p< T > &b)
 Swaps two p objects of the same type. More...
 
template<typename T >
std::ostream & operator<< (std::ostream &os, const p< T > &pp)
 Ostream operator overload.
 
template<typename T >
std::istream & operator>> (std::istream &is, p< T > &pp)
 Istream operator overload.
 
template<typename T >
p< T > & operator++ (p< T > &pp)
 Prefix increment operator overload.
 
template<typename T >
p< T > & operator-- (p< T > &pp)
 Prefix decrement operator overload.
 
template<typename T >
p< T > operator++ (p< T > &pp, int)
 Postfix increment operator overload.
 
template<typename T >
p< T > operator-- (p< T > &pp, int)
 Postfix decrement operator overload.
 
template<typename T , typename Y >
p< T > & operator+= (p< T > &lhs, const p< Y > &rhs)
 Addition assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator+= (p< T > &lhs, const Y &rhs)
 Addition assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator-= (p< T > &lhs, const p< Y > &rhs)
 Subtraction assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator-= (p< T > &lhs, const Y &rhs)
 Subtraction assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator*= (p< T > &lhs, const p< Y > &rhs)
 Multiplication assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator*= (p< T > &lhs, const Y &rhs)
 Multiplication assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator/= (p< T > &lhs, const p< Y > &rhs)
 Division assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator/= (p< T > &lhs, const Y &rhs)
 Division assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator%= (p< T > &lhs, const p< Y > &rhs)
 Modulo assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator%= (p< T > &lhs, const Y &rhs)
 Modulo assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator&= (p< T > &lhs, const p< Y > &rhs)
 Bitwise AND assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator&= (p< T > &lhs, const Y &rhs)
 Bitwise AND assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator|= (p< T > &lhs, const p< Y > &rhs)
 Bitwise OR assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator|= (p< T > &lhs, const Y &rhs)
 Bitwise OR assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator^= (p< T > &lhs, const p< Y > &rhs)
 Bitwise XOR assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator^= (p< T > &lhs, const Y &rhs)
 Bitwise XOR assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator<<= (p< T > &lhs, const p< Y > &rhs)
 Bitwise left shift assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator<<= (p< T > &lhs, const Y &rhs)
 Bitwise left shift assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator>>= (p< T > &lhs, const p< Y > &rhs)
 Bitwise right shift assignment operator overload.
 
template<typename T , typename Y >
p< T > & operator>>= (p< T > &lhs, const Y &rhs)
 Bitwise right shift assignment operator overload.
 

Detailed Description

template<typename T>
class pmem::obj::p< T >

Resides on pmem class.

p class is a property-like template class that has to be used for all variables (excluding persistent pointers), which are used in pmemobj transactions. The p property makes sure that changes to a variable within a transaction are made atomically with respect to persistence. It does it by creating a snapshot of the variable when modified in the transaction scope. The p class is not designed to be used with compound types. For that see the persistent_ptr.

#include <fcntl.h>
using namespace pmem::obj;
void
p_property_example()
{
struct compound_type {
void
set_some_variable(int val)
{
some_variable = val;
}
int some_variable;
double some_other_variable;
};
/* pool root structure */
static struct root {
p<int> counter; /* this is OK */
p<compound_type> whoops; /* this is hard to use */
} proot;
/* create a pmemobj pool */
auto pop = pool<root>::create("poolfile", "layout", PMEMOBJ_MIN_POOL);
/* typical usage schemes */
transaction::run(pop, [&] {
proot.counter = 12; /* atomic */
/* one way to change `whoops` */
proot.whoops.get_rw().set_some_variable(2);
proot.whoops.get_rw().some_other_variable = 3.0;
});
/* Changing a p<> variable outside of a transaction is a volatile
* modification. No way to ensure persistence in case of power failure.
*/
proot.counter = 12;
}
static void run(obj::pool_base &pool, std::function< void()> tx, Locks &... locks)
Execute a closure-like transaction and lock locks.
Definition: transaction.hpp:676
static pool< T > create(const std::string &path, const std::string &layout, std::size_t size=PMEMOBJ_MIN_POOL, mode_t mode=DEFAULT_MODE)
Creates a new transactional object store pool.
Definition: pool.hpp:694
Main libpmemobj namespace.
Definition: allocation_flag.hpp:18
Resides on pmem property template.
C++ pmemobj pool.
C++ pmemobj transactions.

Constructor & Destructor Documentation

◆ p()

template<typename T >
pmem::obj::p< T >::p ( const T &  _val)
inlinenoexcept

Value constructor.

Directly assigns a value to the underlying storage.

Parameters
_valconst reference to the value to be assigned.

Member Function Documentation

◆ get_ro()

template<typename T >
const T& pmem::obj::p< T >::get_ro ( ) const
inlinenoexcept

Retrieves read-only const reference of the object.

This method has no transaction side effects.

Returns
a const reference to the object.

◆ get_rw()

template<typename T >
T& pmem::obj::p< T >::get_rw ( )
inline

Retrieves read-write reference of the object.

The entire object is automatically added to the transaction.

Returns
a reference to the object.
Exceptions
pmem::transaction_errorwhen adding the object to the transaction failed.

◆ operator=() [1/2]

template<typename T >
p& pmem::obj::p< T >::operator= ( const p< T > &  rhs)
inline

Assignment operator.

The p<> class property assignment within a transaction automatically registers this operation so that a rollback is possible.

Exceptions
pmem::transaction_errorwhen adding the object to the transaction failed.

◆ operator=() [2/2]

template<typename T >
template<typename Y , typename = typename std::enable_if< std::is_convertible<Y, T>::value>::type>
p& pmem::obj::p< T >::operator= ( const p< Y > &  rhs)
inline

Converting assignment operator from a different p<>.

Available only for convertible types. Just like regular assignment, also automatically registers itself in a transaction.

Exceptions
pmem::transaction_errorwhen adding the object to the transaction failed.

◆ swap()

template<typename T >
void pmem::obj::p< T >::swap ( p< T > &  other)
inline

Swaps two p objects of the same type.

Exceptions
pmem::transaction_errorwhen adding the object to the transaction failed.

Friends And Related Function Documentation

◆ swap()

template<class T >
void swap ( p< T > &  a,
p< T > &  b 
)
related

Swaps two p objects of the same type.

Non-member swap function as required by Swappable concept. en.cppreference.com/w/cpp/concept/Swappable


The documentation for this class was generated from the following files: