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/*
2 © 2015 Intel Corporation
3
4 This software and the related documents are Intel copyrighted materials, and
5 your use of them is governed by the express license under which they were
6 provided to you ("License"). Unless the License provides otherwise, you may
7 not use, modify, copy, publish, distribute, disclose or transmit this software
8 or the related documents without Intel's prior written permission.
9
10 This software and the related documents are provided as is, with no express or
11 implied warranties, other than those that are expressly stated in the License.
12*/
13
14#ifndef SIMICS_TYPES_ANY_TYPE_H
15#define SIMICS_TYPES_ANY_TYPE_H
16
17#include <stddef.h>
18#include <stdexcept>
19
20namespace simics {
21namespace types {
22
24class AnyType {
25 class ValueInterface {
26 public:
27 virtual ValueInterface *copy() const = 0;
28 virtual ~ValueInterface() {}
29 };
30
31 template<typename T>
32 class Value : public ValueInterface {
33 public:
34 explicit Value(const T &value) : value_(value) {}
35 ValueInterface *copy() const override {
36 return new Value<T>(value_);
37 }
38 T &value() {
39 return value_;
40 }
41 private:
42 T value_;
43 };
44
45 public:
46 AnyType() : value_(NULL) {}
47 AnyType(const AnyType &other) {
48 value_ = other.value_ ? other.value_->copy() : NULL;
49 }
50 AnyType(AnyType &&other) {
51 value_ = other.value_;
52 other.value_ = NULL;
53 }
54 template<typename T>
55 AnyType(const T &value) : value_(new Value<T>(value)) {} // NOLINT
56
58 if (this == &other)
59 return *this;
60
61 if (value_)
62 delete value_;
63
64 value_ = other.value_;
65 other.value_ = NULL;
66 return *this;
67 }
68 AnyType &operator=(const AnyType &other) {
69 if (this == &other)
70 return *this;
71
72 if (value_)
73 delete value_;
74
75 value_ = other.value_ ? other.value_->copy() : NULL;
76 return *this;
77 }
78
79 template<typename T>
80 AnyType &operator=(const T &value) {
81 if (value_)
82 delete value_;
83 value_ = new Value<T>(value);
84 return *this;
85 }
86 template<typename T>
87 T value() {
88 if (!value_) {
89 throw std::runtime_error("value_ is not set");
90 }
91 return static_cast<Value<T> *>(value_)->value();
92 }
93 bool isSet() {
94 return !!value_;
95 }
97 if (value_)
98 delete value_;
99 }
100
101 private:
102 ValueInterface *value_;
103};
104
105} // namespace types
106} // namespace simics
107
108#endif
Generic type class.
Definition: any_type.h:24
AnyType(const T &value)
Definition: any_type.h:55
bool isSet()
Definition: any_type.h:93
~AnyType()
Definition: any_type.h:96
AnyType(AnyType &&other)
Definition: any_type.h:50
AnyType & operator=(const T &value)
Definition: any_type.h:80
AnyType & operator=(AnyType &&other)
Definition: any_type.h:57
AnyType & operator=(const AnyType &other)
Definition: any_type.h:68
AnyType(const AnyType &other)
Definition: any_type.h:47
AnyType()
Definition: any_type.h:46
T value()
Definition: any_type.h:87
Definition: adapter.h:79