Small library providing getters/setters

Hi guys,
I've written a small library, allowing you to simply add getter/setters to existings variables, with minimal change in basecode.

https://github.com/m-ri/cpp_GetterSetter_Utilities

Any comment would be useful.

Thanks,
Marco

fix the example in your readme,
you create the variable `positiveVariable', but operate on some undeclared `t'


and on your experiments, (new GetSet_Experiments)->test();
... memory leak.
Well, there is a huge memory leak in line 12 of GetSet_Experiements.cpp.

The function names are a bit confusing at first read-through, but I don't know how to improve on that without a lot more thought than it warrants.

I would change std::function<void(T &, T const &)> _setterFunction; to std::function<void(T const &)> _setterFunction; Then change line 59 to item = _setterFunction(new_value);

You don't need to use this-> in line 61 (and 87).




This is an interesting coding exercise. However, I'm not exactly sure of the benefit of a class like this. If you have 2 different (for instance) integer values--let's say Apples and Oranges--that have different getter and setter functions, you probably don't want to mix them. However, with your code, you could write the following:

1
2
3
4
5
6
7
GetSet<int> apples;
GetSet<int> oranges;
apples.setSetter(appleSetter);
oranges.setSetter(orangesSetter);
apples=5;
oranges=7;
apples=oranges;


It would be better to have an Apple class and an Orange class, each with its own setter built into the class. Then you would not have the danger of mixing apples and oranges.

Why don't you propose some real-life applications for your class? I don't want to dismiss your work without understanding why you did it and how you plan to use it.
Last edited on
If you use it with a pointer type and initialize the variable to nullptr it fails to compile because the compiler doesn't know which constructor it should use.

 
GetSet<int*> p = nullptr;

To fix this issue you might want to consider removing the GetSet(T*) constructor.
Last edited on
Thank you for all your suggestions, this is my first post here and I strongly appreciate your tips.

@ne555
fix the example in your readme,
you create the variable `positiveVariable', but operate on some undeclared `t'

Thanks, I didn't recompiled after this small refactoring.

and on your experiments, ... memory leak.

Now I'm using static methods. Is it better, in you opinion? (or can you suggest me another way to implement this?)



@doug4
I would change std::function<void(T &, T const &)> _setterFunction; to std::function<void(T const &)> _setterFunction;

I suppose you're suggesting the method std::function<T(T const &)> .I've considered your idea, using my way you have also the old/current values. Maybe I can consider a method like std::function<T(T const &,T const &)> , passing old and new values as parameters, I need to consider implicit copy/move constructors (for instance, when I want to maintain old value, or modify the old object in order to release some resource).

Good point about oranges and apples. Maybe it would be cleaner to force the user to indicate getter/setter during declaration,like GetSet<int,funcGetter,funcSetter> apples;, where funcGetter and funcSetter are two functions/functor. Another way would be GetSet<int> apples(funcGetter,funcSetter);, but the user would have now the opportunity to change getter and setter simply by reinitializing the variable.

Why I've written this library, other than play a bit with templates and other C++ features?
Well, in java I've found setter/getter useful, but I've hated the associated boilerplate. Then I've loved Lombok's annotations.

Some scenario:
- debug/tracing: you want to track when some variable is read/written/overwritten (maybe the variable is an object, referencing other resources)
- refactoring: in your code, you're exposing some variable to other class. Now you want to enforce some constrain on assignment, without change all references to what variable (which has to become a method).
- lazy initialization.
Last edited on
Topic archived. No new replies allowed.