Giter Club home page Giter Club logo

mozart's Introduction

logo

Action Status GitHub top language license

Mozart++ Template Library made with ❤️

English 中文

What is Mozart++

Mozart++ is a cross-platform template library written in Modern C++ designed to make up for essential but missing components in STL.

Mozart++ was born in our daily development. We usually build all kinds of "wheels" in a project, but the process is just so trivial, as a result, we decided to put these "wheels" out as a separate template library.

Currently our project is written in C++14 because our main project which Mozart++ originally aimed to support was written in C++14.

Supported Compilers

Compiler Version Tested Platform Status
gcc 8.1.0-x86_64 Ubuntu 18.04
gcc 7.4.0-x86_64 WSL Ubuntu 18.04
Apple Clang 11.0.0 macOS Catalina
mingw-gcc 8.1.0 (x86_64-posix-seh-rev0) Windows 10 Pro 1903
msvc 19.24.28316 Windows 10 Pro 1903

Code Convension

Mozart++ has two namespaces, mpp and mpp_impl. Usually developers only need to use mpp. We promise that all implementations will be hidden in mpp_impl.

What does Mozart++ have now?

Currently we have these tools listed below (ordered alphabetically):

  • A
    • mpp::any: A super effective std::any.
    • mpp::allocator_type: Memory allocator.
  • C
    • mpp::codecvt: Wide string and string converter supports ascii, utf-8 and GBK.
  • E
    • mpp::event_emitter: A NodeJS-like, non-invasive EventEmitter (two implementations):
      • mpp::event_emitter_fast (for release)
      • mpp::event_emitter_attentive (for debug, with more debug information)
  • F
    • mpp::function: An alias for std::function.
    • mpp::function_parser: A function type trait for extracting all information from a callable type.
    • mpp::function_type: An alias for parsed function type (aka mpp::function).
    • mpp::fdistream: Wrap a C file descriptor and Windows File Handle into C++ std::istream.
    • mpp::fdostream: Wrap a C file descriptor and Windows File Handle into C++ std::ostream.
    • mpp::format: Rust-like string formatter.
  • I
    • mpp::iterator_range: A range adapter for a pair of iterators, wrapping two iterators into range-compatible interface.
  • O
    • mpp::optional: Something like std::optional
  • P
    • mpp::process: Cross-platform process interacting library.
  • R
    • mpp::runtime_error: Customized runtime exception.
  • S
    • mpp::stream: Stream API support.
    • mpp::string_ref: String wrapper with plenty of useful string manipulation methods.
  • T
    • mpp::timer: Time operation wrapper.
    • mpp::typelist: Compile-time list holding types as its elements.
    • mpp::throw_ex(): Exception thrower which triggers global event emitter.

How to integrate Mozart++ into your project?

For cmake-based project

First of all, open your terminal (or powershell, or cmd)

$ cd /path/to/your/project
$ git submodule init
$ git submodule add https://github.com/libmozart/mozart.git third-party/mozart

Then, add the following lines to your root CMakeLists.txt:

add_subdirectory(third-party/mozart)
include_directories(third-party/mozart)

NOTICE: Do not forget to link mozart++ to your target. Please make sure your CMakeLists.txt contains the following line:

target_link_libraries(<your-target> mozart++)

For other build tools

We are currently working on it. If you have any idea, feel free to create issues or pull requests.

How to build Mozart++ separately?

$ git clone --recursive https://github.com/libmozart/mozart.git
$ cd mozart
$ mkdir build
$ cd build
$ cmake .. && make
$ make test

Demo

  • Event Emitter

    mpp::event_emitter e;
    
    // register events
    e.on("player-login", [](mpp::string_ref username) {
        checkUsername(username);
    });
    
    e.on("player-move", [](point from, point to) {
        checkMovement(from, to);
    });
    
    // some other places
    e.emit("player-login", username);
    e.emit("player-move", {1, 2}, {3, 4});
  • Format

    using mpp::format;
    // format to string
    auto str = mpp::format("Hello {}, welcome to C++\n", name);
    // format to stream
    mpp::format(std::cout, "Position {}\n", std::vector<int>{1, 2, 3});
  • Stream

    stream<int>::iterate(1, [](int x) { return x * 2; })
            .map([](int x) { return x - 1; })
            .filter([](int x) { return x > 1000; })
            .drop_while([](int x) { return x <= 100000; })
            .drop(5)
            .take_while([](int x) { return x <= 5000000; })
            .for_each([](int x) { printf("%d\n", x); });
  • Optional

    int sum(const mpp::optional<std::vector<int>> &nums) {
        return nums.apply_or<int>(0, [](auto &&v) -> int {
            int s = 0;
            for (auto &&e : v) {
                s += e;
            }
            return s;
        });
    }
  • Process

    using mpp::process;
    
    process p = process::exec("/bin/bash");
    p.in() << "ls /" << std::endl;
    p.in() << "exit" << std::endl;
    p.wait_for();
    
    std::string s;
    while (std::getline(p.out(), s)) {
        printf("%s\n", s.c_str());
    }

