For Programmers: Free Programming Magazines  


Home > Archive > VC STL > August 2005 > How to make a class for both ANSI and Unicode?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to make a class for both ANSI and Unicode?
P

2005-08-11, 4:03 am


Hi, I've one SAX class and it supports only ANSI character.
Because my project's setting is Unicode,
I changed all code to support UNICODE.
Then I found it should suffort ANSI code the other place in the same
project.
So, I can't use the technic using #define _Unicode or something like that.

I want to know is there any good solution to use same class to use for
Unicode in one place and on the same hand, use for ANSI code in the same
project.

I tried to use template by

template<typename t_char, typename t_string>
class parser;

typedef parser<char, std::string> parser_a;
typedef parser<wchar_t, std::wstring> parser_w;

but, because it is sax parser, it has callback handler, and
some callback method pass parameter by t_char and t_string, but,
if it is template, it's awful to know what callback method should be called,
and if I specialized this class, I need to write all methods. It's not
template class anymore.

If you any clue, Help me.
Thanks in advance.


Ulrich Eckhardt

2005-08-11, 9:05 am

P wrote:
> Hi, I've one SAX class and it supports only ANSI character.
> Because my project's setting is Unicode,
> I changed all code to support UNICODE.
> Then I found it should suffort ANSI code the other place in the same
> project.
> So, I can't use the technic using #define _Unicode or something like that.
>
> I want to know is there any good solution to use same class to use for
> Unicode in one place and on the same hand, use for ANSI code in the same
> project.


I have a project here, and we support Unicode (various encodings) almost
regardless of whether we're compiling for the wchar_t version of the wi32
API or the char version.

> I tried to use template by
>
> template<typename t_char, typename t_string>
> class parser;


template< typename StringT>
class parser
{
typedef StringT string_type;
typedef string_type::char_type char_type;
};

Alternatively you could also use something like std::basic_string, i.e.

template<typename CharT, typename TraitsT = std::char_traits<CharT>, ...>
class parser
{...};

> If you any clue, Help me.


Following strategy has served us well here: only use wchar_t internally,
converting them to UTF-8 for network or file serialisation and adapting as
necessary when interfacing with win32 or similar APIs.

Uli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com