]> gitweb.factorcode.org Git - factor.git/blobdiff - vm/tagged.hpp
io.streams.256color: faster by caching styles
[factor.git] / vm / tagged.hpp
old mode 100755 (executable)
new mode 100644 (file)
index 8eb492a..0af2130
@@ -1,72 +1,58 @@
-namespace factor
-{
+namespace factor {
 
-template <typename TYPE> cell tag(TYPE *value)
-{
-       return RETAG(value,tag_for(TYPE::type_number));
+template <typename Type> cell tag(Type* value) {
+  return RETAG(value, Type::type_number);
 }
 
-inline static cell tag_dynamic(object *value)
-{
-       return RETAG(value,tag_for(value->h.hi_tag()));
+inline static cell tag_dynamic(object* value) {
+  return RETAG(value, value->type());
 }
 
-template <typename TYPE>
-struct tagged
-{
-       cell value_;
+template <typename Type> struct tagged {
+  cell value_;
 
-       cell value() const { return value_; }
-       TYPE *untagged() const { return (TYPE *)(UNTAG(value_)); }
+  cell type() const { return TAG(value_); }
 
-       cell type() const {
-               cell tag = TAG(value_);
-               if(tag == OBJECT_TYPE)
-                       return untagged()->h.hi_tag();
-               else
-                       return tag;
-       }
+  bool type_p() const {
+    if (Type::type_number == TYPE_COUNT)
+      return true;
+    return type() == Type::type_number;
+  }
 
-       bool type_p(cell type_) const { return type() == type_; }
+  cell value() const {
+    FACTOR_ASSERT(type_p());
+    return value_;
+  }
 
-       TYPE *untag_check(factorvm *myvm) const {
-               if(TYPE::type_number != TYPE_COUNT && !type_p(TYPE::type_number))
-                       myvm->type_error(TYPE::type_number,value_);
-               return untagged();
-       }
+  Type* untagged() const {
+    FACTOR_ASSERT(type_p());
+    return (Type*)(UNTAG(value_));
+  }
 
-       explicit tagged(cell tagged) : value_(tagged) {
-#ifdef FACTOR_DEBUG
-               untag_check(SIGNAL_VM_PTR());
-#endif
-       }
+  explicit tagged(cell tagged) : value_(tagged) {}
+  explicit tagged(Type* untagged) : value_(factor::tag(untagged)) {}
 
-       explicit tagged(TYPE *untagged) : value_(factor::tag(untagged)) {
-#ifdef FACTOR_DEBUG
-               untag_check(SIGNAL_VM_PTR()); 
-#endif
-       }
+  void set_value(const cell ptr) {
+    value_ = ptr;
+  }
 
-       TYPE *operator->() const { return untagged(); }
-       cell *operator&() const { return &value_; }
+  void set_untagged(const Type *untagged) {
+    set_value(tag(untagged));
+  }
 
-       const tagged<TYPE>& operator=(const TYPE *x) { value_ = tag(x); return *this; }
-       const tagged<TYPE>& operator=(const cell &x) { value_ = x; return *this; }
+  Type* operator->() const { return untagged(); }
+  cell* operator&() const { return &value(); }
 
-       bool operator==(const tagged<TYPE> &x) { return value_ == x.value_; }
-       bool operator!=(const tagged<TYPE> &x) { return value_ != x.value_; }
+  bool operator==(const tagged<Type>& x) { return value_ == x.value_; }
+  bool operator!=(const tagged<Type>& x) { return value_ != x.value_; }
 
-       template<typename X> tagged<X> as() { return tagged<X>(value_); }
+  template <typename NewType> tagged<NewType> as() {
+    return tagged<NewType>(value_);
+  }
 };
 
-template <typename TYPE> TYPE *factorvm::untag_check(cell value)
-{
-       return tagged<TYPE>(value).untag_check(this);
-}
-
-template <typename TYPE> TYPE *factorvm::untag(cell value)
-{
-       return tagged<TYPE>(value).untagged();
+template <typename Type> Type* untag(cell value) {
+  return tagged<Type>(value).untagged();
 }
 
 }