Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXMarkedPtr.h
1 /********************************************************************************
2 * *
3 * M a r k e d P o i n t e r C l a s s *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2023 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXMARKEDPTR_H
22 #define FXMARKEDPTR_H
23 
24 namespace FX {
25 
26 
33 template<typename TYPE>
34 class FXMarkedPtr {
35 private:
36  union Un { FXuval val; TYPE* ptr; };
37 private:
38  Un as;
39 public:
40 
41  // Default constructor
42  FXMarkedPtr(){ as.ptr=nullptr; }
43 
44  // Init with pointer
45  FXMarkedPtr(TYPE* p){ as.ptr=p; }
46 
47  // Copy constructor
48  FXMarkedPtr(const FXMarkedPtr<TYPE>& org):as(org.as){ }
49 
50  // Assignment
51  FXMarkedPtr<TYPE>& operator=(TYPE* p){ as.ptr=p; return *this; }
52 
53  // Assignment
54  FXMarkedPtr<TYPE>& operator=(const FXMarkedPtr<TYPE>& org){ as=org.as; return *this; }
55 
56  // Obtain the pointer part, stripping off the flag
57  TYPE* ptr() const { Un x={as.val&~1L}; return x.ptr; }
58 
59  // Conversion operators
60  operator TYPE*() const { return ptr(); }
61 
62  // Dereference operator
63  TYPE& operator*() const { return *ptr(); }
64 
65  // Follow pointer operator
66  TYPE* operator->() const { return ptr(); }
67 
68  // Test for non-null
69  operator FXbool() const { return !!ptr(); }
70 
71  // Test for NULL
72  FXbool operator!() const { return !ptr(); }
73 
74  // Comparison operator.
75  FXbool operator==(TYPE *p) const { return ptr()==p; }
76 
77  // Comparison operator.
78  FXbool operator!=(TYPE *p) const { return ptr()!=p; }
79 
80  // Get flag
81  FXbool flag() const { return !!(as.val&1L); }
82 
83  // Set flag
84  void flag(FXbool flg){ as.val^=((0-flg)^as.val)&1L; }
85 
86  // Flip flag
87  void flip(){ as.val^=1L; }
88 
89  // Destroy
90  ~FXMarkedPtr(){ }
91  };
92 
93 }
94 
95 #endif
Marked pointer keeps a flag bit inside of a dynamically allocated pointer; this is possible because m...
Definition: FXMarkedPtr.h:34
Definition: FX4Splitter.h:28

Copyright © 1997-2022 Jeroen van der Zijp