1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
10 #include <type_traits>
13 #include <vpu/utils/io.hpp>
14 #include <vpu/utils/dot_io.hpp>
20 using Ptr = std::unique_ptr<Holder>;
22 virtual ~Holder() = default;
24 virtual Holder::Ptr clone() const = 0;
26 virtual void printImpl(std::ostream& os) const = 0;
27 virtual void printImpl(DotLabel& lbl) const = 0;
31 struct HolderImpl final : Holder {
34 explicit inline HolderImpl(const T& val) :
37 explicit inline HolderImpl(T&& val) :
38 _val(std::move(val)) {
41 inline void set(const T& val) {
44 inline void set(T&& val) {
45 _val = std::move(val);
48 ~HolderImpl() override = default;
50 Holder::Ptr clone() const override {
51 return Holder::Ptr(new HolderImpl<T>(_val));
54 void printImpl(std::ostream& os) const override {
57 void printImpl(DotLabel& lbl) const override {
63 inline Any() = default;
65 inline Any(Any&&) = default;
66 inline Any& operator=(Any&&) = default;
68 inline Any(const Any& other) :
69 _impl(other._impl != nullptr ? other._impl->clone() : nullptr) {
71 inline Any& operator=(const Any& other) {
73 if (other._impl == nullptr) {
76 _impl = other._impl->clone();
83 explicit Any(const T& arg) :
84 _impl(new HolderImpl<T>(arg)) {
88 typename _Check = typename std::enable_if<!std::is_reference<T>::value, void>::type>
89 explicit inline Any(T&& arg) :
90 _impl(new HolderImpl<typename std::decay<T>::type>(std::move(arg))) {
93 inline bool empty() const {
94 return _impl == nullptr;
98 inline void set(const T& arg) {
99 if (auto casted = dynamic_cast<HolderImpl<T>*>(_impl.get())) {
102 _impl.reset(new HolderImpl<T>(arg));
107 typename _Check = typename std::enable_if<!std::is_reference<T>::value, void>::type>
108 inline void set(T&& arg) {
109 if (auto casted = dynamic_cast<HolderImpl<typename std::decay<T>::type>*>(_impl.get())) {
110 casted->set(std::move(arg));
112 _impl.reset(new HolderImpl<T>(std::move(arg)));
116 template <typename T>
117 inline const T& get() const {
118 auto casted = dynamic_cast<const HolderImpl<T>*>(_impl.get());
119 IE_ASSERT(casted != nullptr);
122 template <typename T>
124 auto casted = dynamic_cast<HolderImpl<T>*>(_impl.get());
125 IE_ASSERT(casted != nullptr);
129 inline void swap(Any& other) {
130 std::swap(_impl, other._impl);
133 inline void printImpl(std::ostream& os) const {
134 if (_impl != nullptr) {
135 _impl->printImpl(os);
139 inline void printImpl(DotLabel& lbl) const {
140 if (_impl != nullptr) {
141 _impl->printImpl(lbl);