gem5 v24.0.0.0
Loading...
Searching...
No Matches
extensible.hh
Go to the documentation of this file.
1/*
2 * Copyright 2023 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/* @file
29 * Extensible Object Base Class Declaration
30 *
31 * This class can be used to add an "extension" field to packet/request which
32 * will be passed along with the original packet/request pointer. This allows
33 * developers to extend packet/request without modifying the original class.
34 */
35
36#ifndef __BASE_EXTENSIBLE_HH__
37#define __BASE_EXTENSIBLE_HH__
38
39#include <cassert>
40#include <list>
41#include <memory>
42#include <type_traits>
43#include <utility>
44
45namespace gem5
46{
47
52{
53 public:
54 explicit ExtensionBase(const unsigned int id)
55 : extID(id) {}
56
57 virtual ~ExtensionBase() = default;
58
59 virtual std::unique_ptr<ExtensionBase> clone() const = 0;
60
61 static unsigned int
63 {
64 static unsigned int max_num = 0;
65 return ++max_num;
66 }
67
68 unsigned int getExtensionID() const { return extID; }
69
70 private:
71 const unsigned int extID;
72};
73
106template <typename Target, typename T>
108{
109 public:
111
112 const static unsigned int extensionID;
113};
114
115template <typename Target, typename T>
116const unsigned int Extension<Target, T>::extensionID =
118
119template <typename Target>
121{
122 public:
123 Extensible() = default;
124 Extensible(const Extensible& other)
125 {
126 // Clone every extension from other.
127 for (auto& ext : other.extensions) {
128 extensions.emplace_back(ext->clone());
129 }
130 }
131 virtual ~Extensible() = default;
132
140 template <typename T>
141 void
142 setExtension(std::shared_ptr<T> ext)
143 {
144 static_assert(std::is_base_of<ExtensionBase, T>::value,
145 "Extension should inherit from ExtensionBase.");
146 assert(ext.get() != nullptr);
147
148 auto it = findExtension<T>();
149
150 if (it != extensions.end()) {
151 // There exists the same type of extension in the list.
152 // Replace it to the new one.
153 *it = std::move(ext);
154 } else {
155 // Add ext into the linked list.
156 extensions.emplace_back(std::move(ext));
157 }
158 }
159
165 template <typename T>
166 void
168 {
169 static_assert(std::is_base_of<ExtensionBase, T>::value,
170 "Extension should inherit from ExtensionBase.");
171
172 auto it = findExtension<T>();
173 if (it != extensions.end())
174 extensions.erase(it);
175 }
176
180 template <typename T>
181 std::shared_ptr<T>
183 {
184 static_assert(std::is_base_of<ExtensionBase, T>::value,
185 "Extension should inherit from ExtensionBase.");
186 auto it = findExtension<T>();
187 if (it == extensions.end())
188 return nullptr;
189 return std::static_pointer_cast<T>(*it);
190 }
191
192 protected:
193
201 template <typename T>
204 {
205 auto it = extensions.begin();
206 while (it != extensions.end()) {
207 if ((*it)->getExtensionID() == T::extensionID)
208 break;
209 it++;
210 }
211 return it;
212 }
213
214 // Linked list of extensions.
216};
217
218} // namespace gem5
219
220#endif //__BASE_EXTENSIBLE_HH__
Extensible()=default
Extensible(const Extensible &other)
void setExtension(std::shared_ptr< T > ext)
Set a new extension to the packet and replace the old one, if there already exists the same type of e...
virtual ~Extensible()=default
std::shared_ptr< T > getExtension()
Get the extension pointer by linear search with the extensionID.
std::list< std::shared_ptr< ExtensionBase > > extensions
std::list< std::shared_ptr< ExtensionBase > >::iterator findExtension()
Go through the extension list and return the iterator to the instance of the type of extension.
void removeExtension(void)
Remove the extension based on its type.
This is base of every extension.
Definition extensible.hh:52
ExtensionBase(const unsigned int id)
Definition extensible.hh:54
unsigned int getExtensionID() const
Definition extensible.hh:68
const unsigned int extID
Definition extensible.hh:71
virtual ~ExtensionBase()=default
virtual std::unique_ptr< ExtensionBase > clone() const =0
static unsigned int maxNumExtensions()
Definition extensible.hh:62
This is the extension for carrying additional information.
static const unsigned int extensionID
STL list class.
Definition stl.hh:51
Bitfield< 33 > id
Bitfield< 12 > ext
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0