Giter VIP home page Giter VIP logo

cpp-property's Introduction

cpp-property

C#-like property for C++20.

Features

  • C# like syntax.
  • Minimum overhead.
  • Operator overloadings for transparent accesses.
  • Compile-time checks for dangling reference.

Example

Basic Usage

#include <iostream>
#include "cpp_property.hpp"

// macro for importing keywords of properties without namespace specifier
import_cpp_property();

class A
{
    double num_ = 0;

public:
    property<const double&> num
    {
        get_cref
        {
            return num_;
        },
        set_val
        {
            // "value" is implicit argument like C#
            if (value < 0) throw std::invalid_argument("value must be >= 0");
            num_ = value;
        }
    };
};

...

auto a = A();

// set access
a.num = 1.0;

// get access
std::cout << a.num << std::endl;

// operator+= with accessor check
a.num += -2.0  // throw

Auto-Implemented Getter and Setter

The trivial getters and setters can be implemented automatically, which is more efficient and has minimum overhead when optimized.

#include "cpp_property.hpp"

import_cpp_property();

class A
{
    double num_ = 0;

public:
    // auto-impelemented getter
    property<const double&> num_auto_get
    {
        get_auto { num_ },
        set_val
        {
            if (value < 0) throw std::invalid_argument("value must be >= 0");
            num_ = value;
        }
    };

    // auto-impelemented setter
    property<const double&> num_auto_set
    {
        get_cref
        {
            return num_;
        },
        set_auto { num_ }
    };

    // auto-impelemented getter and setter
    property<const double&> num_auto_both
    {
        get_auto { num_ },
        set_auto { num_ }
    };
};

Get/Set-Only Properties

It may be simpler to implement the function directly, but get/set-only properties are also available.

#include "cpp_property.hpp"

import_cpp_property();

class A
{
    double num_ = 0;

public:
    // get-only property is specified in the template argument
    property<double, get_only> numsq_get_only
    {
        get_val
        {
            return num_ * num_;
        }
    }

    // set-only property is specified in the template argument
    property<double, set_only> num_set_only
    {
        set_val
        {
            num_ = value;
        }
    };

    // syntax like "Expression bodies on method-like members in C#"
    property<double, get_only> numsq_get_only2 = get_val { return num_ * num_; };
    property<const double&, get_only> num_get_only_auto = get_auto { num_ };
    property<double, set_only> num_set_only_auto = set_auto { num_ };
};

Auto-Implemented Properties

Auto-implemented properties have backing fields. The difference with C# is that the return type can be specified.

#include "cpp_property.hpp"

import_cpp_property();

class A
{
    double num_;

public:
    // auto_property includes backing field
    auto_property<double> num { get, set, 3.14 };

    // get/set-only auto-implemented property
    auto_property<const double&, get_only> num_get_only { get, num_ };
    auto_property<double&, set_only> num_get_only { set, num_ };
};

Future Issues

  • Property types cannot be deduced in member types and look dirty.
  • Reduce overhead of std::function
    • Is std::move_only_function in C++23 more efficient?

Author

Yoshiki Matsuda (@yosh-matsuda)

cpp-property's People

Contributors

yosh-matsuda avatar

Stargazers

TS avatar hatsuka20_ avatar  avatar  avatar

Watchers

 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.