16#ifndef SIMICS_ATTRIBUTE_TRAITS_H
17#define SIMICS_ATTRIBUTE_TRAITS_H
19#include <simics/base/attr-value.h>
39 static attr_value_t
f(
const T &src);
44 static T
f(
const attr_value_t &src);
53 return SIM_make_attr_object(src);
55 attr_value_t ret = SIM_alloc_attr_list(2);
56 SIM_attr_list_set_item(&ret, 0, SIM_make_attr_object(src));
57 attr_value_t
string = SIM_make_attr_string(
59 SIM_attr_list_set_item(&ret, 1,
string);
63 return SIM_make_attr_nil();
71 if (SIM_attr_is_list(src)) {
72 if (SIM_attr_list_size(src) != 2) {
74 "Expected Simics list type with exactly 2 members"
78 auto item = SIM_attr_list_item(src, 0);
79 if (!SIM_attr_is_object(item)) {
81 "The first item should be Simics object type"
86 item = SIM_attr_list_item(src, 1);
87 if (!SIM_attr_is_string(item)) {
89 "The second item should be Simics string type"
95 if (!SIM_attr_is_object(src) && !SIM_attr_is_nil(src)) {
97 "Expected Simics object or NIL type"
100 return SIM_attr_object_or_nil(src);
109template <
typename T>
inline
110typename std::enable_if<std::is_enum<T>::value, attr_value_t>::type
113 static_cast<std::underlying_type_t<T>
>(src));
119template <
typename T>
inline
120typename std::enable_if<std::is_base_of<ConnectBase, T>::value,
129template <
typename T>
inline
130typename std::enable_if<!std::is_enum<T>::value
131 && !std::is_base_of<ConnectBase, T>::value,
140template <
typename T>
inline
141typename std::enable_if<std::is_enum<T>::value, T>::type
143 return static_cast<T
>(
150template <
typename T>
inline
151typename std::enable_if<std::is_base_of<ConnectBase, T>::value, T>::type
155 throw std::runtime_error {
156 "Unable to set to an illegal object"
165template <
typename T>
inline
166typename std::enable_if<!std::is_enum<T>::value
167 && !std::is_base_of<ConnectBase, T>::value,
176#define _STD2ATTR_INT_HELPER(type) \
178 struct attr_from_std_helper<type> { \
179 static attr_value_t f(const type &src) { \
180 return SIM_make_attr_int64(src); \
183#define _STD2ATTR_UINT_HELPER(type) \
185 struct attr_from_std_helper<type> { \
186 static attr_value_t f(const type &src) { \
187 return SIM_make_attr_uint64(src); \
201#undef _STD2ATTR_INT_HELPER
202#undef _STD2ATTR_UINT_HELPER
204#define _ATTR2STD_INT_HELPER(type) \
206 struct attr_to_std_helper<type> { \
207 static type f(const attr_value_t &src) { \
208 if (!SIM_attr_is_integer(src)) { \
209 throw SetIllegalType { \
210 "Expected Simics integer type" \
213 intmax_t i = SIM_attr_integer(src); \
214 if (i > (std::numeric_limits<type>::max)() \
215 || i < (std::numeric_limits<type>::min)()) { \
216 throw SetIllegalValue { \
217 "Value does not fit in type" \
224#define _ATTR2STD_UINT_HELPER(type) \
226 struct attr_to_std_helper<type> { \
227 static type f(const attr_value_t &src) { \
228 if (!SIM_attr_is_integer(src)) { \
229 throw SetIllegalType { \
230 "Expected Simics integer type" \
233 uintmax_t i = SIM_attr_integer(src); \
234 if (sizeof(type) != 8 \
235 && i > (std::numeric_limits<type>::max)()) { \
236 throw SetIllegalValue { \
237 "Value does not fit in type" \
254#undef _ATTR2STD_INT_HELPER
255#undef _ATTR2STD_UINT_HELPER
259#define _STD2ATTR_FLOAT_HELPER(type) \
261 struct attr_from_std_helper<type> { \
262 static attr_value_t f(const type &src) { \
263 return SIM_make_attr_floating(src); \
268#undef _STD2ATTR_FLOAT_HELPER
270#define _ATTR2STD_FLOAT_HELPER(type) \
272 struct attr_to_std_helper<type> { \
273 static type f(const attr_value_t &src) { \
274 if (!SIM_attr_is_floating(src)) { \
275 throw SetIllegalType { \
276 "Expected Simics floating type" \
279 return (type) SIM_attr_floating(src); \
284#undef _ATTR2STD_FLOAT_HELPER
290 static attr_value_t
f(
const std::string &src) {
291 return SIM_make_attr_string(src.c_str());
297 static std::string
f(
const attr_value_t &src) {
298 if (!SIM_attr_is_string(src)) {
300 "Expected Simics string type"
303 return SIM_attr_string(src);
309 static attr_value_t
f(
const char *src) {
310 return SIM_make_attr_string(src);
316 static const char *
f(
const attr_value_t &src) {
317 if (SIM_attr_is_nil(src)) {
320 if (!SIM_attr_is_string(src)) {
322 "Expected Simics string type"
325 return SIM_attr_string(src);
334 static attr_value_t
f(
const bool &src) {
335 return SIM_make_attr_boolean(src);
341 static bool f(
const attr_value_t &src) {
342 if (!SIM_attr_is_boolean(src)) {
344 "Expected Simics boolean type"
347 return SIM_attr_boolean(src);
355 static attr_value_t
f(
const attr_value_t &src) {
362 static attr_value_t
f(
const attr_value_t &src) {
369template <
typename C,
typename T =
typename C::value_type>
371 auto it = src.cbegin();
372 const size_t size = src.size();
373 attr_value_t dst = SIM_alloc_attr_list(size);
374 for (
unsigned i = 0; i < size; ++i, ++it) {
380template <
typename V, std::
size_t N>
382 static attr_value_t
f(
const std::array<V, N> &src) {
383 return from_container<std::array<V, N>>(src);
389 static attr_value_t
f(
const std::vector<V> &src) {
390 return from_container<std::vector<V>>(src);
396 static attr_value_t
f(
const std::list<V> &src) {
397 return from_container<std::list<V>>(src);
403 static attr_value_t
f(
const std::deque<V> &src) {
404 return from_container<std::deque<V>>(src);
408template <
typename X,
typename Y>
410 static attr_value_t
f(
const std::pair<X, Y>& src) {
411 attr_value_t dst = SIM_alloc_attr_list(2);
412 SIM_attr_list_set_item(&dst, 0,
std_to_attr(src.first));
413 SIM_attr_list_set_item(&dst, 1,
std_to_attr(src.second));
418template <
typename X,
typename Y>
420 static attr_value_t
f(
const std::map<X, Y>& src) {
421 const unsigned size = src.size();
422 attr_value_t dst = SIM_alloc_attr_list(size);
423 auto it = src.cbegin();
424 for (
unsigned i = 0; i < size; ++i, ++it) {
425 std::pair<X, Y> p {it->first, it->second};
432template <
typename C,
typename T =
typename C::value_type>
434 if (!SIM_attr_is_list(src)) {
436 "Expected Simics list type"
439 const unsigned size = SIM_attr_list_size(src);
443 auto it = dst.begin();
444 for (
unsigned i = 0; i < size; ++i, ++it) {
445 *it = attr_to_std<T>(SIM_attr_list_item(src, i));
450template <
typename V, std::
size_t N>
452 static std::array<V, N>
f(
const attr_value_t &src) {
453 std::array<V, N> arr;
455 auto vec = to_container<std::vector<V>>(src);
456 std::copy_n(vec.begin(), N, arr.begin());
463 static std::vector<V>
f(
const attr_value_t &src) {
464 return to_container<std::vector<V>>(src);
470 static std::list<V>
f(
const attr_value_t &src) {
471 return to_container<std::list<V>>(src);
477 static std::deque<V>
f(
const attr_value_t &src) {
478 return to_container<std::deque<V>>(src);
482template <
typename X,
typename Y>
484 static std::pair<X, Y>
f(
const attr_value_t& src) {
485 if (!SIM_attr_is_list(src) || SIM_attr_list_size(src) != 2) {
487 "Expected Simics list type with exactly two members"
490 return {attr_to_std<X>(SIM_attr_list_item(src, 0)),
491 attr_to_std<Y>(SIM_attr_list_item(src, 1))};
495template <
typename X,
typename Y>
497 static std::map<X, Y>
f(
const attr_value_t& src) {
498 if (!SIM_attr_is_list(src)) {
500 "Expected Simics list type"
504 for (
unsigned i = 0; i < SIM_attr_list_size(src); ++i) {
506 SIM_attr_list_item(src, i)));
518 return SIM_make_attr_data(src.size(), src.data());
525 if (!SIM_attr_is_data(src)) {
527 "Expected Simics data type"
530 size_t size = SIM_attr_data_size(src);
531 const uint8 *data = SIM_attr_data(src);
#define _STD2ATTR_UINT_HELPER(type)
Definition: attribute-traits.h:183
#define _ATTR2STD_UINT_HELPER(type)
Definition: attribute-traits.h:224
#define _STD2ATTR_INT_HELPER(type)
Definition: attribute-traits.h:176
#define _ATTR2STD_FLOAT_HELPER(type)
Definition: attribute-traits.h:270
#define _ATTR2STD_INT_HELPER(type)
Definition: attribute-traits.h:204
#define _STD2ATTR_FLOAT_HELPER(type)
Definition: attribute-traits.h:259
Represents Simics C type conf_object_t.
Definition: conf-object.h:37
const std::string & port_name() const
Get & set name for the port implements the interface.
Definition: conf-object.h:63
void set_port_name(const std::string &name)
Definition: conf-object.h:64
Definition: attribute-exceptions.h:38
std::vector< uint8 > data_attribute
Definition: attribute-traits.h:513
C to_container(const attr_value_t &src)
Definition: attribute-traits.h:433
attr_value_t from_container(const C &src)
Definition: attribute-traits.h:370
Definition: attr-value.h:23
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:111
std::enable_if< std::is_enum< T >::value, T >::type attr_to_std(attr_value_t src)
Function transforms Simics attr_value_t to C++ enum type.
Definition: attribute-traits.h:142
Definition: common-types.h:63
static attr_value_t f(const ConfObjectRef &src)
Definition: attribute-traits.h:50
static attr_value_t f(const attr_value_t &src)
Definition: attribute-traits.h:355
static attr_value_t f(const bool &src)
Definition: attribute-traits.h:334
static attr_value_t f(const char *src)
Definition: attribute-traits.h:309
static attr_value_t f(const data_attribute &src)
Definition: attribute-traits.h:517
static attr_value_t f(const std::array< V, N > &src)
Definition: attribute-traits.h:382
static attr_value_t f(const std::deque< V > &src)
Definition: attribute-traits.h:403
static attr_value_t f(const std::list< V > &src)
Definition: attribute-traits.h:396
static attr_value_t f(const std::map< X, Y > &src)
Definition: attribute-traits.h:420
static attr_value_t f(const std::pair< X, Y > &src)
Definition: attribute-traits.h:410
static attr_value_t f(const std::string &src)
Definition: attribute-traits.h:290
static attr_value_t f(const std::vector< V > &src)
Definition: attribute-traits.h:389
Definition: attribute-traits.h:38
static attr_value_t f(const T &src)
static ConfObjectRef f(const attr_value_t &src)
Definition: attribute-traits.h:70
static attr_value_t f(const attr_value_t &src)
Definition: attribute-traits.h:362
static bool f(const attr_value_t &src)
Definition: attribute-traits.h:341
static const char * f(const attr_value_t &src)
Definition: attribute-traits.h:316
static data_attribute f(const attr_value_t &src)
Definition: attribute-traits.h:524
static std::array< V, N > f(const attr_value_t &src)
Definition: attribute-traits.h:452
static std::deque< V > f(const attr_value_t &src)
Definition: attribute-traits.h:477
static std::list< V > f(const attr_value_t &src)
Definition: attribute-traits.h:470
static std::map< X, Y > f(const attr_value_t &src)
Definition: attribute-traits.h:497
static std::pair< X, Y > f(const attr_value_t &src)
Definition: attribute-traits.h:484
static std::string f(const attr_value_t &src)
Definition: attribute-traits.h:297
static std::vector< V > f(const attr_value_t &src)
Definition: attribute-traits.h:463
Definition: attribute-traits.h:43
static T f(const attr_value_t &src)