GithubHelp home page GithubHelp logo

sean-parent / value_types Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jbcoe/value_types

0.0 0.0 0.0 175 KB

value types for composite class design - with allocators

License: MIT License

C++ 87.14% CMake 10.28% Starlark 2.58%

value_types's Introduction

Value types for composite class design

codecov language license issues

This repository contains two class templates: indirect and polymorphic. Both templates are designed to be used for member data in composite types.

  • An instance of indirect<T> owns an object of class T.

  • An instance of polymorphic<T> owns an object of class T or a class derived from T.

Both classes behave as value types and allow special member functions for a class that contains them as members to be generated correctly. Our experience suggests that use of these class templates can significantly decrease the burden of writing and maintaining error-prone boilerplate code.

Standardization

We'd like to see indirect and polymorphic included in a future version of the C++ standard. Prior work on standardizing similar types, indirect_value and polymorphic_value can be found at

Design of these two types is so deeply coupled that future work will proceed in an updated paper.

Use

The indirect and polymorphic class templates are header-only. To use them, include the headers indirect.h and polymorphic.h in your project.

#include "indirect.h"

class Composite {
  xyz::indirect<A> a_; // a_ owns an object of type A
  xyz::indirect<B> b_; // b_ owns an object of type B
public:
  Composite(const A& a, const B& b) : 
    a_(std::in_place, a), 
    b_(std::in_place, b) {}

  // ...
};
#include "polymorphic.h"

class CompositeWithPolymorphicMembers {
  xyz::polymorphic<X> x_; // x_ owns an object of type X or derived from X
  xyz::polymorphic<Y> y_; // y_ owns an object of type Y or derived from Y
public:
  template <typename Tx, typename Ty>
  Composite(const Tx& x, const Ty& y) : 
    a_(std::in_place_type<Tx>, x), 
    b_(std::in_place_type<Ty>, y) {}

    // ...
};

Compiler explorer

You can try out indirect and polymorphic in Compiler explorer by adding the includes:

#include <https://raw.githubusercontent.com/jbcoe/value_types/main/indirect.h>
#include <https://raw.githubusercontent.com/jbcoe/value_types/main/polymorphic.h>

License

This code is licensed under the MIT License. See LICENSE for details.

Talks and presentations

We spoke about an earlier draft at C++ on Sea in 2022.

There are some significant design changes since this talk was given (after feedback and discussion at a C++ London meetup). We've pared down the number of constructors and made the null state unobservable.

References

value_types's People

Contributors

jbcoe avatar twon avatar nbx8 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.