QMCPACK
ResourceHandle.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2022 QMCPACK developers.
6 //
7 // File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef QMCPLUSPLUS_RESOURCHANDLE_H
13 #define QMCPLUSPLUS_RESOURCHANDLE_H
14 
15 #include <functional>
16 #include <optional>
17 
18 namespace qmcplusplus
19 {
20 /** ResourceHandle manages the temporary resource referenced from a collection
21  */
22 template<class RS>
23 class ResourceHandle : private std::optional<std::reference_wrapper<RS>>
24 {
25 public:
26  using Base = std::optional<std::reference_wrapper<RS>>;
27 
28  ResourceHandle() = default;
29  ResourceHandle(RS& res) { Base::emplace(res); }
30 
31  bool hasResource() const { return Base::has_value(); }
32  operator bool() const { return Base::has_value(); }
33 
34  RS& getResource() { return Base::value(); }
35  const RS& getResource() const { return Base::value(); }
36 
37  operator RS&() { return this->value(); }
38  operator const RS&() const { return Base::value(); }
39 
40  RS& release()
41  {
42  RS& res = Base::value();
43  Base::reset();
44  return res;
45  }
46 };
47 
48 } // namespace qmcplusplus
49 #endif
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
ResourceHandle manages the temporary resource referenced from a collection.
const RS & getResource() const
std::optional< std::reference_wrapper< qmcplusplus::BareKineticEnergy::MultiWalkerResource > > Base