All Projects → TheLartians → Revisited

TheLartians / Revisited

Licence: other
🧑‍🤝‍🧑 The visitor pattern revisited. An inheritance-aware acyclic visitor template, any and any-function templates.

Programming Languages

C++
36643 projects - #6 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to Revisited

pyCompiler
Python Compiler
Stars: ✭ 13 (-7.14%)
Mutual labels:  visitor, visitor-pattern
core
🌈 light, fast, and easy to use, dependencies free javascript syntax highlighter, with automatic language detection
Stars: ✭ 40 (+185.71%)
Mutual labels:  fast
ObjectUI
Create SwiftUI Views with any data
Stars: ✭ 19 (+35.71%)
Mutual labels:  any
RocketXPlugin
🔥🔥 android 端编译加速插件🚀 自动识别未改动 module 并在编译流程中替换为 aar ,只编译改动模块,加速 Android apk 的编译速度。
Stars: ✭ 408 (+2814.29%)
Mutual labels:  fast
touch-callable
Automatically generate a Web UI for Python function using type annotations.
Stars: ✭ 36 (+157.14%)
Mutual labels:  function
Restoring-Extremely-Dark-Images-In-Real-Time
The project is the official implementation of our CVPR 2021 paper, "Restoring Extremely Dark Images in Real Time"
Stars: ✭ 79 (+464.29%)
Mutual labels:  fast
perseverance
Make your functions 💪 resilient and 🚥 fail-fast to 💩 failures or ⌚ delays
Stars: ✭ 12 (-14.29%)
Mutual labels:  function
fast-xml2js
In-place replacement for xml2js parseString. This is about 20x faster and makes use of the rapidxml C++ library.
Stars: ✭ 12 (-14.29%)
Mutual labels:  fast
Contemplate
Contemplate: Fast, extendable object-oriented and light-weight Template Engine for PHP, Python, Node.js, Browser and XPCOM/SDK JavaScript
Stars: ✭ 15 (+7.14%)
Mutual labels:  fast
blazingly-ssr
A blazing fast server side rendering & project optimiser cli tool using Parcel (POC/Experiment)
Stars: ✭ 41 (+192.86%)
Mutual labels:  fast
whip
Simple fast http server for nim based on httpbeast and nest for high performance routing
Stars: ✭ 55 (+292.86%)
Mutual labels:  fast
infrared
✨🚀 Blazing fast, inferred static type checker for JavaScript.
Stars: ✭ 46 (+228.57%)
Mutual labels:  fast
OOP-In-CPlusPlus
An Awesome Repository On Object Oriented Programming In C++ Language. Ideal For Computer Science Undergraduates, This Repository Holds All The Resources Created And Used By Me - Code & Theory For One To Master Object Oriented Programming. Filled With Theory Slides, Number Of Programs, Concept-Clearing Projects And Beautifully Explained, Well Doc…
Stars: ✭ 27 (+92.86%)
Mutual labels:  inheritance
parse-function
(!! moved to tunnckoCore/opensource multi-package repository !!) 🔱 Parse a function into an object using espree, acorn or babylon parsers. Extensible through Smart Plugins.
Stars: ✭ 37 (+164.29%)
Mutual labels:  function
brisk-ioc
fast light brisk ioc/di container on nodejs; Node下快速 轻量的IoC/DI容器,依赖注入,配合装饰器使用
Stars: ✭ 12 (-14.29%)
Mutual labels:  fast
MissElizabethRobot
MashaRoBot : 📑Editor's choice
Stars: ✭ 43 (+207.14%)
Mutual labels:  fast
laravel-any
🏓 Laravel collection macro that determine if `any` item from the collection passes the given truth test.
Stars: ✭ 38 (+171.43%)
Mutual labels:  any
Rocket-Notes
The World's Fastest Note Taking App. Fast. Simple. Create a note in one tap! Create image and text notes directly from your home screen!
Stars: ✭ 20 (+42.86%)
Mutual labels:  fast
pascal-interpreter
A simple interpreter for a large subset of Pascal language written for educational purposes
Stars: ✭ 21 (+50%)
Mutual labels:  visitor
Visitor-Parser-JS
Visitor Parser JS
Stars: ✭ 20 (+42.86%)
Mutual labels:  visitor

