tags:

views:

77

answers:

2

I'm seeking of an API that can be used to dump most data structures,is there one in c/c++?

+1  A: 

boost has a serialisation library you can explicitly use to make your data structures dumpable.

If you want it to happen more automatically, your options are bleak. A C++ program can inspect its own debug symbols, or compile up some extra code - perhaps auto-generated with reference to GCC-XML output, or using a tool like OpenC++ to auto-generate some meta-data.

Tony
+3  A: 

I'm seeking of an API that can be used to dump most data structures,is there one in c/c++?

Short answer: No, there is not.

Longer answer: C++ doesn't have reflection. That is, there is no way to analyze unknown data structures at runtime. You will have to have to write dump routines yourself for any data structure you want to dump, building on what's available for its data members.

However, note that C++ has a whole lot of tools to make that easier. For example, given a simple generic dump() template:

template< typename T >
inline void dump(std::ostream& os, const T& obj) {os << obj;}

the elements of any sequence can be dumped using this simple function:

template< typename OutIt >
void dump(std::ostream& os, OutIt begin, OutIt end)
{
  if(begin != end)
    os << *begin++;
  while(begin != end) {
    os << ", ";
    dump(*begin++);
  }
}
sbi
Need a specialisation for `std::pair<>` to dump set<>s and map<>s. Some complications e.g. escaping strings, floating point precision....
Tony
@Tony: Yes, for maps and multimaps (not for sets, though) there's indeed an overload necessary. (No specialization, BTW, as this would have to be a _partial_ specialization, which we don't have for function templates, because there's overloading...) And, yes, for some other types you might want to add more overloads. However, I deliberately left this a simple example. It already gets you quite far as it is.
sbi