Main Page   Modules   Namespace List   Class Hierarchy   Data Structures   File List   Namespace Members   Data Fields   Globals   Related Pages  

SequenceMap.cpp

Go to the documentation of this file.
00001 #include "SequenceMap.h"
00002 #include "Sequence.h"
00003 
00004 #include <iostream>
00005 using namespace std;
00006 
00007 namespace SctCalibrationController {
00008     
00009 SequenceMap::SequenceMap() throw() {
00010 }
00011 
00012 SequenceMap& SequenceMap::instance() throw() {
00013     static SequenceMap map;
00014     return map;
00015 }
00016     
00017 weak_ptr<Sequence> SequenceMap::getSequence(const string& name) throw() {
00018     if (map.count(name) == 0) return weak_ptr<Sequence>();
00019     SequenceMapData& d = (*map.find(name)).second;
00020     d.lastRequested = time(0);
00021     
00022     return weak_ptr<Sequence>(d.sequence);
00023 }
00024 
00025 void SequenceMap::addSequence(shared_ptr<Sequence> sequence) throw() {
00026     purge();
00027     SequenceMapData d;
00028     d.sequence = sequence;
00029     map[sequence->getUniqueID()] = d;
00030     cout << "Sequence added" << endl;
00031 }
00032 
00033 //Only remove objects if there are more than 10 of them!
00034 //Remove at most 1!
00035 void SequenceMap::purge() throw() {
00036     if (map.size() < 10) return;
00037     
00038     for (DataMap::iterator i = map.begin(); i!=map.end(); ++i) {
00039     SequenceMapData& d = (*i).second;
00040     time_t now = time(0);
00041     if (difftime(now, d.lastRequested) > 3600 && d.sequence.unique()) {
00042         remove(i);
00043         return;
00044     }
00045     }
00046   
00047     //For some reason we couldn't remove any of them - remove the oldest!
00048     if (map.size() < 20) return;
00049  
00050     DataMap::iterator oldest = map.begin();
00051     for (DataMap::iterator i = ++map.begin(); i!=map.end(); ++i) {
00052     SequenceMapData& d = (*i).second;
00053     SequenceMapData& old = (*oldest).second;
00054     if (d.lastRequested < old.lastRequested) {
00055         oldest = i;
00056     }
00057     }
00058     remove(oldest);
00059 }
00060 
00061 void SequenceMap::remove(DataMap::iterator& i) throw() {
00062     cout << "Removing element from SequenceMap: " << (*i).second.sequence->getUniqueID() << endl;
00063     map.erase(i);
00064     
00065 }
00066 
00067 SequenceMap::SequenceMapData::SequenceMapData() throw(): 
00068     created(time(&created)), lastRequested(time(&lastRequested)) {
00069 }
00070 
00071 }

Generated on Mon Dec 15 19:36:16 2003 for SCT DAQ/DCS Software by doxygen1.3-rc3