increasing usage of p<>, minor bugs fixed [code still broken]
[quarnos.git] / libs / pointer.h
blob1719230b8a90e5a4002dba1f882bb0ed1a8af543
1 /* Quarn OS
3 * Pointer
5 * Copyright (C) 2009 Pawel Dziepak
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef _POINTER_H_
24 #define _POINTER_H_
26 #include "value.h"
28 template<typename T>
29 class p {
30 private:
31 T *pointer;
32 // mutable checked<unsigned int> &refs;
34 /* Pointers can not be placed on a heap */
35 // static void *operator new(unsigned int);
36 // static void operator delete(void *);
37 public:
38 static const p<T> invalid;
40 /* Pure evil */
41 p() : pointer(0)/*, refs(*new checked<unsigned int>(0)) */{}
43 p(T *ptr) : pointer(ptr)/*, refs(*new checked<unsigned int>(1))*/ {
44 /* if (!ptr)
45 while(1);*/
48 p(T *ptr, checked<unsigned int> &r) : pointer(ptr)/*, refs(r)*/ {}
50 ~p() {
51 // refs--;
52 // if (refs < 1) {
53 //delete pointer;
54 //delete &refs;
55 // }
58 p(const p<T> &cpy) : pointer(cpy.pointer)/*, refs(cpy.refs) */{
59 // refs++;
62 template<typename U>
63 p<U> cast() {
64 p<U> pt(dynamic_cast<U*>(pointer)/*, refs*/);
66 // refs++;
68 return pt;
71 p<T> &operator=(const p<T> mve) {
72 //if (pointer) {
73 // refs--;
74 // if (refs < 1) {
75 // delete pointer;
76 // delete &refs;
77 // }
78 // }
80 pointer = mve.pointer;
81 // refs = mve.refs;
82 //refs++;
84 return *this;
87 bool operator==(unsigned int x) {
88 return (unsigned int)pointer == x;
91 bool operator!=(unsigned int x) {
92 return (unsigned int)pointer != x;
95 template<typename U>
96 U *cast(bool unsafe) {
97 return (U*)pointer;
100 operator T*() {
101 //debug("we dont like this");
102 /* We won't be able to collect garbage if pointer goes unsafe. */
103 // refs = 99;
104 if (valid())
105 return pointer;
106 else
107 while(1);
110 T &operator*() {
111 //assert("attempt to dereference a null pointer", !pointer);
112 if (valid())
113 return *pointer;
114 else
115 while(1);
118 T *operator->() {
119 //assert("attempt to dereference a null pointer", !pointer);
120 if (valid())
121 return pointer;
122 else
123 while(1);
126 bool valid() {
127 //if (((unsigned int)pointer <= 0x100000 && (int)pointer != 0) || (unsigned int)pointer >= 0x400000)
128 // asm("cli\nhlt"::"a"(pointer), "b"(0xbad0713));
129 return (pointer != (T*)0 && (unsigned int)pointer < 0x400000);
132 template<typename U>
133 bool operator==(p<U> _x) {
134 p<T> x = _x.cast<T>();
135 return x.pointer == pointer;
138 bool operator==(p<T> x) {
139 return x.pointer == pointer;
143 template<typename T>
144 const p<T> p<T>::invalid = p<T>(0);
146 #endif