OpendTect-6_4  6.4
changetracker.h
Go to the documentation of this file.
1 #ifndef changetracker_h
2 #define changetracker_h
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: A.H. Bril
8  Date: 26/09/2000
9  RCS: $Id$
10 ________________________________________________________________________
11 
12 -*/
13 
14 #include "algomod.h"
15 #include "gendefs.h"
16 
26 {
27 public:
28  ChangeTracker( bool* c=0 )
29  : chgd_(c), chgid_(0) {}
30  ChangeTracker( bool& c )
31  : chgd_(&c), chgid_(0) {}
32  ChangeTracker( unsigned int& c )
33  : chgid_(&c), chgd_(0) {}
34  ChangeTracker( unsigned int* c )
35  : chgid_(c), chgd_(0) {}
36  ChangeTracker( bool& c, unsigned int& ci )
37  : chgd_(&c), chgid_(&ci) {}
38 
40  template <class T,class U>
41  inline bool set(const T& oldval,const U& newval);
43  template <class T,class U>
44  inline bool update(T& val,const U& newval);
45 
47  inline bool set(const char*&,const char*&);
48 
49  bool isChanged() const
50  { return chgd_ ? *chgd_ : (bool)(chgid_ ? *chgid_ : 0);}
51  unsigned int changeId() const
52  { return chgid_ ? *chgid_ : (chgd_ ? (*chgd_?1:0) : 0);}
53  inline void setChanged(bool yn=true);
54  void setChangeId( unsigned int c )
55  { if ( chgid_ ) *chgid_ = c; }
56 
57  bool hasBoolVar() const { return chgd_; }
58  bool hasIntVar() const { return chgid_; }
59  const bool& boolVar() const { return *chgd_; }
61  const unsigned int& intVar() const { return *chgid_; }
63 
64  void setVar( bool* m ) { chgd_ = m; }
65  void setVar( bool& m ) { chgd_ = &m; }
66  void setVar( unsigned int* m ) { chgid_ = m; }
67  void setVar( unsigned int& m ) { chgid_ = &m; }
68 
69 protected:
70 
71  bool* chgd_;
72  unsigned int* chgid_;
73 
74 };
75 
76 
88 #define mChgTrackGetSet(chtr,obj,getfn,setfn,newval) { \
89  if ( chtr.set( obj->getfn(), newval ) ) \
90  obj->setfn( newval ); }
91 
92 
93 inline void ChangeTracker::setChanged( bool ischgd )
94 {
95  if ( chgd_ )
96  { if ( !*chgd_ ) *chgd_ = ischgd; }
97  else if ( chgid_ )
98  { if ( ischgd ) (*chgid_)++; }
99 }
100 
101 
102 template <class T,class U>
103 inline bool ChangeTracker::set( const T& val, const U& newval )
104 {
105  bool ret = !(val == mCast(T,newval));
106  setChanged( ret );
107  return ret;
108 }
109 
110 
111 inline bool ChangeTracker::set( const char*& val, const char*& newval )
112 {
113  bool ret = (val && newval) || (!val && !newval);
114  if ( !ret ) { setChanged(true); return true; }
115  if ( !val ) return false;
116 
117  ret = FixedString(val)!=newval;
118  setChanged( ret );
119  return ret;
120 }
121 
122 
123 template <class T,class U>
124 inline bool ChangeTracker::update( T& val, const U& newval )
125 {
126  bool ret = set( val, newval );
127  val = newval;
128  return ret;
129 }
130 
131 
132 #endif
const bool & boolVar() const
Don&#39;t call if !hasBoolVar()
Definition: changetracker.h:59
const unsigned int & intVar() const
Don&#39;t call if !hasIntVar()
Definition: changetracker.h:61
void setChanged(bool yn=true)
Definition: changetracker.h:93
bool update(T &val, const U &newval)
Changes and returns wether this value is changed.
Definition: changetracker.h:124
OD::String that holds an existing text string.
Definition: fixedstring.h:29
#define mCast(tp, v)
Definition: commondefs.h:124
bool * chgd_
Definition: changetracker.h:71
bool isChanged() const
Definition: changetracker.h:49
ChangeTracker(bool *c=0)
Definition: changetracker.h:28
ChangeTracker(unsigned int *c)
Definition: changetracker.h:34
unsigned int * chgid_
Definition: changetracker.h:72
bool hasIntVar() const
Definition: changetracker.h:58
void setVar(bool &m)
Definition: changetracker.h:65
ChangeTracker(unsigned int &c)
Definition: changetracker.h:32
ChangeTracker(bool &c)
Definition: changetracker.h:30
bool set(const T &oldval, const U &newval)
returns wether this value is changed
Definition: changetracker.h:103
ChangeTracker(bool &c, unsigned int &ci)
Definition: changetracker.h:36
void setVar(unsigned int *m)
Definition: changetracker.h:66
Updates a variable when changes occur.
Definition: changetracker.h:25
bool hasBoolVar() const
Definition: changetracker.h:57
unsigned int changeId() const
Definition: changetracker.h:51
void setChangeId(unsigned int c)
Definition: changetracker.h:54
#define mClass(module)
Definition: commondefs.h:164
void setVar(unsigned int &m)
Definition: changetracker.h:67
void setVar(bool *m)
Definition: changetracker.h:64

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