easy_qtimer.cpp 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 implementation 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. #include "easy_qtimer.h"
  56. //////////////////////////////////////////////////////////////////////////
  57. EasyQTimer::EasyQTimer()
  58. : QObject()
  59. {
  60. connect(&m_timer, &QTimer::timeout, [this](){ m_handler(); });
  61. }
  62. EasyQTimer::EasyQTimer(::std::function<void()>&& _handler, bool _isSignleShot)
  63. : QObject()
  64. , m_handler(::std::forward<::std::function<void()>&&>(_handler))
  65. {
  66. m_timer.setSingleShot(_isSignleShot);
  67. connect(&m_timer, &QTimer::timeout, [this](){ m_handler(); });
  68. }
  69. EasyQTimer::~EasyQTimer()
  70. {
  71. }
  72. void EasyQTimer::setHandler(::std::function<void()>&& _handler)
  73. {
  74. m_handler = _handler;
  75. }
  76. //////////////////////////////////////////////////////////////////////////