Storing class names in a string?

Started by
26 comments, last by stodge 21 years, 6 months ago
Ignore it. It does not affect the way the program works.

VC''s debugging information cannot store class names longer than 255 characters. Given that std::string and std::map are both typedefs for more complicated (templated) names, the fully expanded names end up being longer than that limit. Hence the "identifier was truncated to ''255'' characters in the debug information".

So, yeah, it''s a VC limitation. If it really bothers you (and it does me ) disable it.
e.g. #pragma warning( disable : 4786 ) There is another related warning to disable, but I can''t remember the code.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Thanks Fruny, I took Martee''s advice (the same as yours) and it now looks much friendlier.
---------------------http://www.stodge.net
Hey, since you''re experimenting, think about that one for a while :


  class BaseObject{public:  virtual ~BaseObject() = 0 {};  virtual BaseObject* Clone() = 0;};class MyObject : public BaseObject{public:  BaseObject* Clone() { return new MyObject(*this); }};...BaseObject* obj = fact.Create( some_name ); BaseObject* clone = obj->Clone(); // creates a second object, with the same type.  


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Let me take this a step further. What I ultimately need is a way of creating an object, which is derived from BaseObject, on the server.

Then the server sends the name of the new object over to the client, which then creates an exact copy of the object using this name.

The killer part is that I can''t hardcode any registering of objects, because I want to be able to add new derived objects without adding new register functions. If that makes sense!

I can''t see how this can be done with these examples.

Thanks again
---------------------http://www.stodge.net
This is perhaps one of the only times that preprocessor MACROS are actually useful in C++

Use a macro to declare and instantiate a static class whose constructor registers the passed classname with the global object factory.
daerid@gmail.com
quote:Original post by stodge
The killer part is that I can''t hardcode any registering of objects, because I want to be able to add new derived objects without adding new register functions. If that makes sense!


You will still have to have the code for the derived class somewhere and, therefore, can put the builder function along with it. Since you seem now to be aiming for a Pluggable Factory (e.g. with new object classes in a dynamic library), you need to register the new classes and functions when the library is loaded.

The API changes depending on the operating system but, usually, a dynamic library ( dll, dso ...) can specify a function to be called when the library is loaded ( with LoadLibrary(), dlopen() ...). It is at this point that you register the builder functions for the objects provided by the library with the server''s factory.

I hope that was clear. Note that I''m not addressing the issues of actually implementing the classes within a library (hint: you can only call virtual or static member functions through a dynamic library boundary)

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by daerid
This is perhaps one of the only times that preprocessor MACROS are actually useful in C++

Use a macro to declare and instantiate a static class whose constructor registers the passed classname with the global object factory.


This will not work, as there are no way (short of using a singleton pattern) to guarantee that the factory object would be initialised before the registrations objects. Aside from that detail, this can be a valid use of preprocessor macros ... but templates would make it work better


  #include <typeinfo>template <class Type> struct Registration{public:   Registration( Factory& fact )   {      fact.Register( typeid(Type).name(), &Builder );   }   static BaseObject* Builder() { return new Type; }};  


This relies on typeid and RTTI to provide the name of the class. The actual string returned by name() is compiler-dependent.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I''m in waaaay over my head now!
---------------------http://www.stodge.net
quote:Original post by stodge
I''m in waaaay over my head now!


Awwwww. Come on, it''s easy

The template parameter lets you choose what Type you are trying to register :

Factory& GetFactory()
{
static Factory fact;
return fact;
}

Registration<FooObject> RegisterFooObject( GetFactory() );
Registration<BarObject> RegisterBarObject( GetFactory() );

Then, within the class, all instances of ''Type'' are replaced with the appropriate type. Which means that Registration<FooObject>::Builder() executes ''return new FooObject;'', and so on.

typeid(Type) is a built-in operator that returns a typeinfo object. This typeinfo object contains (minimal ) data about Type. Which includes the name of the type in typeinfo::name().

More questions ?

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by Fruny
This will not work, as there are no way (short of using a singleton pattern) to guarantee that the factory object would be initialised before the registrations objects. Aside from that detail, this can be a valid use of preprocessor macros ... but templates would make it work better

You might be able to guarantee it by using a base-class that has a static factory in it and then deriving all the pluggable''s from this one.
Are statics guaranteed to be initialize prior to the construction of a class instance? of a baseclass?
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement