trunk 20080912
[gitenigma.git] / lib / dvb / servicecache.cpp
blob271bf5f892be54da1b84c932dd3163aa39279cb3
1 #include <lib/dvb/servicecache.h>
2 #include <lib/system/econfig.h>
4 void eServiceCacheBase::enterDirectory(const eServiceReference &parent, Signal1<void, const eServiceReference&> &callback)
6 // int pLockActive = eConfig::getInstance()->pLockActive();
7 std::map<eServiceReference,eNode>::iterator i=cache.find(parent);
8 eNode *node=0;
9 if (i == cache.end())
11 node=&cache.insert(std::pair<eServiceReference,eNode>(parent, eNode())).first->second;
12 loadNode(*node, parent);
13 } else
14 node=&i->second;
15 node->addRef();
16 for (std::list<eServiceReference>::iterator i(node->content.begin()); i != node->content.end(); ++i)
17 callback(*i);
20 void eServiceCacheBase::leaveDirectory(const eServiceReference &parent)
22 std::map<eServiceReference,eNode>::iterator i=cache.find(parent);
23 if (i == cache.end())
24 eDebug("leaveDirectory on non-cached directory!");
25 else
27 eNode &node=i->second;
28 if (node.removeRef() <= 0)
29 cache.erase(i);
33 void eServiceCacheBase::addPersistentService(const eServiceReference &serviceref, eService *service)
35 eCachedService c;
36 c.refcnt=-1;
37 c.service=service;
38 services.insert(std::pair<eServiceReference, eCachedService>(serviceref, c));
41 eService *eServiceCacheBase::addRef(const eServiceReference &serviceref)
43 if (services.find(serviceref) == services.end()) // service not exist in cache ?
45 eCachedService c; // create new Cache Entry
46 c.refcnt=1; // currently one Object holds a reference to the new cache entry
47 c.service=createService(serviceref);
48 if (!c.service)
50 eDebug("createService failed!");
51 return 0;
53 services.insert(std::pair<eServiceReference,eCachedService>(serviceref, c));
54 return c.service;
55 } else
57 eCachedService &c=services.find(serviceref)->second;
58 if (c.refcnt != -1)
59 c.refcnt++;
60 return c.service;
64 void eServiceCacheBase::removeRef(const eServiceReference &serviceref)
66 std::map<eServiceReference,eCachedService>::iterator c=services.find(serviceref);
67 if (c == services.end())
69 eDebug("removeRef on non-existing service!");
70 return;
72 if ((c->second.refcnt != -1) && ! --c->second.refcnt)
74 delete c->second.service;
75 services.erase(c);