SlideIO 2.0.0
Open-source library for reading of medical images
Loading...
Searching...
No Matches
refcounter.hpp
1// This file is part of slideio project.
2// It is subject to the license terms in the LICENSE file found in the top-level directory
3// of this distribution and at http://slideio.com/license.html.
4#pragma once
5#include "slideio/core/slideio_core_def.hpp"
6#include <atomic>
7namespace slideio
8{
9 class SLIDEIO_CORE_EXPORTS RefCounter
10 {
11 public:
12 void increaseCounter() {
13 if (m_counter.fetch_add(1, std::memory_order_acq_rel) == 0)
14 initializeCounter();
15 }
16 void decreaseCounter() {
17 if (m_counter.fetch_sub(1, std::memory_order_acq_rel) == 1)
18 cleanCounter();
19 }
20 protected:
21 virtual void initializeCounter(){};
22 virtual void cleanCounter(){};
23 private:
24 std::atomic<int> m_counter{0};
25 };
26
27 class SLIDEIO_CORE_EXPORTS RefCounterGuard
28 {
29 public:
30 RefCounterGuard(RefCounter* counter) : m_counter(counter) {
31 if (m_counter)
32 m_counter->increaseCounter();
33 }
34 ~RefCounterGuard() {
35 if (m_counter)
36 m_counter->decreaseCounter();
37 }
38 RefCounter* m_counter;
39 };
40};
Definition: exceptions.hpp:15