Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

GeneralFncs.h

Go to the documentation of this file.
00001 #ifndef GENERAL_FNCS_H_
00002 #define GENERAL_FNCS_H_
00003 
00004 // C++ Library
00005 #include <ctime>
00006 #include <queue>
00007 #include <vector>
00008 
00009 // Linux Library
00010 #include <sys/time.h>
00011 
00012 namespace GeneralFncs {
00013 
00014     //! Deletes and erases each dynamically created element
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     //! Deletes and erases each dynamically created element
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     //! Deletes and erases a particular index in a vector
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     //! Deletes the memory pointed by a pointer
00049     template<typename T>
00050     inline void deletePtr(T*& ptr) {
00051         if (ptr != NULL) {
00052             delete ptr;
00053         }
00054         ptr = NULL;
00055     }
00056     
00057     //! Returns the time since  in seconds (value has millisecond-precision)
00058     inline unsigned long getTime() {
00059         // get the current time of day
00060         struct timeval cTime;
00061         gettimeofday(&cTime, NULL);
00062         // 43 1/2 years in seconds
00063         static const unsigned long kL43AndHalfYrsInSecs = 1372726281L;
00064         // calculate the seconds since middle of 2013 ==> Time Since Epoch (secs) - 43.5 years (secs)
00065         // then multiple answer by 1000 (to have space for millisecond-precision)
00066         unsigned long secsPortion = (static_cast<unsigned long>(cTime.tv_sec) - kL43AndHalfYrsInSecs);
00067         secsPortion *= 1000L;
00068         // calculate milliseconds ==> Microseconds / 1000 (integer division)
00069         unsigned long milliSecsPortion = (static_cast<unsigned long>(cTime.tv_usec) / 1000L);
00070         // add the two parts together and return the answer
00071         return (secsPortion + milliSecsPortion);
00072     }
00073 
00074     //! Returns a subset of a vector from the start position up to, but not including, the end position
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_

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:41 2016 by Doxygen 1.6.3