GeneralFncs.h
Go to the documentation of this file.00001 #ifndef GENERAL_FNCS_H_
00002 #define GENERAL_FNCS_H_
00003
00004
00005 #include <ctime>
00006 #include <queue>
00007 #include <vector>
00008
00009
00010 #include <sys/time.h>
00011
00012 namespace GeneralFncs {
00013
00014
00015 template<typename T>
00016 inline void destroyAllPtrsInQueue(std::queue<T*>& q) {
00017 while (!q.empty()) {
00018 if (q.front() != NULL) {
00019 delete q.front();
00020 q.front() = NULL;
00021 }
00022 q.pop();
00023 }
00024 }
00025
00026
00027 template<typename T>
00028 inline void destroyAllPtrsInVector(std::vector<T*>& vec) {
00029 while (!vec.empty()) {
00030 if (vec[0] != NULL) {
00031 delete vec[0];
00032 vec[0] = NULL;
00033 }
00034 vec.erase(vec.begin());
00035 }
00036 }
00037
00038
00039 template<typename T>
00040 inline void destroyPtrInVector(std::vector<T*>& vec, std::size_t elementIndex) {
00041 if (elementIndex < vec.size() && vec[elementIndex] != NULL) {
00042 delete vec[elementIndex];
00043 vec[elementIndex] = NULL;
00044 vec.erase(vec.begin() + elementIndex);
00045 }
00046 }
00047
00048
00049 template<typename T>
00050 inline void deletePtr(T*& ptr) {
00051 if (ptr != NULL) {
00052 delete ptr;
00053 }
00054 ptr = NULL;
00055 }
00056
00057
00058 inline unsigned long getTime() {
00059
00060 struct timeval cTime;
00061 gettimeofday(&cTime, NULL);
00062
00063 static const unsigned long kL43AndHalfYrsInSecs = 1372726281L;
00064
00065
00066 unsigned long secsPortion = (static_cast<unsigned long>(cTime.tv_sec) - kL43AndHalfYrsInSecs);
00067 secsPortion *= 1000L;
00068
00069 unsigned long milliSecsPortion = (static_cast<unsigned long>(cTime.tv_usec) / 1000L);
00070
00071 return (secsPortion + milliSecsPortion);
00072 }
00073
00074
00075 template <typename T>
00076 inline std::vector<T> subVector(const std::vector<T>& vec,
00077 const std::size_t startPos,
00078 const std::size_t endPos)
00079 {
00080 std::vector<T> subVec;
00081 const std::size_t kSize = vec.size();
00082 if (startPos >= kSize || endPos > kSize || startPos >= endPos)
00083 return subVec;
00084 for (std::size_t pos = startPos; pos < endPos; pos++)
00085 subVec.push_back(vec[pos]);
00086 return subVec;
00087 }
00088 }
00089
00090 #endif // GENERAL_FNCS_H_