Bond
 
Loading...
Searching...
No Matches
single_threaded_counter.h
1// Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4#pragma once
5
6#include <bond/core/config.h>
7
8#include "capped_allocator_fwd.h"
9#include "detail/counter_base.h"
10
11#include <boost/assert.hpp>
12
13namespace bond { namespace ext
14{
18 template <typename T>
20 {
21 public:
22 using is_thread_safe = std::false_type;
23
24 using detail::counter_base<T>::counter_base;
25
26 bool try_add(T n) BOND_NOEXCEPT
27 {
28 if (n <= this->max_value() && _value <= this->max_value() - n)
29 {
30 _value += n;
31 return true;
32 }
33
34 return false;
35 }
36
37 void subtract(T n) BOND_NOEXCEPT
38 {
39 BOOST_ASSERT(_value >= n);
40 _value -= n;
41 }
42
43 T value() const BOND_NOEXCEPT
44 {
45 return _value;
46 }
47
48 private:
49 T _value{};
50 };
51
52} } // namespace bond::ext
Helper base class for counters.
Definition: counter_base.h:17
Single-threaded counter to be used with capped_allocator.
Definition: single_threaded_counter.h:20
namespace bond
Definition: apply.h:17