Getting consistent type names under Windows and Linux

I'm writing a server/client application in C++, one linux and one windows. The generic communication method is to use the boost::serialization package, but I'll also use a header to distinguish the type of the serialized string.

Here is the struct that the two programs communicate with
1
2
3
4
5
6
7
8
9
10
11
struct SerializeString {
	std::string type_name;
	std::string serialized_string;

	friend class boost::serialization::access;
	template<typename Archive>
	void serialize(Archive& ar, const unsigned version) {
		ar & type_name & serialized_string;
	}

};


When one side wants to pass an object to another side, it uses typeid(T).name() for the field type_name. The receiving side receives SerializeString and checks its type_name against various acceptable structs as follows:

1
2
3
4
5
6
7
8
9
if (ss.type_name == typeid(XXX).name()) {
  // deserialize ss.serialized_string to XXX and do something with it
} else if (ss.type_name == typeid(YYY).name()) {
  // deserialize ss.serialized_string to YYY and do something with it
} else if (ss.type_name == typeid(ZZZ).name()) {
  // deserialize ss.serialized_string to ZZZ and do something with it
} else {
  // etc...
}


This worked fine when I was using Windows VS2013 on two different windows platform. However when I switch one to Linux there is a problem because their typeid(XXX).name() returns different things. So question here, is there a generic way to get some sort of unique names across platform / compiler? Or is my method reasonable at all?
> Or is my method reasonable at all?

It is certainly not portable; not guaranteed to work even with the same compiler/library on the same platform.

std::type_info::name() Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given, in particular, the returned string can be identical for several types and change between invocations of the same program.
http://en.cppreference.com/w/cpp/types/type_info/name


Use boost::serialization::nvp with boost::archive::xml_iarchive and boost::archive::xml_oarchive ?
http://www.boost.org/doc/libs/1_57_0/libs/serialization/doc/wrappers.html#nvp
Hi Borges, I've just read the nvp but can't find anything generic enough so that I can uniquely extract the item type across platforms... Do you know where I can find a better example?
I read that. I was hoping for something simpler.

I guess there ain't no shortcut to anything :(
Sorry I ran the sample code but still can't find anywhere that identifies the type of the serialized object. It shows the name of the variables instead of the type

Basically, I would like to get the type of various objects that is cross platform and unique. Ideally it'll be something that looks like:

1
2
3
string this_type_name1= get_type_name(Class1);
string this_type_name2= get_type_name(Struct2);
string this_type_name3= get_type_name(Class3);
XML archives present a somewhat special case. XML format has a nested structure that maps well to the "recursive class member visitor" pattern used by the serialization system. However, XML differs from other formats in that it requires a name for each data member. Our goal is to add this information to the class serialization specification while still permiting the the serialization code to be used with any archive. This is achived by requiring that all data serialized to an XML archive be serialized as a name-value pair. The first member is the name to be used as the XML tag for the data item while the second is a reference to the data item itself. Any attempt to serialize data not wrapped in a in a name-value pair will be trapped at compile time. The system is implemented in such a way that for other archive classes, just the value portion of the data is serialized. The name portion is discarded during compilation. So by always using name-value pairs, it will be guaranteed that all data can be serialized to all archive classes with maximum efficiency.
http://www.boost.org/doc/libs/1_57_0/libs/serialization/doc/special.html#xml_archives



The most obvious and convient name to assign to as the XML data item name is - surprise! -
the name of the C++ class data member. So our serialization code will look like:
ar & make_nvp("my_variable", my_variable);

To simplify typing and enhance readability a macro is defined so we can write:
ar & BOOST_SERIALIZATION_NVP(my_variable);

http://www.boost.org/doc/libs/1_57_0/libs/serialization/doc/wrappers.html#nvp


For archive portability caveats, see: http://www.boost.org/doc/libs/1_57_0/libs/serialization/doc/special.html#portability

> It shows the name of the variables instead of the type

A class may contain more than one variable of the same type.

Something like this can be done: ar & make_nvp("my_class_type#my_variable", my_variable);
where my_class_type is the C++ name of the type: for example, "std::string"

Creating a macro that does this could be handy.
Last edited on
so useful topic....it helped me so much
www.electrotechnic.mihanblog.com
Topic archived. No new replies allowed.