ANN: accessorpp -- open source C++ library for data binding and property

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;

Last edited on
Um, what is the problem this library solves? Is this casting for cout necessary?
>> Is this casting for cout necessary?

No, the casting is not necessary. I edited the sample code in the original post.

>> what is the problem this library solves?

We can use it to simulate the "property" feature in Qt, or in other languages such as C#.
No, the casting is not necessary. I edited the sample code in the original post.
What about the castings on line 16/18 in 'Customized getter/setter' and line 4 int 'Simple usage'?

We can use it to simulate the "property" feature in Qt, or in other languages such as C#.
The question would be: Is that really be useful? In your example you assign 6 but get 5. Too much of a surprise when you ask me.

More interesting appears to me the onChanging()/onChanged() functionality.

You might consider to pass the new value as reference, so that the user may change the input.
But still: Is it safe that the assigned value is not the result value?
What about the castings on line 16/18 in 'Customized getter/setter' and line 4 int 'Simple usage'?


The castings are there to show the Accessor can be casted and can also not be casted when using with stream operators.

The question would be: Is that really be useful? In your example you assign 6 but get 5. Too much of a surprise when you ask me.


The setter limits the value up to 5. That shows how the setter works.

But still: Is it safe that the assigned value is not the result value?


Again, it's up to how you write the setter. Maybe the setter in the sample code is a little misleading.
In reality, the setter may validate the input value, such as if a string contain enough letters, etc.
Topic archived. No new replies allowed.