SystemC Library API Reference Manual
Reference documentation for the Simics SystemC Library.
 
Loading...
Searching...
No Matches
any_type.h
Go to the documentation of this file.
1// -*- mode: C++; c-file-style: "virtutech-c++" -*-
2
3/*
4 © 2015 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_TYPES_ANY_TYPE_H
17#define SIMICS_TYPES_ANY_TYPE_H
18
19#include <stddef.h>
20
21namespace simics {
22namespace types {
23
25class AnyType {
26 class ValueInterface {
27 public:
28 virtual ValueInterface *copy() const = 0;
29 virtual ~ValueInterface() {}
30 };
31
32 template<typename T>
33 class Value : public ValueInterface {
34 public:
35 explicit Value(const T &value) : value_(value) {}
36 virtual ValueInterface *copy() const {
37 return new Value<T>(value_);
38 }
39 T &value() {
40 return value_;
41 }
42 private:
43 T value_;
44 };
45
46 public:
47 AnyType() : value_(NULL) {}
48 AnyType(const AnyType &other) {
49 value_ = other.value_ ? other.value_->copy() : NULL;
50 }
51 template<typename T>
52 AnyType(const T &value) : value_(new Value<T>(value)) {} // NOLINT
53
55 if (this == &other)
56 return *this;
57
58 if (value_)
59 delete value_;
60
61 value_ = other.value_;
62 other.value_ = NULL;
63 return *this;
64 }
65 AnyType &operator=(const AnyType &other) {
66 if (this == &other)
67 return *this;
68
69 if (value_)
70 delete value_;
71
72 value_ = other.value_ ? other.value_->copy() : NULL;
73 return *this;
74 }
75
76 template<typename T>
77 AnyType &operator=(const T &value) {
78 if (value_)
79 delete value_;
80 value_ = new Value<T>(value);
81 return *this;
82 }
83 template<typename T>
84 T value() {
85 return static_cast<Value<T> *>(value_)->value();
86 }
87 bool isSet() {
88 return !!value_;
89 }
91 if (value_)
92 delete value_;
93 }
94
95 private:
96 ValueInterface *value_;
97};
98
99} // namespace types
100} // namespace simics
101
102#endif
Generic type class.
Definition: any_type.h:25
AnyType(const T &value)
Definition: any_type.h:52
bool isSet()
Definition: any_type.h:87
~AnyType()
Definition: any_type.h:90
AnyType & operator=(const T &value)
Definition: any_type.h:77
AnyType & operator=(AnyType &&other)
Definition: any_type.h:54
AnyType & operator=(const AnyType &other)
Definition: any_type.h:65
AnyType(const AnyType &other)
Definition: any_type.h:48
AnyType()
Definition: any_type.h:47
T value()
Definition: any_type.h:84
Definition: pci_bus_interface.h:24