GithubHelp home page GithubHelp logo

aurora-community / nytl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nyorain/nytl

0.0 1.0 0.0 2.11 MB

Modern C++ generic header-only template library.

License: Boost Software License 1.0

C++ 98.82% Meson 1.18%

nytl's Introduction

nytl

A lightweight and generic header-only template library for C++17. Includes various utility of all kind that i needed across multiple projects:

  • Extremely lightweight vector and matrix templates
    • Basically just std::array with mathematical vector/matrix semantics
    • Additionally various useful operations (mat | vec)
  • Simple utf conversion and utf8 parsing helpers: nytl/utf.hpp
  • A Callback implementation for high-level and fast function callbacks.
  • Easily make virtual classes cloneable: nytl/clone.hpp
  • Pseudo-RAII handling with scope guards: nytl/scope.hpp
  • Lightweight and independent span template: nytl/span.hpp
  • Combining c++ class enums into flags: nytl/flags.hpp

All headers were written as modular, independent and generic as possible. Most utilities can be used indenpendently from each other. The only required dependency is a compiler supporting full C++17 and its stl (this means no suppport for msvc at the moment). All files are licensed under the Boost License.

Contributing

Contributions to library, tests, documentation, examples as well as all suggestions and ideas are always appreciated. Just start a pull request or an issue on github.

Using nytl

There are multiple ways to use nytl. Either install all of its headers on your system and make sure the install path can be found by the compiler. If you need just a few header files (or even just a few functions), just copy those files into your project folder. It can also be used as meson subproject.

Remember that nytl requires a solid C++17 compiler, only recent versions of gcc and clang are tested. Below some basic code examples for (only a) few nytl features to give you an idea.

nytl::Callback and nytl::RecursiveCallback

Callbacks mirror the signal/slot principle in modern c++ with many useful features. For the full documentation, see nytl/callback.hpp. If you want to modify (call/register/disconnect) the callback from within a handler, see nytl/recursiveCallback.hpp.

// Example callback
auto onEvent = nytl::RecursiveCallback<void()> {};

// Adds a callback listener
auto connection = onEvent.add([]{ std::cout << "called\n"; });
connection.disconnect(); // unregisters the listener

onEvent = &foo; // sets foo as only listener
onEvent += []{}; // same as onEvent.add

// listener functions can also take an additional Connection argument that
// allows them to unregister themself from within the listener
onEvent += [&](nytl::Connection selfConnection) {
	selfConnection.disconnect(); // will unregister itself on first call
	onEvent += &bar; // one can also add new listeners from inside a listener
};

onEvent(); // calls all registered listener functions
onEvent.call(); // can also be done more explicit

nytl::ScopeGuard

ScopeGuards are another utility concept implemented by nytl. The mirror finally syntax from other languages and allow safe RAII-like handling of non-RAII resources. For the full documentation, see nytl/scope.hpp.

// open a file descriptor we want to close later on
auto fd = ::open("test.txt");

// create a scope guard that will execute the passed functions as soon
// as this scope is left, no matter in which way. Makes closing the fd
// exception safe and also more maintainable since early returns can be
// added without having to care about the fd.
auto fdGuard = nytl::ScopeGuard([&]{ ::close(fd); })

// there are also classes that only execute the passed functions if the
// scope was left normally or due to an exception
auto successGuard = nytl::SuccessGuard([&]{ std::cout << "scope left normally\n"; });
auto exceptionGuard = nytl::ExceptionGuard([&]{ std::cout << "exception thrown\n"; });

nytl's People

Contributors

nyorain avatar kvaz1r avatar dimi309 avatar

Watchers

James Cloos 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.