Add |from x import names| 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_
23 #include "PoolAllocator.h"
30 template <typename T> class Local;
31 template <typename T> class ScopedRoot;
35 static void * const Sentinel;
39 * Internal handles work differently from API handles. An API handle is a
40 * root, whereas as an internal handle points to a rooted location. This
41 * subtle difference makes handles easy for embedders, but somewhat
42 * cumbersome internally.
51 : thingp_(reinterpret_cast<T * const *>(&NullAddress::Sentinel))
56 Handle(const Handle &other) {
57 thingp_ = other.address();
59 template <typename S> Handle(const Handle<S> &handle) {
61 thingp_ = reinterpret_cast<T * const *>(handle.address());
63 template <typename S> inline Handle(const Local<S> &local);
64 template <typename S> inline Handle(const ScopedRoot<S> &root);
66 static inline Handle Null() {
77 operator T *() const { return *thingp_; }
78 operator bool() const {
79 return thingp_ && *thingp_;
81 T * const *address() const {
85 bool operator ==(const Handle<T> &other) const {
86 return (!thingp_ && !other.thingp_) ||
87 *thingp_ == *other.thingp_;
89 bool operator !=(const Handle<T> &other) const {
90 return (!thingp_ != !other.thingp_) ||
91 *thingp_ != *other.thingp_;
106 template <typename T>
113 Local<Object> *prev_;
115 Local<Object> *prev() {
118 Local<Object> *toRaw() {
119 return reinterpret_cast<Local<Object>*>(this);
123 // These are implemented in Zone.h.
124 inline explicit Local(Zone *zone, T *thing = NULL);
127 const Local &operator =(T *thing) {
131 T *operator ->() const {
134 T *operator *() const {
137 operator bool () const {
140 bool operator !=(const Local &other) {
141 return thing_ != other.thing_;
145 return (T **)&thing_;
147 T * const *address() const {
148 return (T * const *)&thing_;
157 Local &operator =(const Local &other) {
158 thing_ = other.thing_;
163 // Disallow heap allocation.
164 void *operator new(size_t size);
165 void *operator new[](size_t size);
167 // Disallow copy construction.
168 Local(const Local<T> &other);
171 // A ScopedRoot is a special kind of handle for storing references on the
173 template <typename T>
174 class ScopedRoot : public PoolObject
179 // This is implemented in Zone.h.
180 inline T **make(T *other);
182 void assign(T *other) {
189 // This is implemented in Zone.h.
190 inline ScopedRoot(T *thing);
192 ScopedRoot &operator =(const ScopedRoot<T> &other) {
196 ScopedRoot &operator =(T *other) {
200 T *operator ->() const {
203 T *operator *() const {
206 operator T *() const {
209 operator bool () const {
212 T **address() const {
217 template <typename T> template <typename S> inline
218 Handle<T>::Handle(const Local<S> &local)
221 thingp_ = reinterpret_cast<T * const *>(local.address());
224 template <typename T> template <typename S> inline
225 Handle<T>::Handle(const ScopedRoot<S> &root)
228 thingp_ = reinterpret_cast<T * const *>(root.address());
233 #endif // _include_jitcraft_rooting_h_