Initial import of some kind of API.
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_
27 template <typename T> class Local;
28 template <typename T> class ScopedRoot;
31 * Internal handles work differently from API handles. An API handle is a
32 * root, whereas as an internal handle points to a rooted location. This
33 * subtle difference makes handles easy for embedders, but somewhat
34 * cumbersome internally.
42 Handle(const Handle &other) {
43 thingp_ = other.address();
45 template <typename S> Handle(const Handle<S> &handle) {
47 thingp_ = reinterpret_cast<T * const *>(handle.address());
49 template <typename S> inline Handle(const Local<S> &local);
50 template <typename S> inline Handle(const ScopedRoot<S> &root);
59 operator T *() { return *thingp_; }
60 operator bool() const {
61 return thingp_ && *thingp_;
63 T * const *address() const {
88 Local<Object> *prev() {
91 Local<Object> *toRaw() {
92 return reinterpret_cast<Local<Object>*>(this);
96 // These are implemented in Zone.h.
97 inline explicit Local(Zone *zone, T *thing = NULL);
100 const Local &operator =(T *thing) {
104 T *operator ->() const {
107 T *operator *() const {
110 operator bool () const {
115 return (Object **)&thing_;
117 Object * const *address() const {
118 return (Object * const *)&thing_;
128 // Disallow heap allocation.
129 void *operator new(size_t size);
130 void *operator new[](size_t size);
132 // Disallow copy construction.
133 Local(const Local<T> &other);
134 Local &operator =(const Local &other);
137 // A ScopedRoot is a special kind of handle for storing references on the
139 template <typename T>
145 // This is implemented in Zone.h.
146 inline T **make(T *other);
148 void assign(T *other) {
155 // This is implemented in Zone.h.
156 inline ScopedRoot(T *thing);
158 ScopedRoot &operator =(const ScopedRoot<T> &other) {
162 ScopedRoot &operator =(T *other) {
166 T *operator ->() const {
169 T *operator *() const {
172 operator T *() const {
175 operator bool () const {
178 T **address() const {
183 template <typename T> template <typename S> inline
184 Handle<T>::Handle(const Local<S> &local)
187 thingp_ = reinterpret_cast<T * const *>(local.address());
190 template <typename T> template <typename S> inline
191 Handle<T>::Handle(const ScopedRoot<S> &root)
194 thingp_ = reinterpret_cast<T * const *>(root.address());
199 #endif // _include_jitcraft_rooting_h_