DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
cptr.hpp
Go to the documentation of this file.
1/************************************************************
2 * cptr.hpp *
3 * *
4 * Created on: Sep 14, 2014 *
5 * Author: Leonidas Georgopoulos *
6 *
7 * Copyright (C) 2014 Leonidas Georgopoulos
8 * Distributed under the Boost Software License, Version 1.0.
9 * (See accompanying file LICENSE_1_0.txt or
10 * copy at http://www.boost.org/LICENSE_1_0.txt)
11 *
12 ************************************************************/
13#ifndef LUTILS_CPTR_HPP
14#define LUTILS_CPTR_HPP
15
16#include <utility>
17#include <iostream>
18#include <mutex>
19#include <memory>
20#include <thread>
21#include <chrono>
22
23template<typename T, typename M>
24class cptr_proxy;
25
26//-----------------------------------------------------------------------------------------------------
49template<typename T, typename M = std::recursive_mutex, int N = 100> class cptr
50{
51protected:
52 std::shared_ptr<M> llock;
53
54 T * base;
55
56public:
57 explicit cptr ( T * p )
58 : llock ( new M() ),
59 base ( p )
60 {
61 }
62
64 {
65 while ( !llock->try_lock() )
66 {
67 std::this_thread::sleep_for ( std::chrono::nanoseconds ( N ) );
68 }
69
70 return *this;
71 }
72
74 {
75 while ( !llock->try_lock() )
76 {
77 std::this_thread::sleep_for ( std::chrono::nanoseconds ( N ) );
78 }
79
80 return *this;
81 }
82
83 T * get()
84 {
85 return base;
86 }
87
88 T * get() const
89 {
90 return base;
91 }
92
93 friend class cptr_proxy<T, M> ;
94};
95//-----------------------------------------------------------------------------------------------------
96//template<typename T, typename M, int N> M cptr<T,M,N>::llock;
97//-----------------------------------------------------------------------------------------------------
105template<typename T, typename M>
107{
108private:
109 T * that;
110
111 std::shared_ptr<M> llock_ptr;
112
113public:
114 cptr_proxy ( cptr<T, M> const & cp )
115 : that ( cp.base ),
116 llock_ptr ( cp.llock )
117 {
118 }
119
121 {
122 return that;
123 }
124
126 {
127 llock_ptr->unlock();
128 }
129};
130//-----------------------------------------------------------------------------------------------------
131
132#endif /*LUTILS_CPTR_HPP*/
cptr_proxy(cptr< T, M > const &cp)
Definition cptr.hpp:114
T * that
Definition cptr.hpp:109
std::shared_ptr< M > llock_ptr
Definition cptr.hpp:111
~cptr_proxy()
Definition cptr.hpp:125
T * operator->()
Definition cptr.hpp:120
Definition cptr.hpp:50
cptr_proxy< T, M > operator->() const
Definition cptr.hpp:73
cptr(T *p)
Definition cptr.hpp:57
cptr_proxy< T, M > operator->()
Definition cptr.hpp:63
T * get()
Definition cptr.hpp:83
std::shared_ptr< M > llock
Definition cptr.hpp:52
T * base
Definition cptr.hpp:54