1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
3 ** Generic Value Test
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Adrian Bentley
7 ** Copyright (c) 2010 Nikita Kitaev
8 **
9 ** This package is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU General Public License as
11 ** published by the Free Software Foundation; either version 2 of
12 ** the License, or (at your option) any later version.
13 **
14 ** This package is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** General Public License for more details.
18 **
19 ** === N O T E S ===========================================================
20 **
21 ** ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #include <ETL/value>
26 #include <stdio.h>
27
28 /* === M A C R O S ========================================================= */
29
30 using namespace etl;
31
32 /* === C L A S S E S ======================================================= */
33
34 /* === P R O C E D U R E S ================================================= */
35
36 /* === E N T R Y P O I N T ================================================= */
37
38 struct stupidv
39 {
40 float x,y;
41
stupidvstupidv42 stupidv(float xin=0, float yin=0) :x(xin),y(yin) {}
printstupidv43 void print() const
44 {
45 printf("(x=%f,y=%f)\n",x,y);
46 }
47 };
48
49 struct stupidp
50 {
51 float z,w;
52
stupidpstupidp53 stupidp(float zin=0, float win=0) :z(zin),w(win) {}
54
printstupidp55 void print() const
56 {
57 printf("(z=%f,w=%f)\n",z,w);
58 }
59 };
60
61 template <>
62 class value_store_type<stupidp>
63 {
64 public:
65 typedef stupidv value_type;
66 };
67
main()68 int main()
69 {
70 try
71 {
72 value v(10.0); //construction
73 value v2; //default construct...
74
75 //get type...
76 printf("type of 10.0: %s\n", v.type().name());
77
78 v2 = 1; //assignment
79 printf("type of 1: %s\n", v2.type().name());
80
81 //extract int test
82
83 int *pi = value_cast<int>(&v2);
84 printf("v2 is an int(%p)\n", pi);
85 printf(" %d\n", value_cast<int>(v2));
86
87 printf(" const version: %d\n", value_cast<int>(value(5)));
88
89 v = 'c'; //assignment again...
90 printf("type of c: %s\n", v.type().name());
91
92 v2 = v; //value assignment
93 printf("type of v2 , v: %s , %s\n", v2.type().name(), v.type().name());
94
95 //random type test
96 v = stupidv(0,1);
97 printf("type of vec: %s\n", v.type().name());
98
99 //type cast with binary change test
100 value_cast<stupidp>(&v)->print();
101 value_cast<stupidv>(stupidp(5,10)).print(); //copy test
102
103 printf("type of v: %s\n", v.type().name());
104 printf("type of v2: %s\n", v2.type().name());
105 v.swap(v2);
106 printf("type of v: %s\n", v.type().name());
107 printf("type of v2: %s\n", v2.type().name());
108
109 // test the exception throwing...
110 value_cast<int>(stupidp(6,66));
111
112 }catch(const etl::bad_value_cast &e)
113 {
114 printf(" Exploded: %s\n",e.what());
115 }catch(...)
116 {
117 printf(" Exploded\n");
118 }
119
120 return 0;
121 }
122