Convert

Various ToString conversion overloads for numeric data types, IID and more.

[Labs] [Home]
#pragma once

#include <Windows.h>
#include <string>
#include <sstream>
#include "Include.h"

class Convert
{
#pragma region Generic Conversions

        // This method will convert a string to type T.
        // NOTE: T is intended for numeric conversions.
public: template<typename T> static bool FromString(const String& text,
           T& tObj,
           std::ios_base& (*pfn)(std::ios_base&) = std::dec)
        {
           IStringStream iss(text);
           return !(iss >> pfn >> tObj).fail();
        }

        // Generic ToString method. Will work for most basic types.
        // To work for user defined types, a (static) << operator must 
        // have been defined.
public: template<typename T> static String ToString(const T& tObj)
        {
           OStringStream ostr;
           ostr << tObj;
           return ostr.str();
        }

public: static String ToString(const char* pszText)
        {
           std::string str(pszText);

#ifdef UNICODE
           return std::wstring(str.begin(), str.end());
#else
           return str;
#endif
        }

public: static String ToString(const wchar_t* pszText)
        {
           std::wstring wstr(pszText);
           
#ifdef UNICODE
           return wstr;
#else
           return std::string(wstr.begin(), wstr.end());
#endif
        }

#pragma endregion

#pragma region Numeric Conversion Methods

public: static String ToString(int num)
        {
           TCHAR szBuffer[128] = { 0 };

#ifdef UNICODE
           errno_t ec = _itow_s(num, szBuffer, 128, 10);
#else
           errno_t ec = _itoa_s(num, szBuffer, 128, 10);
#endif

           return String(szBuffer);
        }

public: static String ToString(long num)
        {
           TCHAR szBuffer[128] = { 0 };

#ifdef UNICODE
           errno_t ec = _ltow_s(num, szBuffer, 128, 10);
#else
           errno_t ec = _ltoa_s(num, szBuffer, 128, 10);
#endif

           return String(szBuffer);
        }

public: static String ToString(DWORD num)
        {
           TCHAR szBuffer[128] = { 0 };

#ifdef UNICODE
           errno_t ec = _ultow_s(num, szBuffer, 128, 10);
#else
           errno_t ec = _ultoa_s(num, szBuffer, 128, 10);
#endif

           return String(szBuffer);
        }

#pragma endregion

#pragma region IID To String Conversion Methods

public: static String ToString(REFIID riid)
        {           
           const TCHAR* separator = _TXT("-");

           OStringStream ostr;

           ostr << riid.Data1 << separator << riid.Data2 << separator
              << riid.Data3 << separator << riid.Data4;

           return ostr.str();
        }

#pragma endregion

#pragma region Date/Time To String Conversion Methods

public: static String ToString(SYSTEMTIME& systemTime)
        {
           OStringStream dateTimeString;
           String ampmString = (systemTime.wHour >= static_cast<WORD>(12) ? _TXT("PM") : _TXT("AM"));

           dateTimeString << systemTime.wMonth  << "/"
              << systemTime.wDay << "/"
              << systemTime.wYear << " "
              << systemTime.wHour % 12 << ":"
              << systemTime.wMinute << ":"
              << systemTime.wSecond << " "
              << ampmString.c_str();

           return dateTimeString.str().c_str();
        }

public: static String ToString(LPSYSTEMTIME lpSystemTime)
        {
           OStringStream dateTimeString;
           String ampmString = (lpSystemTime->wHour >= static_cast<WORD>(12) ? _TXT("PM") : _TXT("AM"));

           dateTimeString << lpSystemTime->wMonth << "/"
              << lpSystemTime->wDay << "/"
              << lpSystemTime->wYear << " "
              << lpSystemTime->wHour % 12 << ":"
              << lpSystemTime->wMinute << ":"
              << lpSystemTime->wSecond << " "
              << ampmString.c_str();

           return dateTimeString.str().c_str();
        }

public: static String CurrentDateTimeToString()
        {
           SYSTEMTIME systemTime;
           ::GetLocalTime(&systemTime);

           return Convert::ToString(systemTime);
        }

#pragma endregion

public: static String CurrentThreadIdToString()
        {
           DWORD threadID = ::GetCurrentThreadId();
           return Convert::ToString(threadID);
        }
};
ċ
Convert.h
(4k)
Ken Base,
Dec 18, 2010, 6:10 AM
Comments