(C++) WtSafeTimer
January 25, 2018 · View on GitHub
(C++) WtSafeTimer



WtSafeTimer is a forward-declaration-safe Wt::WTimer.
Technical facts
./CppWtSafeTimer/wtsafetimer.h
//--------------------------------------------------------------------------- /* WtSafeTimer, forward-declaration-safe Wt::WTimer Copyright 2011 Richel Bilderbeek This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ //--------------------------------------------------------------------------- //From http://www.richelbilderbeek.nl/CppWtSafeTimer.htm //--------------------------------------------------------------------------- #ifndef WTSAFETIMER_H #define WTSAFETIMER_H //--------------------------------------------------------------------------- #include <string> #include <vector> //--------------------------------------------------------------------------- #include <boost/checked_delete.hpp> #include <boost/noncopyable.hpp> #include <boost/scoped_ptr.hpp> //--------------------------------------------------------------------------- namespace Wt { struct WTimer; } //--------------------------------------------------------------------------- #include <Wt/WObject> #include <Wt/WEvent> //--------------------------------------------------------------------------- ///WtSafeTimer is hopefully an even safer timer than Wt::WTimer struct WtSafeTimer : public boost::noncopyable, Wt::WObject { ///Construct a WtSafeTimer from its firing time WtSafeTimer(const int time_msec); ///Get the WtSafeTimer's current version static const std::string GetVersion(); ///Get the WtSafeTimer's version history static const std::vector<std::string> GetVersionHistory(); ///The signal emitted every tick Wt::EventSignal<Wt::WMouseEvent>& timeout(); private: ///\brief ///Only allow a Boost smart pointer to delete WtSafeTimer /// ///This prevents the following trouble, ///cited from http://www.boost.org/libs/utility/checked_delete.html: ///The C++ Standard allows, in 5.3.5/5, pointers to incomplete ///class types to be deleted with a delete-expression. ///When the class has a non-trivial destructor, or a class-specific operator ///delete, the behavior is undefined. Some compilers issue a warning when an ///incomplete type is deleted, but unfortunately, not all do, and programmers ///sometimes ignore or disable warnings. ~WtSafeTimer(); ///Only allow a Boost smart pointer to delete WtSafeTimer //Template syntax from Herb Sutter. Exceptional C++ style. 2005. ISBN: 0-201-76042-8. Item 8: 'Befriending templates'. friend void boost::checked_delete<>(WtSafeTimer*); ///Internal ticking member function void OnTimer(); ///The fortified Wt::WTimer boost::scoped_ptr<Wt::WTimer> m_timer; }; //--------------------------------------------------------------------------- #endif // WTSAFETIMER_H
./CppWtSafeTimer/wtsafetimer.cpp
//--------------------------------------------------------------------------- /* WtSafeTimer, forward-declaration-safe Wt::WTimer Copyright 2011 Richel Bilderbeek This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ //--------------------------------------------------------------------------- //From http://www.richelbilderbeek.nl/CppWtSafeTimer.htm //--------------------------------------------------------------------------- #include <iostream> //--------------------------------------------------------------------------- #include <Wt/WTimer> #include <Wt/WEvent> //--------------------------------------------------------------------------- #include "wtsafetimer.h" //--------------------------------------------------------------------------- WtSafeTimer::WtSafeTimer(const int time_msec) : m_timer(new Wt::WTimer) { m_timer->setInterval(time_msec); m_timer->start(); } //--------------------------------------------------------------------------- WtSafeTimer::~WtSafeTimer() { } //--------------------------------------------------------------------------- const std::string WtSafeTimer::GetVersion() { return "1.0"; } //--------------------------------------------------------------------------- const std::vector<std::string> WtSafeTimer::GetVersionHistory() { std::vector<std::string> v; v.push_back("YYYY-MM-DD: version X.Y: [description]"); v.push_back("2011-06-22: version 1.0: initial version"); return v; } //--------------------------------------------------------------------------- void WtSafeTimer::OnTimer() { std::clog << __func__ << '\n'; timeout().emit(Wt::WMouseEvent()); } //--------------------------------------------------------------------------- Wt::EventSignal<Wt::WMouseEvent>& WtSafeTimer::timeout() { assert(m_timer); return m_timer->timeout(); } //---------------------------------------------------------------------------