PMDK C++ bindings  1.13.0-git107.g7e59f08f
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-2021, 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 
37 /*
38  * Specialization for non-throwing constructors and objects smaller than
39  * LIBPMEMOBJ_CPP_MAX_STACK_ALLOC_SIZE bytes. Constructs and stores value in
40  * underlying field.
41  */
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 
62 /*
63  * Specialization for throwing constructors or objects greater than or equal to
64  * LIBPMEMOBJ_CPP_MAX_STACK_ALLOC_SIZE bytes. Constructs and stores value in
65  * underlying field in pmem.
66  */
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 */
Persistent pointer class.
Definition: persistent_ptr.hpp:153
persistent_ptr transactional allocation functions for objects.
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Template class for caching objects based on constructor's variadic template arguments and LIBPMEMOBJ_...
Definition: temp_value.hpp:35