2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_ENTRY_HPP_INCLUDED
|
|
|
|
#define TORRENT_ENTRY_HPP_INCLUDED
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* This file declares the entry class. It is a
|
|
|
|
* variant-type that can be an integer, list,
|
|
|
|
* dictionary (map) or a string. This type is
|
|
|
|
* used to hold bdecoded data (which is the
|
|
|
|
* encoding BitTorrent messages uses).
|
|
|
|
*
|
|
|
|
* it has 4 accessors to access the actual
|
|
|
|
* type of the object. They are:
|
|
|
|
* integer()
|
|
|
|
* string()
|
|
|
|
* list()
|
|
|
|
* dict()
|
|
|
|
* The actual type has to match the type you
|
|
|
|
* are asking for, otherwise you will get an
|
|
|
|
* assertion failure.
|
|
|
|
* When you default construct an entry, it is
|
|
|
|
* uninitialized. You can initialize it through the
|
|
|
|
* assignment operator, copy-constructor or
|
|
|
|
* the constructor that takes a data_type enum.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <map>
|
2004-03-27 23:02:31 +01:00
|
|
|
#include <list>
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2004-01-18 02:58:33 +01:00
|
|
|
#include "libtorrent/size_type.hpp"
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2007-09-01 06:08:39 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2009-02-23 02:21:19 +01:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/max.hpp"
|
2004-01-18 02:58:33 +01:00
|
|
|
|
2009-04-04 23:44:04 +02:00
|
|
|
#if TORRENT_USE_IOSTREAM
|
|
|
|
#include <iosfwd>
|
|
|
|
#endif
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
struct TORRENT_EXPORT type_error: std::runtime_error
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
type_error(const char* error): std::runtime_error(error) {}
|
|
|
|
};
|
|
|
|
|
2004-03-30 01:25:13 +02:00
|
|
|
class entry;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
class TORRENT_EXPORT entry
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2005-08-11 13:06:52 +02:00
|
|
|
// the key is always a string. If a generic entry would be allowed
|
|
|
|
// as a key, sorting would become a problem (e.g. to compare a string
|
|
|
|
// to a list). The definition doesn't mention such a limit though.
|
2005-09-11 11:58:34 +02:00
|
|
|
typedef std::map<std::string, entry> dictionary_type;
|
2003-10-23 01:00:57 +02:00
|
|
|
typedef std::string string_type;
|
2004-03-27 23:02:31 +01:00
|
|
|
typedef std::list<entry> list_type;
|
2004-01-18 02:58:33 +01:00
|
|
|
typedef size_type integer_type;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
enum data_type
|
|
|
|
{
|
|
|
|
int_t,
|
|
|
|
string_t,
|
|
|
|
list_t,
|
|
|
|
dictionary_t,
|
|
|
|
undefined_t
|
|
|
|
};
|
|
|
|
|
2004-03-28 19:45:37 +02:00
|
|
|
data_type type() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
entry(dictionary_type const&);
|
|
|
|
entry(string_type const&);
|
|
|
|
entry(list_type const&);
|
|
|
|
entry(integer_type const&);
|
2004-01-07 01:48:02 +01:00
|
|
|
|
2004-03-28 19:45:37 +02:00
|
|
|
entry();
|
|
|
|
entry(data_type t);
|
2006-11-14 01:08:16 +01:00
|
|
|
entry(entry const& e);
|
2004-03-28 19:45:37 +02:00
|
|
|
~entry();
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-08-11 13:06:52 +02:00
|
|
|
bool operator==(entry const& e) const;
|
|
|
|
|
|
|
|
void operator=(entry const&);
|
|
|
|
void operator=(dictionary_type const&);
|
|
|
|
void operator=(string_type const&);
|
|
|
|
void operator=(list_type const&);
|
|
|
|
void operator=(integer_type const&);
|
2004-01-07 01:48:02 +01:00
|
|
|
|
2004-03-28 19:45:37 +02:00
|
|
|
integer_type& integer();
|
|
|
|
const integer_type& integer() const;
|
|
|
|
string_type& string();
|
|
|
|
const string_type& string() const;
|
|
|
|
list_type& list();
|
|
|
|
const list_type& list() const;
|
|
|
|
dictionary_type& dict();
|
|
|
|
const dictionary_type& dict() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
void swap(entry& e);
|
|
|
|
|
2004-03-17 13:14:44 +01:00
|
|
|
// these functions requires that the entry
|
|
|
|
// is a dictionary, otherwise they will throw
|
|
|
|
entry& operator[](char const* key);
|
|
|
|
entry& operator[](std::string const& key);
|
2007-11-02 03:02:52 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2004-03-17 13:14:44 +01:00
|
|
|
const entry& operator[](char const* key) const;
|
|
|
|
const entry& operator[](std::string const& key) const;
|
2007-11-02 03:02:52 +01:00
|
|
|
#endif
|
2004-03-17 13:14:44 +01:00
|
|
|
entry* find_key(char const* key);
|
|
|
|
entry const* find_key(char const* key) const;
|
2008-05-19 09:15:44 +02:00
|
|
|
entry* find_key(std::string const& key);
|
|
|
|
entry const* find_key(std::string const& key) const;
|
2004-03-17 13:14:44 +01:00
|
|
|
|
2009-07-21 10:44:27 +02:00
|
|
|
#if (defined TORRENT_VERBOSE_LOGGING || defined TORRENT_DEBUG) && TORRENT_USE_IOSTREAM
|
2003-10-28 02:20:50 +01:00
|
|
|
void print(std::ostream& os, int indent = 0) const;
|
2009-04-04 23:44:04 +02:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-04-07 05:03:34 +02:00
|
|
|
protected:
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
void construct(data_type t);
|
|
|
|
void copy(const entry& e);
|
|
|
|
void destruct();
|
|
|
|
|
2008-04-07 05:03:34 +02:00
|
|
|
private:
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
data_type m_type;
|
|
|
|
|
2010-03-03 08:42:51 +01:00
|
|
|
#if (defined(_MSC_VER) && _MSC_VER < 1310) || TORRENT_COMPLETE_TYPES_REQUIRED
|
2003-10-31 05:02:51 +01:00
|
|
|
// workaround for msvc-bug.
|
|
|
|
// assumes sizeof(map<string, char>) == sizeof(map<string, entry>)
|
2004-03-30 01:25:13 +02:00
|
|
|
// and sizeof(list<char>) == sizeof(list<entry>)
|
2003-10-23 01:00:57 +02:00
|
|
|
union
|
|
|
|
{
|
2004-03-30 01:25:13 +02:00
|
|
|
char data[
|
2009-11-23 09:38:50 +01:00
|
|
|
max4<sizeof(std::list<char>)
|
2003-10-23 01:00:57 +02:00
|
|
|
, sizeof(std::map<std::string, char>)
|
|
|
|
, sizeof(string_type)
|
|
|
|
, sizeof(integer_type)>::value];
|
|
|
|
integer_type dummy_aligner;
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
union
|
|
|
|
{
|
2009-11-23 09:38:50 +01:00
|
|
|
char data[max4<sizeof(list_type)
|
2003-10-23 01:00:57 +02:00
|
|
|
, sizeof(dictionary_type)
|
|
|
|
, sizeof(string_type)
|
|
|
|
, sizeof(integer_type)>::value];
|
|
|
|
integer_type dummy_aligner;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2008-01-03 04:10:25 +01:00
|
|
|
public:
|
|
|
|
// in debug mode this is set to false by bdecode
|
|
|
|
// to indicate that the program has not yet queried
|
|
|
|
// the type of this entry, and sould not assume
|
|
|
|
// that it has a certain type. This is asserted in
|
|
|
|
// the accessor functions. This does not apply if
|
|
|
|
// exceptions are used.
|
|
|
|
mutable bool m_type_queried;
|
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
2009-06-10 00:02:02 +02:00
|
|
|
#if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM
|
2003-10-23 01:00:57 +02:00
|
|
|
inline std::ostream& operator<<(std::ostream& os, const entry& e)
|
|
|
|
{
|
|
|
|
e.print(os, 0);
|
|
|
|
return os;
|
|
|
|
}
|
2009-04-04 23:44:04 +02:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2009-02-23 02:21:19 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
inline void throw_type_error()
|
|
|
|
{
|
|
|
|
throw libtorrent_exception(error_code(errors::invalid_entry_type
|
2009-11-29 08:06:38 +01:00
|
|
|
, get_libtorrent_category()));
|
2009-02-23 02:21:19 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-12-28 20:07:28 +01:00
|
|
|
inline entry::data_type entry::type() const
|
|
|
|
{
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-12-28 20:07:28 +01:00
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
|
|
|
return m_type;
|
|
|
|
}
|
2004-03-28 19:45:37 +02:00
|
|
|
|
|
|
|
inline entry::~entry() { destruct(); }
|
|
|
|
|
|
|
|
inline void entry::operator=(const entry& e)
|
|
|
|
{
|
|
|
|
destruct();
|
|
|
|
copy(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline entry::integer_type& entry::integer()
|
|
|
|
{
|
2006-11-14 01:08:16 +01:00
|
|
|
if (m_type == undefined_t) construct(int_t);
|
2007-11-02 03:02:52 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2009-02-23 02:21:19 +01:00
|
|
|
if (m_type != int_t) throw_type_error();
|
2008-11-29 22:33:21 +01:00
|
|
|
#elif defined TORRENT_DEBUG
|
2007-12-28 20:07:28 +01:00
|
|
|
TORRENT_ASSERT(m_type_queried);
|
2007-11-02 03:02:52 +01:00
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(m_type == int_t);
|
2004-03-28 19:45:37 +02:00
|
|
|
return *reinterpret_cast<integer_type*>(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline entry::integer_type const& entry::integer() const
|
|
|
|
{
|
2007-11-02 03:02:52 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2009-02-23 02:21:19 +01:00
|
|
|
if (m_type != int_t) throw_type_error();
|
2008-11-29 22:33:21 +01:00
|
|
|
#elif defined TORRENT_DEBUG
|
2007-12-28 20:07:28 +01:00
|
|
|
TORRENT_ASSERT(m_type_queried);
|
2007-11-02 03:02:52 +01:00
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(m_type == int_t);
|
2004-03-28 19:45:37 +02:00
|
|
|
return *reinterpret_cast<const integer_type*>(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline entry::string_type& entry::string()
|
|
|
|
{
|
2006-11-14 01:08:16 +01:00
|
|
|
if (m_type == undefined_t) construct(string_t);
|
2007-11-02 03:02:52 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2009-02-23 02:21:19 +01:00
|
|
|
if (m_type != string_t) throw_type_error();
|
2008-11-29 22:33:21 +01:00
|
|
|
#elif defined TORRENT_DEBUG
|
2007-12-28 20:07:28 +01:00
|
|
|
TORRENT_ASSERT(m_type_queried);
|
2007-11-02 03:02:52 +01:00
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(m_type == string_t);
|
2004-03-28 19:45:37 +02:00
|
|
|
return *reinterpret_cast<string_type*>(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline entry::string_type const& entry::string() const
|
|
|
|
{
|
2007-11-02 03:02:52 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2009-02-23 02:21:19 +01:00
|
|
|
if (m_type != string_t) throw_type_error();
|
2008-11-29 22:33:21 +01:00
|
|
|
#elif defined TORRENT_DEBUG
|
2007-12-28 20:07:28 +01:00
|
|
|
TORRENT_ASSERT(m_type_queried);
|
2007-11-02 03:02:52 +01:00
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(m_type == string_t);
|
2004-03-28 19:45:37 +02:00
|
|
|
return *reinterpret_cast<const string_type*>(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline entry::list_type& entry::list()
|
|
|
|
{
|
2006-11-14 01:08:16 +01:00
|
|
|
if (m_type == undefined_t) construct(list_t);
|
2007-11-02 03:02:52 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2009-02-23 02:21:19 +01:00
|
|
|
if (m_type != list_t) throw_type_error();
|
2008-11-29 22:33:21 +01:00
|
|
|
#elif defined TORRENT_DEBUG
|
2007-12-28 20:07:28 +01:00
|
|
|
TORRENT_ASSERT(m_type_queried);
|
2007-11-02 03:02:52 +01:00
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(m_type == list_t);
|
2004-03-28 19:45:37 +02:00
|
|
|
return *reinterpret_cast<list_type*>(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline entry::list_type const& entry::list() const
|
|
|
|
{
|
2007-11-02 03:02:52 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2009-02-23 02:21:19 +01:00
|
|
|
if (m_type != list_t) throw_type_error();
|
2008-11-29 22:33:21 +01:00
|
|
|
#elif defined TORRENT_DEBUG
|
2007-12-28 20:07:28 +01:00
|
|
|
TORRENT_ASSERT(m_type_queried);
|
2007-11-02 03:02:52 +01:00
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(m_type == list_t);
|
2004-03-28 19:45:37 +02:00
|
|
|
return *reinterpret_cast<const list_type*>(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline entry::dictionary_type& entry::dict()
|
|
|
|
{
|
2006-11-14 01:08:16 +01:00
|
|
|
if (m_type == undefined_t) construct(dictionary_t);
|
2007-11-02 03:02:52 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2009-02-23 02:21:19 +01:00
|
|
|
if (m_type != dictionary_t) throw_type_error();
|
2008-11-29 22:33:21 +01:00
|
|
|
#elif defined TORRENT_DEBUG
|
2007-12-28 20:07:28 +01:00
|
|
|
TORRENT_ASSERT(m_type_queried);
|
2007-11-02 03:02:52 +01:00
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(m_type == dictionary_t);
|
2004-03-28 19:45:37 +02:00
|
|
|
return *reinterpret_cast<dictionary_type*>(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline entry::dictionary_type const& entry::dict() const
|
|
|
|
{
|
2007-11-02 03:02:52 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2009-02-23 02:21:19 +01:00
|
|
|
if (m_type != dictionary_t) throw_type_error();
|
2008-11-29 22:33:21 +01:00
|
|
|
#elif defined TORRENT_DEBUG
|
2007-12-28 20:07:28 +01:00
|
|
|
TORRENT_ASSERT(m_type_queried);
|
2007-11-02 03:02:52 +01:00
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(m_type == dictionary_t);
|
2004-03-28 19:45:37 +02:00
|
|
|
return *reinterpret_cast<const dictionary_type*>(data);
|
|
|
|
}
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_ENTRY_HPP_INCLUDED
|
2008-11-29 22:33:21 +01:00
|
|
|
|