Back to index

easystroke  0.5.5.1
util.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008-2009, Thomas Jaeger <ThJaeger@gmail.com>
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00011  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
00013  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
00014  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 #ifndef __UTIL_H__
00017 #define __UTIL_H__
00018 
00019 #include <glibmm.h>
00020 
00021 class Timeout {
00022        // Invariant: c == &connection || c == NULL
00023        sigc::connection *c;
00024        sigc::connection connection;
00025        // We have to account for the possibilty that timeout() destroys the object
00026        bool to() { c = NULL; timeout(); return false; }
00027 public:
00028        Timeout() : c(0) {}
00029 protected:
00030        virtual void timeout() = 0;
00031 public:
00032        bool remove_timeout() {
00033               if (c) {
00034                      c->disconnect();
00035                      c = 0;
00036                      return true;
00037               }
00038               return false;
00039        }
00040        void set_timeout(int ms) {
00041               remove_timeout();
00042               connection = Glib::signal_timeout().connect(sigc::mem_fun(*this, &Timeout::to), ms);
00043               c = &connection;
00044        }
00045        virtual ~Timeout() {
00046               remove_timeout();
00047        }
00048 };
00049 #endif