Giter VIP home page Giter VIP logo

timer's Introduction

Timer

A single-header C++ class that provides a high-level programming interface for timers.

Timer functionalities are usually implemented in large libraries (e.g., the QTimer of Qt). This Timer class provides a single-header implementation.

With Timer, tasks (i.e., calling to functions) can be easily scheduled at either constant intervals or after a specified period. Timer supports any types of functions (e.g., function pointers, function objects, Lambda functions, class member functions) with any number of arguments.

Example 1: calling to non-member functions

    void foo(int value, const std::string& msg) {
        std::lock_guard<std::mutex> guard(mutex);
        std::cout << "foo: " << value << ", " << msg << std::endl;
    }
    
    void timeout() {
        std::lock_guard<std::mutex> guard(mutex);
        std::cout << "timeout" << std::endl;
    }
    
    {   // non-member function without argument
        easy3d::Timer<> t;
        t.set_timeout(3000, &timeout);
    }

    {   // non-member function with arguments
        easy3d::Timer<int, const std::string &> t;
        const int v = 333;
        const std::string msg = "some random string";
        t.set_interval(2000, &foo, v, msg);
    }

Example 2: calling to member functions

    // a trivial class
    class Car {
        public:
        Car(const std::string& name, float speed) : name_(name), speed_(speed) {}
        void print_speed() {
            std::lock_guard<std::mutex> guard(mutex);
            std::cout << "speed of car "<< name_<< ": " << speed_ << std::endl;
        }
        void stop() {
            std::lock_guard<std::mutex> guard(mutex);
            speed_ = 0;
            std::cout << "car " << name_ << " stoped" << std::endl;
        }
        private:
            std::string name_;
            float       speed_;
    };

    { // member function without argument
        easy3d::Timer<> t;
        Car car1("BMW", 180);
        t.set_interval(1000, &car1, &Car::print_speed);
        t.set_timeout(6000, &car1, &Car::stop);
    }
    
    { // member function without argument
        easy3d::Timer<> t;
        Car car2("Chevrolet", 120);
        t.set_interval(1000, &car2, &Car::print_speed);
        t.set_timeout(4000, &car2, &Car::stop);
    }

Example 3: calling to lambda functions

    {   // lambda function without arguments
        easy3d::Timer<> t;
        t.set_interval(2050, [&]() {
            std::lock_guard<std::mutex> guard(mutex);
            std::cout << "After each 2 sec." << std::endl;
        });
    }

    {   // lambda function with arguments
        easy3d::Timer<int, const std::string &> t;
        t.set_interval(3030, [&](float value, const std::string& msg) {
            std::lock_guard<std::mutex> guard(mutex);
            std::cout << "After each 3 sec. value: " << value << ", msg: " << msg << std::endl;
        }, 5, "blabla...");
    }

Build

No need to build, it is a single hearder file ๐Ÿ˜„

You should be able to use Timer on any platforms with a C++ 11 compatible compiler.


Should you have any questions, comments, or suggestions, please contact me at [email protected]

Liangliang Nan

https://3d.bk.tudelft.nl/liangliang/

Jan. 3, 2019

Copyright (C) 2019

timer's People

Contributors

liangliangnan 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.