diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2009-02-15 12:57:47 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2009-02-15 12:57:47 +0000 |
| commit | 3ae7633f32df9461eae4d95ee93490af2ddb4d28 (patch) | |
| tree | 8b08e3c0336df32fb4caa14e96f48a697bacb81a /src/bencode.h | |
| parent | eafb1bc615828a275805ee8f05f0eb1c4314ebca (diff) | |
2009-02-15 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Rewritten BDE to make it efficient.
* src/bencode.cc
* src/bencode.h
Diffstat (limited to 'src/bencode.h')
| -rw-r--r-- | src/bencode.h | 232 |
1 files changed, 227 insertions, 5 deletions
diff --git a/src/bencode.h b/src/bencode.h index 3be16609..283b958a 100644 --- a/src/bencode.h +++ b/src/bencode.h @@ -43,6 +43,8 @@ #include <iosfwd> #include "SharedHandle.h" +#include "A2STR.h" +#include "RecoverableException.h" namespace aria2 { @@ -64,12 +66,232 @@ private: TYPE_LIST, }; + class BObject { + public: + //////////////////////////////////////////////////////////////////////////// + // Integer Interface + + // Returns Integer. + virtual Integer i() const + { + throw RecoverableException("Not Integer"); + } + + //////////////////////////////////////////////////////////////////////////// + // String Interface + + // Returns std::string. + virtual const std::string& s() const + { + throw RecoverableException("Not String"); + } + + // Returns std::string.data() casted to unsigned char*. + // Use s().size() to get length. + virtual const unsigned char* uc() const + { + throw RecoverableException("Not String"); + } + + //////////////////////////////////////////////////////////////////////////// + // Dictionary Interface + + // Returns the reference to BDE object associated with given key. + // If the key is not found, new pair with that key is created + // using default values, which is then returned. In other words, + // this is the same behavior of std::map's operator[]. + virtual BDE& operator[](const std::string& key) + { + throw RecoverableException("Not Dict"); + } + + // Returns true if the given key is found in dict. + virtual bool containsKey(const std::string& key) const + { + throw RecoverableException("Not Dict"); + } + + // Removes specified key from dict. + virtual void removeKey(const std::string& key) + { + throw RecoverableException("Not Dict"); + } + + // Returns a read/write iterator that points to the first pair in + // the dict. + virtual Dict::iterator dictBegin() + { + throw RecoverableException("Not Dict"); + } + + // Returns a read/write read-only iterator that points to one past + // the last pair in the dict. + virtual Dict::iterator dictEnd() + { + throw RecoverableException("Not Dict"); + } + + //////////////////////////////////////////////////////////////////////////// + // List Interface + + // Appends given bde to list. + virtual void append(const BDE& bde) + { + throw RecoverableException("Not List"); + } + + // Alias for append() + virtual void operator<<(const BDE& bde) + { + throw RecoverableException("Not List"); + } + + // Returns the reference of the object at the given index. + virtual BDE& operator[](size_t index) + { + throw RecoverableException("Not List"); + } + + // Returns a read/write iterator that points to the first object + // in list. + virtual List::iterator listBegin() + { + throw RecoverableException("Not List"); + } + + // Returns a read/write iterator that points to the one past the + // last object in list. + virtual List::iterator listEnd() + { + throw RecoverableException("Not List"); + } + + // Returns size of list or dict. + virtual size_t size() const + { + throw RecoverableException("Neither Dict nor List"); + } + + // Returns true if size of list or dict is 0. + virtual bool empty() const + { + throw RecoverableException("Neither Dict nor List"); + } + }; + + class BInteger : public BObject { + private: + Integer _integer; + public: + BInteger(Integer i):_integer(i) {} + + virtual BDE::Integer i() const + { + return _integer; + } + }; + + class BString : public BObject { + private: + std::string _string; + public: + BString(const std::string& string):_string(string) {} + + virtual const std::string& s() const + { + return _string; + } + + virtual const unsigned char* uc() const + { + return reinterpret_cast<const unsigned char*>(_string.data()); + } + }; + + class BDict : public BObject { + private: + Dict _dict; + public: + + virtual BDE& operator[](const std::string& key) + { + return _dict[key]; + } + + virtual bool containsKey(const std::string& key) const + { + return _dict.find(key) != _dict.end(); + } + + virtual void removeKey(const std::string& key) + { + _dict.erase(key); + } + + virtual BDE::Dict::iterator dictBegin() + { + return _dict.begin(); + } + + virtual BDE::Dict::iterator dictEnd() + { + return _dict.end(); + } + + virtual size_t size() const + { + return _dict.size(); + } + + virtual bool empty() const + { + return _dict.empty(); + } + }; + + class BList : public BObject { + private: + List _list; + public: + virtual void append(const BDE& bde) + { + _list.push_back(bde); + } + + virtual void operator<<(const BDE& bde) + { + _list.push_back(bde); + } + + virtual BDE& operator[](size_t index) + { + return _list[index]; + } + + virtual BDE::List::iterator listBegin() + { + return _list.begin(); + } + + virtual BDE::List::iterator listEnd() + { + return _list.end(); + } + + virtual size_t size() const + { + return _list.size(); + } + + virtual bool empty() const + { + return _list.empty(); + } + }; + TYPE _type; - SharedHandle<Dict> _dict; - SharedHandle<List> _list; - SharedHandle<std::string> _string; - SharedHandle<Integer> _integer; + SharedHandle<BObject> _bobject; public: BDE(); @@ -140,7 +362,7 @@ public: // Removes specified key from dict. // Requires this object to be Dict. - void removeKey(const std::string& key) const; + void removeKey(const std::string& key); // Returns a read/write iterator that points to the first pair in the dict. // Requires this object to be Dict. |
