0

I have a C++ embedded app and I would like to create a web interface to configure it.

My plan is to describe the configuration structs in Cap'nProto then use the generated code on C++ and Javascript side.

A config scenario would be the following:

  1. web app asks for the actual config (Javascript)

  2. native app serves the actual config - serialization/write (C++)

  3. web app displays the actual config after deserialization/read (Javascript)
  4. user can modify the config in the web app - HOW? (Javascript)
  5. web app sends back the new config - serialization/write (Javascript)
  6. native app uses the new config after deserialization/read (C++)
  7. native app can modify the config - HOW? (C++)

4 and 7 are the tricky parts, because as far as I understand the API I can only deserialize a reader that is read only, however I would like to modify and re-serialize it later.

My questions are the followings:

  • Is the described scenario the best approach to do what I want or I should do something totally different?
  • Can I de-serialize a builder? Or somehow transform a reader into a builder (without copying)
  • Should I use the generated C++ / Javascript structs as a direct source of configuration (actual code <-> Cap'nProto structs) or I should introduce "native" structs to interact with (actual code <-> "native" structs <->(serialize/deserialize) Cap'nProto structs)
Broothy
  • 659
  • 5
  • 20

1 Answers1

1

To answer your second question, you can initialize a Builder from a Reader, like:

fooBulider.setBar(someBarReader);

Or for the top-level MessageReader/MessageBuilder:

messageBuilder.setRoot<RootType>(messageReader.getRoot<RootType>());

This does require a copy, but for your use case, that copy is probably not a big deal. Configs usually aren't multi-gigabyte files nor performance-sensitive.

In theory, it is also possible to create a MessageBuilder that is directly initialized from existing message data which it then modifies in-place. However, there are some major caveats with this. See:

https://github.com/capnproto/capnproto/blob/3aa2b2aa02edb1c160b154ad74c08c929a02512a/c++/src/capnp/message.h#L168-L187

Regarding your other two questions, it's really up to you. There are legitimate arguments both ways and it'll really come down to the specific use case and your personal taste.

Kenton Varda
  • 41,353
  • 8
  • 121
  • 105