Sha256: c50699f7302161dae3483c6af838c031e1529e78fec19704a74871d3cb73d632

Contents?: true

Size: 1.95 KB

Versions: 26

Compression:

Stored size: 1.95 KB

Contents

/*
 *     Copyright 2016 Couchbase, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

#ifndef LCBIO_TIMER_CXX
#define LCBIO_TIMER_CXX

#include <lcbio/timer-ng.h>
#include <cstdlib>

namespace lcb {
namespace io {

class SimpleTimer {
public:
    typedef void (Callback)(void*);
    SimpleTimer(lcbio_pTABLE iot, void *data, Callback cb)
        : inner(lcbio_timer_new(iot, data, cb)) {
    }
    ~SimpleTimer() {
        release();
    }
    void release() {
        if (inner != NULL) {
            lcbio_timer_destroy(inner);
            inner = NULL;
        }
    }
    void signal() {
        lcbio_async_signal(inner);
    }
    void cancel() {
        lcbio_timer_disarm(inner);
    }
    bool is_armed() const {
        return lcbio_timer_armed(inner);
    }
    void rearm(uint32_t usec) {
        lcbio_timer_rearm(inner, usec);
    }
    void arm_if_disarmed(uint32_t usec) {
        if (!is_armed()) {
            rearm(usec);
        }
    }
    void dump(FILE *fp) const {
        lcbio_timer_dump(inner, fp);
    }
private:
    lcbio_pTIMER inner;
    SimpleTimer(const SimpleTimer&);
};

template <typename T, void (T::*M)(void)>
class Timer : public SimpleTimer {
public:
    Timer(lcbio_pTABLE iot, T* ptr)
        : SimpleTimer(iot, ptr, cb) {
    }

    ~Timer() {
        release();
    }

private:
    static void cb(void *arg) {
        T *obj = reinterpret_cast<T*>(arg);
        (obj->*M)();
    }
    Timer(const Timer&);
};

}
}
#endif

Version data entries

26 entries across 26 versions & 2 rubygems

Version Path
mt-libcouchbase-1.4.03 ext/libcouchbase/src/lcbio/timer-cxx.h
mt-libcouchbase-1.4.02 ext/libcouchbase/src/lcbio/timer-cxx.h
mt-libcouchbase-1.4.01 ext/libcouchbase/src/lcbio/timer-cxx.h
mt-libcouchbase-1.4.0 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.3.2 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.3.0 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.2.8 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.2.7 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.2.6 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.2.5 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.2.4 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.2.3 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.2.2 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.2.1 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.2.0 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.1.1 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.1.0 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.0.4 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.0.3 ext/libcouchbase/src/lcbio/timer-cxx.h
libcouchbase-1.0.2 ext/libcouchbase/src/lcbio/timer-cxx.h