Actions Status Actions Status Actions Status Actions Status Actions Status codecov

Revisited

A C++17 acyclic visitor template and inheritance-aware any and any-function class. Using revisited::Visitor greatly reduces the boilerplate code required for implementing the visitor pattern in C++. It uses only compile time type information and has better performance than solutions relying on run time type information such as dynamic_cast.

Examples

See the examples directory for full examples.

Revisited Examples

Simple Visitor

#include <memory>
#include <iostream>
#include <revisited/visitor.h>

struct Base: public virtual revisited::VisitableBase { };
struct A: public Base, public revisited::Visitable<A> { };
struct B: public Base, public revisited::Visitable<B> { };

struct Visitor: public revisited::Visitor<A &,B &> {
  void visit(A &){ std::cout << "Visiting A" << std::endl; }
  void visit(B &){ std::cout << "Visiting B" << std::endl; }
};

int main() {
  std::shared_ptr<Base> a = std::make_shared<A>();
  std::shared_ptr<Base> b = std::make_shared<B>();
  
  Visitor visitor;
  a->accept(visitor); // -> Visiting A
  b->accept(visitor); // -> Visiting B
}

Derived Classes

revisited::Visitor also understands derived classes and classes with multiple visitable base classes. Virtual visitable base classes are also supported. When visiting a derived object, the first class matching the visitor is used (starting from parent classes). Multiple and virtual inheritance is fully supported.

// C is inherited from A (both can be visited)
struct C: public revisited::DerivedVisitable<C, A> { };
// D is inherited from A and B (A and B can be visited)
struct D: public revisited::JoinVisitable<A, B> { };
// E is virtually inherited from  A and B (E, A and B can be visited)
struct E: public revisited::DerivedVisitable<E, revisited::VirtualVisitable<A, B>> { };

revisited::Any Examples

Implicit casting

revisited::Any v;
v = 42;
std::cout << v.get<int>() << std::endl; // -> 42
std::cout << v.get<double>() << std::endl; // -> 42
v = "Hello Any!";
std::cout << v.get<std::string>() << std::endl; // -> Hello Any!

Reference aware casting

int x = 42;
revisited::Any a = std::reference_wrapper(x);
std::cout << a.get<double>() << std::endl; // -> 42
std::cout << &a.get<int&>() == &x << std::endl; // -> 1

Inheritance aware casting

// inheritance aware
struct MyClassBase{ int value; };
struct MyClass: public MyClassBase{ MyClass(int value):MyClassBase{value}{ } };
revisited::Any v;
v.setWithBases<MyClass, MyClassBase>(42);
std::cout << v.get<MyClassBase &>().value << std::endl; // -> 42
std::cout << v.get<MyClass &>().value << std::endl; // -> 42

revisited::AnyFunction Examples

revisited::AnyFunction f;
f = [](int x, float y){ return x + y; };
std::cout << f(40,2).get<int>() << std::endl; // -> 42

Installation and usage

With CPM, revisited::Visitor can be used in a CMake project simply by adding the following to the project's CMakeLists.txt.

CPMAddPackage(
  NAME Revisited
  GIT_REPOSITORY https://github.com/TheLartians/Visitor.git
  VERSION 2.0
)

target_link_libraries(myProject Revisited)

Alternatively, the repository can be cloned locally and included it via add_subdirectory. Installing revisited::Visitor will make it findable in CMake's find_package.

Performance

revisited::Visitor uses meta-programming to determine the inheritance hierarchy at compile-time for optimal performance. Compared to the traditional visitor pattern revisited::Visitor requires an additional virtual calls (as the type of the visitor and the visitable object are unknown). With compiler optimizations enabled, these calls should be hardly noticeable in real-world applications.

There is an benchmark suite included in the repository that compares the pure cost of the different approaches.

cmake -Hbenchmark -Bbuild/bench -DCMAKE_BUILD_TYPE=Release
cmake --build build/bench -j8
./build/bench/RevisitedBenchmark
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].