gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
45 namespace 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 
106 template <typename Target, typename T>
107 class Extension : public ExtensionBase
108 {
109  public:
111 
112  const static unsigned int extensionID;
113 };
114 
115 template <typename Target, typename T>
116 const unsigned int Extension<Target, T>::extensionID =
118 
119 template <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__
gem5::Extensible::findExtension
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.
Definition: extensible.hh:203
gem5::ExtensionBase::getExtensionID
unsigned int getExtensionID() const
Definition: extensible.hh:68
gem5::Extensible::Extensible
Extensible()=default
gem5::Extensible
Definition: extensible.hh:120
gem5::ExtensionBase::clone
virtual std::unique_ptr< ExtensionBase > clone() const =0
gem5::ExtensionBase::extID
const unsigned int extID
Definition: extensible.hh:71
gem5::Extensible::~Extensible
virtual ~Extensible()=default
gem5::Extension::Extension
Extension()
Definition: extensible.hh:110
gem5::ExtensionBase
This is base of every extension.
Definition: extensible.hh:51
gem5::ExtensionBase::ExtensionBase
ExtensionBase(const unsigned int id)
Definition: extensible.hh:54
gem5::ExtensionBase::~ExtensionBase
virtual ~ExtensionBase()=default
gem5::Extensible::setExtension
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...
Definition: extensible.hh:142
gem5::ExtensionBase::maxNumExtensions
static unsigned int maxNumExtensions()
Definition: extensible.hh:62
gem5::Extensible::extensions
std::list< std::shared_ptr< ExtensionBase > > extensions
Definition: extensible.hh:215
gem5::ArmISA::ext
Bitfield< 12 > ext
Definition: misc_types.hh:485
gem5::Extension::extensionID
const static unsigned int extensionID
Definition: extensible.hh:112
gem5::Extensible::Extensible
Extensible(const Extensible &other)
Definition: extensible.hh:124
gem5::Extensible::getExtension
std::shared_ptr< T > getExtension()
Get the extension pointer by linear search with the extensionID.
Definition: extensible.hh:182
gem5::Extensible::removeExtension
void removeExtension(void)
Remove the extension based on its type.
Definition: extensible.hh:167
gem5::ArmISA::id
Bitfield< 33 > id
Definition: misc_types.hh:305
gem5::Extension
This is the extension for carrying additional information.
Definition: extensible.hh:107
std::list
STL list class.
Definition: stl.hh:51
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37

Generated on Sun Jul 30 2023 01:56:51 for gem5 by doxygen 1.8.17