Fix linux/mac support.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * JITCraft. If not, see http://www.gnu.org/licenses/.
19 #ifndef _include_jitcraft_rooting_h_
20 #define _include_jitcraft_rooting_h_
29 template <typename T> class Local;
30 template <typename T> class ScopedRoot;
34 static void * const Sentinel;
38 * Internal handles work differently from API handles. An API handle is a
39 * root, whereas as an internal handle points to a rooted location. This
40 * subtle difference makes handles easy for embedders, but somewhat
41 * cumbersome internally.
50 : thingp_(reinterpret_cast<T * const *>(&NullAddress::Sentinel))
55 Handle(const Handle &other) {
56 thingp_ = other.address();
58 template <typename S> Handle(const Handle<S> &handle) {
60 thingp_ = reinterpret_cast<T * const *>(handle.address());
62 template <typename S> inline Handle(const Local<S> &local);
63 template <typename S> inline Handle(const ScopedRoot<S> &root);
65 static inline Handle Null() {
76 operator T *() const { return *thingp_; }
77 operator bool() const {
78 return thingp_ && *thingp_;
80 T * const *address() const {
84 bool operator ==(const Handle<T> &other) const {
85 return (!thingp_ && !other.thingp_) ||
86 *thingp_ == *other.thingp_;
88 bool operator !=(const Handle<T> &other) const {
89 return (!thingp_ != !other.thingp_) ||
90 *thingp_ != *other.thingp_;
105 template <typename T>
112 Local<Object> *prev_;
114 Local<Object> *prev() {
117 Local<Object> *toRaw() {
118 return reinterpret_cast<Local<Object>*>(this);
122 // These are implemented in Zone.h.
123 inline explicit Local(Zone *zone, T *thing = NULL);
126 const Local &operator =(T *thing) {
130 T *operator ->() const {
133 T *operator *() const {
136 operator bool () const {
139 bool operator !=(const Local &other) {
140 return thing_ != other.thing_;
144 return (T **)&thing_;
146 T * const *address() const {
147 return (T * const *)&thing_;
156 Local &operator =(const Local &other) {
157 thing_ = other.thing_;
162 // Disallow heap allocation.
163 void *operator new(size_t size);
164 void *operator new[](size_t size);
166 // Disallow copy construction.
167 Local(const Local<T> &other);
170 // A ScopedRoot is a special kind of handle for storing references on the
172 template <typename T>
178 // This is implemented in Zone.h.
179 inline T **make(T *other);
181 void assign(T *other) {
188 // This is implemented in Zone.h.
189 inline ScopedRoot(T *thing);
191 ScopedRoot &operator =(const ScopedRoot<T> &other) {
195 ScopedRoot &operator =(T *other) {
199 T *operator ->() const {
202 T *operator *() const {
205 operator T *() const {
208 operator bool () const {
211 T **address() const {
216 template <typename T> template <typename S> inline
217 Handle<T>::Handle(const Local<S> &local)
220 thingp_ = reinterpret_cast<T * const *>(local.address());
223 template <typename T> template <typename S> inline
224 Handle<T>::Handle(const ScopedRoot<S> &root)
227 thingp_ = reinterpret_cast<T * const *>(root.address());
232 #endif // _include_jitcraft_rooting_h_