Skip to content

Commit

Permalink
Merge pull request #65 from karel-burda/release/1.2
Browse files Browse the repository at this point in the history
Release/1.2
  • Loading branch information
karel-burda authored Jun 10, 2018
2 parents fa6af4f + 47d84b3 commit c7247b1
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.0)

project(timers VERSION 1.1.0 LANGUAGES CXX)
project(timers VERSION 1.2.0 LANGUAGES CXX)

include("cmake-helpers/messages.cmake")
include("cmake-helpers/settings.cmake")
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Version](https://img.shields.io/badge/version-1.1.0-green.svg)
![Version](https://img.shields.io/badge/version-1.2.0-green.svg)
[![License](https://img.shields.io/badge/license-MIT_License-green.svg?style=flat)](LICENSE)
[![Build Status](https://travis-ci.org/karel-burda/timers.svg?branch=master)](https://travis-ci.org/karel-burda/timers)
[![Coverage Status](https://coveralls.io/repos/github/karel-burda/timers/badge.svg?branch=master)](https://coveralls.io/github/karel-burda/timers?branch=master)
Expand All @@ -11,7 +11,7 @@
* Its asynchronous version: `single_shot_async`
* Timer that does some action periodically: `periodic`
* Its asynchronous version: `periodic_async`
* Scoped "RAII" timer that stops underlying timer automatically upon destruction: `scoped`
* Scoped "RAII" timer that stops underlying timer automatically upon destruction: `scoped`

Implemented using C++11 with the use of `std::conditional_variable`, `std::promise` and `std::async`.

Expand Down Expand Up @@ -77,7 +77,7 @@ timer.start(2m, nullptr);
### Asynchronous single-shot
```cpp
timers::single_shot_async timer_async;
timers::single_shot_async timer;
// this call is asynchronous
timer.start(2s, [](){ std::cout << "Hi there" << std::endl; });
Expand Down Expand Up @@ -139,14 +139,14 @@ private:
class_that_uses_timers foo;
foo.work();

// "foo" goes out of scope, so the scoped timer (member of "foo") and its underlying
// asynchronous periodic timer will be stopped as well
// "foo" goes out of scope, so the scoped timer (member of "foo") will go out of scope as well
// and its underlying asynchronous periodic timer will be stopped
```

For full use cases, see [main.cpp](example/src/main.cpp) or implementation of unit tests at [tests/unit](tests/unit).

# Build Process
Library itself is just header-only, so no need for additional linking, just `pthreads` have to be linked to the final executable on POSIX systems.
Library itself is just header-only, so no need for additional linking, just threading library might need to be linked to the final executable on most Linux standard library implementations. See section [Usage](#Usage) for more info.

In order to build the usage example ([main.cpp](example/src/main.cpp)) run the cmake in the top-level directory like this:

Expand Down
2 changes: 1 addition & 1 deletion include/timers/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static_assert(std::is_same<underlying_timer, periodic>::value || std::is_same<un
}

private:
std::future<traits::return_type<decltype(&underlying_timer::start)>> m_async_task;
std::future<detail::traits::return_type<decltype(&underlying_timer::start)>> m_async_task;
std::mutex m_async_protection;
};
}
Expand Down
5 changes: 3 additions & 2 deletions include/timers/blocking.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
#include "timers/exceptions.h"
#include "timers/type_definitions.h"

#include "timers/private/disable_copy_and_move.h"
#include "timers/private/disable_copy.h"
#include "timers/private/disable_move.h"

namespace burda
{
namespace timers
{
/// Timer that blocks current thread for given amount of time
class blocking : disable_copy_and_move
class blocking : private detail::disable_copy, private detail::disable_move
{
public:
/// Waits and blocks current thread until the "time" elapses OR client code calls "stop()"
Expand Down
19 changes: 19 additions & 0 deletions include/timers/private/disable_copy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

namespace burda
{
namespace timers
{
namespace detail
{
/// Helper class that enables default construction and disables copy operations
struct disable_copy
{
disable_copy() = default;

disable_copy(const disable_copy &) = delete;
disable_copy(disable_copy &&) = delete;
};
}
}
}
18 changes: 0 additions & 18 deletions include/timers/private/disable_copy_and_move.h

This file was deleted.

19 changes: 19 additions & 0 deletions include/timers/private/disable_move.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

namespace burda
{
namespace timers
{
namespace detail
{
/// Helper class that enables default construction and disables move operations
struct disable_move
{
disable_move() = default;

disable_move & operator=(const disable_move &) = delete;
disable_move & operator=(disable_move &&) = delete;
};
}
}
}
3 changes: 3 additions & 0 deletions include/timers/private/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace burda
{
namespace timers
{
namespace detail
{
namespace traits
{
/// Helper structure to deduce return type of given method
Expand All @@ -25,3 +27,4 @@ using return_type = typename return_type_templated<T>::type;
}
}
}
}
20 changes: 17 additions & 3 deletions include/timers/scoped.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

#include <type_traits>

#include "timers/private/disable_copy_and_move.h"
#include "timers/private/disable_copy.h"
#include "timers/private/disable_move.h"

namespace burda
{
namespace timers
{
class blocking;

/// RAII-like timer wrapper that automatically stops inner timer when going out of scope (during destructor)
/// @tparam underlying_timer might be whichever one except for the blocking
template <typename underlying_timer>
class scoped : public disable_copy_and_move
class scoped : private detail::disable_copy, private detail::disable_move
{
/// We allow only timers to be scoped except for the "blocking" that doesn't make sence
static_assert(!std::is_same<underlying_timer, blocking>::value, "Blocking timer is not allowed be scoped");
Expand All @@ -20,7 +23,7 @@ static_assert(std::is_base_of<blocking, underlying_timer>::value, "Only timers i
public:
~scoped()
{
m_timer.stop();
stop();
}

underlying_timer * operator->()
Expand All @@ -34,6 +37,17 @@ static_assert(std::is_base_of<blocking, underlying_timer>::value, "Only timers i
}

private:
void stop() noexcept
{
try
{
m_timer.stop();
}
catch (...)
{
}
}

underlying_timer m_timer;
};
}
Expand Down

0 comments on commit c7247b1

Please sign in to comment.