Add main page for Doxygen generated documentation.
[tairent.git] / src / core / bencode.h
blob2f93cf2f9af4ba9da05dc5c6d6e712734348bb96
1 /***************************************************************************
2 * *
3 * Copyright (C) <year> <author> *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation and appearing *
8 * in the file LICENSE.GPL included in the packaging of this file. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #ifndef _core_bencode_h
18 #define _core_bencode_h
20 #include <stdint.h>
21 #include <list>
22 #include <map>
24 #include <tairon/core/exceptions.h>
26 using Tairon::Core::String;
28 namespace Tairent
31 namespace Core
34 /** \brief Class that is used to hold bencoded informations.
36 class BEncode
38 public:
39 /** List type.
41 typedef std::list<BEncode> List;
43 /** Dictionary type.
45 typedef std::map<String, BEncode> Map;
47 /** BEncode types.
49 enum Type {
50 NONE, VALUE, STRING, LIST, MAP
53 public:
54 /** Constructs BEncode object of type NONE.
56 BEncode();
58 /** Constructs BEncode object of type VALUE.
60 * \param v Value to store in object.
62 BEncode(const int64_t v);
64 /** Constructs BEncode object of type STRING.
66 * \param s String to store in object.
68 BEncode(const String &s);
70 /** Copy constructor.
72 * \param b BEncode object to copy.
74 BEncode(const BEncode &b);
76 /** Explicitly constructs BEncode object of given type.
78 * \param t Type of the object to construct.
80 explicit BEncode(Type t);
82 /** Destroys the object.
84 ~BEncode();
86 /** Converts this BEncode object to LIST and returns it as a reference.
88 BEncode::List &asList();
90 /** Converts this BEncode object to MAP and returns it as a reference.
92 BEncode::Map &asMap();
94 /** Converts this BEncode object to STRING and returns it as a
95 * reference.
97 String &asString();
99 /** Converts this BEncode object to VALUE and returns it as a
100 * reference.
102 int64_t &asValue();
104 /** Converts this BEncode object to LIST and returns a constant
105 * reference to it.
107 const BEncode::List &asList() const;
109 /** Converts this BEncode object to MAP and returns a constant
110 * reference to it.
112 const BEncode::Map &asMap() const;
114 /** Converts this BEncode object to STRING and returns a constant
115 * reference to it.
117 const String &asString() const;
119 /** Converts this BEncode object to VALUE and returns a constant
120 * reference to it.
122 const int64_t &asValue() const;
124 /** Clears all data from this object and its type sets to NONE.
126 void clear();
128 /** Returns true if this object is LIST; otherwise returns false.
130 bool isList() const;
132 /** Returns true if this object is MAP; otherwise returns false.
134 bool isMap() const;
136 /** Returns true if this object is STRING; otheriwse returns false.
138 bool isString() const;
140 /** Returns true if this object is VALUE; otherwise returns false.
142 bool isValue() const;
144 /** Returns type of this object.
146 Type getType() const;
148 /** Computes SHA1 hash of string representing this object.
150 String computeSHA1() const;
152 /** Assigns value of given object to this object and returns a
153 * reference to
154 * it.
156 * \param b Object to set the value of this object to.
158 BEncode &operator=(const BEncode &b);
160 /** If this object is MAP then it returns reference to BEncode object
161 * belonging to given key; otherwise raises an exception.
163 * \param key Key of the object to return.
165 BEncode &operator[](const String &key);
167 /** If this object is MAP then it returns constant reference to BEncode
168 * object belonging to given key; otherwise raises an exception.
170 * \param key Key of the object to return.
172 const BEncode &operator[](const String &key) const;
174 /** Loads encoded data from stream and stores them to object.
176 * \param s Input stream to read from.
177 * \param b BEncode object to store data into.
179 friend std::istream &operator>>(std::istream &s, BEncode &b);
181 /** Stores given object to stream.
183 * \param s Output stream to write to.
184 * \param b Object to store.
186 friend std::ostream &operator<<(std::ostream &s, const BEncode &b);
188 private:
189 /** Reads a string from a stream.
191 * \param s Input stream from which the data will be read.
192 * \param str String object in which the data will be stored.
194 static bool readString(std::istream &s, String &str);
196 private:
197 /** Type of this BEnocde.
199 Type type;
201 /** Value of this BEncode.
203 union {
204 /** Integer value.
206 int64_t value;
208 /** String value.
210 String *string;
212 /** List value.
214 BEncode::List *list;
216 /** Dictionary value.
218 BEncode::Map *map;
222 /** \brief Exception that is raised by BEncode class.
224 class BEncodeException : public Tairon::Core::Exception
226 public:
227 /** Standard constructor.
229 BEncodeException(const String &desc) : Tairon::Core::Exception(desc) {};
231 /** Default destructor.
233 ~BEncodeException() {};
236 }; // namespace Core
238 }; // namespace Tairent
240 #endif
242 // vim: ai sw=4 ts=4 noet fdm=marker