2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_TYPE_CONVERSIONS_H_
18 #define incl_HPHP_TYPE_CONVERSIONS_H_
20 #include "hphp/runtime/base/types.h"
21 #include "hphp/runtime/base/complex_types.h"
24 ///////////////////////////////////////////////////////////////////////////////
25 // type conversion functions
27 inline bool toBoolean(bool v
) { return v
;}
28 inline bool toBoolean(char v
) { return v
;}
29 inline bool toBoolean(short v
) { return v
;}
30 inline bool toBoolean(int v
) { return v
;}
31 inline bool toBoolean(int64_t v
) { return v
;}
32 inline bool toBoolean(double v
) { return v
;}
33 inline bool toBoolean(litstr v
) = delete;
34 inline bool toBoolean(const StringData
*v
) {
35 return v
? v
->toBoolean() : false;
37 inline bool toBoolean(CStrRef v
) { return toBoolean(v
.get());}
38 inline bool toBoolean(const ArrayData
*v
) {
39 return v
&& !v
->empty();
41 inline bool toBoolean(CArrRef v
) { return toBoolean(v
.get());}
42 inline bool toBoolean(const ObjectData
*v
) {
43 return v
? v
->o_toBoolean() : false;
45 inline bool toBoolean(CObjRef v
) { return toBoolean(v
.get());}
46 inline bool toBoolean(CVarRef v
) { return v
.toBoolean();}
48 inline char toByte(bool v
) { return v
? 1 : 0;}
49 inline char toByte(char v
) { return v
;}
50 inline char toByte(short v
) { return v
;}
51 inline char toByte(int v
) { return v
;}
52 inline char toByte(int64_t v
) { return v
;}
53 inline char toByte(double v
) { return (char)v
;}
54 inline char toByte(litstr v
) = delete;
55 inline char toByte(const StringData
*v
) { return v
? v
->toByte() : 0;}
56 inline char toByte(CStrRef v
) { return toByte(v
.get());}
57 inline char toByte(const ArrayData
*v
) { return (v
&& !v
->empty()) ? 1 : 0;}
58 inline char toByte(CArrRef v
) { return toByte(v
.get());}
59 inline char toByte(const ObjectData
*v
) { return v
? v
->o_toInt64() : 0;}
60 inline char toByte(CObjRef v
) { return toByte(v
.get());}
61 inline char toByte(CVarRef v
) { return v
.toByte();}
63 inline short toInt16(bool v
) { return v
? 1 : 0;}
64 inline short toInt16(char v
) { return v
;}
65 inline short toInt16(short v
) { return v
;}
66 inline short toInt16(int v
) { return v
;}
67 inline short toInt16(int64_t v
) { return v
;}
68 inline short toInt16(double v
) { return (short)v
;}
69 inline short toInt16(litstr v
) = delete;
70 inline short toInt16(const StringData
*v
) { return v
? v
->toInt16() : 0;}
71 inline short toInt16(CStrRef v
) { return toInt16(v
.get());}
72 inline short toInt16(const ArrayData
*v
) { return (v
&& !v
->empty()) ? 1 : 0;}
73 inline short toInt16(CArrRef v
) { return toInt16(v
.get());}
74 inline short toInt16(const ObjectData
*v
) { return v
? v
->o_toInt64() : 0;}
75 inline short toInt16(CObjRef v
) { return toInt16(v
.get());}
76 inline short toInt16(CVarRef v
) { return v
.toInt16();}
78 inline int toInt32(bool v
) { return v
? 1 : 0;}
79 inline int toInt32(char v
) { return v
;}
80 inline int toInt32(short v
) { return v
;}
81 inline int toInt32(int v
) { return v
;}
82 inline int toInt32(int64_t v
) { return v
;}
83 inline int toInt32(double v
) { return (int)v
;}
84 inline int toInt32(litstr v
) = delete;
85 inline int toInt32(const StringData
*v
) { return v
? v
->toInt32() : 0;}
86 inline int toInt32(CStrRef v
) { return toInt32(v
.get());}
87 inline int toInt32(const ArrayData
*v
) { return (v
&& !v
->empty()) ? 1 : 0;}
88 inline int toInt32(CArrRef v
) { return toInt32(v
.get());}
89 inline int toInt32(const ObjectData
*v
) { return v
? v
->o_toInt64() : 0;}
90 inline int toInt32(CObjRef v
) { return toInt32(v
.get());}
91 inline int toInt32(CVarRef v
) { return v
.toInt32();}
93 inline int64_t toInt64(bool v
) { return v
? 1 : 0;}
94 inline int64_t toInt64(char v
) { return v
;}
95 inline int64_t toInt64(short v
) { return v
;}
96 inline int64_t toInt64(int v
) { return v
;}
97 inline int64_t toInt64(int64_t v
) { return v
;}
98 inline int64_t toInt64(double v
) {
99 // If v >= 0 is false and v < 0 is false, then v is NaN. In that case, on
100 // Intel, you get 0x800..00, a.k.a. the minimum int64_t. We mimic that on all
101 // platforms, though this makes us sad.
103 ? (v
> std::numeric_limits
<uint64_t>::max() ? 0u : (uint64_t)v
)
104 : (v
< 0 ? (int64_t)v
: std::numeric_limits
<int64_t>::min()));
106 inline int64_t toInt64(litstr v
) = delete;
107 inline int64_t toInt64(const StringData
*v
) { return v
? v
->toInt64() : 0;}
108 inline int64_t toInt64(CStrRef v
) { return toInt64(v
.get());}
109 inline int64_t toInt64(const ArrayData
*v
) { return (v
&& !v
->empty()) ? 1 : 0;}
110 inline int64_t toInt64(CArrRef v
) { return toInt64(v
.get());}
111 inline int64_t toInt64(const ObjectData
*v
) { return v
? v
->o_toInt64() : 0;}
112 inline int64_t toInt64(CObjRef v
) { return toInt64(v
.get());}
113 inline int64_t toInt64(CVarRef v
) { return v
.toInt64();}
115 inline double toDouble(bool v
) { return v
? 1 : 0;}
116 inline double toDouble(char v
) { return v
;}
117 inline double toDouble(short v
) { return v
;}
118 inline double toDouble(int v
) { return v
;}
119 inline double toDouble(int64_t v
) { return v
;}
120 inline double toDouble(double v
) { return v
;}
121 inline double toDouble(litstr v
) = delete;
122 inline double toDouble(const StringData
*v
) { return v
? v
->toDouble() : 0;}
123 inline double toDouble(CStrRef v
) { return toDouble(v
.get());}
124 inline double toDouble(const ArrayData
*v
) {
125 return (v
&& !v
->empty()) ? 1.0 : 0.0;
127 inline double toDouble(CArrRef v
) { return toDouble(v
.get());}
128 inline double toDouble(const ObjectData
*v
) { return v
? v
->o_toDouble() : 0;}
129 inline double toDouble(CObjRef v
) { return toDouble(v
.get());}
130 inline double toDouble(CVarRef v
) { return v
.toDouble();}
132 inline String
toString(bool v
) { return v
? "1" : "";}
133 inline String
toString(char v
) { return (int64_t)v
;}
134 inline String
toString(short v
) { return (int64_t)v
;}
135 inline String
toString(int v
) { return (int64_t)v
;}
136 inline String
toString(int64_t v
) { return v
;}
137 inline String
toString(double v
) { return v
;}
138 inline String
toString(litstr v
) = delete;
139 inline String
toString(StringData
*v
) { return v
? String(v
) : String("");}
140 inline String
toString(CStrRef v
) { return toString(v
.get());}
141 inline String
toString(const ArrayData
*v
) { return v
? "Array" : "";}
142 inline String
toString(CArrRef v
) { return toString(v
.get());}
143 inline String
toString(ObjectData
*v
) {
144 return v
? v
->t___tostring() : String("");
146 inline String
toString(CObjRef v
) { return toString(v
.get());}
147 inline String
toString(CVarRef v
) { return v
.toString();}
149 inline Array
toArray(bool v
) { return Array::Create(v
);}
150 inline Array
toArray(char v
) { return Array::Create(v
);}
151 inline Array
toArray(short v
) { return Array::Create(v
);}
152 inline Array
toArray(int v
) { return Array::Create(v
);}
153 inline Array
toArray(int64_t v
) { return Array::Create(v
);}
154 inline Array
toArray(double v
) { return Array::Create(v
);}
155 inline Array
toArray(litstr v
) = delete;
156 inline Array
toArray(StringData
*v
) {
157 return v
? Array::Create(v
) : Array::Create();
159 inline Array
toArray(CStrRef v
) { return toArray(v
.get());}
160 inline Array
toArray(ArrayData
*v
) { return v
? Array(v
) : Array::Create();}
161 inline Array
toArray(CArrRef v
) { return toArray(v
.get());}
162 inline Array
toArray(const ObjectData
*v
) {
163 return v
? v
->o_toArray() : Array::Create();
165 inline Array
toArray(CObjRef v
) { return toArray(v
.get());}
166 inline Array
toArray(CVarRef v
) { return v
.toArray();}
168 inline Object
toObject(bool v
) { return Variant(v
).toObject();}
169 inline Object
toObject(char v
) { return Variant(v
).toObject();}
170 inline Object
toObject(short v
) { return Variant(v
).toObject();}
171 inline Object
toObject(int v
) { return Variant(v
).toObject();}
172 inline Object
toObject(int64_t v
) { return Variant(v
).toObject();}
173 inline Object
toObject(double v
) { return Variant(v
).toObject();}
174 inline Object
toObject(litstr v
) = delete;
175 inline Object
toObject(const StringData
*v
) { return Variant(StrNR(v
)).toObject();}
176 inline Object
toObject(CStrRef v
) { return Variant(v
).toObject();}
177 Object
toObject(ArrayData
*v
);
178 inline Object
toObject(CArrRef v
) { return toObject(v
.get());}
179 Object
toObject(ObjectData
*v
);
180 inline Object
toObject(CObjRef v
) { return toObject(v
.get());}
181 inline Object
toObject(CVarRef v
) { return v
.toObject();}
183 inline const String
*toSPOD(CStrRef v
) { return &v
;}
184 inline const Variant
*toVPOD(CVarRef v
) { return &v
;}
185 inline const Object
*toOPOD(CObjRef v
) { return &v
;}
187 ///////////////////////////////////////////////////////////////////////////////
190 #endif // incl_HPHP_TYPE_CONVERSIONS_H_