3 * Copyright (c) 2007-2010 SlimDX Group
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
6 * of this software and associated documentation files (the "Software"), to deal
\r
7 * in the Software without restriction, including without limitation the rights
\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
9 * copies of the Software, and to permit persons to whom the Software is
\r
10 * furnished to do so, subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in
\r
13 * all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
25 #include "../InternalHelpers.h"
\r
26 #include "../math/Half.h"
\r
28 #include "HalfConverter.h"
\r
30 using namespace System;
\r
31 using namespace System::Collections;
\r
32 using namespace System::Drawing;
\r
33 using namespace System::ComponentModel;
\r
34 using namespace System::ComponentModel::Design::Serialization;
\r
35 using namespace System::Globalization;
\r
36 using namespace System::Reflection;
\r
42 bool HalfConverter::CanConvertTo(ITypeDescriptorContext^ context, Type^ destinationType)
\r
44 if( destinationType == String::typeid || destinationType == InstanceDescriptor::typeid )
\r
47 return ExpandableObjectConverter::CanConvertTo(context, destinationType);
\r
50 bool HalfConverter::CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)
\r
52 if( sourceType == String::typeid )
\r
55 return ExpandableObjectConverter::CanConvertFrom(context, sourceType);
\r
58 Object^ HalfConverter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destinationType)
\r
60 if( destinationType == nullptr )
\r
61 throw gcnew ArgumentNullException( "destinationType" );
\r
63 if( culture == nullptr )
\r
64 culture = CultureInfo::CurrentCulture;
\r
66 Half^ half = dynamic_cast<Half^>( value );
\r
68 if( destinationType == String::typeid && half != nullptr )
\r
70 String^ separator = culture->TextInfo->ListSeparator + " ";
\r
71 TypeConverter^ converter = TypeDescriptor::GetConverter(float::typeid);
\r
72 array<String^>^ stringArray = gcnew array<String^>( 1 );
\r
74 stringArray[0] = converter->ConvertToString( context, culture, safe_cast<float>( *half ) );
\r
76 return String::Join( separator, stringArray );
\r
78 else if( destinationType == InstanceDescriptor::typeid && half != nullptr )
\r
80 ConstructorInfo^ info = (Half::typeid)->GetConstructor( gcnew array<Type^> { float::typeid } );
\r
81 if( info != nullptr )
\r
82 return gcnew InstanceDescriptor( info, gcnew array<Object^> { safe_cast<float>( *half ) } );
\r
85 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);
\r
88 Object^ HalfConverter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value)
\r
90 if( culture == nullptr )
\r
91 culture = CultureInfo::CurrentCulture;
\r
93 String^ string = dynamic_cast<String^>( value );
\r
95 if( string != nullptr )
\r
97 string = string->Trim();
\r
98 TypeConverter^ converter = TypeDescriptor::GetConverter(float::typeid);
\r
99 array<String^>^ stringArray = string->Split( culture->TextInfo->ListSeparator[0] );
\r
101 if( stringArray->Length != 1 )
\r
102 throw gcnew ArgumentException("Invalid half format.");
\r
104 float H = safe_cast<float>( converter->ConvertFromString( context, culture, stringArray[0] ) );
\r
106 return gcnew Half(H);
\r
109 return ExpandableObjectConverter::ConvertFrom(context, culture, value);
\r