C++ Device API Reference Manual
Reference documentation for the Simics C++ Device API.
 
Loading...
Searching...
No Matches
conf-class.h
Go to the documentation of this file.
1// -*- mode: C++; c-file-style: "virtutech-c++" -*-
2
3/*
4 © 2021 Intel Corporation
5
6 This software and the related documents are Intel copyrighted materials, and
7 your use of them is governed by the express license under which they were
8 provided to you ("License"). Unless the License provides otherwise, you may
9 not use, modify, copy, publish, distribute, disclose or transmit this software
10 or the related documents without Intel's prior written permission.
11
12 This software and the related documents are provided as is, with no express or
13 implied warranties, other than those that are expressly stated in the License.
14*/
15
16#ifndef SIMICS_CONF_CLASS_H
17#define SIMICS_CONF_CLASS_H
18
19#include <simics/base/conf-object.h> // class_kind_t
20
21#include <map>
22#include <memory> // unique_ptr
23#include <string>
24#include <vector>
25
26#include "simics/attribute.h"
27#include "simics/event.h"
28#include "simics/init-class.h"
30#if defined INTC_EXT
32#endif
33#include "simics/log.h" // LogGroups
35
36namespace simics {
37
38using ConfClassPtr = std::unique_ptr<ConfClass>;
39
52class ConfClass {
53 public:
65 const std::string &name,
66 const std::string &short_desc,
67 const std::string &description,
68 const class_kind_t kind,
69 const ObjectFactoryInterface &factory);
70
71#if defined INTC_EXT
74 const std::string &name,
75 const std::string &short_desc,
76 const std::string &description,
77 const class_kind_t kind,
78 const iface::ObjectFactoryInterface &factory);
79#endif
80
82 ConfClass(const ConfClass&) = delete;
83 ConfClass& operator=(const ConfClass&) = delete;
84
85 virtual ~ConfClass();
86
90 static uint64 getGroupId(conf_class_t *cls, const std::string &name);
91
95 operator conf_class_t*() const;
96
100 const std::string &name() const;
101
105 const std::string &description() const;
106
110 const std::vector<std::string> &log_groups() const;
111
122
130 ConfClass *add(const Attribute &attr);
132
145 ConfClass *add(const char * const *names);
146 ConfClass *add(const LogGroups &names);
147
160 ConfClass *add(ConfClass *port, const std::string &name);
161 ConfClass *add(const ConfClassPtr &port, const std::string &name);
162
171
172 protected:
174 explicit ConfClass(conf_class_t *cls, const std::string &name,
175 const std::string &description)
176 : cls_(cls), name_(name), description_(description) {}
177
178 private:
180 void register_log_groups() const noexcept;
181
183 void register_interfaces() noexcept;
184
185 conf_class_t *cls_ {nullptr};
186 std::string name_;
187 std::string description_;
188 std::vector<std::string> log_groups_;
189 std::map<std::string, const interface_t *> pending_interfaces_;
190};
191
194template <typename T>
195void decorate_class(...) {}
196
209template <typename T> ConfClassPtr
210make_class(const std::string &name, const std::string &short_desc,
211 const std::string &description,
212 const class_kind_t kind = Sim_Class_Kind_Vanilla) {
213 auto cls = ConfClass::createInstance(name, short_desc, description, kind,
215 detail::init_class<T>(cls.get());
216 decorate_class<T>(nullptr, cls.get());
217 return cls;
218}
219
233template <typename T, typename A> ConfClassPtr
234make_class(const std::string &name, const std::string &short_desc,
235 const std::string &description, A *constructor_arg,
236 const class_kind_t kind = Sim_Class_Kind_Vanilla) {
237 auto cls = ConfClass::createInstance(name, short_desc, description, kind,
239 constructor_arg));
240 detail::init_class<T>(cls.get());
241 decorate_class<T>(nullptr, cls.get());
242 return cls;
243}
244
245// Custom empty class used as a placeholder for "no additional argument"
246class None {};
247
273template <typename T, typename A = None>
275 // This type is defined (as int) only when A is None
276 template <typename U = A>
277 using EnableIfNone = std::enable_if_t<std::is_same<U, None>::value, int>;
278
279 // This type is defined (as int) only when A is not None
280 template <typename U = A>
281 using EnableIfNotNone = std::enable_if_t<!std::is_same<U, None>::value,
282 int>;
283
284 public:
285 // Constructor for classes without additional arguments
286 template <typename U = A, EnableIfNone<U> = 0> constexpr
287 RegisterClassWithSimics(const std::string &name,
288 const std::string &short_desc,
289 const std::string &description,
290 const class_kind_t kind = Sim_Class_Kind_Vanilla) {
291 make_class<T>(name, short_desc, description, kind);
292 }
293
294 // Constructor for classes with additional arguments
295 template <typename U = A, EnableIfNotNone<U> = 0> constexpr
296 RegisterClassWithSimics(const std::string &name,
297 const std::string &short_desc,
298 const std::string &description,
299 A *constructor_arg,
300 const class_kind_t kind = Sim_Class_Kind_Vanilla) {
301 make_class<T, A>(name, short_desc, description, constructor_arg, kind);
302 }
303};
304
305#define GROUP_ID(...) \
306 IMPL(GET_MACRO(__VA_ARGS__, GROUP_ID_TWO_ARGS, \
307 GROUP_ID_ONE_ARG)(__VA_ARGS__))
308
309#define GROUP_ID_ONE_ARG(NAME) \
310 simics::ConfClass::getGroupId(SIM_object_class(obj()), #NAME)
311
312#define GROUP_ID_TWO_ARGS(OBJ, NAME) \
313 simics::ConfClass::getGroupId(SIM_object_class(OBJ), #NAME)
314
315} // namespace simics
316
317#endif
Represents a Simics attribute.
Definition: attribute.h:79
Represents a Simics class attribute.
Definition: attribute.h:152
Represents Simics C type conf_class_t.
Definition: conf-class.h:52
static uint64 getGroupId(conf_class_t *cls, const std::string &name)
Return the ID of a log group.
ConfClass * add(ConfClass *port, const std::string &name)
A function to add a port object to the set of ports defined by the class.
ConfClass * add(const Attribute &attr)
A function to add an attribute to the set of attributes of ConfClass.
const std::string & name() const
Return the class name.
const std::string & description() const
Return the class description.
virtual ~ConfClass()
ConfClass(const ConfClass &)=delete
Avoid implicit copy.
ConfClass * add(EventInfo &&event)
A function to add an event to the set of events of ConfClass.
ConfClass * add(const ClassAttribute &attr)
static ConfClassPtr createInstance(const std::string &name, const std::string &short_desc, const std::string &description, const class_kind_t kind, const ObjectFactoryInterface &factory)
Factory function to create a ConfClass instance All parameters except the last one is used to call th...
ConfClass * add(const iface::InterfaceInfo &iface)
Stores the provided InterfaceInfo for later registration.
ConfClass(conf_class_t *cls, const std::string &name, const std::string &description)
Must use factory method to create instance.
Definition: conf-class.h:174
ConfClass * add(const ConfClassPtr &port, const std::string &name)
ConfClass * add(const LogGroups &names)
ConfClass * add(const char *const *names)
Functions to add log groups that an object can use to separate messages.
const std::vector< std::string > & log_groups() const
Return the class log groups.
ConfClass & operator=(const ConfClass &)=delete
Definition: conf-class.h:246
Interface for a factory pattern to create ConfObject instances.
Definition: object-factory-interface.h:31
A factory for creating instances of ConfObject-derived classes with an argument.
Definition: object-factory.h:78
A factory for creating instances of ConfObject-derived classes.
Definition: object-factory.h:35
Utility class for automatic registration of C++ classes with Simics.
Definition: conf-class.h:274
constexpr RegisterClassWithSimics(const std::string &name, const std::string &short_desc, const std::string &description, A *constructor_arg, const class_kind_t kind=Sim_Class_Kind_Vanilla)
Definition: conf-class.h:296
constexpr RegisterClassWithSimics(const std::string &name, const std::string &short_desc, const std::string &description, const class_kind_t kind=Sim_Class_Kind_Vanilla)
Definition: conf-class.h:287
Definition: interface-info.h:27
Definition: after-bank.h:33
std::initializer_list< std::string > LogGroups
Type used for log group names.
Definition: log.h:27
std::unique_ptr< ConfClass > ConfClassPtr
Definition: conf-class.h:38
void decorate_class(...)
Overload it with specific implementation by including the other header before this file.
Definition: conf-class.h:195
ConfClassPtr make_class(const std::string &name, const std::string &short_desc, const std::string &description, const class_kind_t kind=Sim_Class_Kind_Vanilla)
A factory method to create a ConfClassPtr which associate with the C++ class T.
Definition: conf-class.h:210
Definition: event.h:47