XRootD
XrdSsiRRTable.hh
Go to the documentation of this file.
1 #ifndef __XRDSSIRRTABLE_HH__
2 #define __XRDSSIRRTABLE_HH__
3 /******************************************************************************/
4 /* */
5 /* X r d S s i R R T a b l e . h h */
6 /* */
7 /* (c) 2017 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Department of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /******************************************************************************/
31 
32 #include <map>
33 #include <cstdint>
34 
35 #include "XrdSsi/XrdSsiAtomics.hh"
36 
37 template<class T>
39 {
40 public:
41 
42 void Add(T *item, uint64_t itemID)
43  {rrtMutex.Lock();
44  if (baseItem != 0) theMap[itemID] = item;
45  else {baseKey = itemID;
46  baseItem = item;
47  }
48  rrtMutex.UnLock();
49  }
50 
51 void Clear() {rrtMutex.Lock(); theMap.clear(); rrtMutex.UnLock();}
52 
53 void Del(uint64_t itemID, bool finit=false)
54  {XrdSsiMutexMon lck(rrtMutex);
55  if (baseItem && baseKey == itemID)
56  {if (finit) baseItem->Finalize();
57  baseItem = 0;
58  } else {
59  if (!finit) theMap.erase(itemID);
60  else {typename std::map<uint64_t,T*>::iterator it = theMap.find(itemID);
61  if (it != theMap.end()) it->second->Finalize();
62  theMap.erase(it);
63  }
64  }
65  }
66 
67 T *LookUp(uint64_t itemID)
68  {XrdSsiMutexMon lck(rrtMutex);
69  if (baseItem && baseKey == itemID) return baseItem;
70  typename std::map<uint64_t,T*>::iterator it = theMap.find(itemID);
71  return (it == theMap.end() ? 0 : it->second);
72  }
73 
74 int Num() {return theMap.size() + (baseItem ? 1 : 0);}
75 
76 void Reset()
77  {XrdSsiMutexMon lck(rrtMutex);
78  typename std::map<uint64_t, T*>::iterator it = theMap.begin();
79  while(it != theMap.end())
80  {it->second->Finalize();
81  it++;
82  }
83  theMap.clear();
84  if (baseItem)
85  {baseItem->Finalize();
86  baseItem = 0;
87  }
88  }
89 
90  XrdSsiRRTable() : baseItem(0), baseKey(0) {}
91 
93 
94 private:
95 XrdSsiMutex rrtMutex;
96 T *baseItem;
97 uint64_t baseKey;
98 std::map<uint64_t, T*> theMap;
99 };
100 #endif
void Add(T *item, uint64_t itemID)
void Del(uint64_t itemID, bool finit=false)
T * LookUp(uint64_t itemID)