memory.h Source File

CPP API: memory.h Source File
memory.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Daniel Abele
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MEMORY_H
21 #define MEMORY_H
22 
23 #include <utility>
24 
25 namespace mio
26 {
27 
35 template <typename T>
37 {
38 public:
40  : m_raw_ptr(p)
41  {
42  }
43 
44  observer_ptr(const observer_ptr& other) = default;
45 
49  void reset(T* p = nullptr)
50  {
51  m_raw_ptr = p;
52  }
53 
57  T* get() const
58  {
59  return m_raw_ptr;
60  }
61 
62  T* operator->() const
63  {
64  return m_raw_ptr;
65  }
66 
67  T& operator*() const
68  {
69  return *m_raw_ptr;
70  }
71 
72  operator bool() const
73  {
74  return m_raw_ptr != nullptr;
75  }
76 
77  bool operator==(const observer_ptr& other) const
78  {
79  return m_raw_ptr == other.m_raw_ptr;
80  }
81  bool operator!=(const observer_ptr& other) const
82  {
83  return !(*this == other);
84  }
85 
86 private:
88 };
89 
90 template <typename T>
92 {
93  return observer_ptr<T>(p);
94 }
95 
96 } // namespace mio
97 
98 #endif // MEMORY_H
A non-owning pointer.
Definition: memory.h:37
bool operator!=(const observer_ptr &other) const
Definition: memory.h:81
observer_ptr(const observer_ptr &other)=default
T * m_raw_ptr
Definition: memory.h:87
void reset(T *p=nullptr)
Replaces the watched object.
Definition: memory.h:49
observer_ptr(T *p)
Definition: memory.h:39
T * get() const
Returns a raw pointer to the watched object.
Definition: memory.h:57
T & operator*() const
Definition: memory.h:67
T * operator->() const
Definition: memory.h:62
bool operator==(const observer_ptr &other) const
Definition: memory.h:77
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
observer_ptr< T > make_observer(T *p)
Definition: memory.h:91