Object Creation Through XML (Pluggable Factories?)

Started by
14 comments, last by vovansim 19 years, 8 months ago
Well, but you do actually have a neat trick like that. It's called a boost lexical cast. You can learn about it more here, but it all boils down to the syntax:

char* value = "123";int num = lexical_cast<int>(value); //num now contains 123.


And that also works with standard C++ strings, I believe. The only thing to remember here is that that lexical cast actually has runtime overhead, because it's a function call, not a classic cast, and uses C++ streams to convert from one type to the other. But hey, otherwise, you'd have to do it manually any way, so that really doesn't detract that much from performance, specially if you are doing it on startup or during some level loading time, when the user expects to have to wait.

Vovan aka Scorpion
Vovan
Advertisement
Oh, and also, I didn't make typos above. Technically speaking it is called Memento, whatever that is. [smile] Like you can see the Java pattern here: Design Patterns [PDF].

Vovan aka Scorpion
Vovan
That would work, but i would like to not introduce more libs or dependencies than i have to. Those make it harder to finally install on someone else's machine as i've learned.

*cough* ParaGUI *cough* :-)

So far, i'm only using SDL and OpenGL. I'm using TinyXML also, but that doesn't count as a lib since it compiles directly into your project.
Quote:Original post by leiavoia
That would work, but i would like to not introduce more libs or dependencies than i have to. Those make it harder to finally install on someone else's machine as i've learned.

*cough* ParaGUI *cough* :-)

So far, i'm only using SDL and OpenGL. I'm using TinyXML also, but that doesn't count as a lib since it compiles directly into your project.


Haha, yes, indeed. ParaGUI...

Well, I guess you could roll your own quick little version of the caster. It's really easy to do actually. I just made one copy that seems to work just fine. The caster itself is here:

// File: cast.h#include <sstream>namespace cast {	// Just a little exception to throw in case something goes wrong.	// Note here, that you may want to derive from std::bad_cast or	// something, just to make sense out of this class.	class BadCast {};	//Local static stream to use when casting.	static std::stringstream stream;	// This is where the magic happens. You can cast anything into 	// anything else, elthough of course casting say float to double 	// would be silly using this.	template< typename Target, typename Source >	Target lexical_cast(Source arg) {		//Create a placeholder for the result, of the type according to the template.		Target result;		//Clear stream, so if there's old junk in there, it goes away.		stream.clear();		//Stringinize the argument, and check that everything went along fine.		stream << arg;		if (stream.fail()) { throw BadCast(); }		//Convert the string to whatever it is we want here:		stream >> result;		if (stream.fail()) { throw BadCast(); }		//Return the result:		return result;	}}


And so then you can use this as follows:

#include <iostream>#include "cast.h"int main() {	std::string intStr = "123";	std::string floatStr = "3.1415926";	int num = cast::lexical_cast<int>(intStr);	float fPi = cast::lexical_cast<float>(floatStr);	double dPi = cast::lexical_cast<double>(floatStr);	std::cout << "Converted " << intStr << " to (int)" << num << std::endl;	std::cout << "Converted " << floatStr << " to (float)" << fPi << std::endl;	std::cout << "Converted " << floatStr << " to (double)" << dPi << std::endl;	return 0;}


The output of the above then is:

Converted 123 to (int)123Converted 3.1415926 to (float)3.14159Converted 3.1415926 to (double)3.14159Press any key to continue


Of course, the fact that the pi has too few digits up there is only due to the way I printed the float and the double. If you look through the debugger at the real values, no precision was lost in the cast.

Vovan
Vovan
Hey, you're just full of cool tricks! I hope you work professionally. If you don't, some employer out there is really missing out :-)

So basically, if you shove something into and out of a stream, the standard stream will convert it for you, say INT<->STRING ?

Quote:Original post by leiavoia
Hey, you're just full of cool tricks! I hope you work professionally. If you don't, some employer out there is really missing out :-)

So basically, if you shove something into and out of a stream, the standard stream will convert it for you, say INT<->STRING ?


Well, like I said, it's not my trick. Got it from boost. [smile]

But yes, the streams will convert the strings to whatever format you want, and that's I believe simply because the << and >> operators are overloaded for all kinds of different primitive types (and even some objects, like string), so even if you have your own user-defined type, then you can overload these operators to operate on streams, like so:

void operator>>(stringstream str, myType& target) {  str >> target.data1;  str >> target.data2;}


Though of course, you probably won't need that, since the whole idea of this thread was to come up with a more structured way of storing your own types - namely, XML. [smile]

Vovan (aka Scropion)
Vovan

This topic is closed to new replies.

Advertisement