OpendTect-6_4  6.4
hiddenparam.h
Go to the documentation of this file.
1 #ifndef hiddenparam_h
2 #define hiddenparam_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: K. Tingdahl
9  Date: April 2011
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "sets.h"
16 #include "threadlock.h"
17 
18 #include <iostream>
19 
53 template <class O, class V>
55 {
56 public:
57  HiddenParam( const V& undefval )
58  : lock_(true)
59  , undef_(undefval) {}
60  ~HiddenParam();
61 
62  void setParam(O* obj,const V& val);
63  bool setParamIfValueIs(O* obj,const V& curval, const V& newval );
67  const V& getParam(const O* obj) const;
68  bool hasParam(const O* obj) const;
69  void removeParam(O* obj);
70  void removeAndDeleteParam(O* obj);
71  void deleteAndZeroPtrParam(O* obj);
72 
73 protected:
74 
78  V undef_;
79 
80 };
81 
82 
83 template <class O, class V>
85 {
86 #ifdef __debug__
87  Threads::Locker locker( lock_ );
88  if ( objects_.size() )
89  { //Using std C++ function because we cannot use pErrMsg or BufferString
90  std::cerr << "(PE) HiddenParam | Deleting a HiddenParam prematurely\n";
91  }
92 #endif
93 }
94 
95 
96 template <class O, class V>
97 void HiddenParam<O,V>::setParam( O* obj, const V& val )
98 {
99  Threads::Locker locker( lock_ );
100  const int idx = objects_.indexOf( obj );
101  if ( idx==-1 )
102  {
103  objects_ += obj;
104  params_ += val;
105  return;
106  }
107 
108  params_[idx] = val;
109 }
110 
111 
112 template <class O, class V>
113 bool HiddenParam<O,V>::setParamIfValueIs( O* obj, const V& curval,
114  const V& newval )
115 {
116  Threads::Locker locker( lock_ );
117  const int idx = objects_.indexOf( obj );
118  if ( idx==-1 )
119  {
120  objects_ += obj;
121  params_ += newval;
122  return true;
123  }
124 
125  if ( params_[idx]==curval )
126  {
127  params_[idx] = newval;
128  return true;
129  }
130 
131  return false;
132 }
133 
134 
135 template <class O, class V>
136 const V& HiddenParam<O,V>::getParam( const O* obj ) const
137 {
138  Threads::Locker locker( lock_ );
139  const int idx = objects_.indexOf( obj );
140  if ( !objects_.validIdx(idx) )
141  {
142  pErrMsg("Object not found");
143  return undef_;
144  }
145 
146  return params_[idx];
147 }
148 
149 
150 template <class O, class V>
151 bool HiddenParam<O,V>::hasParam( const O* obj ) const
152 {
153  Threads::Locker locker( lock_ );
154  return objects_.isPresent( obj );
155 }
156 
157 
158 template <class O, class V>
160 {
161  Threads::Locker locker( lock_ );
162  const int idx = objects_.indexOf( obj );
163  if ( idx==-1 )
164  return;
165 
166  params_.removeSingle( idx );
167  objects_.removeSingle( idx );
168 }
169 
170 
171 template <class O, class V>
173 {
174  Threads::Locker locker( lock_ );
175  const int idx = objects_.indexOf( obj );
176  if ( idx==-1 )
177  {
178  pErrMsg("Object not found");
179  return;
180  }
181 
182  delete params_[idx];
183  params_.removeSingle( idx );
184  objects_.removeSingle( idx );
185 }
186 
187 
188 template <class O, class V>
190 {
191  Threads::Locker locker( lock_ );
192  const int idx = objects_.indexOf( obj );
193  if ( idx==-1 )
194  {
195  pErrMsg("Object not found");
196  return;
197  }
198 
199  delete params_[idx];
200  params_[idx] = 0;
201 }
202 
203 #endif
~HiddenParam()
Definition: hiddenparam.h:84
Locks the lock, shutting out access from other threads if needed.
Definition: threadlock.h:85
A lock of a type that (hopefully) suits your needs. To use it, you need the Locker class...
Definition: threadlock.h:53
Workaround manager when you cannot add class members to a class due to binary compability issues...
Definition: hiddenparam.h:54
TypeSet< V > params_
Definition: hiddenparam.h:76
void removeParam(O *obj)
Definition: hiddenparam.h:159
const V & getParam(const O *obj) const
Definition: hiddenparam.h:136
V undef_
Definition: hiddenparam.h:78
void removeAndDeleteParam(O *obj)
Definition: hiddenparam.h:172
Threads::Lock lock_
Definition: hiddenparam.h:77
ObjectSet< O > objects_
Definition: hiddenparam.h:75
void deleteAndZeroPtrParam(O *obj)
Definition: hiddenparam.h:189
bool hasParam(const O *obj) const
Definition: hiddenparam.h:151
#define mClass(module)
Definition: commondefs.h:164
void setParam(O *obj, const V &val)
Definition: hiddenparam.h:97
#define pErrMsg(msg)
Definition: errmsg.h:60
HiddenParam(const V &undefval)
Definition: hiddenparam.h:57
bool setParamIfValueIs(O *obj, const V &curval, const V &newval)
Definition: hiddenparam.h:113

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