ResourcePoolEventObserver.hh

00001 //********************************************************************
00002 //*** ResourcePool/ResourcePoolEventObserver.hh
00003 //*** Copyright (c) 2003-2009 by Markus Winand <mws@fatalmind.com>
00004 //*** $Id: ResourcePoolEventObserver.hh,v 1.18 2009-03-14 13:43:43 mws Exp $
00005 //********************************************************************
00006 /*
00007 This file is part of ResourcePool.
00008 
00009 ResourcePool is free software; you can redistribute it
00010 and/or modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2 of 
00012 the License, or (at your option) any later version.
00013 
00014 ResourcePool is distributed in the hope that it will be
00015 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00016 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with ResourcePool; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00022 02111-1307  USA */
00023 
00024 #ifndef RESOURCEPOOL_LOGINTERFACE_HH
00025 #define RESOURCEPOOL_LOGINTERFACE_HH
00026 
00027 #include <map>
00028 #include <algorithm>
00029 #include <sstream>
00030 #include "util/Clone.hh"
00031 #include "mm/gc_ptr.hh"
00032 #include "ResourcePool/Command.hh"
00033 
00034 namespace fatalmind {
00035 
00036 class EventReader; // Fwd decl
00091 class ResourcePoolEventObserver: public Clone {
00092     public:
00093         typedef unsigned int event_t;
00094         static const std::string NoResourceID;
00095 
00096         ResourcePoolEventObserver();
00097 
00110         virtual void event(event_t eventID,  const std::string& resourceID = NoResourceID, const std::string& message ="");
00111 
00112         bool isEvent(event_t eventID) {
00113             return ((logevents & eventID) != 0);
00114         }
00115 
00119         static struct Events {
00120             static const event_t INCREMENT_POOL;
00121             static const event_t DOWNSIZE_POOL;
00122             static const event_t FAIL_RESOURCE;
00123             static const event_t GET_RESOURCE_FAIL;
00124             static const event_t EXECUTE_NFE;
00125             static const event_t EXECUTE_FAIL;
00126 
00127             static const event_t TIME_HIRES;
00128 
00129             static const event_t FAIL;
00130             static const event_t ALL;
00131             static const event_t NONE;
00132         } Event;
00133 
00137         // returns references to the members in the map, they become invalid
00138         // after destruction.
00139         const std::string& getEventName(const event_t event) const;
00140 
00149         const event_t getEvent(const std::string& value) const;
00150 
00158         const event_t registerEvent(const std::string& value) ;
00159 
00167         const event_t getEventMask(const std::string& value) const;
00168 
00169         inline event_t setLogEvents(event_t events) {
00170             event_t rv = logevents;
00171             logevents = events;
00172             return rv;
00173         }
00178         void setDefaultEventMask();
00179 
00180         gc_ptr<EventReader> getReader(const event_t event, const std::string& r);
00181         
00182     protected:
00183 
00184         event_t logevents;
00185         virtual Clone* DoClone() const;
00186 
00187     private:
00188         typedef std::map<event_t, std::string> namemap_t;
00189         namemap_t names;
00190         int nextEventBit;
00191 
00192         static const std::string INVALID;
00193 };
00194 
00195 class EventReader: public usg_cnt_t<DefaultThreadedModel>, public std::ostringstream {
00196         typedef std::ostringstream super;
00197     private:
00198         // todo, could be destructed, would prefere gc_ptr/weak_ptr
00199         EventReader(ResourcePoolEventObserver& observer, ResourcePoolEventObserver::event_t e, std::string resource);
00200     friend  class ResourcePoolEventObserver;
00201     public:
00202         //EventReader(const EventReader& rho);
00203         // the d-tor actually loggs
00204         ~EventReader();
00205 
00206 /*
00207         template<typename T>
00208         inline
00209         EventReader& operator<< (const std::string& val) {
00210             if (observer.isEvent(event)) {
00211                 *((super*)this)<<val;
00212             }
00213             return *this;
00214         }
00215 */
00216 
00217         template<typename T>
00218         inline
00219         EventReader& operator<< (const T& val) {
00220             if (observer.isEvent(event)) {
00221                 super& s = *((super*)this);
00222                 s <<val;
00223             }
00224             return *this;
00225         }
00226 
00227 /*
00228         template<class T>
00229         EventReader& operator<< (const fatalmind::Command<T>& rho) {
00230                 if (observer.isEvent(event)) {
00231                         rho.outputoperator(*this);
00232                 }
00233                 return *this;
00234         }
00235 */
00236     private:
00237         ResourcePoolEventObserver& observer;
00238         ResourcePoolEventObserver::event_t event;
00239         std::string resource;
00240 //      std::ostringstream os;
00241 };
00242 
00243 } // namespace fatalmind
00244 #endif

Generated on Mon Nov 9 16:21:24 2009 for ResourcePool by  doxygen 1.5.3