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

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