cyclonedds-cxx icon indicating copy to clipboard operation
cyclonedds-cxx copied to clipboard

Print topic details

Open zephod77 opened this issue 3 years ago • 7 comments

Is there a generic way to print the details of a topic? Something like
org::eclipse::cyclonedds::core::cdr::write<T>(stream, instance) except where stream can be a std::stream derivative type instead of org::eclipse::cyclonedds::core::cdr::basic_cdr_stream.

zephod77 avatar Dec 20 '21 18:12 zephod77

If by "the details of a topic" you mean as text, there isn't really, but the lower-level interfaces have something you can put to use:

struct ddsi_serdata *x;
dds_sample_info_t si;
if (dds_readcdr (reader, &x, 1, &si, DDS_ANY_STATE) == 1) {
  if (si.valid_data) {
    char buf[1024];
    (void) ddsi_serdata_print (x, buf, sizeof (buf));
    puts (buf);
  }
  ddsi_serdata_unref (x);
}

This works in the C API, but the C++ API currently doesn't provide a real implementation of that "print" function and just makes buf an empty string (unless I have forgotten merging something — @reicheratwork, perhaps I have?). It is possible to combine the two languages in a single executable, so you can do it from C++ but it isn't quite so easy.

I won't bother you with the fact that Python — of course — supports this sort of thing natively ...

eboasson avatar Dec 20 '21 18:12 eboasson

Yes, as text is what I meant. That's too bad, In that case I'd like to put in a feature request for the IDL compiler to generate an operator<<() method. The write template I mentioned already has most of it done. There will clearly be complications with compound types and formatting.

Yeah, the app is not going to get re-written in Python but is there a possibility to use boost.python to call a Python method from my C++ code?

zephod77 avatar Dec 20 '21 20:12 zephod77

@reicheratwork, @zephod77 has a point, and while I wouldn't want to suggest adding it to the existing C++ backend, perhaps it would be easy to add it in https://github.com/eclipse-cyclonedds/cyclonedds-cxx/pull/178, or, perhaps better still, as a separate PR on top of that?

@zephod77 I have no idea whether boost.python route can be done, I simply don't know boost and python well enough for that sort of trick (I'd be able to help you if it were C and Perl ... shows my age 😁). I could show you how to do it using a bit of templated C++ code using the C API and with a bit of digging below the surface. It'd be a hack, but 'tis holiday season and I don't know if any of the proper ways of doing this would be possible before the year is over.

eboasson avatar Dec 21 '21 09:12 eboasson

C and Perl? Ha! I started programming in FORTRAN on an ICL mainframe and in assembly on an Intel 8080 (cue Monty Python's Good Old Days sketch) so I'm pretty sure I've got you beat in the age department. No worries, I'll work around it for now.

zephod77 avatar Dec 21 '21 15:12 zephod77

So basically printing text to an ostream in stead of CDR data to a byte stream.

@zephod77 I assume that by topic details you mean (instance) level information (what they call in the DDS XTypes spec: data representation), like the names and values of fields and not topic (class) level information like member names and types (type representation)?

So for the topic (in IDL): struct msg { long userID; string message; }; which has an instance with: msg.userID = 12345, msg.message = "Why hello there!"

The text output writer would then put something like the following in the output stream: msg { userID: 12345, message: "Why hello there!" } or something similar

This could also be something that can also be combined with supporting the XML data representation mode as described by the DDS XTypes Specification v1.3 section 7.4.4, your thoughts @eboasson ?

reicheratwork avatar Dec 22 '21 10:12 reicheratwork

You've got the idea but as I said I can see some complications. For example:

struct t { int hour, int min, int sec );
struct x { string s; t t1 };

and given instances my_t = { 1,2 3 }; and my_x = { "abc", my_t }; the output would be "my_x { s: "abc", my_t: { hour: "1", min: "2", sec: "3" }}"

It would be nice to allow different formatting, eg change ':' for " = ' and ',' for '\n' in the above output in which case it might be better to have a method that returns a vector of pairs and allow the user to format however they want.

Just thinking out loud here.

zephod77 avatar Dec 22 '21 18:12 zephod77

C and Perl? Ha! I started programming in FORTRAN on an ICL mainframe and in assembly on an Intel 8080 (cue Monty Python's Good Old Days sketch) so I'm pretty sure I've got you beat in the age department.

Sounds like it! 😀

Well, at least I have had the privilege of seeing a version of SPLICE — i.e., the first pub/sub-based global data space, i.e., the one true ancestor of DDS — running on a bunch of Atari ATW800s! Not quite the same as having used those, of course, though I am sure I have pressed a few keys on the keyboard ...

eboasson avatar Dec 23 '21 09:12 eboasson