scc  2022.4.0
SystemC components library
tlm_id.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 #pragma once
18 
19 #include "tlm_gp_shared.h"
20 #include <cstdint>
21 
23 namespace tlm {
25 namespace scc {
26 
27 struct tlm_id_extension : public tlm_extension<tlm_id_extension> {
28  virtual tlm_extension_base* clone() const {
29  tlm_id_extension* t = new tlm_id_extension(this->id);
30  return t;
31  }
32  virtual void copy_from(tlm_extension_base const& from) { id = static_cast<tlm_id_extension const&>(from).id; }
34  : tlm_id_extension(reinterpret_cast<uintptr_t>(i.get())) {}
35  tlm_id_extension(void* i)
36  : tlm_id_extension(reinterpret_cast<uintptr_t>(i)) {}
37  tlm_id_extension(uintptr_t i)
38  : id(i) {}
39  uintptr_t id;
40 };
41 
42 inline uintptr_t getId(tlm::tlm_generic_payload& gp) {
43  if(auto ext = gp.get_extension<tlm_id_extension>())
44  return ext->id;
45  else
46  return (uintptr_t)&gp;
47 }
48 
49 inline uintptr_t getId(tlm::tlm_generic_payload* gp) {
50  if(!gp)
51  return 0;
52  if(auto ext = gp->get_extension<tlm_id_extension>())
53  return ext->id;
54  else
55  return (uintptr_t)gp;
56 }
57 
58 inline void setId(tlm::tlm_generic_payload& gp, uintptr_t id) {
59  if(auto ext = gp.get_extension<tlm_id_extension>())
60  ext->id = id;
61  else if(gp.has_mm())
62  gp.set_auto_extension(new tlm_id_extension(id));
63  else
64  gp.set_extension(new tlm_id_extension(id));
65 }
66 } // namespace scc
67 } // namespace tlm
tlm::tlm_generic_payload * get() const noexcept
Return the stored pointer.
Definition: tlm_gp_shared.h:91
SCC SystemC utilities.
SystemC TLM.