Skip to content

hiitiger/CoolerCppIdiom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CoolerCppIdiom

Codacy Badge

Collection of useful c++ tools / common idioms you might not found elsewhere, provide with clean code and easy to use api, mose of then are just one file with minimal dependency.

How to

Just include the corresponding file.

Useful kits

A cooler version of promise pattern, we can create our own dispatcher to dispatch task to threads(ui,pool,io,http,etc) just like using std::async and future::then.

This specific implementation uses ppltask, but we can implement it on any primise-like library for c++.

using namespace concurrency_;

auto t = delayed(1000)
| ui([] {
    std::cout << "running on ui" << std::endl;
})
| io([] {
    std::cout << "running on io" << std::endl;
    return std::this_thread::get_id();
})
| delay<std::thread::id>(100)
| pool([](std::thread::id id) {
    std::cout << "running on pool" << std::endl;
    std::cout << "received thread_id" << id  << std::endl;
});

Make json serialization easier for your life.

With nlohmann's JSON for Modern C++ plus a helper file, we can do this now

struct Person{
    std::string name;
    std::optional<std::uint32_t> age;
    std::optional<std::vector<Person>> friends;
};

JSON_AUTO(Person, name, age, friends)

void func()
{
    Person p;
    p.name = "John";
    json obj = p;
    Person op = obj;
}

A simple yet powerful event delegate implementation, support trackable listener, provide an alternate bind to std::bind which support bind to smart pointer.

Event<void(const std::string&, std::string&&)> signal1;

auto func1 = Storm::lambda([&, nocopy]() {
    signal1("123", "123");
});

auto conn1 = signal1.add([&slot1](const std::string& v, std::string&& str){
    slot1 = true;
    std::cout << "\nslot 1: " << v << ", " << std::string(std::move(str)) << std::endl;
});

Connection conn2 = signal1.add([&slot2, &conn2](const std::string& v, std::string&& str) {
    slot2 = true;
    std::cout << "\nslot 2: " << v << ", " << str << std::endl;
    conn2.disconnect();
});


func1();

An async call adapter for Qt which enables user to post async lambda to Qt's UI thread.

A easy to use c++11 thread pool.

Snowflake uuid generator in c++.

A simple throttle control.

Object leak trace, perf timer and extras.

Timetick, Datetime, FpsTimer.

Windows Com ptr implementation with clean and safe interface.

Object tree is a useful way to manage object in c++.

Generica Qt metacall which enables user to call QObject's method with name and QVariantList packed arguments.

...
const QString& object;
const QString& method; 
const QVariantList& args;
//get meta method from method name
QMetaMethod metaMethod;
Qx::metaCall(object, metaMethod, args);

Generica Qt signal map which enables user to connect QObject's signal with name and QVariantList packed arguments.

GenericSignalMapper* mapper1 = new GenericSignalMapper(method, this);
connect(object, qFlagLocation(signature.toUtf8().constData()), mapper1, SLOT(mapSlot()));
connect(mapper1, SIGNAL(mapped(QObject*, QMetaMethod, QVariantList)), this, SLOT(onGenericSignal(QObject*, QMetaMethod, QVariantList)));

About

cooler c++ patterns like async, event delegate, json auto serialization

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages