SIRIUS 7.5.0
Electronic structure library and applications
any_ptr.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Anton Kozhevnikov, Thomas Schulthess
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
5// the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8// following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10// and the following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
13// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
15// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20/** \file any_ptr.hpp
21 *
22 * \brief Implementation of pointer to any object.
23 */
24
25#ifndef __ANY_PTR_HPP__
26#define __ANY_PTR_HPP__
27
28#include <functional>
29
30namespace sirius {
31
32/// Handle deallocation of a poiniter to an object of any type.
33/** Example:
34 \code{.cpp}
35 class Foo
36 {
37 private:
38 int n_{0};
39 public:
40 Foo(int i) : n_(i)
41 {
42 std::cout << "in Foo() constructor\n";
43 }
44
45 ~Foo()
46 {
47 std::cout << "in Foo() destructor\n";
48 }
49
50 void print()
51 {
52 std::cout << "the number is: " << n_ << "\n";
53 }
54 };
55
56 int main(int argn, char** argv)
57 {
58 void* ptr = new any_ptr(new Foo(42));
59 auto& foo = static_cast<any_ptr*>(ptr)->get<Foo>();
60 foo.print();
61 delete static_cast<any_ptr*>(ptr);
62
63 return 0;
64 }
65 \endcode
66 And the output is:
67 \verbatim
68 in Foo() constructor
69 the number is: 42
70 in Foo() destructor
71 \endverbatim
72*/
74{
75 private:
76 /// Untyped pointer to a stored object.
77 void* ptr_;
78 /// Deleter for the stored object.
79 std::function<void(void*)> deleter_;
80
81 public:
82 /// Constructor.
83 template <typename T>
84 any_ptr(T* ptr__)
85 : ptr_(ptr__)
86 {
87 deleter_ = [](void* p) { delete static_cast<T*>(p); };
88 }
89 /// Destructor.
91 {
93 }
94 /// Cast pointer to a given type and return a reference.
95 template <typename T>
96 T& get() const
97 {
98 return *static_cast<T*>(ptr_);
99 }
100};
101
102} // namespace sirius
103
104#endif
Handle deallocation of a poiniter to an object of any type.
Definition: any_ptr.hpp:74
void * ptr_
Untyped pointer to a stored object.
Definition: any_ptr.hpp:77
any_ptr(T *ptr__)
Constructor.
Definition: any_ptr.hpp:84
std::function< void(void *)> deleter_
Deleter for the stored object.
Definition: any_ptr.hpp:79
T & get() const
Cast pointer to a given type and return a reference.
Definition: any_ptr.hpp:96
~any_ptr()
Destructor.
Definition: any_ptr.hpp:90
Namespace of the SIRIUS library.
Definition: sirius.f90:5