Map set / get requests in C ++ class / change structure

I'm trying to figure out what works best here. Basically I have a system where I get external requests to set / get values ​​in my model. The problem is that my model consists of C ++ classes that can be nested, while queries are simple (key, value) pairs.

For instance:

struct Foo { void setX(int x); int getX() const; struct Boo { void setY(float y); float getY() const; }: }; 

If I get a request that says set(y, 21) for a given element e, then the actions I need to perform will be different depending on whether foo and boo already exist. To take care of the various possibilities for each property, a lot of code will be written in the end.

Before inventing the wheel, I was wondering if there is a library in C ++ or a well-known technique that allows you to display these flat actions in changes in C ++ structures (which can be nested) in a general way.

thanks

+1
source share
1 answer

Boost has property maps for this purpose.

The simplest interface it provides is

 get(map, key) put(pmap, key, val) 

For lvalue / readable maps, you can also access the indexer style

 pmap[key]; pmap[key] = newval; // if not const/readonly 

You can use existing property map adapters:

  • identity_property_map and typed_identity_property_map
  • function_property_map
  • iterator_property_map
  • shared_array_property_map
  • associative_property_map
  • const_associative_property_map
  • vector_property_map
  • ref_property_map
  • static_property_map
  • transform_value_property_map
  • compose_property_map

or write your own.

There is even dynamic_property_map , which in practice looks like this:

 put("age",properties,fred,new_age); put("gpa",properties,fred,new_gpa); 

Please note that age and gpa can be saved anywhere (even if a web request is required, perhaps), but the difference in access is abstracted from the smartymap interface that is between them.


Sample answers:

+2
source

Source: https://habr.com/ru/post/1216465/


All Articles