Add String::fromNumber<> template for disambiguation

This commit is contained in:
Vinnie Falco
2013-08-22 17:27:49 -07:00
parent b4855b6ac9
commit c5130e3eb3
4 changed files with 79 additions and 30 deletions

View File

@@ -235,6 +235,7 @@
<ClInclude Include="..\..\modules\beast_core\text\beast_LexicalCast.h" />
<ClInclude Include="..\..\modules\beast_core\text\beast_LocalisedStrings.h" />
<ClInclude Include="..\..\modules\beast_core\text\beast_NewLine.h" />
<ClInclude Include="..\..\modules\beast_core\text\beast_NumberToString.h" />
<ClInclude Include="..\..\modules\beast_core\text\beast_String.h" />
<ClInclude Include="..\..\modules\beast_core\text\beast_StringArray.h" />
<ClInclude Include="..\..\modules\beast_core\text\beast_StringPairArray.h" />
@@ -914,6 +915,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\text\beast_NumberToString.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\text\beast_String.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>

View File

@@ -869,6 +869,9 @@
<ClInclude Include="..\..\modules\beast_asio\handshake\beast_InputParser.h">
<Filter>beast_asio\handshake</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\text\beast_NumberToString.h">
<Filter>beast_core\text</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\beast_core.cpp">
@@ -1336,6 +1339,9 @@
<ClCompile Include="..\..\modules\beast_asio\async\beast_SharedHandler.cpp">
<Filter>beast_asio\async</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\text\beast_NumberToString.cpp">
<Filter>beast_core\text</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\TODO.txt" />

View File

@@ -379,8 +379,16 @@ namespace NumberToStringConverters
return t;
}
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable: 4127) // conditional expression is constant
#pragma warning (disable: 4146) // unary minus operator applied to unsigned type, result still unsigned
#endif
// pass in a pointer to the END of a buffer..
static char* numberToString (char* t, const int64 n) noexcept
template <typename IntegerType>
static char* numberToString (char* t, IntegerType const n) noexcept
{
if (std::numeric_limits <IntegerType>::is_signed)
{
if (n >= 0)
return printDigits (t, static_cast <uint64> (n));
@@ -391,28 +399,11 @@ namespace NumberToStringConverters
*--t = '-';
return t;
}
static char* numberToString (char* t, uint64 v) noexcept
{
return printDigits (t, v);
}
static char* numberToString (char* t, const int n) noexcept
{
if (n >= 0)
return printDigits (t, static_cast<unsigned int> (n));
// NB: this needs to be careful not to call -std::numeric_limits<int>::min(),
// which has undefined behaviour
t = printDigits (t, static_cast<unsigned int> (-(n + 1)) + 1);
*--t = '-';
return t;
}
static char* numberToString (char* t, unsigned int v) noexcept
{
return printDigits (t, v);
return printDigits (t, n);
}
#ifndef _MSC_VER
#pragma warning (pop)
#endif
struct StackArrayStream : public std::basic_streambuf<char, std::char_traits<char> >
{
@@ -501,6 +492,40 @@ String::String (const double number) : text (NumberToStringConverters::c
String::String (const float number, const int numberOfDecimalPlaces) : text (NumberToStringConverters::createFromDouble ((double) number, numberOfDecimalPlaces)) {}
String::String (const double number, const int numberOfDecimalPlaces) : text (NumberToStringConverters::createFromDouble (number, numberOfDecimalPlaces)) {}
template <typename Number>
String String::fromNumber (Number number, int)
{
return String (NumberToStringConverters::createFromInteger <Number> (number), FromNumber ());
}
template <>
String String::fromNumber <float> (float number, int numberOfDecimalPlaces)
{
if (numberOfDecimalPlaces == 0)
number = std::floor (number);
return String (NumberToStringConverters::createFromDouble (
number, numberOfDecimalPlaces));
}
template <>
String String::fromNumber <double> (double number, int numberOfDecimalPlaces)
{
if (numberOfDecimalPlaces == 0)
number = std::floor (number);
return String (NumberToStringConverters::createFromDouble (
number, numberOfDecimalPlaces));
}
template String String::fromNumber <int16> (int16, int);
template String String::fromNumber <int32> (int32, int);
template String String::fromNumber <int64> (int64, int);
template String String::fromNumber <uint16> (uint16, int);
template String String::fromNumber <uint32> (uint32, int);
template String String::fromNumber <uint64> (uint64, int);
template String String::fromNumber <std::size_t> (std::size_t, int);
//==============================================================================
int String::length() const noexcept
{

View File

@@ -160,6 +160,14 @@ public:
/** Destructor. */
~String() noexcept;
/** Create a string from a specific number type (integer or floating point)
If numberOfDecimalPlaces is specified and number is a floating point type,
the resulting string will have that many decimal places. A value below 0
means use exponent notation if necessary.
*/
template <typename Number>
static String fromNumber (Number number, int numberOfDecimalPlaces = -1);
//==============================================================================
/** This is an empty string that can be used whenever one is needed.
@@ -1207,6 +1215,9 @@ public:
private:
//==============================================================================
struct FromNumber { };
String (CharPointerType text_, FromNumber) : text (text_) { }
CharPointerType text;
//==============================================================================