C++ Device API Reference Manual
Reference documentation for the Simics C++ Device API.
 
Loading...
Searching...
No Matches
attribute-getter.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_DETAIL_ATTRIBUTE_GETTER_H
17#define SIMICS_DETAIL_ATTRIBUTE_GETTER_H
18
19#include "simics/attribute-traits.h" // std_to_attr
20#include "simics/conf-object.h" // from_obj
21
22namespace simics {
23namespace detail {
24
25template <typename T> struct attr_getter_helper;
26template <typename T, typename O> struct attr_getter_helper_dual;
27
28/*
29 * Returns a functor of type attr_value_t serving as a wrapper for a given
30 * pointer to a class member/method or a function takes a class as parameter.
31 *
32 * The macro uses simics::attr_getter_helper template, which implements
33 * wrapper functions for member functions with different signatures.
34 *
35 * Wrapper for these signatures uses simics::std_to_attr to convert a standard
36 * C++ type value into resulting attr_value_t. The conversion is not expected
37 * to fail.
38 */
39#define _G_SINGLE(func_ptr) \
40 simics::detail::attr_getter_helper<decltype(&func_ptr)>::\
41 template f<&func_ptr>
42#define _G_DUAL(cls, m) \
43 simics::detail::attr_getter_helper_dual<decltype(&cls::m), cls>::\
44 template f<&cls::m>
45
46// For class member function pointer
47template <typename O, typename T>
48struct attr_getter_helper<T (O::*)()> {
49 // Always fail. Use two arguments version instead
50 static_assert(sizeof(T) == -1,
51 "Pass class member pointer as two arguments to the MACRO:"
52 " cls and member");
53};
54
55template <typename O, typename T>
56struct attr_getter_helper<T (O::*)() const> {
57 // Always fail. Use two arguments version instead
58 static_assert(sizeof(T) == -1,
59 "Pass class member pointer as two arguments to the MACRO:"
60 " cls and member");
61};
62
63// When C differs than O, it means the getter is registered in the base
64// class O, while the attribute is registered with derived class C
65template <typename T, typename O, typename C>
66struct attr_getter_helper_dual<T (O::*)(), C> {
67 static_assert(std::is_base_of<O, C>::value,
68 "Type C should be same as or derived from type O");
69
70 template <T (O::*F)()>
71 static attr_value_t f(conf_object_t *obj) {
72 return std_to_attr((from_obj<C>(obj)->*F)());
73 }
74};
75
76template <typename T, typename O, typename C>
77struct attr_getter_helper_dual<T (O::*)() const, C> {
78 static_assert(std::is_base_of<O, C>::value,
79 "Type C should be same as or derived from type O");
80
81 template <T (O::*F)() const>
82 static attr_value_t f(conf_object_t *obj) {
83 return std_to_attr((from_obj<C>(obj)->*F)());
84 }
85};
86
87// For class member variable pointer
88template <typename T, typename O>
89struct attr_getter_helper<T O::*> {
90 // Always fail. Use two arguments version instead
91 static_assert(sizeof(T) == -1,
92 "Pass class member pointer as two arguments to the MACRO:"
93 " cls and member");
94};
95
96template <typename T, typename O, typename C>
97struct attr_getter_helper_dual<T O::*, C> {
98 static_assert(std::is_base_of<O, C>::value,
99 "Type C should be same as or derived from type O");
100
101 template <T O::*R>
102 static attr_value_t f(conf_object_t *obj) {
103 return std_to_attr(from_obj<C>(obj)->*R);
104 }
105};
106
107// For normal functions take an object reference
108template <typename O, typename T>
109struct attr_getter_helper<T& (*)(O&)> {
110 template <T& (*F)(O&)>
111 static attr_value_t f(conf_object_t *obj) {
112 return std_to_attr(F(*from_obj<O>(obj)));
113 }
114};
115
116template <typename O, typename T>
117struct attr_getter_helper<T (*)(O&)> {
118 template <T (*F)(O&)>
119 static attr_value_t f(conf_object_t *obj) {
120 return std_to_attr(F(*from_obj<O>(obj)));
121 }
122};
123
124} // namespace detail
125} // namespace simics
126
127#endif
Definition: after-bank.h:33
std::enable_if< std::is_enum< T >::value, attr_value_t >::type std_to_attr(const T &src)
Function transforms C++ enum type T to Simics attr_value_t.
Definition: attribute-traits.h:163
static attr_value_t f(conf_object_t *obj)
Definition: attribute-getter.h:111
static attr_value_t f(conf_object_t *obj)
Definition: attribute-getter.h:119
static attr_value_t f(conf_object_t *obj)
Definition: attribute-getter.h:102
static attr_value_t f(conf_object_t *obj)
Definition: attribute-getter.h:71
static attr_value_t f(conf_object_t *obj)
Definition: attribute-getter.h:82
Definition: attribute-getter.h:26
Definition: attribute-getter.h:25