scc  2024.06
SystemC components library
tlm_gp_shared.h
1 /*******************************************************************************
2  * Copyright 2021-2022 MINRES Technologies GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************/
16 
17 #ifndef _SYSC_TLM_TLM_GP_SHARED_H_
18 #define _SYSC_TLM_TLM_GP_SHARED_H_
19 
20 #include <tlm>
21 
23 namespace tlm {
25 namespace scc {
26 
27 template <typename T> class tlm_payload_shared_ptr {
28  T* ptr{nullptr};
29 
30 public:
32  tlm_payload_shared_ptr() noexcept = default;
33 
35  inline tlm_payload_shared_ptr(T* p) noexcept
36  : ptr(p) {
37  if(ptr && ptr->has_mm())
38  ptr->acquire();
39  }
42  : ptr(p.ptr) {
43  if(ptr && ptr->has_mm())
44  ptr->acquire();
45  }
48  : ptr(std::move(p.ptr)) {
49  p.ptr = nullptr;
50  }
53  if(ptr && ptr->has_mm())
54  ptr->release();
55  }
58  if(ptr && ptr->has_mm())
59  ptr->release();
60  ptr = p.ptr;
61  if(ptr && ptr->has_mm())
62  ptr->acquire();
63  return *this;
64  }
67  if(ptr && ptr->has_mm())
68  ptr->release();
69  ptr = p.ptr;
70  p.ptr = nullptr;
71  return *this;
72  }
73 
76  if(ptr && ptr->has_mm())
77  ptr->release();
78  ptr = p;
79  if(ptr && ptr->has_mm())
80  ptr->acquire();
81  return *this;
82  }
83 
85  inline T& operator*() const noexcept { return *ptr; }
86 
88  inline T* operator->() const noexcept { return ptr; }
89 
91  inline T* get() const noexcept { return ptr; }
92 
93  inline operator bool() const noexcept { return ptr != nullptr; }
94 };
95 
96 template <typename T> inline std::ostream& operator<<(std::ostream& os, tlm_payload_shared_ptr<T> const& p) {
97  os << p.get();
98  return os;
99 }
100 
101 template <typename T> inline bool operator==(tlm_payload_shared_ptr<T> const& x, tlm_payload_shared_ptr<T> const& y) noexcept {
102  return x.get() == y.get();
103 }
104 
105 template <typename T> inline bool operator==(tlm_payload_shared_ptr<T> const& x, T* y) noexcept { return x.get() == y; }
106 
107 template <typename T> inline bool operator!=(tlm_payload_shared_ptr<T> const& x, tlm_payload_shared_ptr<T> const& y) noexcept {
108  return x.get() != y.get();
109 }
110 
111 using tlm_gp_shared_ptr = tlm_payload_shared_ptr<tlm::tlm_generic_payload>;
112 } // namespace scc
113 } // namespace tlm
114 #endif /* _SYSC_TLM_TLM_GP_SHARED_H_ */
tlm_payload_shared_ptr(tlm_payload_shared_ptr const &p) noexcept
Copy constructor.
Definition: tlm_gp_shared.h:41
T * get() const noexcept
Return the stored pointer.
Definition: tlm_gp_shared.h:91
tlm_payload_shared_ptr() noexcept=default
Default constructor, creates a unique_ptr that owns nothing.
tlm_payload_shared_ptr & operator=(tlm_payload_shared_ptr &&p) noexcept
Move assignment operator.
Definition: tlm_gp_shared.h:66
T * operator->() const noexcept
Return the stored pointer.
Definition: tlm_gp_shared.h:88
tlm_payload_shared_ptr & operator=(tlm_payload_shared_ptr const &p) noexcept
Copy assignment operator.
Definition: tlm_gp_shared.h:57
tlm_payload_shared_ptr & operator=(T *p) noexcept
raw pointer assignment operator.
Definition: tlm_gp_shared.h:75
tlm_payload_shared_ptr(tlm_payload_shared_ptr &&p) noexcept
Move constructor.ยด
Definition: tlm_gp_shared.h:47
T & operator*() const noexcept
Dereference the stored pointer.
Definition: tlm_gp_shared.h:85
SCC TLM utilities.
SystemC TLM.
Definition: cxs_tlm.h:69