~ubuntu-branches/debian/experimental/libtorrent/experimental

« back to all changes in this revision

Viewing changes to src/torrent/object.h

  • Committer: Bazaar Package Importer
  • Author(s): Qingning Huo
  • Date: 2006-05-10 21:43:03 UTC
  • mfrom: (1.1.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20060510214303-ufl3iim4lr93uv1j
Tags: 0.9.1-1
* New upstream release.
- Added views of the downloads that can be sorted and filtered.
  The keys '1' to '6' are bound to a set which can be modified with
  the 'view_*' options.  (See man page)
- Disconnect incoming connections early if the download is full.
  (Patch by Josef Drexler)
- Made IPv6 and ncursesw configurable, neither of which are actually
  used yet.
- Added a "session_save" option which is scheduled by default every
  30 minutes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// libTorrent - BitTorrent library
 
2
// Copyright (C) 2005-2006, Jari Sundell
 
3
//
 
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.
 
8
// 
 
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.
 
13
// 
 
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
 
17
//
 
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
 
22
// including the two.
 
23
//
 
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.
 
31
//
 
32
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
33
//
 
34
//           Skomakerveien 33
 
35
//           3185 Skoppum, NORWAY
 
36
 
 
37
#ifndef LIBTORRENT_OBJECT_H
 
38
#define LIBTORRENT_OBJECT_H
 
39
 
 
40
#include <string>
 
41
#include <map>
 
42
#include <list>
 
43
#include <inttypes.h>
 
44
#include <torrent/exceptions.h>
 
45
 
 
46
namespace torrent {
 
47
 
 
48
// Look into making a custom comp and allocator classes for the
 
49
// map_type which use a const char* for key_type.
 
50
 
 
51
class Object {
 
52
public:
 
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;
 
58
 
 
59
  enum type_type {
 
60
    TYPE_NONE,
 
61
    TYPE_VALUE,
 
62
    TYPE_STRING,
 
63
    TYPE_LIST,
 
64
    TYPE_MAP
 
65
  };
 
66
 
 
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);
 
72
 
 
73
  explicit Object(type_type t);
 
74
  
 
75
  ~Object() { clear(); }
 
76
 
 
77
  void                clear();
 
78
 
 
79
  type_type           type() const                            { return m_type; }
 
80
 
 
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; }
 
85
 
 
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; }
 
88
 
 
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; }
 
91
 
 
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; }
 
94
 
 
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; }
 
97
 
 
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); }
 
103
 
 
104
  // Should have an interface for that returns pointer or something,
 
105
  // so we don't need to search twice.
 
106
 
 
107
  Object&             get_key(const key_type& k);
 
108
  const Object&       get_key(const key_type& k) const;
 
109
 
 
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); }
 
112
 
 
113
  Object&             operator = (const Object& b);
 
114
 
 
115
 private:
 
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."); }
 
118
 
 
119
  type_type           m_type;
 
120
 
 
121
  union {
 
122
    int64_t             m_value;
 
123
    string_type*        m_string;
 
124
    list_type*          m_list;
 
125
    map_type*           m_map;
 
126
  };
 
127
};
 
128
 
 
129
inline
 
130
Object::Object(type_type t) :
 
131
  m_type(t) {
 
132
 
 
133
  switch (m_type) {
 
134
  case TYPE_NONE:
 
135
    break;
 
136
  case TYPE_VALUE:
 
137
    m_value = value_type();
 
138
    break;
 
139
  case TYPE_STRING:
 
140
    m_string = new string_type();
 
141
    break;
 
142
  case TYPE_LIST:
 
143
    m_list = new list_type();
 
144
    break;
 
145
  case TYPE_MAP:
 
146
    m_map = new map_type();
 
147
    break;
 
148
  }
 
149
}
 
150
 
 
151
}
 
152
 
 
153
#endif