Tuesday, December 17, 2013

My two string classes could be "templatized" into one, but for one problem...

Newsgroup: comp.lang.c++

Subject: My two string classes could be "templatized" into one, but for one problem...

From: DSF <notavalid@...>

Date: Mon, 09 Dec 2013 00:52:07 -0500



Hello group!



Under Windows, I have two string classes: FStringA and FStringW, as

per Windows' naming convention. I have done a lot of work recently on

the Wide version and decided to update the ANSI version. As I sat

there with two windows synchronized, looking for updates from W to

apply to A, it occurred to me that these lengthy classes are identical

save for two differences, and would be ideal as a template. That way

only one code set to update!



The first difference is simple: FStringA uses char whilst FStringW

uses wchar_t. If that were the only difference, it would be a piece

of cake. But it's never that simple, is it?



The second difference is the plethora of C sting calls these classes

use. I've rewritten the parts of the string RTL that I use, both for

a speed increase and to handle wide strings. I preface a 'd' to the

front since 'str' is reserved for the RTL. This leaves me with many

'dstringsomethingA' and 'dstringsomethingW' calls.



But how do I get the compiler to determine which call to make when

building the template into code? I could put preprocessor if/else

around all the d...A/Ws. That is actually built-in to them (based on

the definition of UNICODE), but the implementation seems sloppy. I

would have to preface each creation with preprocessor code such as:



// This is off the top of my head and I'm not even sure it would work,

// but it's clumsy anyway.

// FStr being the template class

typedef FStr<char> FStringA;

typedef FStr<wchar_t> FStringW;



#ifdef UNICODE

#define UCFLAG

#undef UNICODE

#endif

FStringA title;

#ifdef UCFLAG

#undef UCFLAG

#define UNICODE

#endif



#ifndef UNICODE

#define UCFLAG

#define UNICODE

#endif

FStringW name;

#ifdef UCFLAG

#undef UCFLAG

#undef UNICODE

#endif



Ugh!



So it's the old question of determining the type at compile time,

perhaps simplified because there are only two choices. Also, it would

be nice to throw a compilation error if the type is not one of the

two.



Any ideas?



TIA!

"'Later' is the beginning of what's not to be."

D.S. Fiscus







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?146527-My-two-string-classes-could-be-templatized-into-one-but-for-one-problem&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

No comments:

Post a Comment