<commit_before>/**
 * @file Timer.hpp
 * @author <NAME>
 * @date 10 Jun 2017
 * @brief Thread safe version of Timer class
 * It is based on boost::asio::deadline_timer
 * @copyright MIT License. Open source:
 */

#ifndef ICC_TIMER_HPP
#define ICC_TIMER_HPP

#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "IComponent.hpp"
#include "ITimerLisener.hpp"

class Timer
  : protected IComponent,
    public Event<void(const TimerEvents &)> {
 public:
  Timer(IComponent * _parent)
    : IComponent(_parent),
      timer_(*service_) {
  }

 public:
  /**
   * Setting continuos mode for the timer
   * @param is_continuos
   */
  void setContinuos(const bool & is_continuos) {
    push([=]{
      is_continuos_ = is_continuos;
    });
  }

  /**
   * Setting interval mode for the timer
   * @param _duration
   */
  void setInterval(const boost::posix_time::time_duration & _duration) {
    push([=]{
      duration_ = _duration;
    });
  }

  /**
   * Method is used to start async waiting timer
   */
  void start() {
    push([=]{
      timer_.expires_from_now(duration_);
      operator()(TimerEvents::STARTED);
      timer_.async_wait(std::bind(&Timer::timerExpired, this, std::placeholders::_1));
    });
  }

  /**
   * Method is used to stop waiting timer
   */
  void stop() {
    push([=]{
      operator()(TimerEvents::STOPED);
      timer_.cancel();
    });
  }

  /**
   * Method is used to add the listener
   * @param _listener Listener that is being adding
   */
  template <typename _Lisener>
  void addListener(_Lisener * _listener) {
    static_assert(std::is_base_of<IComponent, _Lisener>::value,
                  "_listener is not derived from IComponent");
    static_assert(std::is_base_of<ITimerLisener, _Lisener>::value,
                  "_listener is not derived from ITimerLisener");
    if (_listener) {
      this->connect(
          static_cast<void(IComponent::*)(const TimerEvents &)>(
              &_Lisener::processEvent),
          static_cast<IComponent*>(_listener));
    }
  }

  /**
   * Method is used to add the listener
   * @param _listener Listener that is being adding
   */
  template <typename _Lisener>
  void addListener(std::shared_ptr<_Lisener> _listener) {
    static_assert(std::is_base_of<IComponent, _Lisener>::value,
                  "_listener is not derived from IComponent");
    static_assert(std::is_base_of<ITimerLisener, _Lisener>::value,
                  "_listener is not derived from ITimerLisener");
    if(_listener) {
      this->connect(
          static_cast<void(IComponent::*)(const TimerEvents &)>(
              &_Lisener::processEvent),
          static_cast<std::shared_ptr<IComponent>>(_listener));
    }
  }

  /**
   * Method is used to remove the listener
   * @param _listener Listener that is being removing
   */
  template <typename _Lisener>
  void removeListener(_Lisener * _listener) {
    static_assert(std::is_base_of<IComponent, _Lisener>::value,
                  "_listener is not derived from IComponent");
    static_assert(std::is_base_of<ITimerLisener, _Lisener>::value,
                  "_listener is not derived from ITimerLisener");
    if (_listener) {
      this->disconnect(
          static_cast<void(IComponent::*)(const TimerEvents &)>(
              &_Lisener::processEvent),
          static_cast<IComponent*>(_listener));
    }
  }

  /**
   * Method is used to remove the listener
   * @param _listener Listener that is being removing
   */
  template <typename _Lisener>
  void removeListener(std::shared_ptr<_Lisener> _listener) {
    static_assert(std::is_base_of<IComponent, _Lisener>::value,
                  "_listener is not derived from IComponent");
    static_assert(std::is_base_of<ITimerLisener, _Lisener>::value,
                  "_listener is not derived from ITimerLisener");
    if(_listener) {
      this->disconnect(
          static_cast<void(IComponent::*)(const TimerEvents &)>(
              &_Lisener::processEvent),
          static_cast<std::shared_ptr<IComponent>>(_listener));
    }
  }

  /**
   * Overrided function to specify exit event
   */
  void exit() override {
    push([=]{
      IComponent::exit();
      stop();
    });
  }

 protected:
  /**
   * Method that handle Timer expire event
   * @param _error
   */
  virtual void timerExpired(const boost::system::error_code& _error) {
    if (!_error) {
      operator()(TimerEvents::EXPIRED);<commit_msg>Renamed _Lisener to _Listener in templates<commit_after>/**
 * @file Timer.hpp
 * @author <NAME>
 * @date 10 Jun 2017
 * @brief Thread safe version of Timer class
 * It is based on boost::asio::deadline_timer
 * @copyright MIT License. Open source:
 */

#ifndef ICC_TIMER_HPP
#define ICC_TIMER_HPP

#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "IComponent.hpp"
#include "ITimerLisener.hpp"

class Timer
  : protected IComponent,
    public Event<void(const TimerEvents &)> {
 public:
  Timer(IComponent * _parent)
    : IComponent(_parent),
      timer_(*service_) {
  }

 public:
  /**
   * Setting continuos mode for the timer
   * @param is_continuos
   */
  void setContinuos(const bool & is_continuos) {
    push([=]{
      is_continuos_ = is_continuos;
    });
  }

  /**
   * Setting interval mode for the timer
   * @param _duration
   */
  void setInterval(const boost::posix_time::time_duration & _duration) {
    push([=]{
      duration_ = _duration;
    });
  }

  /**
   * Method is used to start async waiting timer
   */
  void start() {
    push([=]{
      timer_.expires_from_now(duration_);
      operator()(TimerEvents::STARTED);
      timer_.async_wait(std::bind(&Timer::timerExpired, this, std::placeholders::_1));
    });
  }

  /**
   * Method is used to stop waiting timer
   */
  void stop() {
    push([=]{
      operator()(TimerEvents::STOPED);
      timer_.cancel();
    });
  }

  /**
   * Method is used to add the listener
   * @param _listener Listener that is being adding
   */
  template <typename _Listener>
  void addListener(_Listener * _listener) {
    static_assert(std::is_base_of<IComponent, _Listener>::value,
                  "_listener is not derived from IComponent");
    static_assert(std::is_base_of<ITimerLisener, _Listener>::value,
                  "_listener is not derived from ITimerLisener");
    if (_listener) {
      this->connect(
          static_cast<void(IComponent::*)(const TimerEvents &)>(
              &_Listener::processEvent),
          static_cast<IComponent*>(_listener));
    }
  }

  /**
   * Method is used to add the listener
   * @param _listener Listener that is being adding
   */
  template <typename _Listener>
  void addListener(std::shared_ptr<_Listener> _listener) {
    static_assert(std::is_base_of<IComponent, _Listener>::value,
                  "_listener is not derived from IComponent");
    static_assert(std::is_base_of<ITimerLisener, _Listener>::value,
                  "_listener is not derived from ITimerLisener");
    if(_listener) {
      this->connect(
          static_cast<void(IComponent::*)(const TimerEvents &)>(
              &_Listener::processEvent),
          static_cast<std::shared_ptr<IComponent>>(_listener));
    }
  }

  /**
   * Method is used to remove the listener
   * @param _listener Listener that is being removing
   */
  template <typename _Listener>
  void removeListener(_Listener * _listener) {
    static_assert(std::is_base_of<IComponent, _Listener>::value,
                  "_listener is not derived from IComponent");
    static_assert(std::is_base_of<ITimerLisener, _Listener>::value,
                  "_listener is not derived from ITimerLisener");
    if (_listener) {
      this->disconnect(
          static_cast<void(IComponent::*)(const TimerEvents &)>(
              &_Listener::processEvent),
          static_cast<IComponent*>(_listener));
    }
  }

  /**
   * Method is used to remove the listener
   * @param _listener Listener that is being removing
   */
  template <typename _Listener>
  void removeListener(std::shared_ptr<_Listener> _listener) {
    static_assert(std::is_base_of<IComponent, _Listener>::value,
                  "_listener is not derived from IComponent");
    static_assert(std::is_base_of<ITimerLisener, _Listener>::value,
                  "_listener is not derived from ITimerLisener");
    if(_listener) {
      this->disconnect(
          static_cast<void(IComponent::*)(const TimerEvents &)>(
              &_Listener::processEvent),
          static_cast<std::shared_ptr<IComponent>>(_listener));
    }
  }

  /**
   * Overrided function to specify exit event
   */
  void exit() override {
    push([=]{
      IComponent::exit();
      stop();
    });
  }

 protected:
  /**
   * Method that handle Timer expire event
   * @param _error
   */
  virtual void timerExpired(const boost::system::error_code& _error) {
    if (!_error) {
      operator()(TimerEvents::EXPIRED);
{'fraction_non_alphanumeric': 0.122357678112068, 'fraction_numerical': 0.001565820378033777, 'mean_word_length': 3.046153846153846, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4460338', 'n_tokens_mistral': 2802, 'n_tokens_neox': 2738, 'n_words': 620}