0

Can I use an existing code range (means some lines of code from a cpp file) as example in the doxygen docu?

/*!
 * \brief My Foo class
 * \details More about foo
 * \example?? Foo used like here, some LOC from cpp follow (how can I do that??)
 */
class Foo  

I want to show 3-10 lines of existing code of how I have used Foo. Those 3-10 line are supposed to come from a cpp file.

I understand that I have to tag the very cpp file with \file, but how do I refer to some lines of code there?

Something like this (incorrect pseudo code, as I have no idea how to do it)

somewhereelse.cpp
...
...
//! \example Foo (supposed to display with Foo docu). This is how Foo is used
//! @{
Foo f;
f.init(); // init first
f.start(); // then start
//! }@
...
Horst Walter
  • 13,663
  • 32
  • 126
  • 228
  • 1
    Seems like a maintainance burden, no? If you add/remove lines in the file where the lines have to come from you have to update the documentation in another file? – stijn Mar 02 '16 at 19:01
  • No, exactly the opposite. I mark a range for the example and when I change the real world code the documentation is updated. Now it is a maintenance nightmare as the example is copy and paste. When the real code is changed you have to find and update the documentation elsewhere. – Horst Walter Mar 02 '16 at 19:03
  • That's not really the opposite, it's only shifting around what has to be updated: now you copy/paste code, when referring to lines you'll be adjusting line numbers. Number of keystrokes/mouseclicks to keep both in sync won't differ much. – stijn Mar 02 '16 at 19:05
  • Who has said that the reference has to be marked by line numbers. I do not know how to mark such a range, that``s why I ask. It could be block with a tag I can reference, then your statement is wrong. As said, I have no idea if this is possible and how to do it. Do you? After I know that I can tell if it is suitable for my problem. – Horst Walter Mar 02 '16 at 19:09
  • did you have a look at the \snippet possibilities or the \dontinclude / \skip / ... commands? – albert Mar 02 '16 at 19:28
  • Not yet, have looked at the `\code` command, but snippet looks promising – Horst Walter Mar 02 '16 at 19:35
  • After some fiddling around and some examples as her http://stackoverflow.com/a/16034375/356726 it does exactly what I want. And no static line numbers referenced, but a surrounding tag (so no maintenance nightmare). albert if you post your hint as answer i will accept it. – Horst Walter Mar 02 '16 at 21:34

1 Answers1

0

As pointed out by albert (comments above) \snippet can be used. A full example can be found here https://stackoverflow.com/a/16034375/356726

You refer to a file and in that file (.cpp in my scenario) you mark the block by [] brackets. It then will appear as code block.

Community
  • 1
  • 1
Horst Walter
  • 13,663
  • 32
  • 126
  • 228