QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxAny.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** https://www.qxorm.com/
4** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
5**
6** This file is part of the QxOrm library
7**
8** This software is provided 'as-is', without any express or implied
9** warranty. In no event will the authors be held liable for any
10** damages arising from the use of this software
11**
12** Commercial Usage
13** Licensees holding valid commercial QxOrm licenses may use this file in
14** accordance with the commercial license agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and Lionel Marty
17**
18** GNU General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU
20** General Public License version 3.0 as published by the Free Software
21** Foundation and appearing in the file 'license.gpl3.txt' included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU General Public License version 3.0 requirements will be
24** met : http://www.gnu.org/copyleft/gpl.html
25**
26** If you are unsure which license is appropriate for your use, or
27** if you have questions regarding the use of this file, please contact :
28** contact@qxorm.com
29**
30****************************************************************************/
31
32#ifndef _QX_ANY_H_
33#define _QX_ANY_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#ifndef _QX_NO_RTTI
47#include <typeinfo>
48#define QX_TYPE_ID(T) typeid(T)
49#else // _QX_NO_RTTI
52#define QX_TYPE_ID(T) std::string(qx::trait::get_class_name< T >::get())
53#endif // _QX_NO_RTTI
54
55#ifndef Q_OS_WIN
56#if (__GNUC__ >= 4)
57#define QX_ANY_FORCE_HIDDEN_VISIBILITY __attribute__ ((visibility("hidden"))) // To avoid a GCC warning : 'qx::any::holder<T>' declared with greater visibility than the type of its field 'qx::any::holder<T>::held' [-Wattributes]
58#endif // (__GNUC__ >= 4)
59#endif // Q_OS_WIN
60
61#ifndef QX_ANY_FORCE_HIDDEN_VISIBILITY
62#define QX_ANY_FORCE_HIDDEN_VISIBILITY /* Nothing */
63#endif // QX_ANY_FORCE_HIDDEN_VISIBILITY
64
65namespace qx {
66
67class any;
68template <typename ValueType> ValueType * any_cast(any *);
69template <typename ValueType> ValueType * unsafe_any_cast(any *);
70
71class any
72{
73
74 template <typename ValueType> friend ValueType * qx::any_cast(any *);
75 template <typename ValueType> friend ValueType * qx::unsafe_any_cast(any *);
76
77public:
78
79#ifndef _QX_NO_RTTI
80 typedef const std::type_info & type_check;
81#else // _QX_NO_RTTI
82 typedef std::string type_check;
83#endif // _QX_NO_RTTI
84
85 any() : content(NULL) { ; }
86 any(const any & other) : content(other.content ? other.content->clone() : NULL) { ; }
87 ~any() { if (content) { delete content; } }
88
89 template <typename ValueType>
90 any(const ValueType & value) : content(new holder<typename std::remove_cv<typename std::decay<const ValueType>::type>::type>(value)) { ; }
91
92 any & swap(any & other) { std::swap(content, other.content); return (* this); }
93
94 template <typename ValueType>
95 any & operator=(const ValueType & other) { any(other).swap(* this); return (* this); }
96
97 any & operator=(any other) { any(other).swap(* this); return (* this); }
98 bool empty() const { return (! content); }
99 void clear() { any().swap(* this); }
100 type_check type() const { return (content ? content->type() : QX_TYPE_ID(void)); }
101
102private:
103
105 {
106 virtual ~placeholder() { ; }
107 virtual type_check type() const = 0;
108 virtual placeholder * clone() const = 0;
109 };
110
111 template <typename ValueType>
113 {
114 holder(const ValueType & value) : held(value) { ; }
115 virtual type_check type() const { return QX_TYPE_ID(ValueType); }
116 virtual placeholder * clone() const { return new holder(held); }
117 ValueType held;
118
119 private:
121 };
122
124
125};
126
127inline void swap(any & lhs, any & other) { lhs.swap(other); }
128
129struct bad_any_cast : public std::exception
130{ virtual const char * what() const throw() { return "qx::bad_any_cast : failed conversion using qx::any_cast"; } };
131
132template <typename ValueType>
133ValueType * any_cast(any * operand)
134{ return ((operand && (operand->type() == QX_TYPE_ID(ValueType))) ? (& static_cast<any::holder<typename std::remove_cv<ValueType>::type> *>(operand->content)->held) : NULL); }
135
136template <typename ValueType>
137const ValueType * any_cast(const any * operand)
138{ return any_cast<ValueType>(const_cast<any *>(operand)); }
139
140template <typename ValueType>
141ValueType any_cast(any & operand)
142{
143 typedef typename std::remove_reference<ValueType>::type nonref;
144 nonref * result = any_cast<nonref>(& operand);
145 if (! result) { throw qx::bad_any_cast(); }
146 return static_cast<ValueType>(* result);
147}
148
149template <typename ValueType>
150ValueType any_cast(const any & operand)
151{
152 typedef typename std::remove_reference<ValueType>::type nonref;
153 return any_cast<const nonref &>(const_cast<any &>(operand));
154}
155
156template <typename ValueType>
157ValueType * unsafe_any_cast(any * operand)
158{ return (& static_cast<any::holder<ValueType> *>(operand->content)->held); }
159
160template <typename ValueType>
161const ValueType * unsafe_any_cast(const any * operand)
162{ return unsafe_any_cast<ValueType>(const_cast<any *>(operand)); }
163
164} // namespace qx
165
166#endif // _QX_ANY_H_
#define QX_TYPE_ID(T)
Definition QxAny.h:48
#define QX_ANY_FORCE_HIDDEN_VISIBILITY
Definition QxAny.h:62
bool empty() const
Definition QxAny.h:98
any(const any &other)
Definition QxAny.h:86
const std::type_info & type_check
Definition QxAny.h:80
~any()
Definition QxAny.h:87
any & swap(any &other)
Definition QxAny.h:92
type_check type() const
Definition QxAny.h:100
placeholder * content
Definition QxAny.h:123
any(const ValueType &value)
Definition QxAny.h:90
any & operator=(any other)
Definition QxAny.h:97
any & operator=(const ValueType &other)
Definition QxAny.h:95
void clear()
Definition QxAny.h:99
any()
Definition QxAny.h:85
#define new
Definition debug_new.h:125
qx::trait::get_class_name<T>::get() : return class name of type T under const char * format,...
Register all primitive and useful types of stl, boost and Qt libraries using QX_REGISTER_CLASS_NAME(T...
std::shared_ptr< T > clone(const T &obj)
qx::clone(const T & obj) : return a boost smart-pointer (std::shared_ptr<T>) of a new instance of typ...
Definition QxClone.h:128
Root namespace for all QxOrm library features.
void swap(any &lhs, any &other)
Definition QxAny.h:127
ValueType * any_cast(any *)
Definition QxAny.h:133
ValueType * unsafe_any_cast(any *)
Definition QxAny.h:157
virtual type_check type() const
Definition QxAny.h:115
holder(const ValueType &value)
Definition QxAny.h:114
virtual placeholder * clone() const
Definition QxAny.h:116
ValueType held
Definition QxAny.h:117
holder & operator=(const holder &)
virtual ~placeholder()
Definition QxAny.h:106
virtual placeholder * clone() const =0
virtual type_check type() const =0
virtual const char * what() const
Definition QxAny.h:130