const variant& variant::operator[](const variant& v) const { if(type_ == TYPE_CALLABLE) { assert(v.as_int() == 0); return *this; } if(type_ == TYPE_MAP) { assert(map_); std::map<variant,variant>::const_iterator i = map_->elements.find(v); if (i == map_->elements.end()) { static variant null_variant; return null_variant; } return i->second; } else if(type_ == TYPE_LIST) { return operator[](v.as_int()); } else { throw type_error((formatter() << "type error: " << " expected a list or a map but found " << variant_type_to_string(type_) << " (" << to_debug_string() << ")").str()); } }
void variant::must_be(variant::TYPE t) const { if(type_ != t) { throw type_error((formatter() << "type error: " << " expected " << variant_type_to_string(t) << " but found " << variant_type_to_string(type_) << " (" << to_debug_string() << ")").str()); } }
int variant::as_decimal() const { if( type_ == TYPE_DECIMAL) { return decimal_value_; } else if( type_ == TYPE_INT ) { return int_value_*1000; } else if( type_ == TYPE_NULL) { return 0; } else { throw type_error((formatter() << "type error: " << " expected integer or decimal but found " << variant_type_to_string(type_) << " (" << to_debug_string() << ")").str()); } }
size_t variant::num_elements() const { if(type_ == TYPE_CALLABLE) { return 1; } if (type_ == TYPE_LIST) { assert(list_); return list_->elements.size(); } else if (type_ == TYPE_MAP) { assert(map_); return map_->elements.size(); } else { throw type_error((formatter() << "type error: " << " expected a list or a map but found " << variant_type_to_string(type_) << " (" << to_debug_string() << ")").str()); } }
void variant::throw_type_error(variant::TYPE t) const { throw type_error(formatter() << "type error: " << " expected " << variant_type_to_string(t) << " but found " << variant_type_to_string(type_) << " (" << to_debug_string() << ")"); }