/* */ #include "ValueBase.h" namespace aria2 { const SharedHandle ValueBase::none; String::String(const ValueType& string):str_(string) {} String::String(const char* cstring):str_(cstring) {} String::String(const char* data, size_t length):str_(&data[0], &data[length]) {} String::String(const unsigned char* data, size_t length): str_(&data[0], &data[length]) {} String::String() {} String::~String() {} const String::ValueType& String::s() const { return str_; } const unsigned char* String::uc() const { return reinterpret_cast(str_.data()); } SharedHandle String::g(const ValueType& string) { return SharedHandle(new String(string)); } SharedHandle String::g(const unsigned char* data, size_t length) { return SharedHandle(new String(data, length)); } void String::accept(ValueBaseVisitor& v) const { v.visit(*this); } Integer::Integer(ValueType integer):integer_(integer) {} Integer::Integer():integer_(0) {} Integer::~Integer() {} Integer::ValueType Integer::i() const { return integer_; } SharedHandle Integer::g(ValueType integer) { return SharedHandle(new Integer(integer)); } void Integer::accept(ValueBaseVisitor& v) const { v.visit(*this); } List::List() {} List::~List() {} const SharedHandle& List::get(size_t index) const { return list_[index]; } void List::set(size_t index, const SharedHandle& v) { list_[index] = v; } void List::append(const SharedHandle& v) { list_.push_back(v); } void List::append(const String::ValueType& string) { list_.push_back(String::g(string)); } List& List::operator<<(const SharedHandle& v) { list_.push_back(v); return *this; } const SharedHandle& List::operator[](size_t index) const { return list_[index]; } List::ValueType::iterator List::begin() { return list_.begin(); } List::ValueType::iterator List::end() { return list_.end(); } List::ValueType::const_iterator List::begin() const { return list_.begin(); } List::ValueType::const_iterator List::end() const { return list_.end(); } size_t List::size() const { return list_.size(); } bool List::empty() const { return list_.empty(); } SharedHandle List::g() { return SharedHandle(new List()); } void List::accept(ValueBaseVisitor& v) const { v.visit(*this); } Dict::Dict() {} Dict::~Dict() {} void Dict::put(const std::string& key, const SharedHandle& vlb) { ValueType::value_type p = std::make_pair(key, vlb); std::pair r = dict_.insert(p); if(!r.second) { (*r.first).second = vlb; } } void Dict::put(const std::string& key, const String::ValueType& string) { put(key, String::g(string)); } const SharedHandle& Dict::get(const std::string& key) const { ValueType::const_iterator itr = dict_.find(key); if(itr == dict_.end()) { return ValueBase::none; } else { return (*itr).second; } } SharedHandle& Dict::operator[](const std::string& key) { return dict_[key]; } const SharedHandle& Dict::operator[](const std::string& key) const { return get(key); } bool Dict::containsKey(const std::string& key) const { return dict_.count(key) == 1; } void Dict::removeKey(const std::string& key) { dict_.erase(key); } Dict::ValueType::iterator Dict::begin() { return dict_.begin(); } Dict::ValueType::iterator Dict::end() { return dict_.end(); } Dict::ValueType::const_iterator Dict::begin() const { return dict_.begin(); } Dict::ValueType::const_iterator Dict::end() const { return dict_.end(); } size_t Dict::size() const { return dict_.size(); } bool Dict::empty() const { return dict_.empty(); } SharedHandle Dict::g() { return SharedHandle(new Dict()); } void Dict::accept(ValueBaseVisitor& v) const { v.visit(*this); } const String* asString(const ValueBase* v) { if(v) { return downcast(v); } else { return 0; } } String* asString(ValueBase* v) { if(v) { return const_cast(downcast(v)); } else { return 0; } } String* asString(const SharedHandle& v) { if(v.get()) { return const_cast(downcast(v)); } else { return 0; } } const Integer* asInteger(const ValueBase* v) { if(v) { return downcast(v); } else { return 0; } } Integer* asInteger(ValueBase* v) { if(v) { return const_cast(downcast(v)); } else { return 0; } } Integer* asInteger(const SharedHandle& v) { if(v.get()) { return const_cast(downcast(v)); } else { return 0; } } const List* asList(const ValueBase* v) { if(v) { return downcast(v); } else { return 0; } } List* asList(ValueBase* v) { if(v) { return const_cast(downcast(v)); } else { return 0; } } List* asList(const SharedHandle& v) { if(v.get()) { return const_cast(downcast(v)); } else { return 0; } } const Dict* asDict(const ValueBase* v) { if(v) { return downcast(v); } else { return 0; } } Dict* asDict(ValueBase* v) { if(v) { return const_cast(downcast(v)); } else { return 0; } } Dict* asDict(const SharedHandle& v) { if(v.get()) { return const_cast(downcast(v)); } else { return 0; } } } // namespace aria2