Giter Club home page Giter Club logo

Comments (3)

ldionne avatar ldionne commented on May 27, 2024

This is really interesting, thanks a lot for the heads up!

Looking at the implementation, it's pretty similar to what we'd get if we had both reflection and code injection in C++ (both of which D has). I had drafted an implementation of Dyno using these features and it looked more or less like your D code. This is all toy code of course, and it doesn't handle multiple storage strategies:

namespace dyno {
  constexpr std::meta::type vtable_layout_impl(std::meta::type interface) {
    auto vtable = reflexpr(struct { });
    for (auto method : interface.methods()) {
      // signature of the method, with void* as first argument
      auto signature = method.signature().insert_argument(0, reflexpr(void*));
      vtable.add_public_member(method.name(), method.add_ptr());
    }
    return vtable;
  }

  template <typename Interface>
  using vtable_layout_t = unreflexpr(vtable_layout_impl(reflexpr(Interface)));

  constexpr auto fill_vtable(std::meta::type interface, std::meta::type model) {
    vtable_layout_t<Interface> vtable;
    constexpr {
      for (auto method : interface) {
        // -> { vtable. (. method.name() .) = (. method.address() .) }
        vtable.unreflexpr(method.name()) = unreflexpr(method.address());
      }
    }
    return vtable;
  }

  template <typename Interface, typename Model>
  static constexpr vtable_layout_t<Interface> vtable_for =
                          fill_vtable(reflexpr(Interface), reflexpr(Model));
}

template <typename Signature>
struct Callable;

template <typename R, typename ...Args>
struct Callable<R(Args...)> {
  R operator()(Args...) const;
  ~Callable();
};

template <typename Signature>
class function

template <typename R, typename ...Args>
class function<R(Args...)> {
  using VTable = dyno::vtable_layout_t<Callable<R(Args...)>>;
  VTable* vtable_;
  void* storage_;

public:
  template <typename Function>
  function(Function f)
    : vtable_{&dyno::vtable_for<Callable≤...≥, Function>}
    , storage_{new Function{f}}
  { }

  R operator()(Args ...args) const {
    return vtable_->operator()(storage_, args...);
  }

  ~function() {
    vtable_->~???(storage_);
  }
};

This has been presented at a C++ Committee Meeting, and this is one of the (many) motivating use cases for reflection and code generation.

from dyno.

baryluk avatar baryluk commented on May 27, 2024

Really cool and fascinating. Is there some working draft for the reflexpr and unreflexpr ? The fact that you can pass stuff using constexpr std::meta::type looks very promising. The code is a bit verbose, but actually it looks to be reasonably condensed and indeed maintainable. In D you can do both string mixin style, and symbol injection using construction and helper templates, but sometimes string mixin are just easier to use.

Nice to see working being done on it.

from dyno.

ldionne avatar ldionne commented on May 27, 2024

The latest draft of the Reflection TS is here: https://github.com/cplusplus/reflection-ts

It is not part of the main C++ Standard yet, but it might be merged into it at some point in the future. That is certainly the intent. Note that the syntax used in the TS differs from the above syntax, but we have guidance inside the committee to change the syntax to something that's close to what I wrote above.

from dyno.

Related Issues (20)

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.