Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:27

0001 #ifndef PHOOL_PHTIMESERVER_H
0002 #define PHOOL_PHTIMESERVER_H
0003 
0004 /*!
0005 \file       PHTimeServer.h
0006 \brief   PHTimer server for accessing external information
0007 \author Hugo Pereira
0008 \version $Revision: 1.5 $
0009 \date       $Date: 2014/01/12 16:15:05 $
0010 */
0011 
0012 #include "PHTimer.h"
0013 
0014 #include <iostream>
0015 #include <map>
0016 #include <memory>
0017 #include <string>
0018 #include <utility>
0019 
0020 /*!
0021 \class   PHTimeServer
0022 \brief   PHTimer server for accessing external information
0023 */
0024 
0025 class PHTimeServer
0026 {
0027  public:
0028   //! wrapper around PHTimer, for storage in a map
0029   class timer
0030   {
0031    public:
0032     timer(const std::string& key, unsigned short uid)
0033       : _timer(new PHTimer(key))
0034       , _uid(uid)
0035     {
0036     }
0037 
0038     PHTimer* get(void)
0039     {
0040       return _timer.get();
0041     }
0042 
0043     const PHTimer* get(void) const
0044     {
0045       return _timer.get();
0046     }
0047 
0048     unsigned short get_uid(void) const
0049     {
0050       return _uid;
0051     }
0052 
0053    private:
0054     std::shared_ptr<PHTimer> _timer;
0055     unsigned short _uid;
0056   };
0057 
0058   //! singleton accessor
0059   static PHTimeServer* get(void)
0060   {
0061     static PHTimeServer* _server = new PHTimeServer();
0062     return _server;
0063   }
0064 
0065   //! destructor
0066   virtual ~PHTimeServer()
0067   {
0068   }
0069 
0070   //! insert new timer in map.
0071   timer insert_new(const std::string&);
0072 
0073   //! insert new single_shot timer in map.
0074   timer insert_new_single_shot(const std::string&);
0075 
0076   //! retrieve existing timer. throw exception if not found
0077   timer get_timer(const std::string&);
0078 
0079   //! retrieve existing timer. throw exception if not found
0080   timer get_single_shot_timer(const std::string&);
0081 
0082   //! dump all registered timer value.
0083   void print(std::ostream& out = std::cout) const;
0084 
0085   //! dump all registered timer statistics.
0086   void print_stat(std::ostream& out = std::cout) const;
0087 
0088  protected:
0089   //! constructor
0090   PHTimeServer()
0091     : _timer_id(0)
0092     , _single_shot_timer_id(0)
0093   {
0094   }
0095 
0096  private:
0097   //! map
0098   typedef std::map<std::string, timer> time_map;
0099 
0100   //! iterator over the map.
0101   typedef std::map<std::string, timer>::iterator time_iterator;
0102 
0103   //! iterator over the map.
0104   typedef std::map<std::string, timer>::const_iterator const_time_iterator;
0105 
0106   //! list of timers
0107   time_map _timers;
0108 
0109   //! list of single shot timers
0110   time_map _single_shot_timers;
0111 
0112   //! running timer unique id
0113   unsigned short _timer_id;
0114 
0115   //! running single shot timer unique id
0116   unsigned short _single_shot_timer_id;
0117 
0118  public:
0119   //! light iterator over PHTimer map
0120   class iterator
0121   {
0122    public:
0123     //! get PHTimer associated to current iterator position, advance iterator
0124     PHTimeServer::timer* next()
0125     {
0126       if (_iter == _map.end()) return nullptr;
0127       PHTimeServer::timer* out(&_iter->second);
0128       ++_iter;
0129       return out;
0130     }
0131 
0132     //! get PHTimer associated to current iterator position
0133     PHTimeServer::timer* current()
0134     {
0135       if (_iter == _map.end()) return nullptr;
0136       return &_iter->second;
0137     }
0138 
0139    protected:
0140     //! creator
0141     explicit iterator(PHTimeServer::time_map& map)
0142       : _map(map)
0143       , _iter(_map.begin())
0144     {
0145     }
0146 
0147    private:
0148     //! map of PHTimer
0149     PHTimeServer::time_map _map;
0150 
0151     //! iterator over the map
0152     PHTimeServer::time_iterator _iter;
0153 
0154     friend class PHTimeServer;
0155   };
0156 
0157   //! return iterator over the map, located at begin
0158   iterator range(void) { return iterator(_timers); }
0159 };
0160 #endif