1 /**********************************************************************
2 
3   WrappedType.cpp
4 
5   James Crook
6   (C) Audacity Developers, 2007
7 
8   wxWidgets license. See Licensing.txt
9 
10 **********************************************************************//*!
11 
12 \class WrappedType
13 \brief
14   Used in type conversions, this wrapper for ints, strings, doubles and
15   enums provides conversions between all the types.  Functions that
16   work on wrapped types can quickly be reused to work on any of
17   these types.  This cuts out a lot of repetitive code.
18 
19   JKC: This class grows in size with the square of the number of
20   types it supports.  It has to do all conversions between all pairs,
21   so try to re-use existing types if you can.
22 
23   It's probable that not all the cases are actually used in practice.
24   The most heavily used will be conversions to <-> from string.
25 
26 *//**********************************************************************/
27 
28 
29 #include "WrappedType.h"
30 
31 #include <wx/wxprec.h>
32 #include "Internat.h"
33 
34 /// @return true iff the wrapped type is a string.
IsString()35 bool WrappedType::IsString()
36 {
37    return eWrappedType == eWrappedString;
38 }
39 
40 
41 
ReadAsString()42 wxString WrappedType::ReadAsString()
43 {
44    switch( eWrappedType )
45    {
46    case eWrappedString:
47       return *mpStr;
48       break;
49    case eWrappedInt:
50       return wxString::Format(wxT("%i"),*mpInt );
51       break;
52    case eWrappedDouble:
53       return wxString::Format(wxT("%.8g"),*mpDouble );
54       break;
55    case eWrappedBool:
56       return (* mpBool) ? wxT("true") : wxT("false" );
57       break;
58    case eWrappedEnum:
59       wxASSERT( false );
60       break;
61    default:
62       wxASSERT( false );
63       break;
64    }
65    return wxT("ERROR"); //Compiler pacifier
66 }
67 
ReadAsInt()68 int WrappedType::ReadAsInt()
69 {
70    switch( eWrappedType )
71    {
72    case eWrappedString:
73    {
74       long l;
75       mpStr->ToLong(&l);
76       return (int) l;
77       break;
78    }
79    case eWrappedInt:
80       return *mpInt;
81       break;
82    case eWrappedDouble:
83       return (int)*mpDouble;
84       break;
85    case eWrappedBool:
86       return (* mpBool) ? 1 : 0;
87       break;
88    case eWrappedEnum:
89       wxASSERT( false );
90       break;
91    default:
92       wxASSERT( false );
93       break;
94    }
95    return -1;//Compiler pacifier
96 }
97 
ReadAsDouble()98 double WrappedType::ReadAsDouble()
99 {
100    switch( eWrappedType )
101    {
102    case eWrappedString:
103       return Internat::CompatibleToDouble( *mpStr );
104       break;
105    case eWrappedInt:
106       return (double)*mpInt;
107       break;
108    case eWrappedDouble:
109       return * mpDouble;
110       break;
111    case eWrappedBool:
112       return (* mpBool)? 1.0 : 0.0;
113       break;
114    case eWrappedEnum:
115       wxASSERT( false );
116       break;
117    default:
118       wxASSERT( false );
119       break;
120    }
121    return -1.0f;//Compiler pacifier
122 }
123 
ReadAsBool()124 bool WrappedType::ReadAsBool()
125 {
126    switch( eWrappedType )
127    {
128    case eWrappedString:
129       return mpStr->IsSameAs( wxT("true"), false ); // case free comparison.
130       break;
131    case eWrappedInt:
132       return  *mpInt != 0;
133       break;
134    case eWrappedDouble:
135       wxASSERT( false );// DANGEROUS USE OF WrappedType.  Can't rely on equality.
136       return * mpDouble != 0.0f; // this is what the code would be...
137       break;
138    case eWrappedBool:
139       return * mpBool;
140       break;
141    case eWrappedEnum:
142       wxASSERT( false );
143       break;
144    default:
145       wxASSERT( false );
146       break;
147    }
148    return false;//Compiler pacifier
149 }
150 
151 
WriteToAsString(const wxString & InStr)152 void WrappedType::WriteToAsString( const wxString & InStr)
153 {
154    switch( eWrappedType )
155    {
156    case eWrappedString:
157       *mpStr = InStr;
158       break;
159    case eWrappedInt:
160    {
161       long l;
162       InStr.ToLong(&l);
163       *mpInt = (int) l;
164       break;
165    }
166    case eWrappedDouble:
167       *mpDouble = Internat::CompatibleToDouble( InStr );
168       break;
169    case eWrappedBool:
170       *mpBool = InStr.IsSameAs( wxT("true"), false ); // case free comparison.;
171       break;
172    case eWrappedEnum:
173       wxASSERT( false );
174       break;
175    default:
176       wxASSERT( false );
177       break;
178    }
179 }
180 
WriteToAsInt(const int InInt)181 void WrappedType::WriteToAsInt( const int InInt)
182 {
183    switch( eWrappedType )
184    {
185    case eWrappedString:
186       *mpStr = wxString::Format( wxT("%i"), InInt );
187       break;
188    case eWrappedInt:
189       *mpInt = InInt;
190       break;
191    case eWrappedDouble:
192       *mpDouble = (double)InInt;
193       break;
194    case eWrappedBool:
195       * mpBool = (InInt !=0);
196       break;
197    case eWrappedEnum:
198       wxASSERT( false );
199       break;
200    default:
201       wxASSERT( false );
202       break;
203    }
204 }
205 
WriteToAsDouble(const double InDouble)206 void WrappedType::WriteToAsDouble( const double InDouble)
207 {
208    switch( eWrappedType )
209    {
210    case eWrappedString:
211       *mpStr = wxString::Format( wxT("%.8g"), InDouble );
212       break;
213    case eWrappedInt:
214       *mpInt = (int)InDouble;
215       break;
216    case eWrappedDouble:
217       *mpDouble = InDouble;
218       break;
219    case eWrappedBool:
220       wxASSERT( false );
221       * mpBool = InDouble != 0.0;
222       break;
223    case eWrappedEnum:
224       wxASSERT( false );
225       break;
226    default:
227       wxASSERT( false );
228       break;
229    }
230 }
231 
232 
WriteToAsBool(const bool InBool)233 void WrappedType::WriteToAsBool( const bool InBool)
234 {
235    switch( eWrappedType )
236    {
237    case eWrappedString:
238       *mpStr = InBool ? wxT("true") : wxT("false" );
239       break;
240    case eWrappedInt:
241       *mpInt = InBool ? 1 : 0;
242       break;
243    case eWrappedDouble:
244       *mpDouble = InBool ? 1.0 : 0.0;
245       break;
246    case eWrappedBool:
247       *mpBool = InBool;
248       break;
249    case eWrappedEnum:
250       wxASSERT( false );
251       break;
252    default:
253       wxASSERT( false );
254       break;
255    }
256 }
257 
258