00001 /* 00002 Copyright (C) 2010 - 2014 Christoph Haenisch 00003 00004 Chair of Medical Engineering (mediTEC) 00005 RWTH Aachen University 00006 Pauwelsstr. 20 00007 52074 Aachen 00008 Germany 00009 00010 See license.txt for more information. 00011 00012 Version 0.1.1 (2011-09-09) 00013 */ 00014 00020 #include <cassert> 00021 #include <cstddef> 00022 #include <queue> 00023 00024 #include "TRTK/Timestamp.hpp" 00025 00026 00027 using std::queue; 00028 using std::map; 00029 using std::set; 00030 using std::string; 00031 00032 00033 namespace TRTK 00034 { 00035 00036 00038 // Timestamp // 00040 00043 Timestamp::Timestamp() 00044 { 00045 }; 00046 00047 00050 Timestamp::~Timestamp() 00051 { 00052 }; 00053 00054 00067 Timestamp::ObjectInfo & Timestamp::operator[](const string & object_name) 00068 { 00069 if (objects.count(object_name)) 00070 { 00071 return objects[object_name]; 00072 } 00073 else 00074 { 00075 // First initialize the ObjectInfo object, store it in the 00076 // map and then return its reference. Otherwise it would 00077 // not know its parent collection, that is /this/. 00078 00079 objects.insert(std::pair<string, ObjectInfo>(object_name, ObjectInfo(*this))); 00080 return objects[object_name]; 00081 } 00082 }; 00083 00084 00086 // ObjectInfo // 00088 00096 Timestamp::ObjectInfo::ObjectInfo() : 00097 parent_collection(*reinterpret_cast<Timestamp *>(NULL)), 00098 timestamp(0) 00099 { 00100 const bool DO_NO_USE_THIS_CONSTRUCTOR = false; 00101 assert(DO_NO_USE_THIS_CONSTRUCTOR); 00102 } 00103 00104 00107 Timestamp::ObjectInfo::ObjectInfo(Timestamp & parent_collection) : 00108 parent_collection(parent_collection), 00109 timestamp(0) 00110 { 00111 } 00112 00113 00116 Timestamp::ObjectInfo::~ObjectInfo() 00117 { 00118 } 00119 00120 00131 void Timestamp::ObjectInfo::addDependency(const std::string & object_name) 00132 { 00133 dependencies.insert(object_name); 00134 } 00135 00136 00148 bool Timestamp::ObjectInfo::dependenciesHaveChanged() const 00149 { 00150 // Check if the timestamps of all dependencies are further back in 00151 // time than the timestamp of this object. 00152 00153 std::set<std::string>::const_iterator it; 00154 00155 for (it = dependencies.begin(); it != dependencies.end(); ++it) 00156 { 00157 if (!(parent_collection[*it].dependenciesUpToDate(timestamp))) 00158 { 00159 return true; 00160 } 00161 } 00162 00163 return false; 00164 } 00165 00166 00177 bool Timestamp::ObjectInfo::dependencyHasChanged(const std::string & other_object) const 00178 { 00179 return !parent_collection[other_object].dependenciesUpToDate(timestamp); 00180 } 00181 00182 00190 bool Timestamp::ObjectInfo::dependenciesUpToDate(const clock_t & timestamp) const 00191 { 00192 // Are there any dependencies? 00193 00194 if (dependencies.size() == 0) 00195 { 00196 // No, there aren't any dependencies. 00197 00198 if (this->timestamp < timestamp) 00199 { 00200 return true; 00201 } 00202 else 00203 { 00204 return false; 00205 } 00206 } 00207 else 00208 { 00209 // Yes, there are dependencies. Thus, check, if all dependencies 00210 // have a timestamp that is further back in time than the given 00211 // one. In this case return true, else return false. 00212 00213 std::set<std::string>::const_iterator it; 00214 00215 for (it = dependencies.begin(); it != dependencies.end(); ++it) 00216 { 00217 if (!(parent_collection[*it].dependenciesUpToDate(timestamp))) 00218 { 00219 return false; 00220 } 00221 } 00222 00223 return true; 00224 } 00225 } 00226 00227 00240 bool Timestamp::ObjectInfo::isMoreRecentThan(const std::string & object, bool dependencies) const 00241 { 00242 if (!dependencies) 00243 { 00244 return timestamp > parent_collection[object].timestamp; 00245 } 00246 else 00247 { 00248 // Check if 'object' is back in time compared to *this. If this 00249 // is the case also check its dependencies. Do this by inserting 00250 // direct dependencies into a queue and subsequently comparing 00251 // these entries in the same manner as above. 00252 00253 queue<string> dependencies; 00254 dependencies.push(object); 00255 00256 while (!dependencies.empty()) 00257 { 00258 string object = dependencies.front(); 00259 dependencies.pop(); 00260 00261 if (timestamp < parent_collection[object].timestamp) 00262 { 00263 return false; 00264 } 00265 00266 set<string>::const_iterator it = parent_collection[object].dependencies.begin(); 00267 00268 while (it != parent_collection[object].dependencies.end()) 00269 { 00270 dependencies.push(*it++); 00271 } 00272 } 00273 00274 return true; 00275 } 00276 } 00277 00278 00286 void Timestamp::ObjectInfo::removeDependency(const std::string & object_name) 00287 { 00288 dependencies.erase(object_name); 00289 } 00290 00291 00297 void Timestamp::ObjectInfo::update() 00298 { 00299 timestamp = std::clock(); 00300 } 00301 00302 00303 } // namespace TRTK
Documentation generated by Doxygen