Comments (3)
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.
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.
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)
- Provide a vtable implementation based on a switch statement
- Tone down documentation about speed of vtable dispatch
- Use a less generic user-defined literal HOT 4
- Cannot find HanaConfig.cmake HOT 4
- Allow specifying special member functions with DYNO_INTERFACE HOT 1
- Refactor dyno::poly
- Add test coverage for storage policies
- Add test coverage for vtable policies
- Please add Conan Repository support HOT 9
- Please create a C++20 proposal HOT 1
- Please submit to BOOST HOT 1
- make dyno::non_owning_storage the default HOT 1
- Arity and multiple SBO
- Documentation revisions: additional reasons for using this library
- Unnecessary copies for non-macro remote_storage interfaces HOT 1
- Memory is not laundered after placement newing
- Wrong static_assert in detail::erase
- Problem with GCC 8: Example does not compile b/c `poly::unerase_poly` is not declared. HOT 3
- 'concept' and 'requires' are keywords in c++2a HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dyno.