mlot / nanopb

Automatically exported from code.google.com/p/nanopb
zlib License
0 stars 0 forks source link

Use #defines for expressing max_size/max_count #103

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Suppose I have the following structure

message Foo {
    required bytes bar = 1 [(nanopb).max_size = 40];
}

nanopb generates the following:

typedef struct {
    size_t size;
    uint8_t bytes[16];
} Foo_bar_t;
typedef struct _Foo {
    Foo_bar_t pin;
} Foo;

It would be nice if it used

#define Foo_bar_max_size 16
typedef struct {
    size_t size;
    uint8_t bytes[Foo_bar_max_size];
} Foo_bar_t;

So I could do error checking in my code using this macro.

Original issue reported on code.google.com by rusn...@gmail.com on 8 Feb 2014 at 9:39

GoogleCodeExporter commented 9 years ago
Could you perhaps use   pb_membersize(Foo, bytes)  instead? That avoids 
generating a bunch of messy #defines.

Also, if you already have a variable of type Foo declared, you can use just  
sizeof(Foo.bytes).

Original comment by Petteri.Aimonen on 8 Feb 2014 at 9:43

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Ah, OK, got it. I guess your suggestion solves this issue. For max_count you 
would use pb_arraysize right?

Original comment by rusn...@gmail.com on 8 Feb 2014 at 9:50

GoogleCodeExporter commented 9 years ago
Indeed. Those are the same ones as nanopb uses internally.

Original comment by Petteri.Aimonen on 8 Feb 2014 at 9:51

GoogleCodeExporter commented 9 years ago
Feel free to close then. Using pb_membersize/pb_arraysize makes sense and has 
even better readability then introducing new macros.

Original comment by rusn...@gmail.com on 8 Feb 2014 at 9:53