Properly root CopyStruct/CopyArray.
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;
33 * Internal handles work differently from API handles. An API handle is a
34 * root, whereas as an internal handle points to a rooted location. This
35 * subtle difference makes handles easy for embedders, but somewhat
36 * cumbersome internally.
44 Handle(const Handle &other) {
45 thingp_ = other.address();
47 template <typename S> Handle(const Handle<S> &handle) {
49 thingp_ = reinterpret_cast<T * const *>(handle.address());
51 template <typename S> inline Handle(const Local<S> &local);
52 template <typename S> inline Handle(const ScopedRoot<S> &root);
61 operator T *() { return *thingp_; }
62 operator bool() const {
63 return thingp_ && *thingp_;
65 T * const *address() const {
90 Local<Object> *prev() {
93 Local<Object> *toRaw() {
94 return reinterpret_cast<Local<Object>*>(this);
98 // These are implemented in Zone.h.
99 inline explicit Local(Zone *zone, T *thing = NULL);
102 const Local &operator =(T *thing) {
106 T *operator ->() const {
109 T *operator *() const {
112 operator bool () const {
115 bool operator !=(const Local &other) {
116 return thing_ != other.thing_;
120 return (T **)&thing_;
122 T * const *address() const {
123 return (T * const *)&thing_;
133 // Disallow heap allocation.
134 void *operator new(size_t size);
135 void *operator new[](size_t size);
137 // Disallow copy construction.
138 Local(const Local<T> &other);
139 Local &operator =(const Local &other);
142 // A ScopedRoot is a special kind of handle for storing references on the
144 template <typename T>
150 // This is implemented in Zone.h.
151 inline T **make(T *other);
153 void assign(T *other) {
160 // This is implemented in Zone.h.
161 inline ScopedRoot(T *thing);
163 ScopedRoot &operator =(const ScopedRoot<T> &other) {
167 ScopedRoot &operator =(T *other) {
171 T *operator ->() const {
174 T *operator *() const {
177 operator T *() const {
180 operator bool () const {
183 T **address() const {
188 template <typename T> template <typename S> inline
189 Handle<T>::Handle(const Local<S> &local)
192 thingp_ = reinterpret_cast<T * const *>(local.address());
195 template <typename T> template <typename S> inline
196 Handle<T>::Handle(const ScopedRoot<S> &root)
199 thingp_ = reinterpret_cast<T * const *>(root.address());
204 #endif // _include_jitcraft_rooting_h_