1 /*
2  * This source file is part of libRocket, the HTML/CSS Interface Middleware
3  *
4  * For the latest information, see http://www.librocket.com
5  *
6  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  *
26  */
27 
28 #include "precompiled.h"
29 #include "../../Include/Rocket/Core/Variant.h"
30 
31 namespace Rocket {
32 namespace Core {
33 
Variant()34 Variant::Variant() : type(NONE)
35 {
36 	// Make sure our object size assumptions fit inside the static buffer
37 	ROCKET_STATIC_ASSERT(sizeof(Colourb) <= LOCAL_DATA_SIZE, LOCAL_DATA_TOO_SMALL_FOR_Colourb);
38 	ROCKET_STATIC_ASSERT(sizeof(Colourf) <= LOCAL_DATA_SIZE, LOCAL_DATA_TOO_SMALL_FOR_Colourf);
39 	ROCKET_STATIC_ASSERT(sizeof(String) <= LOCAL_DATA_SIZE, LOCAL_DATA_TOO_SMALL_FOR_String);
40 }
41 
Variant(const Variant & copy)42 Variant::Variant(const Variant& copy) : type(NONE) { Set(copy); }
43 
~Variant()44 Variant::~Variant() { Clear(); }
45 
Clear()46 void Variant::Clear()
47 {
48 	// Free any allocated types.
49 	switch (type) {
50 	case STRING: {
51 		// Clean up the string.
52 		String* string = (String*)&data;
53 		string->~String();
54 		break;
55 	}
56 	case VECTOR2: {
57 		auto d = (Vector2f*)&data;
58 		d->~Vector2f();
59 		break;
60 	}
61 	case COLOURF: {
62 		auto d = (Colourf*)&data;
63 		d->~Colourf();
64 		break;
65 	}
66 	case COLOURB: {
67 		auto d = (Colourb*)&data;
68 		d->~Colourb();
69 		break;
70 	}
71 
72 	default:
73 		break;
74 	}
75 	type = NONE;
76 }
77 
GetType() const78 Variant::Type Variant::GetType() const { return type; }
79 
80 //////////////////////////////////////////////////
81 // Set methods
82 //////////////////////////////////////////////////
83 
84 #define SET_VARIANT(type) Clear(); new (std::addressof(data))(type)(value)
85 
Set(const Variant & copy)86 void Variant::Set(const Variant& copy)
87 {
88 	switch (copy.type) {
89 	case STRING: {
90 		// Create the string
91 		Set(*(String*)std::addressof(copy.data));
92 		break;
93 	}
94 	case VECTOR2: {
95 		Set(*(Vector2f*)std::addressof(copy.data));
96 		break;
97 	}
98 	case COLOURF: {
99 		Set(*(Colourf*)std::addressof(copy.data));
100 		break;
101 	}
102 	case COLOURB: {
103 		Set(*(Colourb*)std::addressof(copy.data));
104 		break;
105 	}
106 
107 	default:
108 		Clear();
109 		memcpy(std::addressof(data), std::addressof(copy.data), LOCAL_DATA_SIZE);
110 		break;
111 	}
112 	type = copy.type;
113 }
114 
Set(const byte value)115 void Variant::Set(const byte value)
116 {
117 	SET_VARIANT(byte);
118 	type = BYTE;
119 }
120 
Set(const char value)121 void Variant::Set(const char value)
122 {
123 	SET_VARIANT(char);
124 	type = CHAR;
125 }
126 
Set(const float value)127 void Variant::Set(const float value)
128 {
129 	SET_VARIANT(float);
130 	type = FLOAT;
131 }
132 
Set(const int value)133 void Variant::Set(const int value)
134 {
135 	SET_VARIANT(int);
136 	type = INT;
137 }
138 
Set(const String & value)139 void Variant::Set(const String& value)
140 {
141 	if (type == STRING) {
142 		((String*)&data)->Assign(value);
143 	} else {
144 		SET_VARIANT(String);
145 	}
146 	type = STRING;
147 }
148 
Set(const word value)149 void Variant::Set(const word value)
150 {
151 	SET_VARIANT(word);
152 	type = WORD;
153 }
154 
Set(const char * value)155 void Variant::Set(const char* value) { Set(String(value)); }
156 
Set(void * value)157 void Variant::Set(void* value)
158 {
159 	SET_VARIANT(void*);
160 	type = VOIDPTR;
161 }
162 
Set(const Vector2f & value)163 void Variant::Set(const Vector2f& value)
164 {
165 	SET_VARIANT(Vector2f);
166 	type = VECTOR2;
167 }
168 
Set(const Colourf & value)169 void Variant::Set(const Colourf& value)
170 {
171 	SET_VARIANT(Colourf);
172 	type = COLOURF;
173 }
174 
Set(const Colourb & value)175 void Variant::Set(const Colourb& value)
176 {
177 	SET_VARIANT(Colourb);
178 	type = COLOURB;
179 }
180 
Set(ScriptInterface * value)181 void Variant::Set(ScriptInterface* value)
182 {
183 	SET_VARIANT(ScriptInterface*);
184 	type = SCRIPTINTERFACE;
185 }
186 
operator =(const Variant & copy)187 Variant& Variant::operator=(const Variant& copy)
188 {
189 	Set(copy);
190 	return *this;
191 }
192 
193 } // namespace Core
194 } // namespace Rocket
195