1
// libTorrent - BitTorrent library
2
// Copyright (C) 2005-2006, Jari Sundell
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
// In addition, as a special exception, the copyright holders give
19
// permission to link the code of portions of this program with the
20
// OpenSSL library under certain conditions as described in each
21
// individual source file, and distribute linked combinations
24
// You must obey the GNU General Public License in all respects for
25
// all of the code used other than OpenSSL. If you modify file(s)
26
// with this exception, you may extend this exception to your version
27
// of the file(s), but you are not obligated to do so. If you do not
28
// wish to do so, delete this exception statement from your version.
29
// If you delete this exception statement from all source files in the
30
// program, then also delete it here.
32
// Contact: Jari Sundell <jaris@ifi.uio.no>
35
// 3185 Skoppum, NORWAY
37
#ifndef LIBTORRENT_OBJECT_H
38
#define LIBTORRENT_OBJECT_H
44
#include <torrent/exceptions.h>
48
// Look into making a custom comp and allocator classes for the
49
// map_type which use a const char* for key_type.
53
typedef int64_t value_type;
54
typedef std::string string_type;
55
typedef std::list<Object> list_type;
56
typedef std::map<std::string, Object> map_type;
57
typedef map_type::key_type key_type;
67
Object() : m_type(TYPE_NONE) {}
68
Object(const int64_t v) : m_type(TYPE_VALUE), m_value(v) {}
69
Object(const char* s) : m_type(TYPE_STRING), m_string(new string_type(s)) {}
70
Object(const string_type& s) : m_type(TYPE_STRING), m_string(new string_type(s)) {}
71
Object(const Object& b);
73
explicit Object(type_type t);
75
~Object() { clear(); }
79
type_type type() const { return m_type; }
81
bool is_value() const { return m_type == TYPE_VALUE; }
82
bool is_string() const { return m_type == TYPE_STRING; }
83
bool is_list() const { return m_type == TYPE_LIST; }
84
bool is_map() const { return m_type == TYPE_MAP; }
86
value_type& as_value() { check_throw(TYPE_VALUE); return m_value; }
87
const value_type& as_value() const { check_throw(TYPE_VALUE); return m_value; }
89
string_type& as_string() { check_throw(TYPE_STRING); return *m_string; }
90
const string_type& as_string() const { check_throw(TYPE_STRING); return *m_string; }
92
list_type& as_list() { check_throw(TYPE_LIST); return *m_list; }
93
const list_type& as_list() const { check_throw(TYPE_LIST); return *m_list; }
95
map_type& as_map() { check_throw(TYPE_MAP); return *m_map; }
96
const map_type& as_map() const { check_throw(TYPE_MAP); return *m_map; }
98
bool has_key(const key_type& k) const { check_throw(TYPE_MAP); return m_map->find(k) != m_map->end(); }
99
bool has_key_value(const key_type& k) const { check_throw(TYPE_MAP); return check(m_map->find(k), TYPE_VALUE); }
100
bool has_key_string(const key_type& k) const { check_throw(TYPE_MAP); return check(m_map->find(k), TYPE_STRING); }
101
bool has_key_list(const key_type& k) const { check_throw(TYPE_MAP); return check(m_map->find(k), TYPE_LIST); }
102
bool has_key_map(const key_type& k) const { check_throw(TYPE_MAP); return check(m_map->find(k), TYPE_MAP); }
104
// Should have an interface for that returns pointer or something,
105
// so we don't need to search twice.
107
Object& get_key(const key_type& k);
108
const Object& get_key(const key_type& k) const;
110
Object& insert_key(const key_type& k, const Object& b) { check_throw(TYPE_MAP); return (*m_map)[k] = b; }
111
void erase_key(const key_type& k) { check_throw(TYPE_MAP); m_map->erase(k); }
113
Object& operator = (const Object& b);
116
inline bool check(map_type::const_iterator itr, type_type t) const { return itr != m_map->end() && itr->second.m_type == t; }
117
inline void check_throw(type_type t) const { if (t != m_type) throw bencode_error("Wrong object type."); }
123
string_type* m_string;
130
Object::Object(type_type t) :
137
m_value = value_type();
140
m_string = new string_type();
143
m_list = new list_type();
146
m_map = new map_type();