OpendTect  6.3
perthreadrepos.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*@+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: K. Tingdahl
8  Date: Jan 2014
9 ________________________________________________________________________
10 -*/
11 
12 
13 #include "basicmod.h"
14 
15 #include "manobjectset.h"
16 #include "threadlock.h"
17 #include "thread.h"
18 
25 template <class T>
27 {
28 public:
29  T& getObject();
30 
31 private:
35 
36 };
37 
39 
40 #define _mDeclStaticString(nm) \
41  mDefineStaticLocalObject( StaticStringManager, nm##_ssm, \
42  = StaticStringManager() ) \
43  BufferString& nm = nm##_ssm.getObject()
44 
45 #ifdef __debug__
46 #define mDeclStaticString(nm) \
47  _mDeclStaticString(nm); \
48  addToStaticStringRepos( &nm )
49 #else
50 #define mDeclStaticString(nm) \
51  _mDeclStaticString(nm)
52 #endif
53 
57 mGlobal(Basic) void addToStaticStringRepos(const OD::String*);
58 mGlobal(Basic) bool isStaticString(const OD::String*);
59 
60 //Implementation
61 
62 template <class T>
63 T& PerThreadObjectRepository<T>::getObject()
64 {
65  const Threads::ThreadID threadid = Threads::currentThread();
66  Threads::Locker lock( lock_ );
67  int idx = threadids_.indexOf( threadid );
68  if ( idx<0 )
69  {
70  idx = threadids_.size();
71  threadids_ += threadid;
72  objects_ += new T;
73  }
74 
75  return *objects_[idx];
76 }
Locks the lock, shutting out access from other threads if needed.
Definition: threadlock.h:83
ThreadID currentThread()
A lock of a type that (hopefully) suits your needs. To use it, you need the Locker class...
Definition: threadlock.h:51
#define mGlobal(module)
Definition: commondefs.h:160
Threads::Lock lock_
Definition: perthreadrepos.h:34
PerThreadObjectRepository< BufferString > StaticStringManager
Definition: perthreadrepos.h:38
void * ThreadID
Definition: thread.h:33
bool isStaticString(const OD::String *)
Class that keeps one object per thread. This enables temporary passing of objects (such as strings) w...
Definition: perthreadrepos.h:26
ManagedObjectSet< T > objects_
Definition: perthreadrepos.h:32
void addToStaticStringRepos(const OD::String *)
OpendTect.
Definition: commontypes.h:25
ObjectSet< const void > threadids_
Definition: perthreadrepos.h:33
#define mClass(module)
Definition: commondefs.h:161
ObjectSet where the objects contained are owned by this set.
Definition: manobjectset.h:49

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2017