PMDK C++ bindings  1.10.1
This is the C++ bindings documentation for PMDK's libpmemobj.
temp_value.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2019, Intel Corporation */
3 
9 #ifndef LIBPMEMOBJ_CPP_TEMP_VALUE_HPP
10 #define LIBPMEMOBJ_CPP_TEMP_VALUE_HPP
11 
13 
14 namespace pmem
15 {
16 
17 namespace detail
18 {
19 
25 #ifndef LIBPMEMOBJ_CPP_MAX_STACK_ALLOC_SIZE
26 #define LIBPMEMOBJ_CPP_MAX_STACK_ALLOC_SIZE 64 * (1 << 10)
27 #endif
28 
34 template <typename T, bool NoExcept, typename Enable = void>
35 struct temp_value;
36 
42 template <typename T, bool NoExcept>
43 struct temp_value<
44  T, NoExcept,
45  typename std::enable_if<NoExcept &&
46  (sizeof(T) <
47  LIBPMEMOBJ_CPP_MAX_STACK_ALLOC_SIZE)>::type> {
48  template <typename... Args>
49  temp_value(Args &&... args) noexcept : t(std::forward<Args>(args)...)
50  {
51  }
52 
53  T &
54  get() noexcept
55  {
56  return t;
57  }
58 
59  T t;
60 };
61 
67 template <typename T, bool NoExcept>
68 struct temp_value<
69  T, NoExcept,
70  typename std::enable_if<!NoExcept ||
71  (sizeof(T) >=
72  LIBPMEMOBJ_CPP_MAX_STACK_ALLOC_SIZE)>::type> {
73  template <typename... Args>
74  temp_value(Args &&... args)
75  {
76  ptr = pmem::obj::make_persistent<T>(
77  std::forward<Args>(args)...);
78  }
79 
80  ~temp_value()
81  {
82  pmem::obj::delete_persistent<T>(ptr);
83  }
84 
85  T &
86  get()
87  {
88  return *ptr;
89  }
90 
92 };
93 
94 } /* namespace detail */
95 
96 } /* namespace pmem */
97 
98 #endif /* LIBPMEMOBJ_CPP_TEMP_VALUE_HPP */
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:15
make_persistent.hpp
Persistent_ptr transactional allocation functions for objects.
pmem::detail::temp_value
Template class for caching objects based on constructor's variadic template arguments and LIBPMEMOBJ_...
Definition: temp_value.hpp:35
pmem::obj::persistent_ptr
Persistent pointer class.
Definition: persistent_ptr.hpp:183
pmem::obj::get
T & get(pmem::obj::array< T, N > &a)
Non-member get function.
Definition: array.hpp:894