Skip to content
another c#-like property accessor for C++11 and above.
C++ CMake Shell Batchfile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs/pictures revue doxygen parameters Feb 13, 2019
examples
scripts
src Update xtd_tunit Dec 29, 2019
tests replace properties and properties.hpp by xtd.properties and xtd.prope… Jul 11, 2019
.appveyor.yml
.codedocs fix properties namespace Feb 21, 2018
.cppcheck_false_positive.txt
.doxygen.txt Update .doxygen.txt Oct 27, 2019
.gitignore first commit Dec 5, 2017
.gitmodules
.travis.yml
CMakeLists.txt add IS_MAIN_PROJECT Oct 10, 2019
LICENSE.md change licensse to MIT Mar 4, 2018
README.md add docs Feb 12, 2019

README.md

properties picture

Build Status Build status Documentation codecov Website license SourceForge Download properties GitHub top language os Windows os macOS os Linux os iOS os android Try online

properties

  • properties add c#-like property accessor to your c++ class.
  • properties is distributed as a single header file.

What is it ?

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

There are three types of properties :

readwrite :

The property accessor can be read and write.

class Foo {
public:
  //...
  
  property_<int> Number {
    get_ {return this->number;},
    set_ {this->number = value;}
  };
  
  //...
  
private:
  int number = 0;
};

readonly :

The property accessor can be read only.

class Foo {
public:
  //...
  
  property_<int, readonly_> Number {
    get_ {return this->number;}
  };
  
  //...
  
private:
  int number = 0;
};

writeonly :

The property accessor can be write only.

class Foo {
public:
  //...
  
  property_<int, writeonly_> Number {
    set_ {this->number = value;}
  };
  
  //...
  
private:
  int number = 0;
};

Read and write properties full example

person.cpp:

#include "properties.hpp"
#include <iostream>
#include <string>

class Person {
public:
  Person() = default;

  // Must be specified because the copy contructor of property class is deleted.
  // The implicit or default copy constructor is not sufficient.
  Person(const Person& person) : name(person.name), age(person.age) {}

  Person& operator=(const Person& person) = default;
  
  // Declare a Name property of type std::string:
  property_<std::string> Name {
    get_ {return this->name;},
    set_ {this->name = value;}
  };
  
  // Declare an Age property of type int:
  property_<int> Age {
    get_ {return this->age;},
    set_ {this->age = value;}
  };
  
  friend std::ostream& operator<<(std::ostream& os, const Person& person) {return os << "Name = " << person.Name << ", Age = " << person.Age;}
   
private:
  std::string name = "N/A";
  int age = 0;
};

int main(int argc, char* argv[]) {
  std::cout << "Simple Properties" << std::endl;
    
  // Create a new Person object:
  Person person;
  
  // Print out the name and the age associated with the pers:
  std::cout << "Person details - " << person << std::endl;

  // Set some values on the pers object:
  person.Name = "Joe";
  person.Age = 99;
  std::cout << "Person details - " << person << std::endl;

  // Increment the Age property:
  person.Age += 1;
  std::cout << "Person details - " << person << std::endl;
}

Console output:

Simple Properties
Person details - Name = N/A, Age = 0
Person details - Name = Joe, Age = 99
Person details - Name = Joe, Age = 100

You can try and play this example here.

For more examples see examples.

See the online Reference guide.

You can’t perform that action at this time.