I have many concrete-structs and I want to designate fields as optional (present or not-present). Just wondering what ideas people have for achieving this. Here is an example struct (fields can be other structs as well, even vectors of structs):
struct LogonMessage_t
{
Header_t header; // this points to another struct containing all primitives
std::string username;
std::string password;
std::vector<LogonOption_t> LogonOptions;
int subaccountid;
std::string Text;
}
I'd like to default all fields to not-present and enable them one by one, perhaps in their setters. Since these structs are generated, a generic method would be preferable.
My ideas so far are:
Btw, using maps or other STL containers to encapsulate the fields won't work here, they need to be concrete structs.
Sounds like you want boost.optional.
Keep it simple. Use a flag member variable that you can set by or-ing constants together and inspect by and-ing them.
Problem with sentinel values is choosing ones that are not also legal field values (now and in the future).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With