File indexing completed on 2025-08-06 08:17:27
0001 #ifndef PHOOL_PHTIMESERVER_H
0002 #define PHOOL_PHTIMESERVER_H
0003
0004
0005
0006
0007
0008
0009
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
0022
0023
0024
0025 class PHTimeServer
0026 {
0027 public:
0028
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
0059 static PHTimeServer* get(void)
0060 {
0061 static PHTimeServer* _server = new PHTimeServer();
0062 return _server;
0063 }
0064
0065
0066 virtual ~PHTimeServer()
0067 {
0068 }
0069
0070
0071 timer insert_new(const std::string&);
0072
0073
0074 timer insert_new_single_shot(const std::string&);
0075
0076
0077 timer get_timer(const std::string&);
0078
0079
0080 timer get_single_shot_timer(const std::string&);
0081
0082
0083 void print(std::ostream& out = std::cout) const;
0084
0085
0086 void print_stat(std::ostream& out = std::cout) const;
0087
0088 protected:
0089
0090 PHTimeServer()
0091 : _timer_id(0)
0092 , _single_shot_timer_id(0)
0093 {
0094 }
0095
0096 private:
0097
0098 typedef std::map<std::string, timer> time_map;
0099
0100
0101 typedef std::map<std::string, timer>::iterator time_iterator;
0102
0103
0104 typedef std::map<std::string, timer>::const_iterator const_time_iterator;
0105
0106
0107 time_map _timers;
0108
0109
0110 time_map _single_shot_timers;
0111
0112
0113 unsigned short _timer_id;
0114
0115
0116 unsigned short _single_shot_timer_id;
0117
0118 public:
0119
0120 class iterator
0121 {
0122 public:
0123
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
0133 PHTimeServer::timer* current()
0134 {
0135 if (_iter == _map.end()) return nullptr;
0136 return &_iter->second;
0137 }
0138
0139 protected:
0140
0141 explicit iterator(PHTimeServer::time_map& map)
0142 : _map(map)
0143 , _iter(_map.begin())
0144 {
0145 }
0146
0147 private:
0148
0149 PHTimeServer::time_map _map;
0150
0151
0152 PHTimeServer::time_iterator _iter;
0153
0154 friend class PHTimeServer;
0155 };
0156
0157
0158 iterator range(void) { return iterator(_timers); }
0159 };
0160 #endif