ANN: accessorpp -- open source C++ library for data binding and property
https://github.com/wqking/accessorpp
I appreciate if you can give your opinions, review, and suggestions.
## Facts and features
Powerful
* Support arbitrary data type as property or data binding.
* Support event dispatching on changing data.
* Configurable using policies.
Robust
* Well tested. Backed by unit tests.
Flexible and easy to use
* Header only, no source file, no need to build. Does not depend on other libraries.
* Requires C++ 11.
* Written in portable and standard C++, no hacks or quirks.
## License
Apache License, Version 2.0
## Sample code
Simple usage
1 2 3 4 5 6 7
|
#include "accessorpp/accessor.h"
accessorpp::Accessor<int> accessor;
// output 0
std::cout << (int)accessor << std::endl;
accessor = 5
// output 5, no need the explicit type casting, Accessor support the input/output stream operators
std::cout << accessor << std::endl;
|
Customized getter/setter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
accessorpp::Accessor<int> accessor(
// This is the getter
[&accessor]() {
return accessor.directGet();
},
// This is the setter, it limits the value not exceeding 5.
[&accessor](int value) {
if(value > 5) {
value = 5;
}
accessor.directSet(value);
}
);
accessor = 3;
// output 3
std::cout << (int)accessor << std::endl;
accessor = 6;
// output 5
std::cout << (int)accessor << std::endl;
|
Handle on change events
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
struct Policies {
using OnChangingCallback = std::function<void (int)>;
using OnChangedCallback = std::function<void ()>;
};
using AccessorType = accessorpp::Accessor<
int,
Policies
>;
AccessorType accessor;
accessor.onChanging() = [&accessor](const int newValue) {
std::cout << "onChanging: new value = " << newValue << " old value = " << accessor << std::endl;
};
accessor.onChanged() = [&accessor]() {
std::cout << "onChanged: new value = " << accessor << std::endl;
};
// output
// onChanging: new value = 5 old value = 0
// onChanged: new value = 5
accessor = 5;
// output
// onChanging: new value = 38 old value = 5
// onChanged: new value = 38
accessor = 38;
|