jitcraft/Handles.h
author David Anderson <dvander@alliedmods.net>
Sun Jul 01 14:52:57 2012 -0700 (2012-07-01)
changeset 21 af5d8e985502
parent 11 a350c2cc736a
child 27 28697d267222
permissions -rw-r--r--
Added initial String: support.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #ifndef _include_jitcraft_rooting_h_
    19 #define _include_jitcraft_rooting_h_
    20 
    21 #include "Heap.h"
    22 
    23 namespace jc {
    24 
    25 class Zone;
    26 
    27 template <typename T> class Local;
    28 template <typename T> class ScopedRoot;
    29 
    30 template <typename T>
    31 class Handle
    32 {
    33     T * const *thingp_;
    34 
    35   public:
    36     template <typename S> Handle(const Handle<S> &handle) {
    37         typeCheck<S>();
    38         thingp_ = reinterpret_cast<const T **>(handle.thingp_);
    39     }
    40     template <typename S> inline Handle(const Local<S> &local);
    41     template <typename S> inline Handle(const ScopedRoot<S> &root);
    42 
    43   public:
    44     T *operator ->() {
    45         return *thingp_;
    46     }
    47     T *operator *() {
    48         return *thingp_;
    49     }
    50     operator T() { return *thingp_; }
    51     operator bool() const {
    52         return thingp_ && *thingp_;
    53     }
    54     T **addr() const {
    55         return thingp_;
    56     }
    57 
    58   private:
    59     template <typename S>
    60     void typeCheck() {
    61 #ifdef DEBUG
    62         T *v1 = NULL;
    63         S *v2 = NULL;
    64         v1 = v2;
    65         (void)v1;
    66 #endif
    67     }
    68 };
    69 
    70 template <typename T>
    71 class Local
    72 {
    73     friend class Zone;
    74 
    75     Zone *zone_;
    76     Local<Object> *prev_;
    77     T *thing_;
    78 
    79     Local<Object> *prev() {
    80         return prev_;
    81     }
    82     Local<Object> *toRaw() {
    83         return reinterpret_cast<Local<Object>*>(this);
    84     }
    85 
    86   public:
    87     inline explicit Local(Zone *zone);
    88     inline explicit Local(Zone *zone, T *thing);
    89     inline ~Local();
    90 
    91     const Local &operator =(T *thing) {
    92         thing_ = thing;
    93         return *this;
    94     }
    95     T *operator ->() const {
    96         return operator *();
    97     }
    98     T *operator *() const {
    99         return thing_;
   100     }
   101     operator bool () const {
   102         return !!thing_;
   103     }
   104 
   105     Object **addr() {
   106         return (Object **)&thing_;
   107     }
   108     Object * const *addr() const {
   109         return (Object * const *)&thing_;
   110     }
   111     operator T *() {
   112         return thing_;
   113     }
   114 
   115   private:
   116     // Disallow heap allocation.
   117     void *operator new(size_t size);
   118     void *operator new[](size_t size);
   119 
   120     // Disallow copy construction.
   121     Local(const Local<T> &other);
   122     Local &operator =(const Local &other);
   123 };
   124 
   125 template <typename T> template <typename S> inline
   126 Handle<T>::Handle(const Local<S> &local)
   127 {
   128     typeCheck<S>();
   129     thingp_ = reinterpret_cast<T * const*>(local.addr());
   130 }
   131 
   132 }
   133 
   134 #endif // _include_jitcraft_rooting_h_