Contributions

Contributions and ideas are welcomed through issues and PRs.

Developers

Acknowledge

We would like to thank JetBrains for sharing free open-source licences of amazing tools such as CLion.

License

MIT License

Copyright (c) 2020 Covariant Institute

mozart's People

Contributors

imkiva avatar mikecovlee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mozart's Issues

[Build Tools] Help users compose their wanted components

Why

Since Mozart++ had separated its modules into separate repos,
let's be good people to do it to the end.

Supposed usage

If the user wants to use core and system:

$ cd /path/to/the/project
$ mpp use core system

and the mpp program will automatically clone core and system and their dependencies
to the project third-party/ directory each as a git submodule.

So the resulting directory structure will be like:

project/
    third-party/
        mpp_core/
            ...
        mpp_system/
            ...
        ...

[IMPORTANT] CODE STYLE STANDARD

Seeing #11, I found that we can no longer rely on external tools or IDE to help us ensure the unification of code style, so I think we should specify a code standard, according to which the code style will be checked in the future code review.
见及请之十一,夫复恃外具以护代码体之一,可乎?故私以为,宜以一准,后之码者,具以此格校之是也。

LET'S SOLVE PROBLEMS ONE BY ONE.
请一一而治之。

The first problem to be solved is braces, here is my opinion:
所急者,大括号也,吾言如是:

namespace NS {
    class FUCK {
    public:
        FUCK() {
            // do something
        }
    };
}

[Feature] Good Stream

std::iostream is tardiness, bloated and hard to use. We should create a better one.

Target

  • Based on System module, implemented under raw file descriptor and provide friendly APIs to manage these descriptor
  • Widely support useful functions like redirection and system level pipe
  • Support legacy std::iostream through mpp::fdstream
  • Thread safe, can use for ITCs
  • Cooperate with object serialization #33
  • Support asynchronous operations
  • Multi-charset and wide character support

[Suggestions Requested] I need some suggestions about ITC framework

Recently I made a inter-thread event emitter based on the event_emitter_fast.
And I tested, feeling comfortable.

Please please please give me some advice!!

The modified event emitter for inter-thread communication purpose: Here

Usage:

void thread_run(handler *&H) {
    handler h;
    H = &h;
    h.on("key-enter", []() {
        printf("thread: Enter pressed\n");
    });
    h.on("key", [](int ch) {
        printf("thread: pressed %c\n", ch);
    });
    printf("MessageQueue started\n");
    h.loop();
    printf("MessageQueue exited\n");
}

int main() {
    handler *H = nullptr;
    std::thread thread(thread_run, std::ref(H));
    int ch = 0;
    while ((ch = getchar()) != 'q') {
        if (ch == '\n') {
            H->emit("key-enter");
        } else {
            H->emit("key", ch);
        }
    }
    H->quit();
    thread.join();
}

This would probably fix #33
Thanks!!!!!!!!!!!!!!!!!
@mikecovlee @shirakamiemmmer

[Feature] String format

Why?

All modern languages have user-friendly string format library or builtin functions/macros, except modern C++.

Someone once said, if you don't think C++ is good enough, you can try your best to draft C++21 standard.
Although on earch there will never be C++21, but we will try our best to make up for the essential but missing components of the STL.

We will take the responsibility of making a powerful and safe format library.

RUA!

[Feature] Object Serialization and Message Queue

Why?

We need a better way to perform ITC (stands for inter-thread communication), with serialization framework, we can implement a Message Queue for general purpose multi-thread programming.

Inspiration

When I was making a console progress bar in my repo, I found std::thread are hard to communicate with other threads.
In Android development, we have Handler class to be the bridge between two threads transmitting messages and operations, but we have nothing in modern C++ development, which I think is important.

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.