1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
/***************************************************************************
* Copyright (C) 2006 by Robert Hogan *
* robert@roberthogan.net *
* Copyright (C) 2005 by Joris Guisson *
* joris.guisson@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <qstring.h>
#include <qstringlist.h>
#include <qdir.h>
#include "constants.h"
#include <map>
QString getFullLocation(const char *additionalPaths, const QString &name);
QStringList findPrograms(const QStringList &programList);
QStringList addPaths(const char *env_path);
using namespace bt;
namespace tk
{
QString BytesToString(bt::Uint64 bytes,int precision = -1);
QString KBytesPerSecToString(double speed,int precision = 1);
QString BytesPerSecToString(double bytes,int precision = 1);
QString DurationToString(bt::Uint32 nsecs);
QString calcBW(const QStringList &bwlist, int num);
template<class T> int CompareVal(T a,T b)
{
if (a < b)
return -1;
else if (a > b)
return 1;
else
return 0;
}
}
namespace bt
{
typedef Uint64 TimeStamp;
void UpdateCurrentTime();
extern TimeStamp global_time_stamp;
inline TimeStamp GetCurrentTime() {return global_time_stamp;}
TimeStamp Now();
/**
* @author Joris Guisson
* @brief Map of pointers
*
* A Map where the data is a pointer. The PtrMap has an autodeletion feature.
* When autodelete is on, every time we remove something from the map, the data
* will be deleted.
*/
template <class Key,class Data>
class PtrMap
{
bool autodel;
std::map<Key,Data*> pmap;
public:
/**
* Constructor.
* @param auto_del Wether or not to enable auto deletion
*/
PtrMap(bool autodel = false) : autodel(autodel)
{}
/**
* Destructor. Will delete all objects, if auto deletion is on.
*/
virtual ~PtrMap()
{
clear();
}
/**
* Return the number of key data pairs in the map.
*/
unsigned int count() const {return pmap.size();}
/**
* Enable or disable auto deletion.
* @param yes Enable if true, disable if false
*/
void setAutoDelete(bool yes)
{
autodel = yes;
}
typedef typename std::map<Key,Data*>::iterator iterator;
typedef typename std::map<Key,Data*>::const_iterator const_iterator;
iterator begin() {return pmap.begin();}
iterator end() {return pmap.end();}
const_iterator begin() const {return pmap.begin();}
const_iterator end() const {return pmap.end();}
/**
* Remove all objects, will delete them if autodelete is on.
*/
void clear()
{
if (autodel)
{
for (iterator i = pmap.begin();i != pmap.end();i++)
{
delete i->second;
i->second = 0;
}
}
pmap.clear();
}
/**
* Insert a key data pair.
* @param k The key
* @param d The data
* @param overwrite Wether or not to overwrite
* @return true if the insertion took place
*/
bool insert(const Key & k,Data* d,bool overwrite = true)
{
iterator itr = pmap.find(k);
if (itr != pmap.end())
{
if (overwrite)
{
if (autodel)
delete itr->second;
itr->second = d;
return true;
}
else
{
return false;
}
}
else
{
pmap[k] = d;
return true;
}
}
/**
* Find a key in the map and returns it's data.
* @param k The key
* @return The data of the key, 0 if the key isn't in the map
*/
Data* find(const Key & k)
{
iterator i = pmap.find(k);
return (i == pmap.end()) ? 0 : i->second;
}
/**
* Find a key in the map and returns it's data.
* @param k The key
* @return The data of the key, 0 if the key isn't in the map
*/
const Data* find(const Key & k) const
{
const_iterator i = pmap.find(k);
return (i == pmap.end()) ? 0 : i->second;
}
/**
* Check to see if a key is in the map.
* @param k The key
* @return true if it is part of the map
*/
bool contains(const Key & k) const
{
const_iterator i = pmap.find(k);
return i != pmap.end();
}
/**
* Erase a key from the map. Will delete
* the data if autodelete is on.
* @param key The key
* @return true if an erase took place
*/
bool erase(const Key & key)
{
iterator i = pmap.find(key);
if (i == pmap.end())
return false;
if (autodel)
delete i->second;
pmap.erase(i);
return true;
}
};
/**
* @author Joris Guisson
*
* Template array classes, makes creating dynamic buffers easier
* and safer.
*/
template<class T>
class Array
{
Uint32 num;
T* data;
public:
Array(Uint32 num = 0) : num(num),data(0)
{
if (num > 0)
data = new T[num];
}
~Array()
{
delete [] data;
}
T & operator [] (Uint32 i) {return data[i];}
const T & operator [] (Uint32 i) const {return data[i];}
operator const T* () const {return data;}
operator T* () {return data;}
/// Get the number of elements in the array
Uint32 size() const {return num;}
/**
* Fill the array with a value
* @param val The value
*/
void fill(T val)
{
for (Uint32 i = 0;i < num;i++)
data[i] = val;
}
};
}
#endif
|