Merge pull request #113 from tesselode/fix-multi-targets
[wdl/wdl-ol.git] / WDL / destroycheck.h
blob2fa1247d9404b977fb853a896ab578a9f7a6a905
1 #ifndef _WDL_DESTROYCHECK_H_
2 #define _WDL_DESTROYCHECK_H_
4 // this is a useful class for verifying that an object (usually "this") hasn't been destroyed:
5 // to use it you add a WDL_DestroyState as a member of your class, then use the WDL_DestroyCheck
6 // helper class (creating it when the pointer is known valid, and checking it later to see if it
7 // is still valid).
8 //
9 // example:
10 // class myClass {
11 // WDL_DestroyState dest;
12 // ...
13 // };
15 // calling code (on myClass *classInstnace):
16 // WDL_DestroyCheck chk(&classInstance->dest);
17 // somefunction();
18 // if (!chk.isOK()) printf("classInstance got deleted!\n");
20 // NOTE: only use this when these objects will be accessed from the same thread -- it will fail miserably
21 // in a multithreaded environment
26 class WDL_DestroyCheck
28 public:
29 class WDL_DestroyStateNextRec { public: WDL_DestroyCheck *next; };
31 WDL_DestroyStateNextRec n, *prev;
32 WDL_DestroyCheck(WDL_DestroyStateNextRec *state)
34 n.next=NULL;
35 if ((prev=state))
37 if ((n.next=prev->next)) n.next->prev = &n;
38 prev->next=this;
41 ~WDL_DestroyCheck()
43 if (prev)
45 prev->next = n.next;
46 if (n.next) n.next->prev = prev;
50 bool isOK() { return !!prev; }
53 class WDL_DestroyState : public WDL_DestroyCheck::WDL_DestroyStateNextRec
55 public:
56 WDL_DestroyState() { next=NULL; }
58 ~WDL_DestroyState()
60 WDL_DestroyCheck *p = next;
61 while (p) { WDL_DestroyCheck *np = p->n.next; p->prev=NULL; p->n.next=NULL; p=np; }
65 #endif