stlab.adobe.com Adobe Systems Incorporated
conversion.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
4  or a copy at http://stlab.adobe.com/licenses.html)
5 */
6 
7 /*************************************************************************************************/
8 
9 #ifndef ADOBE_CONVERSION_HPP
10 #define ADOBE_CONVERSION_HPP
11 
12 #include <adobe/config.hpp>
13 #include <string>
14 
15 #include <adobe/string_fwd.hpp>
16 
17 /*************************************************************************************************/
18 
19 namespace adobe {
20 
21 /*************************************************************************************************/
22 
23 template <typename T>
24 struct promote
25 {
26  typedef T type;
27 };
28 
29 template <> struct promote<short> { typedef double type; };
30 template <> struct promote<int> { typedef double type; };
31 template <> struct promote<long> { typedef double type; };
32 
33 template <> struct promote<unsigned short> { typedef double type; };
34 template <> struct promote<unsigned int> { typedef double type; };
35 template <> struct promote<unsigned long> { typedef double type; };
36 
37 template <> struct promote<float> { typedef double type; };
38 
39 // Under 64-bit Windows size_t and ptrdiff_t do not fall into the categories above,
40 // and can cause bad cast failures in any_regular_t when one presumes it would, as
41 // is the case in a 32-bit environment. By default, then, we emit an error when an
42 // attempt is made to promote these types. You can turn off the error by defining
43 // ADOBE_NO_MSVC64_PROMOTION_ERROR in your project.
44 #if defined(BOOST_MSVC) && defined(_M_X64) && !defined(ADOBE_NO_MSVC64_PROMOTION_ERROR)
45 template <> struct promote<std::size_t> { };
46 template <> struct promote<std::ptrdiff_t> { };
47 #endif
48 
49 template <> struct promote<const char*> { typedef version_1::string_t type; };
50 template <> struct promote<std::string> { typedef version_1::string_t type; };
51 
52 /*************************************************************************************************/
53 
54 template <typename lht, typename rht>
55 inline lht explicit_cast(const rht& rhs)
56 { return static_cast<lht>(rhs); }
57 
58 /*************************************************************************************************/
59 
60 template <typename R, typename T>
62  R operator()(T& x) const { return dynamic_cast<R>(x); }
63 };
64 
65 template <typename R, typename T>
66 inline R runtime_cast(T& x)
67 { return runtime_cast_t<R, T>()(x); }
68 
69 template <typename R, typename T>
70 inline R runtime_cast(T* x)
71 { return runtime_cast_t<R, T*>()(x); }
72 
73 template <typename R, typename T>
74 inline bool runtime_cast(const T& x, R& r)
75 {
76  const R* p = runtime_cast<const R*>(&x);
77  if (!p) return false;
78  r = *p;
79  return true;
80 }
81 
82 /*************************************************************************************************/
83 
84 template <typename T>
85 inline T& remove_const(const T& x)
86 { return const_cast<T&>(x); }
87 
88 /*************************************************************************************************/
89 
90 } // namespace adobe
91 
92 /*************************************************************************************************/
93 
94 #endif
95 
96 /*************************************************************************************************/
version_1::string_t type
Definition: conversion.hpp:49
A struct for compile-time type promotion.
Definition: conversion.hpp:24
STL namespace.
lht explicit_cast(const rht &rhs)
Definition: conversion.hpp:55
R runtime_cast(T &x)
Definition: conversion.hpp:66
R operator()(T &x) const
Definition: conversion.hpp:62
version_1::string_t type
Definition: conversion.hpp:50
T & remove_const(const T &x)
Definition: conversion.hpp:85

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google