2010-06-18 14:47:09 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "ValueBase.h"
|
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<ValueBase> ValueBase::none;
|
2010-06-18 14:47:09 +00:00
|
|
|
|
|
|
|
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() {}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
String::~String() {}
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
const String::ValueType& String::s() const
|
|
|
|
{
|
|
|
|
return str_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char* String::uc() const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<const unsigned char*>(str_.data());
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<String> String::g(const ValueType& string)
|
2010-06-18 14:47:09 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
return std::shared_ptr<String>(new String(string));
|
2010-06-18 14:47:09 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<String> String::g(const unsigned char* data, size_t length)
|
2010-06-19 17:54:54 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
return std::shared_ptr<String>(new String(data, length));
|
2010-06-19 17:54:54 +00:00
|
|
|
}
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
void String::accept(ValueBaseVisitor& v) const
|
|
|
|
{
|
|
|
|
v.visit(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Integer::Integer(ValueType integer):integer_(integer) {}
|
|
|
|
|
|
|
|
Integer::Integer():integer_(0) {}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
Integer::~Integer() {}
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
Integer::ValueType Integer::i() const
|
|
|
|
{
|
|
|
|
return integer_;
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Integer> Integer::g(ValueType integer)
|
2010-06-18 14:47:09 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
return std::shared_ptr<Integer>(new Integer(integer));
|
2010-06-18 14:47:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Integer::accept(ValueBaseVisitor& v) const
|
|
|
|
{
|
|
|
|
v.visit(*this);
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<Bool> Bool::trueValue_(new Bool(true));
|
|
|
|
const std::shared_ptr<Bool> Bool::falseValue_(new Bool(false));
|
2011-03-09 14:07:27 +00:00
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Bool> Bool::gTrue()
|
2011-03-09 14:07:27 +00:00
|
|
|
{
|
|
|
|
return trueValue_;
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Bool> Bool::gFalse()
|
2011-03-09 14:07:27 +00:00
|
|
|
{
|
|
|
|
return falseValue_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bool::val() const
|
|
|
|
{
|
|
|
|
return val_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bool::accept(ValueBaseVisitor& v) const
|
|
|
|
{
|
|
|
|
v.visit(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bool::Bool(bool val):val_(val) {}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<Null> Null::nullValue_(new Null());
|
2011-03-09 14:07:27 +00:00
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Null> Null::g()
|
2011-03-09 14:07:27 +00:00
|
|
|
{
|
|
|
|
return nullValue_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Null::accept(ValueBaseVisitor& v) const
|
|
|
|
{
|
|
|
|
v.visit(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Null::Null() {}
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
List::List() {}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
List::~List() {}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<ValueBase>& List::get(size_t index) const
|
2010-06-18 14:47:09 +00:00
|
|
|
{
|
|
|
|
return list_[index];
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
void List::set(size_t index, const std::shared_ptr<ValueBase>& v)
|
2010-06-19 17:54:54 +00:00
|
|
|
{
|
|
|
|
list_[index] = v;
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
void List::append(const std::shared_ptr<ValueBase>& v)
|
2010-06-18 14:47:09 +00:00
|
|
|
{
|
|
|
|
list_.push_back(v);
|
|
|
|
}
|
|
|
|
|
2010-06-19 17:54:54 +00:00
|
|
|
void List::append(const String::ValueType& string)
|
|
|
|
{
|
|
|
|
list_.push_back(String::g(string));
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
List& List::operator<<(const std::shared_ptr<ValueBase>& v)
|
2010-06-18 14:47:09 +00:00
|
|
|
{
|
|
|
|
list_.push_back(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<ValueBase>& List::operator[](size_t index) const
|
2010-06-18 14:47:09 +00:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<List> List::g()
|
2010-06-18 14:47:09 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
return std::shared_ptr<List>(new List());
|
2010-06-18 14:47:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void List::accept(ValueBaseVisitor& v) const
|
|
|
|
{
|
|
|
|
v.visit(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dict::Dict() {}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
Dict::~Dict() {}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
void Dict::put(const std::string& key, const std::shared_ptr<ValueBase>& vlb)
|
2010-06-18 14:47:09 +00:00
|
|
|
{
|
|
|
|
ValueType::value_type p = std::make_pair(key, vlb);
|
|
|
|
std::pair<ValueType::iterator, bool> 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));
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<ValueBase>& Dict::get(const std::string& key) const
|
2010-06-18 14:47:09 +00:00
|
|
|
{
|
|
|
|
ValueType::const_iterator itr = dict_.find(key);
|
|
|
|
if(itr == dict_.end()) {
|
|
|
|
return ValueBase::none;
|
|
|
|
} else {
|
|
|
|
return (*itr).second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<ValueBase>& Dict::get(const std::string& key)
|
2011-09-26 12:45:45 +00:00
|
|
|
{
|
|
|
|
ValueType::iterator itr = dict_.find(key);
|
|
|
|
if(itr == dict_.end()) {
|
|
|
|
return dict_[key];
|
|
|
|
} else {
|
|
|
|
return (*itr).second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<ValueBase>& Dict::operator[](const std::string& key)
|
2010-06-18 14:47:09 +00:00
|
|
|
{
|
2011-09-26 12:45:45 +00:00
|
|
|
return get(key);
|
2010-06-18 14:47:09 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<ValueBase>& Dict::operator[](const std::string& key) const
|
2010-06-18 14:47:09 +00:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Dict> Dict::g()
|
2010-06-18 14:47:09 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
return std::shared_ptr<Dict>(new Dict());
|
2010-06-18 14:47:09 +00:00
|
|
|
}
|
|
|
|
void Dict::accept(ValueBaseVisitor& v) const
|
|
|
|
{
|
|
|
|
v.visit(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|