//---------------------------------------------------------------------
//
// Copyright (C) 2004 Yingle Jia
//
// Permission to copy, use, modify, sell and distribute this software is
// granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied warranty,
// and with no claim as to its suitability for any purpose.
//
#include "stdafx.h"
namespace Acf {
//---------------------------------------------------------------------
// struct Int32
// Static Methods
/*static*/ int Int32::Parse(String* s, IFormatProvider* provider)
{
return Parse(s, NumberStyles::Integer, provider);
}
/*static*/ int Int32::Parse(String* s, NumberStyles style, IFormatProvider* provider)
{
NumberFormatInfoPtr nfi = NumberFormatInfo::GetInstance(provider);
Number number;
number.Parse(s, style, nfi);
int result;
if ((style & NumberStyles::AllowHexSpecifier) != 0)
{
if (!number.TryHexToUInt32((uint*)&result))
throw OverflowException();
}
else
{
if (!number.TryToInt32(&result))
throw OverflowException();
}
return result;
}
/*static*/ bool Int32::TryParse(String* s, int* result)
{
return TryParse(s, NumberStyles::Integer, null, result);
}
/*static*/ bool Int32::TryParse(String* s, NumberStyles style, IFormatProvider* provider,
int* result)
{
NumberFormatInfoPtr nfi = NumberFormatInfo::GetInstance(provider);
Number number;
if (!number.TryParse(s, style, nfi))
return false;
if ((style & NumberStyles::AllowHexSpecifier) != 0)
{
if (!number.TryHexToUInt32((uint*)result))
return false;
}
else
{
if (!number.TryToInt32(result))
return false;
}
return true;
}
// Methods
int Int32::GetHashCode()
{
return this->Value;
}
StringPtr Int32::ToString(IFormatProvider* provider)
{
return ToString((String*)null, provider);
}
StringPtr Int32::ToString(String* format, IFormatProvider* provider)
{
NumberFormatInfoPtr nfi = NumberFormatInfo::GetInstance(provider);
int precision = -1;
wchar_t specifier = Number::ParseFormat(format, &precision);
if (specifier != 0)
{
switch (specifier)
{
case L'D':
case L'd':
return Number::FormatDecimal(this->Value, precision, nfi);
case L'X':
return Number::FormatHexadecimal((uint)this->Value, precision, true);
case L'x':
return Number::FormatHexadecimal((uint)this->Value, precision, false);
default:
{
Number number(this->Value);
return number.Format(specifier, precision, nfi);
}
}
}
else
{
Number number(this->Value);
return number.FormatCustom(format, nfi);
}
}
StringPtr Int32::ToString(const wchar_t* format, IFormatProvider* provider)
{
return ToString(str(format), provider);
}
// Operators
Int32::operator int() const
{
return this->Value;
}
Int32::operator int&()
{
return this->Value;
}
int* Int32::operator&()
{
return &this->Value;
}
//---------------------------------------------------------------------
// class Boxed<Int32>
// Methods
//
// Object Members
//
/*override*/ bool Boxed<Int32>::Equals(Object* obj)
{
Boxed<Int32>* o = dynamic_cast<Boxed<Int32>*>(obj);
if (o == null)
return false;
else
return (this->Value == o->Value);
}
/*override*/ int Boxed<Int32>::GetHashCode()
{
return this->Value.GetHashCode();
}
/*override*/ StringPtr Boxed<Int32>::ToString()
{
return this->Value.ToString();
}
//
// IFormattable Members
//
/*virtual*/ StringPtr Boxed<Int32>::ToString(String* format, IFormatProvider* provider)
{
return this->Value.ToString(format, provider);
}
} // namespace Acf