easy_qtimer.h 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /************************************************************************
  2. * file name : easy_qtimer.h
  3. * ----------------- :
  4. * creation time : 2016/12/05
  5. * author : Victor Zarubkin
  6. * email : [email protected]
  7. * ----------------- :
  8. * description : This file contains declaration of EasyQTimer class used to
  9. * : connect QTimer to non-QObject classes.
  10. * ----------------- :
  11. * change log : * 2016/12/05 Victor Zarubkin: Initial commit.
  12. * :
  13. * : *
  14. * ----------------- :
  15. * license : Lightweight profiler library for c++
  16. * : Copyright(C) 2016-2017 Sergey Yagovtsev, Victor Zarubkin
  17. * :
  18. * : Licensed under either of
  19. * : * MIT license (LICENSE.MIT or http://opensource.org/licenses/MIT)
  20. * : * Apache License, Version 2.0, (LICENSE.APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  21. * : at your option.
  22. * :
  23. * : The MIT License
  24. * :
  25. * : Permission is hereby granted, free of charge, to any person obtaining a copy
  26. * : of this software and associated documentation files (the "Software"), to deal
  27. * : in the Software without restriction, including without limitation the rights
  28. * : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  29. * : of the Software, and to permit persons to whom the Software is furnished
  30. * : to do so, subject to the following conditions:
  31. * :
  32. * : The above copyright notice and this permission notice shall be included in all
  33. * : copies or substantial portions of the Software.
  34. * :
  35. * : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  36. * : INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  37. * : PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  38. * : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  39. * : TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  40. * : USE OR OTHER DEALINGS IN THE SOFTWARE.
  41. * :
  42. * : The Apache License, Version 2.0 (the "License")
  43. * :
  44. * : You may not use this file except in compliance with the License.
  45. * : You may obtain a copy of the License at
  46. * :
  47. * : http://www.apache.org/licenses/LICENSE-2.0
  48. * :
  49. * : Unless required by applicable law or agreed to in writing, software
  50. * : distributed under the License is distributed on an "AS IS" BASIS,
  51. * : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  52. * : See the License for the specific language governing permissions and
  53. * : limitations under the License.
  54. ************************************************************************/
  55. #ifndef EASY__QTIMER__H
  56. #define EASY__QTIMER__H
  57. #include <QTimer>
  58. #include <functional>
  59. //////////////////////////////////////////////////////////////////////////
  60. class EasyQTimer : public QObject
  61. {
  62. Q_OBJECT
  63. private:
  64. QTimer m_timer;
  65. ::std::function<void()> m_handler;
  66. public:
  67. EasyQTimer();
  68. EasyQTimer(::std::function<void()>&& _handler, bool _isSignleShot = false);
  69. virtual ~EasyQTimer();
  70. void setHandler(::std::function<void()>&& _handler);
  71. inline void start(int msec) { m_timer.start(msec); }
  72. inline void stop() { if (m_timer.isActive()) m_timer.stop(); }
  73. inline bool isActive() const { return m_timer.isActive(); }
  74. }; // END of class EasyQTimer.
  75. //////////////////////////////////////////////////////////////////////////
  76. #endif // EASY__QTIMER__H