2 ******************************************************************************
3 * Copyright (C) 2014, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ******************************************************************************
8 ******************************************************************************
11 #ifndef __LRU_CACHE_H__
12 #define __LRU_CACHE_H__
14 #include "unicode/uobject.h"
15 #include "sharedobject.h"
22 * LRUCache keyed by locale ID.
25 class U_COMMON_API LRUCache
: public UObject
{
28 void get(const char *localeId
, const T
*&ptr
, UErrorCode
&status
) {
29 const T
*value
= (const T
*) _get(localeId
, status
);
30 if (U_FAILURE(status
)) {
33 SharedObject::copyPtr(value
, ptr
);
35 UBool
contains(const char *localeId
) const;
38 virtual SharedObject
*create(const char *localeId
, UErrorCode
&status
)=0;
39 LRUCache(int32_t maxSize
, UErrorCode
&status
);
41 class CacheEntry
: public UMemory
{
43 CacheEntry
*moreRecent
;
44 CacheEntry
*lessRecent
;
46 const SharedObject
*cachedData
;
47 UErrorCode status
; // This is the error if any from creating
55 char *adoptedLocId
, SharedObject
*dataToAdopt
, UErrorCode err
);
57 CacheEntry(const CacheEntry
& other
);
58 CacheEntry
&operator=(const CacheEntry
& other
);
61 LRUCache(const LRUCache
&other
);
62 LRUCache
&operator=(const LRUCache
&other
);
64 // TODO (Travis Keep): Consider replacing both of these end nodes with a
66 CacheEntry
*mostRecentlyUsedMarker
;
67 CacheEntry
*leastRecentlyUsedMarker
;
68 UHashtable
*localeIdToEntries
;
71 void moveToMostRecent(CacheEntry
*cacheEntry
);
72 void init(char *localeId
, CacheEntry
*cacheEntry
);
73 const SharedObject
*_get(const char *localeId
, UErrorCode
&status
);
76 typedef SharedObject
*CreateFunc(const char *localeId
, UErrorCode
&status
);
78 class U_COMMON_API SimpleLRUCache
: public LRUCache
{
84 LRUCache(maxSize
, status
), createFunc(cf
) {
86 virtual ~SimpleLRUCache();
88 virtual SharedObject
*create(const char *localeId
, UErrorCode
&status
);
90 CreateFunc
*createFunc
;