Nano Oriented Programming Discussion

Nov 26
Casting to and from STL strings.

Here are a couple of functions to convert numbers to strings and strings to numbers. I didn't invent this technique. Boost used to do it this way a long time ago, but then they convert things over to a more complex and robust technique. I really like this old way, because it is very simple and works for most cases.

#include <sstream>
#include <string>


//
//Template function to convert any numerical type 
//into a stl string.
//
template<typename To, typename From> 
std::wstring string_cast(From value) 
{
	std::wostringstream os;
	os << value;
	return os.str();
}

//
//Template function to convert any string that contains 
//a numerical value into its intrinsic value.
//
template<typename To> 
T lexical_cast(const std::wstring& str) 
{
	std::wistringstream is(str);
	T value;
	is >> value;
	return value;
}


int main()
{

    //
    //Start off with any numerical value as a string
    //
    std::wstring srcStringFloat = L"23.45";
    
    //
    //Cast it into the destination type.
    //
    float destFloatValue = 0.0f;
    destFloatValue = lexical_cast<float>(srcStringFloat);
   
    //
    // Now go the other way.
    //
     
    //
    // Start off with any numerical value.
    //
    float srcFloat = -0.00004F;
    
    //
    //Cast the value into a string.
    //
    std::wstring dstStringValue;
    dstStringValue = string_cast<std::wstring>(srcFloat);
    
    return 0;
}

NanoMan EmailMainPermalink Leave a comment
Nov 26
STL String Tokenizer.

Here is an STL idiom to tokenize a string.



#include <strstream>
#include <string>

int main()
{
    //
    //Initial string.
    //
    std::string str("-s er -d ss -t a s");
	
    //
    //String to hold each token.
    //
    std::string value;
    std::istringstream  token(str);

    //
    //Run through the string breaking at every space
    //
    while(std::getline(token, value, ' '))
    {
        std::cout << value << std::endl;
    }
}

NanoMan EmailMainPermalink Leave a comment
Nov 26
Nano Oriented Code Summary
Article Name Author Summary
Tutorial #1 [Strings] Leonard Ponce Uses NOP to design and implement a String class.
Tutorial #2 [Strings] Leonard Ponce Uses NOP to create a more useful string class.
STL String Formatter Leonard Ponce A small class to format STL strings.
Error Helper Leonard Ponce Small class to transcode Win32 errors in std strings.
Class Sealer
Leonard Ponce Template class used to prevent others from deriving from your class.
Use STL to Tokenize strings
Leonard Ponce This is a very simple idiom to tokenize stl strings.
Cast to and From STL strings
Leonard Ponce This is a very simple idiom to convert to and from strings.
Feb 14
STL string formatter.

Here is a small utility class to format stl strings. With a small modification, it can be used to format wchar_t's only.

#include "wchar.h"
#include "stdarg.h"

namespace std
{

static int __cdecl getFormatedLength( const wchar_t* format, va_list args ) throw()
{
	return _vscwprintf( format, args );
}

static int __cdecl formatV( wchar_t* pszBuffer, const wchar_t* pszFormat, va_list args ) throw()
{
	return vswprintf( pszBuffer, pszFormat, args );
}

class format
{
	std::wstring m_formatted;
public:

	format(const wchar_t* format, ...)
	{
		va_list argList;
		va_start( argList, format );

		int length = getFormatedLength(format, argList);
		wchar_t* tempString = (wchar_t*)_alloca( length * sizeof(wchar_t) +1);
		
		formatV( tempString, format, argList );

		va_end( argList );

		tempString[length] = 0;
		m_formatted = tempString;

	}
 
        //
        //We must return a copy of the formatted string.
        //
	operator const std::wstring () const
	{
		return m_formatted;
	}

};


};

//Usage Example
std::wstring theFormatStr = std::format(L"%S now %d", __FUNCTION__, __LINE__);


NanoMan EmailMainPermalink 1 comment
Jan 21
Error Helper

Here is a small utility class to translate Windows API errors and COM errors.

class ErrHelper
{
public:

	static std::wstring getString(HRESULT hr)
	{
		WCHAR* wszError;

	    //Check if it is a window's error
		if(HRESULT_FACILITY(hr) == FACILITY_WINDOWS)
		{
			//translate the code.
			hr = HRESULT_CODE(hr);
		}
		
		::FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | 
						FORMAT_MESSAGE_FROM_SYSTEM, 
						NULL, 
						hr, 
						MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
						(LPWSTR)&wszError, 0, NULL);

		std::wstring wstr = L"Unknown Error";
		if (wszError) 
			wstr = wszError;
		LocalFree(wszError);
		return wstr;
	}
};

//Usage 
//ErrHelper::getString(hr).data();
NanoMan EmailMainPermalink Leave a comment

:: Next Page >>