Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Automatic syntax line break C++

This thread is locked; no one can reply to it. rss feed Print
Automatic syntax line break C++
Mark Oates
Member #1,146
March 2001
avatar

Hey guys,

I'm working on documentation and have definitions that are too long to fit on a single line:

{"name":"610553","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/0\/2026d678149903eb86fd0e6adc9902f3.png","w":844,"h":144,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/0\/2026d678149903eb86fd0e6adc9902f3"}610553

The code is automatically parsed from the source and then regenerated by the parser so I can't just properly format it in the source itself. I'm looking for something, preferably in python, that can properly add line breaks in the appropriate places. Any ideas?

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

torhu
Member #2,727
September 2002
avatar

Uh, have you tried using Doxygen?

Mark Oates
Member #1,146
March 2001
avatar

Doxygen is pretty hideous in my opinion. I chose to go the custom route so that I could keep my commenting and code style clean.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

torhu
Member #2,727
September 2002
avatar

How does Doxygen care about your coding style? And you can enable Javadoc style comments, they look much better than the default Qt style ;)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

The code is automatically parsed from the source and then regenerated by the parser so I can't just properly format it in the source itself.

I'm not sure what you mean by that.

Are you saying you can't do something like this?

int my_really_long_function(
                            parameter 1,
                            parameter 2,
                            other_parameter 3
);

SiegeLord
Member #7,827
October 2006
avatar

Are you in control of the parser/formatter? If so, why not do it there?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

bamccaig
Member #7,536
July 2006
avatar

I'm not sure it's so much "science" wrapping long lines as art. I'm not sure there's existing software that will get this right, even if you could interface it with your documentation engine. That said, I'm assuming this is HTML/CSS being rendered in a browser. If so I imagine fixing the styling of that (i.e, getting that to wrap) will be easier and sufficient to make the documentation usable. Am I way off? Thoughts?

Mark Oates
Member #1,146
March 2001
avatar

torhu said:

How does Doxygen care about your coding style? And you can enable Javadoc style comments, they look much better than the default Qt style ;)

Haha, that's pretty much what I was referring to. :D I don't want any documentation in my source.

SiegeLord said:

Are you in control of the parser/formatter? If so, why not do it there?

Hmm, that's a possibility. I am considering filtering the string to remove extra spaces between the '<' and '>' characters that are introduced by the parser. It might be easiest to introduce a simple rule for lines that are too long. A rule like "line breaks at non-nested commas if a line is too long" could do the job.

Bjarne Stroustrup might say I should simplify my code, and that's also a good suggestion. I could use more typedefs to simplify this:

static bool Attributes::create_datatype_definition(std::string datatype_identifier,
   bool (*)( void *,::std::string ) * to_val_func,
   std::string (*)( void * ) * to_str_func)

to this:

static bool Attributes::create_datatype_definition(
   std::string datatype_identifier,
   ToValueFunc *to_val_func,
   ToStringFunc *to_str_func)

But I wonder if that would be helping or hurting.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Polybios
Member #12,293
October 2010

I've come to like doxygen, it pretty much just works. Didn't like documentation in the source either, but you can write separate files, too.

Btw., why not use std::function here?

Mark Oates
Member #1,146
March 2001
avatar

Polybios said:

Btw., why not use std::function here?

I've never actually used std::function, and I'm not sure what advantage it might provide. My naive guess would be to implement it like this:

static bool Attributes::create_datatype_definition(
   std::string datatype_identifier,
   std::function<bool(void*, std::string)> to_val_func,
   std::function<std::string(void*)> to_str_func)

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Polybios
Member #12,293
October 2010

Advantages:
Basically, you can take anything callable (functions, member functions with instances or without, functor objects, lambdas with or without captures) and turn it into a std::function. std::bind will let you produce a function with some arguments already bound or you could pass a function that takes less arguments.

Disadvantage:
Since the std::function may need to store some "context" (captures, bound values), it incurs a slight overhead.

Examples:
https://oopscenities.net/2012/02/24/c11-stdfunction-and-stdbind/

I've found it especially useful to bind a member function to an object's instance ("this" pointer), e.g.

#SelectExpand
1#include <functional> 2#include <iostream> 3 4class Foo { 5 public: 6 void bar() { 7 std::cout << "bar called" << std::endl; 8 } 9 10 void bar2(int i, int j) { 11 std::cout << "bar2 called: " << i << ", " << j << std::endl; 12 } 13}; 14 15 16int main() { 17 Foo foo; 18 19 std::function<void()> f = std::bind(&Foo::bar, &foo); 20 std::function<void()> g = std::bind(&Foo::bar2, &foo, 2, 4); 21 std::function<void(int,int)> h = std::bind(&Foo::bar2, &foo, std::placeholders::_1, std::placeholders::_2); 22 std::function<void(int,int)> t = std::bind(&Foo::bar2, &foo, std::placeholders::_2, std::placeholders::_1); 23 std::function<void(int,int)> u = std::bind(&Foo::bar, &foo); 24 25 26 f(); // will call foo.bar() 27 g(); // will call foo.bar2(2, 4) 28 h(2, 4); // will also call foo.bar2(2, 4) 29 t(4, 2); // will also call foo.bar2(2, 4) ;) 30 u(4, 2); // will omit the arguments and call bar 31} 32 33/** output: 34bar called 35bar2 called: 2, 4 36bar2 called: 2, 4 37bar2 called: 2, 4 38bar called 39**/

Edit: Lambdas without captures will decay (I think that's how they call it) into a function pointer, but for lambdas with captures, you'll need a std::function.

Gideon Weems
Member #3,925
October 2003

I'm looking for something, preferably in python, that can properly add line breaks in the appropriate places.

Wha--? Am I missing something? You dorks didn't mention textwrap? ;)

Go to: