Bootstrap

C++仿QML属性动态绑定


#include "property_bindings/src/property.h"
#include <iostream>

int calculateArea(int width, int height) {
  return (width * height) * 0.5;
}

struct rectangle {
  property<rectangle*> parent = nullptr;
  property<int> width = 150;
  property<int> height = 75;
  property<int> area = [&]{ return calculateArea(width, height); };

  property<std::string> color = [&]{
    if (parent() && area > parent()->area)
      return std::string("blue");
    else
      return std::string("red");
  };
};

int test() {
  rectangle parent;
  rectangle child;
  std::cout << child.color() << std::endl;
  //outputs "red"
  child.parent = &parent;
  parent.width = 2;
  std::cout << child.color() << std::endl;
  //outputs "blue"
  return 1;
}

https://github.com/woboq/property_bindings


创作不易,小小的支持一下吧!

;