OpendTect-6_4  6.4
datapack.h
Go to the documentation of this file.
1 #ifndef datapack_h
2 #define datapack_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: A.H.Bril
9  Date: Jan 2007
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "basicmod.h"
16 #include "namedobj.h"
17 #include "manobjectset.h"
18 #include "multiid.h"
19 #include "threadlock.h"
20 #include "od_iosfwd.h"
21 
22 class DataPackMgr;
23 
24 
35 {
36 public:
37 
38  typedef int ID;
39  typedef MultiID FullID;
40  inline static ID getID( const FullID& fid ) { return fid.ID(1); }
41 
42  DataPack( const char* categry )
43  : NamedObject("<?>")
44  , category_(categry)
45  , nrusers_( 0 )
46  , manager_( 0 )
47  , id_(getNewID()) {}
48  DataPack( const DataPack& dp )
49  : NamedObject( dp.name().buf() )
50  , category_( dp.category_ )
51  , nrusers_( 0 )
52  , manager_( 0 )
53  , id_(getNewID()) {}
54  virtual ~DataPack() {}
55 
56  ID id() const { return id_; }
57  FullID fullID( int mgrid ) const { return FullID(mgrid,id()); }
58  virtual const char* category() const { return category_.buf(); }
59 
60  virtual float nrKBytes() const = 0;
61  virtual void dumpInfo(IOPar&) const;
62 
63  static const char* sKeyCategory();
64  static ID cNoID() { return 0; }
65 
66  virtual bool isOK() const { return true; }
67 
68  Threads::Lock& updateLock() const { return updatelock_; }
69 
70  void release();
71  DataPack* obtain();
72 
73 protected:
74 
75  void setManager(const DataPackMgr*);
76  const ID id_;
78  mutable int nrusers_;
82 
83  static ID getNewID();
84  static float sKb2MbFac();
85 
86  void setCategory( const char* c )
87  { *const_cast<BufferString*>(&category_) = c; }
88 
89  friend class DataPackMgr;
90 };
91 
92 
98 {
99 public:
100 
101  BufferDataPack( char* b=0, od_int64 s=0,
102  const char* catgry="Buffer" )
103  : DataPack(catgry)
104  , buf_(b)
105  , sz_(s) {}
106  ~BufferDataPack() { delete [] buf_; }
107 
108  char* buf() { return buf_; }
109  char const* buf() const { return buf_; }
110  od_int64 size() const { return sz_; }
111  void setBuf( char* b, od_int64 s )
112  { delete [] buf_; sz_ = s; buf_ = b; }
113 
114  virtual float nrKBytes() const { return sz_*sKb2MbFac(); }
115 
116 protected:
117 
118  char* buf_;
120 
121 };
122 
123 
142 {
143 public:
144  // You can, but normally should not, construct
145  // a manager. In general, leave it to DPM() - see below.
146 
147  typedef int ID;
148  inline static ID getID( const DataPack::FullID& fid )
149  { return fid.ID(0); }
150 
151  bool haveID(DataPack::ID) const;
152  inline bool haveID( const DataPack::FullID& fid ) const
153  { return id() == fid.ID(0) && haveID( fid.ID(1) ); }
154 
155  void add(DataPack*);
161  inline DataPack* obtain( DataPack::ID dpid )
162  { return doObtain(dpid,false); }
163  inline const DataPack* obtain( DataPack::ID dpid ) const
164  { return doObtain(dpid,false); }
165  inline DataPack* observe( DataPack::ID dpid )
166  { return doObtain(dpid,true); }
167  inline const DataPack* observe( DataPack::ID dpid ) const
168  { return doObtain(dpid,true); }
169 
170  void release(DataPack::ID);
171  void release( const DataPack* dp )
172  { if ( dp ) release( dp->id() ); }
173  void releaseAll(bool donotify);
174 
177 
178  // Standard mgr IDs take the low integer numbers
179  static ID BufID();
180  static ID PointID();
181  static ID SeisID();
182  static ID FlatID();
183  static ID SurfID();
184 
185  // Convenience to get info without any obtain()
186  const char* nameOf(DataPack::ID) const;
187  static const char* nameOf(const DataPack::FullID&);
188  const char* categoryOf(DataPack::ID) const;
189  static const char* categoryOf(const DataPack::FullID&);
190  virtual float nrKBytesOf(DataPack::ID) const;
191  virtual void dumpInfoFor(DataPack::ID,IOPar&) const;
192 
193  ID id() const { return id_; }
194  void dumpInfo(od_ostream&) const;
195  float nrKBytes() const;
196 
197  const ObjectSet<const DataPack>& packs() const { return packs_; }
198 
199 protected:
200 
201  ID id_;
203 
204  DataPack* doObtain(ID,bool) const;
205  int indexOf(ID) const;
208 
211 
212 public:
213 
214  DataPackMgr(ID);
217  ~DataPackMgr();
220 
221  static DataPackMgr& DPM(ID);
222  static DataPackMgr* gtDPM(ID,bool);
223  static void dumpDPMs(od_ostream&);
224 
225 };
226 
227 template <class T>
229 {
230 public:
231  ConstDataPackRef(const DataPack* p);
233  ConstDataPackRef(const ConstDataPackRef<T>&);
234  virtual ~ConstDataPackRef() { releaseNow();}
235 
236  ConstDataPackRef<T>& operator=(const ConstDataPackRef<T>&);
237 
238  void releaseNow();
239 
240  operator bool() const { return ptr_; }
241  const T* operator->() const { return ptr_; }
242  const T& operator*() const { return *ptr_;}
243  const T* ptr() const { return ptr_; }
244  const DataPack* dataPack() const { return dp_; }
245 
246 protected:
247 
249  T* ptr_;
250 };
251 
284 template <class T>
286 {
287 public:
288  DataPackRef(DataPack* p);
290  DataPackRef(const DataPackRef<T>&);
291 
292  DataPackRef<T>& operator=(const DataPackRef<T>&);
293 
294  void releaseNow();
295 
296  T* operator->() { return this->ptr_; }
297  T& operator*() { return *this->ptr_;}
298  T* ptr() { return this->ptr_; }
299  DataPack* dataPack() { return this->dp_; }
300 };
301 
302 
305 mGlobal(Basic) DataPackMgr& DPM(const DataPack::FullID&);
307 
308 #define mObtainDataPack( var, type, mgrid, newid ) \
309 { \
310  if ( var ) \
311  { \
312  DPM( mgrid ).release( var->id() ); \
313  var = 0; \
314  } \
315  \
316  DataPack* __dp = DPM( mgrid ).obtain( newid ); \
317  mDynamicCastGet( type, __dummy, __dp ); \
318  if ( !__dummy && __dp ) \
319  DPM( mgrid ).release( __dp->id() ); \
320  else \
321  var = __dummy; \
322 }
323 
324 
325 #define mObtainDataPackToLocalVar( var, type, mgrid, newid ) \
326 type var = 0; \
327 mObtainDataPack( var, type, mgrid, newid ); \
328 
329 //Implementations
330 template <class T> inline
332  : dp_(const_cast<DataPack*>(p) )
333  , ptr_( 0 )
334 {
335  mDynamicCast(T*, ptr_, dp_ );
336 }
337 
338 
339 template <class T> inline
341  : ptr_( dpr.ptr_ )
342  , dp_( dpr.dp_ )
343 {
344  if ( dp_ ) dp_->obtain();
345 }
346 
347 
348 template <class T> inline
351 {
352  releaseNow();
353  ptr_ = dpr.ptr_;
354  dp_ = dpr.dp_;
355 
356  if ( dp_ ) dp_->obtain();
357  return *this;
358 }
359 
360 
361 template <class T> inline
363 {
364  ptr_ = 0;
365  if ( dp_ )
366  {
367  dp_->release();
368  dp_ = 0;
369  }
370 }
371 
372 
373 template <class T> inline
375  : ConstDataPackRef<T>( p )
376 { }
377 
378 
379 template <class T> inline
381  : ConstDataPackRef<T>( dpr )
382 { }
383 
384 
385 template <class T> inline
387 {
389  return *this;
390 }
391 
392 
393 #endif
#define mExpClass(module)
Definition: commondefs.h:160
Threads::Lock & updateLock() const
Definition: datapack.h:68
To be able to send and/or receive CallBacks, inherit from this class.
Definition: callback.h:272
char * buf()
Definition: datapack.h:108
OD class for stream write common access to the user log file, or std::cout in batch progs...
Definition: od_ostream.h:26
void release(DataPack::ID)
DataPack(const char *categry)
Definition: datapack.h:42
bool haveID(const DataPack::FullID &fid) const
Definition: datapack.h:152
int64_t size() const
Definition: datapack.h:110
A lock of a type that (hopefully) suits your needs. To use it, you need the Locker class...
Definition: threadlock.h:53
char const * buf() const
Definition: datapack.h:109
static ID cNoID()
Definition: datapack.h:64
static void dumpDPMs(od_ostream &)
Simple DataPack based on an unstructured char array buffer.
Definition: datapack.h:97
static ID PointID()
Sets of &#39;unconnected&#39; points: 2.
bool haveID(DataPack::ID) const
#define mGlobal(module)
Definition: commondefs.h:163
virtual ~ConstDataPackRef()
Definition: datapack.h:234
#define od_int64
Definition: plftypes.h:36
BufferDataPack(char *b=0, int64_t s=0, const char *catgry="Buffer")
Definition: datapack.h:101
T & operator*()
Definition: datapack.h:297
int ID
Each Mgr has its own ID.
Definition: datapack.h:147
void add(DataPack *)
The pack becomes mine.
DataPackRef< T > & operator=(const DataPackRef< T > &)
Definition: datapack.h:386
ID id() const
Definition: datapack.h:193
Notifier< DataPackMgr > packToBeRemoved
Passed CallBacker* = Pack.
Definition: datapack.h:176
DataPack * dataPack()
Definition: datapack.h:299
const T * operator->() const
Definition: datapack.h:241
virtual const char * category() const
Definition: datapack.h:58
ConstDataPackRef(const DataPack *p)
Assumes p is obtained.
Definition: datapack.h:331
virtual float nrKBytesOf(DataPack::ID) const
void setBuf(char *b, int64_t s)
Definition: datapack.h:111
static DataPackMgr * gtDPM(ID, bool)
void release()
const DataPackMgr * manager_
Definition: datapack.h:81
Threads::Lock updatelock_
Definition: datapack.h:80
virtual float nrKBytes() const
Definition: datapack.h:114
ObjectSet< T >::size_type indexOf(const ObjectSet< T > &os, const S &val)
Locate object in set.
Definition: objectset.h:169
int ID
Definition: datapack.h:38
void release(const DataPack *dp)
Definition: datapack.h:171
static ID FlatID()
Flat (N1xN2) data: 4.
DataPack * doObtain(ID, bool) const
Definition: datapack.h:228
A data packet: data+positioning and more that needs to be shared.
Definition: datapack.h:34
virtual ~DataPack()
Definition: datapack.h:54
static ID getID(const FullID &fid)
Definition: datapack.h:40
Manages DataPacks.
Definition: datapack.h:141
static ID getID(const DataPack::FullID &fid)
Definition: datapack.h:148
static ID BufID()
Simple data buffer: 1.
T * operator->()
Definition: datapack.h:296
Generalized set of parameters of the keyword-value type.
Definition: iopar.h:47
ID id() const
Definition: datapack.h:56
ID id_
Definition: datapack.h:201
static Threads::Lock mgrlistlock_
Definition: datapack.h:209
DataPackRef(DataPack *p)
Assumes p is obtained.
Definition: datapack.h:374
int64_t sz_
Definition: datapack.h:119
Threads::Lock rwlock_
Definition: datapack.h:207
~BufferDataPack()
Definition: datapack.h:106
const DataPack * observe(DataPack::ID dpid) const
Definition: datapack.h:167
const T & operator*() const
Definition: datapack.h:242
DataPack(const DataPack &dp)
Definition: datapack.h:48
const ObjectSet< const DataPack > & packs() const
Definition: datapack.h:197
DataPack * observe(DataPack::ID dpid)
Definition: datapack.h:165
char * buf_
Definition: datapack.h:118
DataPack * addAndObtain(DataPack *)
T * ptr_
Definition: datapack.h:249
const DataPack * dataPack() const
Definition: datapack.h:244
int nrusers_
Definition: datapack.h:78
virtual bool isOK() const
Definition: datapack.h:66
#define mDynamicCast(typ, out, in)
Definition: commondefs.h:130
const DataPack * obtain(DataPack::ID dpid) const
Definition: datapack.h:163
DataPack * obtain()
Definition: datapack.h:285
const char * categoryOf(DataPack::ID) const
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size...
Definition: bufstring.h:40
DataPack * dp_
Definition: datapack.h:248
float nrKBytes() const
Notifier< DataPackMgr > newPack
Passed CallBacker* = Pack.
Definition: datapack.h:175
Object with a name.
Definition: namedobj.h:35
ObjectSet< const DataPack > packs_
Definition: datapack.h:202
DataPack * obtain(DataPack::ID dpid)
Definition: datapack.h:161
Threads::Lock nruserslock_
Definition: datapack.h:79
const ID id_
Definition: datapack.h:76
T * ptr()
Definition: datapack.h:298
ConstDataPackRef< T > & operator=(const ConstDataPackRef< T > &)
Definition: datapack.h:350
DataPackMgr & DPM(DataPackMgr::ID)
will create a new mgr if needed
int ID(int idx) const
Definition: multiid.h:55
FixedString SeisID()
Definition: keystrs.h:111
virtual void dumpInfoFor(DataPack::ID, IOPar &) const
void releaseAll(bool donotify)
void setCategory(const char *c)
Definition: datapack.h:86
const T * ptr() const
Definition: datapack.h:243
#define mClass(module)
Definition: commondefs.h:164
const char * nameOf(ButtonState)
void releaseNow()
Definition: datapack.h:362
FullID fullID(int mgrid) const
Definition: datapack.h:57
static ManagedObjectSet< DataPackMgr > mgrs_
Definition: datapack.h:210
void dumpInfo(od_ostream &) const
MultiID FullID
Definition: datapack.h:39
Compound key consisting of ints.
Definition: multiid.h:25
const BufferString category_
Definition: datapack.h:77
static ID SurfID()
Surface (triangulated) data: 5.

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