GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
boxed_cast.hpp
Go to the documentation of this file.
1// This file is distributed under the BSD License.
2// See "license.txt" for details.
3// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
4// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
5// http://www.chaiscript.com
6
7// This is an open source non-commercial project. Dear PVS-Studio, please check it.
8// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
9
10
11#ifndef CHAISCRIPT_BOXED_CAST_HPP_
12#define CHAISCRIPT_BOXED_CAST_HPP_
13
15#include "bad_boxed_cast.hpp"
16#include "boxed_cast_helper.hpp"
17#include "boxed_value.hpp"
18#include "type_conversions.hpp"
19#include "type_info.hpp"
20
21namespace chaiscript {
23namespace detail {
24namespace exception {
25class bad_any_cast;
26} // namespace exception
27} // namespace detail
28} // namespace chaiscript
29
30namespace chaiscript
31{
32
75 template<typename Type>
76 decltype(auto) boxed_cast(const Boxed_Value &bv, const Type_Conversions_State *t_conversions = nullptr)
77 {
78 if (!t_conversions || bv.get_type_info().bare_equal(user_type<Type>()) || (t_conversions && !(*t_conversions)->convertable_type<Type>())) {
79 try {
80 return(detail::Cast_Helper<Type>::cast(bv, t_conversions));
82 }
83 }
84
85
86 if (t_conversions && (*t_conversions)->convertable_type<Type>())
87 {
88 try {
89 // We will not catch any bad_boxed_dynamic_cast that is thrown, let the user get it
90 // either way, we are not responsible if it doesn't work
91 return(detail::Cast_Helper<Type>::cast((*t_conversions)->boxed_type_conversion<Type>(t_conversions->saves(), bv), t_conversions));
92 } catch (...) {
93 try {
94 // try going the other way
95 return(detail::Cast_Helper<Type>::cast((*t_conversions)->boxed_type_down_conversion<Type>(t_conversions->saves(), bv), t_conversions));
97 throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type));
98 }
99 }
100 } else {
101 // If it's not convertable, just throw the error, don't waste the time on the
102 // attempted dynamic_cast
103 throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type));
104 }
105
106 }
107
108}
109
110
111
112#endif
113
Type
Definition Database.hh:9
A wrapper for holding any valid C++ type. All types in ChaiScript are Boxed_Value objects.
Definition boxed_value.hpp:28
const Type_Info & get_type_info() const noexcept
Definition boxed_value.hpp:224
Definition type_conversions.hpp:512
Definition type_conversions.hpp:321
constexpr bool bare_equal(const Type_Info &ti) const noexcept
Definition type_info.hpp:75
Thrown in the event that an Any cannot be cast to the desired type.
Definition any.hpp:22
Thrown in the event that a Boxed_Value cannot be cast to the desired type.
Definition bad_boxed_cast.hpp:34
Definition any.hpp:15
Classes and functions reserved for internal use. Items in this namespace are not supported.
Namespace chaiscript contains every API call that the average user will be concerned with.
constexpr Type_Info user_type(const T &)
Creates a Type_Info object representing the type passed in.
Definition type_info.hpp:221
decltype(auto) boxed_cast(const Boxed_Value &bv, const Type_Conversions_State *t_conversions=nullptr)
Function for extracting a value stored in a Boxed_Value object.
Definition boxed_cast.hpp:76
static decltype(auto) cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
Definition boxed_cast_helper.hpp:314