Code Monkey home page Code Monkey logo

propertyclass's Introduction

PropertyClass

this is just a test and a try for property in C++.

There are no get and set functions with the fields in C++. So this project is a try to wrapper the member variable to provide the similar function.

Usage

template<typename T>
class{
    T _t;
  public:
  //read-write variable
  Property<T> t{
  //get
    [this](){
        return _t;
    },
  //set
    [this](auto v){
        if(fun(c)){
            std::cout<<"c has some ...(whatever you wanna say)"<<std::endl;
        	return;
        }
      	_t = v;
      
    },
  //initial value
    T()
  };
  
  //read-only variable
  Property<T> read_only_t{
    [this](){
        return _t;
    }  
  };
  
}

Oh, it's a little ugly but it's more concise than Macro(maybe). The constructor of Property has the following signature.

Property(get,set,init_val)

Set && Get

set should be a lambda with the signature like [this](auto val){/*something*/}, and you can do something to restrict the value of val

get should be a lambda with the signature like [this](){return someval}, and you can do something you like.

Defect

Initially, I want to use exception, but it doesn't work. I will look for a solution.

propertyclass's People

Contributors

kidsunbo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.