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 --> */
|
|
|
|
#ifndef D_VALUE_BASE_H
|
|
|
|
#define D_VALUE_BASE_H
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
2013-06-21 16:10:38 +00:00
|
|
|
#include <memory>
|
2010-06-18 14:47:09 +00:00
|
|
|
|
2013-07-11 12:09:51 +00:00
|
|
|
#include "a2functional.h"
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
class ValueBaseVisitor;
|
|
|
|
|
|
|
|
class ValueBase {
|
|
|
|
public:
|
|
|
|
virtual ~ValueBase() {}
|
|
|
|
|
|
|
|
virtual void accept(ValueBaseVisitor& visitor) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class String;
|
|
|
|
class Integer;
|
2011-03-09 14:07:27 +00:00
|
|
|
class Bool;
|
|
|
|
class Null;
|
2010-06-18 14:47:09 +00:00
|
|
|
class List;
|
|
|
|
class Dict;
|
|
|
|
|
|
|
|
class ValueBaseVisitor {
|
|
|
|
public:
|
|
|
|
virtual ~ValueBaseVisitor() {}
|
|
|
|
virtual void visit(const String& string) = 0;
|
|
|
|
virtual void visit(const Integer& integer) = 0;
|
2011-03-09 14:07:27 +00:00
|
|
|
virtual void visit(const Bool& boolValue) = 0;
|
|
|
|
virtual void visit(const Null& nullValue) = 0;
|
2010-06-18 14:47:09 +00:00
|
|
|
virtual void visit(const List& list) = 0;
|
|
|
|
virtual void visit(const Dict& dict) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class String:public ValueBase {
|
|
|
|
public:
|
|
|
|
typedef std::string ValueType;
|
|
|
|
|
|
|
|
String(const ValueType& string);
|
2013-07-11 12:09:51 +00:00
|
|
|
String(ValueType&& string);
|
2010-06-18 14:47:09 +00:00
|
|
|
|
|
|
|
explicit String(const char* cstring);
|
|
|
|
|
|
|
|
String(const char* data, size_t length);
|
|
|
|
|
|
|
|
String(const unsigned char* data, size_t length);
|
|
|
|
|
2011-11-05 03:15:34 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
String(InputIterator first, InputIterator last)
|
|
|
|
: str_(first, last)
|
|
|
|
{}
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
String();
|
2010-11-14 07:17:55 +00:00
|
|
|
|
|
|
|
// Don't allow copying
|
2013-07-11 12:09:51 +00:00
|
|
|
String(const String&) = delete;
|
|
|
|
String& operator=(const String&) = delete;
|
2010-11-14 07:17:55 +00:00
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
const ValueType& s() const;
|
|
|
|
|
2013-07-11 12:09:51 +00:00
|
|
|
ValueType popValue() const;
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
// Returns std::string.data() casted to unsigned char*.
|
|
|
|
// Use s().size() to get length.
|
|
|
|
const unsigned char* uc() const;
|
|
|
|
|
2013-07-11 12:09:51 +00:00
|
|
|
static std::unique_ptr<String> g(const ValueType& string);
|
|
|
|
static std::unique_ptr<String> g(ValueType&& string);
|
2010-06-18 14:47:09 +00:00
|
|
|
|
2013-07-11 12:09:51 +00:00
|
|
|
static std::unique_ptr<String> g(const unsigned char* data, size_t length);
|
2010-06-19 17:54:54 +00:00
|
|
|
|
2011-11-05 03:15:34 +00:00
|
|
|
template<typename InputIterator>
|
2013-07-11 12:09:51 +00:00
|
|
|
static std::unique_ptr<String> g(InputIterator first, InputIterator last)
|
2011-11-05 03:15:34 +00:00
|
|
|
{
|
2013-07-11 12:09:51 +00:00
|
|
|
return make_unique<String>(first, last);
|
2011-11-05 03:15:34 +00:00
|
|
|
}
|
|
|
|
|
2013-07-06 09:15:09 +00:00
|
|
|
virtual void accept(ValueBaseVisitor& visitor) const CXX11_OVERRIDE;
|
2010-06-18 14:47:09 +00:00
|
|
|
private:
|
|
|
|
ValueType str_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Integer:public ValueBase {
|
|
|
|
public:
|
|
|
|
typedef int64_t ValueType;
|
|
|
|
|
|
|
|
Integer(ValueType integer);
|
|
|
|
|
|
|
|
Integer();
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
// Don't allow copying
|
2013-07-11 12:09:51 +00:00
|
|
|
Integer(const Integer&) = delete;
|
|
|
|
Integer& operator=(const Integer&) = delete;
|
2010-11-14 07:17:55 +00:00
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
// Returns Integer.
|
|
|
|
ValueType i() const;
|
|
|
|
|
2013-07-11 12:09:51 +00:00
|
|
|
static std::unique_ptr<Integer> g(ValueType integer);
|
2010-06-18 14:47:09 +00:00
|
|
|
|
2013-07-06 09:15:09 +00:00
|
|
|
virtual void accept(ValueBaseVisitor& visitor) const CXX11_OVERRIDE;
|
2010-06-18 14:47:09 +00:00
|
|
|
private:
|
|
|
|
ValueType integer_;
|
|
|
|
};
|
|
|
|
|
2011-03-09 14:07:27 +00:00
|
|
|
class Bool:public ValueBase {
|
|
|
|
public:
|
2013-07-11 12:09:51 +00:00
|
|
|
static std::unique_ptr<Bool> gTrue();
|
|
|
|
static std::unique_ptr<Bool> gFalse();
|
|
|
|
Bool(bool val);
|
2011-03-09 14:07:27 +00:00
|
|
|
bool val() const;
|
2013-07-06 09:15:09 +00:00
|
|
|
virtual void accept(ValueBaseVisitor& visitor) const CXX11_OVERRIDE;
|
2011-03-09 14:07:27 +00:00
|
|
|
private:
|
|
|
|
// Don't allow copying
|
2013-07-11 12:09:51 +00:00
|
|
|
Bool(const Bool&) = delete;
|
|
|
|
Bool& operator=(const Bool&) = delete;
|
2011-03-09 14:07:27 +00:00
|
|
|
bool val_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Null:public ValueBase {
|
|
|
|
public:
|
2013-07-11 12:09:51 +00:00
|
|
|
static std::unique_ptr<Null> g();
|
|
|
|
Null();
|
2013-07-06 09:15:09 +00:00
|
|
|
virtual void accept(ValueBaseVisitor& visitor) const CXX11_OVERRIDE;
|
2011-03-09 14:07:27 +00:00
|
|
|
private:
|
|
|
|
// Don't allow copying
|
|
|
|
Null(const Null&);
|
|
|
|
Null& operator=(const Null&);
|
|
|
|
};
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
class List:public ValueBase {
|
|
|
|
public:
|
2013-07-11 12:09:51 +00:00
|
|
|
typedef std::vector<std::unique_ptr<ValueBase>> ValueType;
|
2010-06-18 14:47:09 +00:00
|
|
|
|
|
|
|
List();
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
// Don't allow copying
|
2013-07-11 12:09:51 +00:00
|
|
|
List(const List&) = delete;
|
|
|
|
List& operator=(const List&) = delete;
|
2010-11-14 07:17:55 +00:00
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
// Appends given v to list.
|
2013-07-11 12:09:51 +00:00
|
|
|
void append(std::unique_ptr<ValueBase> v);
|
2010-06-18 14:47:09 +00:00
|
|
|
|
2010-06-19 17:54:54 +00:00
|
|
|
// Appeding string is so common that we provide shortcut function.
|
2013-07-11 12:09:51 +00:00
|
|
|
void append(String::ValueType string);
|
2010-06-19 17:54:54 +00:00
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
// Alias for append()
|
2013-07-11 12:09:51 +00:00
|
|
|
List& operator<<(std::unique_ptr<ValueBase> v);
|
2010-06-18 14:47:09 +00:00
|
|
|
|
|
|
|
// Returns the object at given index.
|
2013-07-11 12:09:51 +00:00
|
|
|
ValueBase* get(size_t index) const;
|
2010-06-18 14:47:09 +00:00
|
|
|
|
2010-06-19 17:54:54 +00:00
|
|
|
// Set the object at given index.
|
2013-07-11 12:09:51 +00:00
|
|
|
void set(size_t index, std::unique_ptr<ValueBase> v);
|
2010-06-19 17:54:54 +00:00
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
// Returns the const reference of the object at the given index.
|
2013-07-11 12:09:51 +00:00
|
|
|
ValueBase* operator[](size_t index) const;
|
2010-06-18 14:47:09 +00:00
|
|
|
|
|
|
|
// Returns a read/write iterator that points to the first object in
|
|
|
|
// list.
|
|
|
|
ValueType::iterator begin();
|
|
|
|
|
|
|
|
// Returns a read/write iterator that points to the one past the
|
|
|
|
// last object in list.
|
|
|
|
ValueType::iterator end();
|
|
|
|
|
|
|
|
// Returns a read/write read-only iterator that points to the first
|
|
|
|
// object in list.
|
|
|
|
ValueType::const_iterator begin() const;
|
|
|
|
|
|
|
|
// Returns a read/write read-only iterator that points to the one
|
|
|
|
// past the last object in list.
|
|
|
|
ValueType::const_iterator end() const;
|
|
|
|
|
2013-07-11 12:09:51 +00:00
|
|
|
// Returns a read/write read-only iterator that points to the first
|
|
|
|
// object in list.
|
|
|
|
ValueType::const_iterator cbegin() const;
|
|
|
|
|
|
|
|
// Returns a read/write read-only iterator that points to the one
|
|
|
|
// past the last object in list.
|
|
|
|
ValueType::const_iterator cend() const;
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
// Returns size of list.
|
|
|
|
size_t size() const;
|
|
|
|
|
|
|
|
// Returns true if size of list is 0.
|
|
|
|
bool empty() const;
|
|
|
|
|
2013-07-11 12:09:51 +00:00
|
|
|
static std::unique_ptr<List> g();
|
2010-06-18 14:47:09 +00:00
|
|
|
|
2013-07-06 09:15:09 +00:00
|
|
|
virtual void accept(ValueBaseVisitor& visitor) const CXX11_OVERRIDE;
|
2010-06-18 14:47:09 +00:00
|
|
|
private:
|
|
|
|
ValueType list_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Dict:public ValueBase {
|
|
|
|
public:
|
2013-07-11 12:09:51 +00:00
|
|
|
typedef std::map<std::string, std::unique_ptr<ValueBase>> ValueType;
|
2010-06-18 14:47:09 +00:00
|
|
|
|
|
|
|
Dict();
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
// Don't allow copying
|
2013-07-11 12:09:51 +00:00
|
|
|
Dict(const Dict&) = delete;
|
|
|
|
Dict& operator=(const Dict&) = delete;
|
2010-11-14 07:17:55 +00:00
|
|
|
|
2013-07-11 12:09:51 +00:00
|
|
|
void put(std::string key, std::unique_ptr<ValueBase> vlb);
|
2010-06-18 14:47:09 +00:00
|
|
|
|
|
|
|
// Putting string is so common that we provide shortcut function.
|
2013-07-11 12:09:51 +00:00
|
|
|
void put(std::string key, String::ValueType string);
|
2010-06-18 14:47:09 +00:00
|
|
|
|
2013-07-11 12:09:51 +00:00
|
|
|
ValueBase* get(const std::string& key) const;
|
2011-09-26 12:45:45 +00:00
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
// Returns the reference to object associated with given key. If
|
2013-07-11 12:09:51 +00:00
|
|
|
// the key is not found, nullptr is returned.
|
|
|
|
ValueBase* operator[](const std::string& key) const;
|
2010-06-18 14:47:09 +00:00
|
|
|
|
|
|
|
// Returns true if the given key is found in dict.
|
|
|
|
bool containsKey(const std::string& key) const;
|
|
|
|
|
|
|
|
// Removes specified key from dict.
|
|
|
|
void removeKey(const std::string& key);
|
|
|
|
|
2013-07-11 12:09:51 +00:00
|
|
|
// Removes specified key from dict and return its associated value.
|
|
|
|
std::unique_ptr<ValueBase> popValue(const std::string& key);
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
// Returns a read/write iterator that points to the first pair in
|
|
|
|
// the dict.
|
|
|
|
ValueType::iterator begin();
|
|
|
|
|
|
|
|
// Returns a read/write read-only iterator that points to one past
|
|
|
|
// the last pair in the dict.
|
|
|
|
ValueType::iterator end();
|
|
|
|
|
|
|
|
// Returns a read/write read-only iterator that points to the first
|
|
|
|
// pair in the dict.
|
|
|
|
ValueType::const_iterator begin() const;
|
|
|
|
|
|
|
|
// Returns a read/write read-only iterator that points to one past
|
|
|
|
// the last pair in the dict.
|
|
|
|
ValueType::const_iterator end() const;
|
|
|
|
|
2013-07-11 12:09:51 +00:00
|
|
|
// Returns a read/write read-only iterator that points to the first
|
|
|
|
// pair in the dict.
|
|
|
|
ValueType::const_iterator cbegin() const;
|
|
|
|
|
|
|
|
// Returns a read/write read-only iterator that points to one past
|
|
|
|
// the last pair in the dict.
|
|
|
|
ValueType::const_iterator cend() const;
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
// Returns size of Dict.
|
|
|
|
size_t size() const;
|
|
|
|
|
|
|
|
// Returns true if size of Dict is 0.
|
|
|
|
bool empty() const;
|
|
|
|
|
2013-07-11 12:09:51 +00:00
|
|
|
static std::unique_ptr<Dict> g();
|
2010-06-18 14:47:09 +00:00
|
|
|
|
2013-07-06 09:15:09 +00:00
|
|
|
virtual void accept(ValueBaseVisitor& visitor) const CXX11_OVERRIDE;
|
2010-06-18 14:47:09 +00:00
|
|
|
private:
|
|
|
|
ValueType dict_;
|
|
|
|
};
|
|
|
|
|
2011-03-09 14:07:27 +00:00
|
|
|
class EmptyDowncastValueBaseVisitor:public ValueBaseVisitor {
|
|
|
|
public:
|
|
|
|
EmptyDowncastValueBaseVisitor() {}
|
2013-07-06 09:15:09 +00:00
|
|
|
virtual void visit(const String& v) CXX11_OVERRIDE {}
|
|
|
|
virtual void visit(const Integer& v) CXX11_OVERRIDE {}
|
|
|
|
virtual void visit(const Bool& v) CXX11_OVERRIDE {}
|
|
|
|
virtual void visit(const Null& v) CXX11_OVERRIDE {}
|
|
|
|
virtual void visit(const List& v) CXX11_OVERRIDE {}
|
|
|
|
virtual void visit(const Dict& v) CXX11_OVERRIDE {}
|
2011-03-09 14:07:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class DowncastValueBaseVisitor:public EmptyDowncastValueBaseVisitor {
|
2010-06-18 14:47:09 +00:00
|
|
|
public:
|
2013-07-11 12:09:51 +00:00
|
|
|
DowncastValueBaseVisitor() : result_{nullptr} {}
|
2010-06-18 14:47:09 +00:00
|
|
|
|
2013-07-06 09:15:09 +00:00
|
|
|
virtual void visit(const T& t) CXX11_OVERRIDE
|
2010-06-18 14:47:09 +00:00
|
|
|
{
|
|
|
|
result_ = &t;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T* getResult() const
|
|
|
|
{
|
|
|
|
return result_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setResult(const T* r)
|
|
|
|
{
|
|
|
|
result_ = r;
|
|
|
|
}
|
2011-03-09 14:07:27 +00:00
|
|
|
private:
|
|
|
|
const T* result_;
|
2010-06-18 14:47:09 +00:00
|
|
|
};
|
|
|
|
|
2011-03-09 14:07:27 +00:00
|
|
|
template<typename T, typename VPtr>
|
2012-03-24 15:27:25 +00:00
|
|
|
T* downcast(const VPtr& v)
|
2011-09-26 12:45:45 +00:00
|
|
|
{
|
|
|
|
if(v) {
|
|
|
|
DowncastValueBaseVisitor<T> visitor;
|
|
|
|
v->accept(visitor);
|
|
|
|
return const_cast<T*>(visitor.getResult());
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2010-06-18 14:47:09 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|
|
|
|
|
|
|
|
#endif // D_VALUE_BASE_H
|