#include <iostream>
#include <functional>
template < typename T >
class my_ {
T value;
std:: function < void ( T const & ) > notifier;
public :
template < typename F >
my_( T const & init, F f )
: value( init ) , notifier( f ) { }
// for write
T& operator= ( T const & next ) {
if ( ! std:: equal_to < T> ( ) ( value, next) )
notifier( next ) ;
return value = next;
}
// for read
operator T const & ( ) const
{ return value; }
} ;
struct Foo {
Foo( int init )
: value( init,
[ ] ( int const & next ) {
std:: cout << "\t value changed to :" << next << std:: endl ;
} )
{ }
my_< int > value;
} ;
int main( ) {
Foo foo( 2 ) ;
// write
int new_values[ ] = { 1 , 1 , 1 , 2 , 3 , 3 } ;
size_t const size = sizeof ( new_values) / sizeof ( * new_values) ;
for ( size_t i = 0 ; i ! = size; ++ i ) {
std:: cout << "set in index: " << i << std:: endl ;
foo.value = new_values[ i ] ;
}
// read
std:: cout << "final value: " << foo.value << std:: endl ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KIAp0ZW1wbGF0ZSA8IHR5cGVuYW1lIFQgPgpjbGFzcyBteV8gewogICAgVCB2YWx1ZTsKICAgIHN0ZDo6ZnVuY3Rpb248dm9pZChUIGNvbnN0ICYpPiBub3RpZmllcjsKIHB1YmxpYzoKICAgIHRlbXBsYXRlIDwgdHlwZW5hbWUgRiA+CiAgICBteV8oIFQgY29uc3QgJiBpbml0LCBGIGYgKQogICAgOiB2YWx1ZSggaW5pdCApLCBub3RpZmllciggZiApIHsgIH0KICAgIC8vIGZvciB3cml0ZQogICAgVCYgb3BlcmF0b3I9KCBUIGNvbnN0ICYgbmV4dCApIHsKICAgICAgICBpZiggIXN0ZDo6ZXF1YWxfdG88VD4oKSh2YWx1ZSwgbmV4dCkgKQogICAgICAgICAgICBub3RpZmllciggbmV4dCApOwogICAgICAgICAgICAKICAgICAgICByZXR1cm4gdmFsdWUgPSBuZXh0OwogICAgfQogICAgLy8gZm9yIHJlYWQKICAgIG9wZXJhdG9yIFQgY29uc3QgJiAoKSBjb25zdAogICAgeyByZXR1cm4gdmFsdWU7IH0KfTsgCiAKc3RydWN0IEZvbyB7CiAgICBGb28oIGludCBpbml0ICkKICAgIDogdmFsdWUoIGluaXQsIAogICAgICAgICAgICAgW10oIGludCBjb25zdCAmIG5leHQgKSB7CiAgICAgICAgICAgICAgICAgc3RkOjpjb3V0IDw8ICJcdHZhbHVlIGNoYW5nZWQgdG8gOiIgPDwgbmV4dCA8PCBzdGQ6OmVuZGw7IAogICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgfSApCiAgICB7IH0KICAgIAogICAgbXlfPGludD4gdmFsdWU7ICAKfTsKIAppbnQgbWFpbigpIHsKICAgIAogICAgRm9vIGZvbyggMiApOwogICAgLy8gd3JpdGUKICAgIGludCBuZXdfdmFsdWVzW10gPSB7IDEsIDEsIDEsIDIsIDMsIDMgfTsKICAgIHNpemVfdCBjb25zdCBzaXplID0gc2l6ZW9mKG5ld192YWx1ZXMpIC8gc2l6ZW9mKCpuZXdfdmFsdWVzKTsKICAgIAogICAgZm9yKCBzaXplX3QgaSA9IDA7IGkgIT0gc2l6ZTsgKytpICkgewogICAgICAgIHN0ZDo6Y291dCA8PCAic2V0IGluIGluZGV4OiAiIDw8IGkgPDwgc3RkOjplbmRsOwogICAgICAgIGZvby52YWx1ZSA9IG5ld192YWx1ZXNbIGkgXTsKICAgIH0KIAogICAgLy8gcmVhZAogICAgc3RkOjpjb3V0IDw8ICJmaW5hbCB2YWx1ZTogIiA8PCBmb28udmFsdWUgPDwgc3RkOjplbmRsOwp9