user.hpp

Go to the documentation of this file.
00001 /* libobby - Network text editing library
00002  * Copyright (C) 2005 0x539 dev group
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public
00015  * License along with this program; if not, write to the Free
00016  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 #ifndef _OBBY_USER_HPP_
00020 #define _OBBY_USER_HPP_
00021 
00022 #include <string>
00023 #include <net6/packet.hpp>
00024 #include <net6/user.hpp>
00025 #include <net6/address.hpp>
00026 #include <net6/non_copyable.hpp>
00027 #include "serialise/object.hpp"
00028 #include "format_string.hpp"
00029 #include "colour.hpp"
00030 
00031 namespace obby
00032 {
00033 
00034 class user_table;
00035 
00038 class user: private net6::non_copyable
00039 {
00040 public:
00043         class flags
00044         {
00045         public:
00046                 static const flags NONE;
00047                 static const flags CONNECTED;
00048 
00049                 flags operator|(flags other) const { return flags(m_value | other.m_value); }
00050                 flags operator&(flags other) const { return flags(m_value & other.m_value); }
00051                 flags operator^(flags other) const { return flags(m_value ^ other.m_value); }
00052                 flags& operator|=(flags other) { m_value |= other.m_value; return *this; }
00053                 flags& operator&=(flags other) { m_value &= other.m_value; return *this; }
00054                 flags& operator^=(flags other) { m_value ^= other.m_value; return *this; }
00055                 flags operator~() const { return flags(~m_value); }
00056 
00057                 operator bool() const { return m_value != NONE.m_value; }
00058                 bool operator!() const { return m_value == NONE.m_value; }
00059                 bool operator==(flags other) const { return m_value == other.m_value; }
00060                 bool operator!=(flags other) const { return m_value != other.m_value; }
00061 
00062                 unsigned int get_value() const { return m_value; }
00063 
00064         protected:
00065                 explicit flags(unsigned int value) : m_value(value) { }
00066 
00067                 unsigned int m_value;
00068         };
00069 
00072         class privileges
00073         {
00074         public:
00075                 static const privileges NONE;
00076                 static const privileges CREATE_DOCUMENT;
00077 
00078                 privileges operator|(privileges other) const { return privileges(m_value | other.m_value); }
00079                 privileges operator&(privileges other) const { return privileges(m_value & other.m_value); }
00080                 privileges operator^(privileges other) const { return privileges(m_value ^ other.m_value); }
00081                 privileges& operator|=(privileges other) { m_value |= other.m_value; return *this; }
00082                 privileges& operator&=(privileges other) { m_value &= other.m_value; return *this; }
00083                 privileges& operator^=(privileges other) { m_value ^= other.m_value; return *this; }
00084                 privileges operator~() const { return privileges(~m_value); }
00085 
00086                 operator bool() const { return m_value != NONE.m_value; }
00087                 bool operator!() const { return m_value == NONE.m_value; }
00088                 bool operator==(privileges other) const { return m_value == other.m_value; }
00089                 bool operator!=(privileges other) const { return m_value != other.m_value; }
00090 
00091                 unsigned int get_value() const { return m_value; }
00092 
00093         protected:
00094                 explicit privileges(unsigned int value) : m_value(value) { }
00095 
00096                 unsigned int m_value;
00097         };
00098 
00104         user(unsigned int id,
00105              const net6::user& user6,
00106              const colour& colour);
00107 
00114         user(unsigned int id,
00115              const std::string& name,
00116              const colour& colour);
00117 
00120         user(const serialise::object& obj);
00121 
00124         void serialise(serialise::object& obj) const;
00125 
00131         void release_net6();
00132 
00137         void assign_net6(const net6::user& user6,
00138                          const colour& colour);
00139 
00142         const net6::user& get_net6() const;
00143 
00146         const std::string& get_name() const;
00147 
00154         const net6::address& get_address() const;
00155 
00158         unsigned int get_id() const;
00159 
00162         const colour& get_colour() const;
00163 
00167         const std::string& get_password() const;
00168 
00171         flags get_flags() const;
00172 
00175         void set_colour(const colour& colour);
00176 
00179         void set_password(const std::string& password);
00180 
00183         void add_flags(flags new_flags);
00184 
00187         void remove_flags(flags old_flags);
00188 
00189 protected:
00190         const net6::user* m_user6;
00191 
00192         unsigned int m_id;
00193         std::string m_name;
00194         colour m_colour;
00195 
00196         std::string m_password;
00197 
00198         flags m_flags;
00199         privileges m_privs;
00200 };
00201 
00202 } // namespace obby
00203 
00204 namespace serialise
00205 {
00206 
00207 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00208 // Equivalent to user_table.find(index). Implemented in user.cpp. Required
00209 // to resolve the caller to user_table::find without having included
00210 // user_table.hpp here.
00211 const obby::user* user_table_find(const obby::user_table& user_table,
00212                                   unsigned int index);
00213 #endif // DOXYGEN_SHOULD_SKIP_THIS
00214 
00217 template<typename User>
00218 class user_context_to: public context_base_to<User>
00219 {
00220 public:
00221         typedef User data_type;
00222 
00223         virtual std::string to_string(const data_type& from) const;
00224 
00225 protected:
00226         virtual void on_stream_setup(std::stringstream& stream) const;
00227 };
00228 
00231 template<>
00232 class default_context_to<obby::user*>:
00233         public user_context_to<obby::user*>
00234 {
00235 };
00236 
00239 template<>
00240 class default_context_to<const obby::user*>:
00241         public user_context_to<const obby::user*>
00242 {
00243 };
00244 
00248 template<typename User>
00249 class user_context_from:
00250         public context_base_from<User>
00251 {
00252 public:
00253         typedef User data_type;
00254 
00255         user_context_from(const obby::user_table& user_table);
00256         virtual data_type from_string(const std::string& from) const;
00257 
00258 protected:
00259         virtual void on_stream_setup(std::stringstream& stream) const;
00260 
00261         const obby::user_table& m_user_table;
00262 };
00263 
00266 template<typename User>
00267 class user_hex_context_from: public user_context_from<User>
00268 {
00269 public:
00270         user_hex_context_from(const obby::user_table& user_table);
00271 
00272 protected:
00273         virtual void on_stream_setup(std::stringstream& stream) const;
00274 };
00275 
00278 template<>
00279 class default_context_from<const obby::user*>:
00280         public user_context_from<const obby::user*>
00281 {
00282 public:
00283         default_context_from(const obby::user_table& user_table);
00284 };
00285 
00288 template<>
00289 class hex_context_from<const obby::user*>:
00290         public user_hex_context_from<const obby::user*>
00291 {
00292 public:
00293         hex_context_from(const obby::user_table& user_table);
00294 };
00295 
00296 template<typename User>
00297 std::string user_context_to<User>::to_string(const data_type& from) const
00298 {
00299         std::stringstream stream;
00300         on_stream_setup(stream);
00301         stream << ( (from != NULL) ? (from->get_id()) : (0) );
00302         return stream.str();
00303 }
00304 
00305 template<typename User>
00306 void user_context_to<User>::on_stream_setup(std::stringstream& stream) const
00307 {
00308 }
00309 
00310 template<typename User>
00311 user_context_from<User>::user_context_from(const obby::user_table& user_table):
00312         m_user_table(user_table)
00313 {
00314 }
00315 
00316 template<typename User>
00317 typename user_context_from<User>::data_type
00318 user_context_from<User>::from_string(const std::string& from) const
00319 {
00320         unsigned int user_id;
00321         std::stringstream stream(from);
00322         on_stream_setup(stream);
00323         stream >> user_id;
00324 
00325         if(stream.bad() )
00326                 throw conversion_error("User ID must be an integer");
00327 
00328         // Special meaning "no user"
00329         if(user_id == 0) return NULL;
00330 
00331         data_type user = user_table_find(m_user_table, user_id);
00332 
00333         if(user == NULL)
00334         {
00335                 obby::format_string str("User ID %0% does not exist");
00336                 str << user_id;
00337                 throw conversion_error(str.str());
00338         }
00339 
00340         return user;
00341 }
00342 
00343 template<typename User>
00344 void user_context_from<User>::on_stream_setup(std::stringstream& stream) const
00345 {
00346 }
00347 
00348 template<typename User>
00349 user_hex_context_from<User>::
00350 	user_hex_context_from(const obby::user_table& user_table):
00351         user_context_from<User>(user_table)
00352 {
00353 }
00354 
00355 template<typename User>
00356 void user_hex_context_from<User>::
00357 	on_stream_setup(std::stringstream& stream) const
00358 {
00359         stream >> std::hex;
00360 }
00361 
00362 } // namespace serialise
00363 
00364 #endif // _OBBY_USER_HPP_

Generated on Fri Jan 11 10:01:32 2008 for obby by  doxygen 1.5.1