Coverage Report

Created: 2026-08-31 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ogre/OgreMain/include/OgreAny.h
Line
Count
Source
1
/*
2
-----------------------------------------------------------------------------
3
This source file is part of OGRE
4
    (Object-oriented Graphics Rendering Engine)
5
For the latest info, see http://www.ogre3d.org/
6
7
Copyright (c) 2000-2014 Torus Knot Software Ltd
8
9
Permission is hereby granted, free of charge, to any person obtaining a copy
10
of this software and associated documentation files (the "Software"), to deal
11
in the Software without restriction, including without limitation the rights
12
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
copies of the Software, and to permit persons to whom the Software is
14
furnished to do so, subject to the following conditions:
15
16
The above copyright notice and this permission notice shall be included in
17
all copies or substantial portions of the Software.
18
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
THE SOFTWARE.
26
-----------------------------------------------------------------------------
27
*/
28
// -- Based on boost::any, original copyright information follows --
29
// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
30
//
31
// Distributed under the Boost Software License, Version 1.0. (See
32
// accompAnying file LICENSE_1_0.txt or copy at
33
// http://www.boost.org/LICENSE_1_0.txt)
34
// -- End original copyright --
35
36
#ifndef __OGRE_ANY_H__
37
#define __OGRE_ANY_H__
38
39
#include "OgrePrerequisites.h"
40
#include <typeinfo>
41
#include "OgreHeaderPrefix.h"
42
43
namespace Ogre
44
{
45
  // resolve circular dependency
46
    class Any;
47
    template<typename ValueType> ValueType
48
    any_cast(const Any & operand);
49
50
    /** \addtogroup Core
51
    *  @{
52
    */
53
    /** \addtogroup General
54
    *  @{
55
    */
56
    /** Variant type that can hold Any other type.
57
    */
58
    class Any 
59
    {
60
    public: // constructors
61
62
        Any()
63
          : mContent(0)
64
0
        {
65
0
        }
66
67
        template<typename ValueType>
68
        Any(const ValueType & value)
69
          : mContent(OGRE_NEW_T(holder<ValueType>, MEMCATEGORY_GENERAL)(value))
70
        {
71
        }
72
73
        Any(const Any & other)
74
          : mContent(other.mContent ? other.mContent->clone() : 0)
75
0
        {
76
0
        }
77
78
        ~Any()
79
0
        {
80
0
            reset();
81
0
        }
82
83
    public: // modifiers
84
85
        Any& swap(Any & rhs)
86
0
        {
87
0
            std::swap(mContent, rhs.mContent);
88
0
            return *this;
89
0
        }
90
91
        template<typename ValueType>
92
        Any& operator=(const ValueType & rhs)
93
        {
94
            Any(rhs).swap(*this);
95
            return *this;
96
        }
97
98
        Any & operator=(const Any & rhs)
99
0
        {
100
0
            Any(rhs).swap(*this);
101
0
            return *this;
102
0
        }
103
104
    public: // queries
105
106
        bool has_value() const
107
0
        {
108
0
            return mContent != NULL;
109
0
        }
110
111
        /// @deprecated use has_value() instead
112
0
        OGRE_DEPRECATED bool isEmpty() const { return !has_value(); }
113
114
        const std::type_info& type() const
115
5.37k
        {
116
5.37k
            return mContent ? mContent->getType() : typeid(void);
117
5.37k
        }
118
119
        /// @deprecated use type() instead
120
0
        OGRE_DEPRECATED const std::type_info& getType() const { return type(); }
121
122
        /// @deprecated no longer supported
123
        OGRE_DEPRECATED friend std::ostream& operator <<
124
            ( std::ostream& o, const Any& v )
125
0
        {
126
0
            if (v.mContent)
127
0
                v.mContent->writeToStream(o);
128
0
            return o;
129
0
        }
130
131
        void reset()
132
0
        {
133
0
            OGRE_DELETE_T(mContent, placeholder, MEMCATEGORY_GENERAL);
134
0
            mContent = NULL;
135
0
        }
136
137
        /// @deprecated use reset() instead
138
0
        OGRE_DEPRECATED void destroy() { reset(); }
139
140
    protected: // types
141
142
        class placeholder 
143
        {
144
        public: // structors
145
    
146
            virtual ~placeholder()
147
0
            {
148
0
            }
149
150
        public: // queries
151
152
            virtual const std::type_info& getType() const = 0;
153
154
            virtual placeholder * clone() const = 0;
155
    
156
            virtual void writeToStream(std::ostream& o) = 0;
157
158
        };
159
160
        template<typename ValueType>
161
        class holder : public placeholder
162
        {
163
        public: // structors
164
165
            holder(const ValueType & value)
166
              : held(value)
167
            {
168
            }
169
170
        public: // queries
171
172
            const std::type_info & getType() const override
173
            {
174
                return typeid(ValueType);
175
            }
176
177
            placeholder * clone() const override
178
            {
179
                return OGRE_NEW_T(holder, MEMCATEGORY_GENERAL)(held);
180
            }
181
182
            void writeToStream(std::ostream& o) override {}
183
184
        public: // representation
185
186
            ValueType held;
187
188
        };
189
190
191
192
    protected: // representation
193
        placeholder * mContent;
194
195
        template<typename ValueType>
196
        friend ValueType * any_cast(Any *);
197
198
199
    public: 
200
        /// @deprecated use Ogre::any_cast instead
201
        template<typename ValueType>
202
        OGRE_DEPRECATED ValueType operator()() const
203
        {
204
            return any_cast<ValueType>(*this);
205
        }
206
207
        /// @deprecated use Ogre::any_cast instead
208
        template <typename ValueType>
209
        OGRE_DEPRECATED ValueType get(void) const
210
        {
211
            return any_cast<ValueType>(*this);
212
        }
213
214
    };
215
216
    template<typename ValueType>
217
    ValueType * any_cast(Any * operand)
218
5.37k
    {
219
5.37k
        return operand &&
220
#if (OGRE_COMPILER == OGRE_COMPILER_GNUC && OGRE_COMP_VER < 450) || OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS
221
                (std::strcmp(operand->type().name(), typeid(ValueType).name()) == 0)
222
#else
223
5.37k
                (operand->type() == typeid(ValueType))
224
5.37k
#endif
225
5.37k
                    ? &static_cast<Any::holder<ValueType> *>(operand->mContent)->held
226
5.37k
                    : 0;
227
5.37k
    }
228
229
    template<typename ValueType>
230
    const ValueType * any_cast(const Any * operand)
231
5.37k
    {
232
5.37k
        return any_cast<ValueType>(const_cast<Any *>(operand));
233
5.37k
    }
234
235
    template<typename ValueType>
236
    ValueType any_cast(const Any & operand)
237
5.37k
    {
238
5.37k
        const ValueType * result = any_cast<ValueType>(&operand);
239
5.37k
        if(!result)
240
0
        {
241
0
            throw std::bad_cast();
242
0
        }
243
5.37k
        return *result;
244
5.37k
    }
245
    /** @} */
246
    /** @} */
247
248
249
}
250
251
#include "OgreHeaderSuffix.h"
252
253
#endif
254