00001 /* $Id: predicates.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */ 00002 00003 /** 00004 * @file 00005 * @brief Implementation details for the ana project dealing with client predicates. 00006 * 00007 * ana: Asynchronous Network API. 00008 * Copyright (C) 2010 - 2012 Guillermo Biset. 00009 * 00010 * This file is part of the ana project. 00011 * 00012 * System: ana 00013 * Language: C++ 00014 * 00015 * Author: Guillermo Biset 00016 * E-Mail: billybiset AT gmail DOT com 00017 * 00018 * ana is free software: you can redistribute it and/or modify 00019 * it under the terms of the GNU General Public License as published by 00020 * the Free Software Foundation, either version 2 of the License, or 00021 * (at your option) any later version. 00022 * 00023 * ana is distributed in the hope that it will be useful, 00024 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00025 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00026 * GNU General Public License for more details. 00027 * 00028 * You should have received a copy of the GNU General Public License 00029 * along with ana. If not, see <http://www.gnu.org/licenses/>. 00030 * 00031 */ 00032 00033 #ifndef ANA_PREDICATES_HPP 00034 #define ANA_PREDICATES_HPP 00035 00036 #include "common.hpp" 00037 00038 namespace ana 00039 { 00040 /** @name Predicates over client ids. 00041 * 00042 * Declaration of types used in conditional send operations, e.g. send_if. 00043 * \sa send_if 00044 */ 00045 //@{ 00046 /** A boolean predicate of client IDs. Used for conditional send operations. */ 00047 struct client_predicate 00048 { 00049 virtual ~client_predicate() {} 00050 00051 /** 00052 * Decides if a given condition applies to a client. 00053 * 00054 * @param client ID of the queried client. 00055 * @returns true if the condition holds for this client. 00056 */ 00057 virtual bool selects(net_id) const = 0; 00058 }; 00059 //@} 00060 00061 /// @cond false 00062 /** 00063 * Intended for private use, should be created with create_predicate. 00064 * 00065 * \sa create_predicate 00066 */ 00067 template<class predicate> 00068 class _generic_client_predicate : public client_predicate 00069 { 00070 public: 00071 /** Construct via a generic object with overloaded operator(). */ 00072 _generic_client_predicate(const predicate& pred) 00073 : pred(pred) 00074 { 00075 } 00076 00077 /** Copy constructor. */ 00078 _generic_client_predicate(const _generic_client_predicate<predicate>& other ) 00079 : client_predicate(), pred(other.pred) 00080 { 00081 } 00082 private: 00083 const predicate pred /** The predicate object. */ ; 00084 00085 /** 00086 * Implementation of the selection method using operator() from the 00087 * predicate object. 00088 * 00089 * \sa client_predicate 00090 */ 00091 virtual bool selects(net_id cid) const 00092 { 00093 return pred(cid); 00094 } 00095 }; 00096 /// @endcond 00097 00098 /** 00099 * Creates a client predicate to be used in send operations. 00100 * 00101 * This function can be used to create predicate objects from the standard library's 00102 * bind1st objects and from boost::bind too. 00103 * 00104 * Examples: 00105 * - server_->send_if(boost::asio::buffer( str ), this, create_predicate( 00106 * boost::bind( std::not_equal_to<net_id>(), client, _1) ) ); 00107 * 00108 * @param pred Predicate of the queried client. 00109 * @returns Predicate for client selection. 00110 */ 00111 template<class predicate> 00112 _generic_client_predicate<predicate> create_predicate(const predicate& pred) 00113 { 00114 return _generic_client_predicate<predicate>(pred); 00115 } 00116 } 00117 00118 #endif
| Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:45 for The Battle for Wesnoth | Gna! | Forum | Wiki | CIA | devdocs |