improve ptr

This commit is contained in:
Rongsong Shen 2025-04-25 10:21:04 +08:00
parent e1013e038b
commit 3425f75440
3 changed files with 25 additions and 23 deletions

View file

@ -1081,7 +1081,7 @@ static void encode_member(capnp_ctx_t *ctx, struct str *func, struct field *f,
if (n != NULL) {
str_add(func, tab, -1);
str_addf(func, "encode_%s_ptr(cs, &(d->%s), &(s->%s));\n", n->name.str,
str_addf(func, "encode_%s_ptr(cs, &(d->%s), s->%s);\n", n->name.str,
var, var2);
}
break;
@ -1315,12 +1315,14 @@ void mk_struct_ptr_decoder(capnp_ctx_t *ctx, struct node *n) {
}
str_addf(&(ctx->SRC),
"void decode_%s_ptr(%s *d,"
"void decode_%s_ptr(%s **d,"
"%s_ptr p) {\n",
n->name.str, buf, n->name.str);
str_addf(&(ctx->SRC), "\tstruct %s s;\n", n->name.str);
str_addf(&(ctx->SRC), "\t*d = (%s *)calloc(1, sizeof(%s));\n",
buf, buf);
str_addf(&(ctx->SRC), "\tread_%s(&s, p);\n", n->name.str);
str_addf(&(ctx->SRC), "\tdecode_%s(d, &s);\n", n->name.str);
str_addf(&(ctx->SRC), "\tdecode_%s(*d, &s);\n", n->name.str);
str_addf(&(ctx->SRC), "}\n");
}
@ -2092,7 +2094,7 @@ static void mk_codec_declares(capnp_ctx_t *ctx, const char *n1,
str_addf(&(ctx->HDR),
"void encode_%s_ptr(struct capn_segment*, %s_ptr *, %s *);\n", n1,
n1, n2);
str_addf(&(ctx->HDR), "void decode_%s_ptr(%s *, %s_ptr);\n", n1, n2, n1);
str_addf(&(ctx->HDR), "void decode_%s_ptr(%s **, %s_ptr);\n", n1, n2, n1);
}
static void declare_codec(capnp_ctx_t *ctx, struct node *file_node) {
struct node *n;

View file

@ -22,7 +22,7 @@ typedef struct {
char **authors;
int n_chapters;
chapter_t *chapters_;
publish_t publish;
publish_t *publish;
int n_magic1;
uint32_t *magic_1;
int acquire_method;

View file

@ -44,7 +44,7 @@ int encode() {
book.authors = authors;
book.n_chapters = 3;
book.chapters_ = &chapters[0];
memcpy(&(book.publish), &publish, sizeof(publish));
book.publish = &publish;
book.n_magic1 = 2;
book.magic_1 = &magic1[0];
book.acquire_method = Book_acquire_buy;
@ -67,7 +67,7 @@ int encode() {
int decode() {
struct capn c;
Book_ptr p;
book_t book;
book_t *book;
int i;
capn_init_fp(&c, stdin, 0);
@ -75,36 +75,36 @@ int decode() {
decode_Book_ptr(&book, p);
printf("title: %s\n", book.title);
printf("title: %s\n", book->title);
printf("authors(%d):\n", book.n_authors);
printf("authors(%d):\n", book->n_authors);
for(i = 0; i < book.n_authors; i ++) {
printf("\t%s\n", book.authors[i]);
for(i = 0; i < book->n_authors; i ++) {
printf("\t%s\n", book->authors[i]);
}
printf("chapters(%d):\n", book.n_chapters);
for(i = 0; i < book.n_chapters; i ++) {
printf("\tcaption: %s\n", book.chapters_[i].caption);
printf("chapters(%d):\n", book->n_chapters);
for(i = 0; i < book->n_chapters; i ++) {
printf("\tcaption: %s\n", book->chapters_[i].caption);
printf("\tfrom %d to %d\n",
book.chapters_[i].start,
book.chapters_[i].end);
book->chapters_[i].start,
book->chapters_[i].end);
}
printf("ISBN: %lu year: %u\n",
book.publish.isbn,
book.publish.year);
book->publish->isbn,
book->publish->year);
printf("magic1:\n");
for(i = 0; i < book.n_magic1; i ++) {
printf("\t%d\n", book.magic_1[i]);
for(i = 0; i < book->n_magic1; i ++) {
printf("\t%d\n", book->magic_1[i]);
}
if (book.acquire_method == Book_acquire_buy) {
printf("%s\n", book.acquire.buy);
if (book->acquire_method == Book_acquire_buy) {
printf("%s\n", book->acquire.buy);
}
else {
printf("%s\n", book.acquire.donation);
printf("%s\n", book->acquire.donation);
}
return 0;