Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 290 lines (248 sloc) 7.267 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
#ifndef RBX_VM_OBJECT_UTILS_HPP
#define RBX_VM_OBJECT_UTILS_HPP

#include <stdint.h>
#include <sys/types.h>
#include <sstream>
#include <string.h>
#include <assert.h>
#include <vector>

#include "builtin/object.hpp"
#include "vm/exception.hpp"

/**
* @file object_utils.hpp
*
* Defines all the most common operations for dealing with
* objects, such as type checking and casting.
*/

// A stupid work around for g++ changing it's behavior on 4.3

#if __GNUC__ >= 4 && __GNUC_MINOR__ >= 3
#define SPECIALIZATION_STORAGE
#else
#define SPECIALIZATION_STORAGE static inline
#endif

namespace rubinius {

#define sassert(cond) if(!(cond)) Assertion::raise(#cond)

  /**
* Ruby type system subtype check.
*
* Given builtin-class +T+, return true if +obj+ is of class +T+
*/
  template <class T>
    static inline bool kind_of(const Object* obj) {
      if(REFERENCE_P(obj)) {
        return obj->type_id() == T::type;
      }
      return false;
    }

  /**
* A specialized version for completeness.
*/
  template <>
    SPECIALIZATION_STORAGE bool kind_of<Object>(const Object* obj) {
      return true;
    }

  /**
* Ruby type system class check.
*
* Another version of kind_of that shouldn't be specialized for subtype
* compatibility. I.e., only whether object's class is this specific
* one.
*/
  template <class T>
    static inline bool instance_of(const Object* obj) {
      if(REFERENCE_P(obj)) {
        return obj->type_id() == T::type;
      }
      return false;
    }

  /**
* A specialized version for completeness.
*/
  template <>
    SPECIALIZATION_STORAGE bool instance_of<Object>(const Object* obj) {
      return obj->reference_p() && (obj->type_id() == ObjectType);
    }

  /*
* There is NO reason why as() or try_as() should be getting
* NULL arguments. That means something has not been properly
* initialised (to Qnil if nothing else.)
*/

  /**
* Cast Object* into builtin T*.
*
* Given builtin class +T+, return +obj+ cast as type +T*+. If
* +obj+ is not of type +T+, throw's a TypeError exception.
*
* @see builtin/object.cpp has specialised versions.
*/
  template <class T>
    static inline T* as(Object* obj) {
      if(!kind_of<T>(obj)) {
        TypeError::raise(T::type, obj);
      }

      return static_cast<T*>(obj);
    }

  /**
* Cast const Object* into builtin const T*.
*
* Given builtin class +T+, return +obj+ cast as type +T*+. If
* +obj+ is not of type +T+, throw's a TypeError exception.
*
* @see builtin/object.cpp has specialised versions.
*/
  template <class T>
    static inline const T* as(const Object* obj) {
      if(!kind_of<T>(obj)) {
        TypeError::raise(T::type, obj);
      }

      return static_cast<const T*>(obj);
    }

  /**
* A specialized version for completeness.
*/
  template <>
    SPECIALIZATION_STORAGE Object* as<Object>(Object* obj) {
      return obj;
    }

  /**
* A specialized version for completeness.
*/
  template <>
    SPECIALIZATION_STORAGE const Object* as<Object>(const Object* obj) {
      return obj;
    }

  /**
* Cast Object* into builtin T*.
*
* Given builtin class +T+, return +obj+ cast as type +T*+. If
* +obj+ is not exactly of type +T+, throw's a TypeError exception.
*
* @see builtin/object.cpp has specialised versions.
*/
  template <class T>
    static inline T* as_instance(Object* obj) {
      if(!instance_of<T>(obj)) {
        TypeError::raise(T::type, obj);
      }

      return static_cast<T*>(obj);
    }

  /**
* Cast const Object* into builtin const T*.
*
* Given builtin class +T+, return +obj+ cast as type +T*+. If
* +obj+ is not exactly of type +T+, throw's a TypeError exception.
*
* @see builtin/object.cpp has specialised versions.
*/
  template <class T>
    static inline const T* as_instance(const Object* obj) {
      if(!instance_of<T>(obj)) {
        TypeError::raise(T::type, obj);
      }

      return static_cast<const T*>(obj);
    }

  /**
* Non-raising version of as().
*
* Similar to as<>, but returns NULL if the type is invalid. ONLY
* use this when doing a conditional cast.
*
* @see builtin/object.cpp has specialised versions.
*/
  template <class T>
    static inline T* try_as(Object* obj) {
      if(!kind_of<T>(obj)) {
        return NULL;
      }

      return static_cast<T*>(obj);
    }

  /**
* Non-raising version of as(), for const objects.
*
* Similar to as<>, but returns NULL if the type is invalid. ONLY
* use this when doing a conditional cast.
*
* @see builtin/object.cpp has specialised versions.
*/
  template <class T>
    static inline const T* try_as(const Object* obj) {
      if(!kind_of<T>(obj)) {
        return NULL;
      }

      return static_cast<const T*>(obj);
    }

  /**
* Non-raising version of as_instance().
*
* Similar to as_instance<>, but returns NULL if the type is invalid. ONLY
* use this when doing a conditional cast.
*
* @see builtin/object.cpp has specialised versions.
*/
  template <class T>
    static inline T* try_as_instance(Object* obj) {
      if(!instance_of<T>(obj)) {
        return NULL;
      }

      return static_cast<T*>(obj);
    }

  /**
* Non-raising version of as(), for const objects.
*
* Similar to as<>, but returns NULL if the type is invalid. ONLY
* use this when doing a conditional cast.
*
* @see builtin/object.cpp has specialised versions.
*/
  template <class T>
    static inline const T* try_as_instance(const Object* obj) {
      if(!instance_of<T>(obj)) {
        return NULL;
      }

      return static_cast<const T*>(obj);
    }

  /**
* Returns Qnil cast to another type of rubinius::Object. Useful in
* cases where e.g. a String* would be returned but Qnil is returned
* as an exceptional value.
*
*/
  template <class T>
    static inline T* nil() { return static_cast<T*>(Qnil); }

  /**
* Converts one type into another without a type check. This is
* like reinterprete_cast<>(), but we use it so we can easily
* find where we're doing explicit force casts.
*/
  template <class T>
    static inline T* force_as(ObjectHeader* obj) {
      return reinterpret_cast<T*>(obj);
    }

  template <class T>
    static inline const T* force_as(const ObjectHeader* obj) {
      return reinterpret_cast<const T*>(obj);
    }

  void type_assert(STATE, Object* obj, object_type type, const char* reason);

  /*
* A rubinius object can be followed by:
* - a series of fields, possibly including an ivar
* - a series of bytes (ByteArray)
* - a fast_context pointer
*/

  /* HACK: refactor this to use the state_setup_type code path. */
  struct wraps_struct {
    void *ptr;
    void (*mark)(void*);
    void (*free)(void*);
  };

#define MARK_WRAPPED_STRUCT(obj) do { \
struct wraps_struct *s = (struct wraps_struct *)BYTES_OF(obj); \
if(s->mark != NULL) { s->mark(s->ptr); } \
} while (0)

#define FREE_WRAPPED_STRUCT(obj) do { \
struct wraps_struct *s = (struct wraps_struct *)BYTES_OF(obj); \
if(s->free != NULL) { s->free(s->ptr); } \
} while (0)

#include "gen/kind_of.hpp"

}

#endif
Something went wrong with that request. Please try again.