unifiedcache.h revision c14898b482f76ecab9026615e2e4c6fe78358bdc
1f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius/*
2f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius******************************************************************************
3c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert* Copyright (C) 2015, International Business Machines Corporation and
4f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius* others. All Rights Reserved.
5f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius******************************************************************************
6f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius*
7f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius* File UNIFIEDCACHE.H - The ICU Unified cache.
8f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius******************************************************************************
9f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius*/
10f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
11f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#ifndef __UNIFIED_CACHE_H__
12f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#define __UNIFIED_CACHE_H__
13f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
14f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#include "utypeinfo.h"  // for 'typeid' to work
15f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
16f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#include "unicode/uobject.h"
17f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#include "unicode/locid.h"
18f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#include "sharedobject.h"
19f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#include "unicode/unistr.h"
20f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#include "cstring.h"
21f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#include "ustr_imp.h"
22f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
23f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusstruct UHashtable;
24f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusstruct UHashElement;
25f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
26f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusU_NAMESPACE_BEGIN
27f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
28f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusclass UnifiedCache;
29f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
30f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius/**
31c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert * A base class for all cache keys.
32f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius */
33f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusclass U_COMMON_API CacheKeyBase : public UObject {
34f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius public:
35c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   CacheKeyBase() : fCreationStatus(U_ZERO_ERROR), fIsMaster(FALSE) {}
36f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
37f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
38f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Copy constructor. Needed to support cloning.
39f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
40f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   CacheKeyBase(const CacheKeyBase &other)
41c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert           : UObject(other), fCreationStatus(other.fCreationStatus), fIsMaster(FALSE) { }
42f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual ~CacheKeyBase();
43f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
44f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
45f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Returns the hash code for this object.
46f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
47f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual int32_t hashCode() const = 0;
48f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
49f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
50f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Clones this object polymorphically. Caller owns returned value.
51f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
52f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual CacheKeyBase *clone() const = 0;
53f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
54f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
55f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Equality operator.
56f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
57f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual UBool operator == (const CacheKeyBase &other) const = 0;
58f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
59f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
60f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Create a new object for this key. Called by cache on cache miss.
61f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * createObject must add a reference to the object it returns. Note
62f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * that getting an object from the cache and returning it without calling
63f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * removeRef on it satisfies this requirement. It can also return NULL
64f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * and set status to an error.
65f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *
66f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * @param creationContext the context in which the object is being
67f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *                        created. May be NULL.
68f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * @param status          Implementations can return a failure here.
69f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *                        In addition, implementations may return a
70f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *                        non NULL object and set a warning status.
71f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
72f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual const SharedObject *createObject(
73f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const void *creationContext, UErrorCode &status) const = 0;
74f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
75f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
76f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Writes a description of this key to buffer and returns buffer. Written
77f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * description is NULL terminated.
78f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
79f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual char *writeDescription(char *buffer, int32_t bufSize) const = 0;
80f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
81f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
82f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Inequality operator.
83f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
84f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   UBool operator != (const CacheKeyBase &other) const {
85f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       return !(*this == other);
86f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   }
87f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius private:
88c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   mutable UErrorCode fCreationStatus;
89c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   mutable UBool fIsMaster;
90f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   friend class UnifiedCache;
91f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius};
92f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
93f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
94f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
95f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius/**
96f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius * Templated version of CacheKeyBase.
97f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius * A key of type LocaleCacheKey<T> maps to a value of type T.
98f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius */
99f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliustemplate<typename T>
100f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusclass CacheKey : public CacheKeyBase {
101f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius public:
102f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual ~CacheKey() { }
103f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
104f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * The template parameter, T, determines the hash code returned.
105f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
106f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual int32_t hashCode() const {
107f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       const char *s = typeid(T).name();
108f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       return ustr_hashCharsN(s, uprv_strlen(s));
109f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   }
110f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
111f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
112f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Use the value type, T,  as the description.
113f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
114f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual char *writeDescription(char *buffer, int32_t bufLen) const {
115f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       const char *s = typeid(T).name();
116f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       uprv_strncpy(buffer, s, bufLen);
117f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       buffer[bufLen - 1] = 0;
118f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       return buffer;
119f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   }
120f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
121f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
122f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Two objects are equal if they are of the same type.
123f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
124f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual UBool operator == (const CacheKeyBase &other) const {
125f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       return typeid(*this) == typeid(other);
126f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   }
127f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius};
128f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
129f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius/**
130f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius * Cache key based on locale.
131f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius * A key of type LocaleCacheKey<T> maps to a value of type T.
132f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius */
133f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliustemplate<typename T>
134f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusclass LocaleCacheKey : public CacheKey<T> {
135f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius protected:
136f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   Locale   fLoc;
137f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius public:
138f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   LocaleCacheKey(const Locale &loc) : fLoc(loc) {};
139f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   LocaleCacheKey(const LocaleCacheKey<T> &other)
140f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           : CacheKey<T>(other), fLoc(other.fLoc) { }
141f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual ~LocaleCacheKey() { }
142f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual int32_t hashCode() const {
143f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       return 37 *CacheKey<T>::hashCode() + fLoc.hashCode();
144f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   }
145f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual UBool operator == (const CacheKeyBase &other) const {
146f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       // reflexive
147f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       if (this == &other) {
148f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           return TRUE;
149f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       }
150f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       if (!CacheKey<T>::operator == (other)) {
151f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           return FALSE;
152f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       }
153f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       // We know this and other are of same class because operator== on
154f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       // CacheKey returned true.
155f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       const LocaleCacheKey<T> *fOther =
156f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius               static_cast<const LocaleCacheKey<T> *>(&other);
157f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       return fLoc == fOther->fLoc;
158f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   }
159f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual CacheKeyBase *clone() const {
160f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       return new LocaleCacheKey<T>(*this);
161f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   }
162f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual const T *createObject(
163f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const void *creationContext, UErrorCode &status) const;
164f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
165f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Use the locale id as the description.
166f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
167f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual char *writeDescription(char *buffer, int32_t bufLen) const {
168f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       const char *s = fLoc.getName();
169f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       uprv_strncpy(buffer, s, bufLen);
170f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       buffer[bufLen - 1] = 0;
171f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       return buffer;
172f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   }
173f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
174f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius};
175f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
176f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius/**
177f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius * The unified cache. A singleton type.
178c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert * Design doc here:
179c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert * https://docs.google.com/document/d/1RwGQJs4N4tawNbf809iYDRCvXoMKqDJihxzYt1ysmd8/edit?usp=sharing
180f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius */
181c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubertclass U_COMMON_API UnifiedCache : public UnifiedCacheBase {
182f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius public:
183f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
184f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * @internal
185c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * Do not call directly. Instead use UnifiedCache::getInstance() as
186c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * there should be only one UnifiedCache in an application.
187f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
188f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   UnifiedCache(UErrorCode &status);
189f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
190f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
191f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Returns the cache instance.
192f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
193c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   static UnifiedCache *getInstance(UErrorCode &status);
194f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
195f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
196f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Fetches a value from the cache by key. Equivalent to
197f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * get(key, NULL, ptr, status);
198f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
199f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   template<typename T>
200f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   void get(
201f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const CacheKey<T>& key,
202f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const T *&ptr,
203f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           UErrorCode &status) const {
204f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       get(key, NULL, ptr, status);
205f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   }
206f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
207f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
208f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Fetches value from the cache by key.
209f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *
210f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * @param key             the cache key.
211f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * @param creationContext passed verbatim to createObject method of key
212f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * @param ptr             On entry, ptr must be NULL or be included if
213f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *                        the reference count of the object it points
214f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *                        to. On exit, ptr points to the fetched object
215f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *                        from the cache or is left unchanged on
216f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *                        failure. Caller must call removeRef on ptr
217f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *                        if set to a non NULL value.
218f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * @param status          Any error returned here. May be set to a
219f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *                        warning value even if ptr is set.
220f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
221f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   template<typename T>
222f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   void get(
223f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const CacheKey<T>& key,
224f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const void *creationContext,
225f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const T *&ptr,
226f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           UErrorCode &status) const {
227f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       if (U_FAILURE(status)) {
228f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           return;
229f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       }
230f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       UErrorCode creationStatus = U_ZERO_ERROR;
231f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       const SharedObject *value = NULL;
232f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       _get(key, value, creationContext, creationStatus);
233f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       const T *tvalue = (const T *) value;
234f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       if (U_SUCCESS(creationStatus)) {
235f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           SharedObject::copyPtr(tvalue, ptr);
236f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       }
237f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       SharedObject::clearPtr(tvalue);
238f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       // Take care not to overwrite a warning status passed in with
239f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       // another warning or U_ZERO_ERROR.
240f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       if (status == U_ZERO_ERROR || U_FAILURE(creationStatus)) {
241f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           status = creationStatus;
242f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       }
243f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   }
244f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
245f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#ifdef UNIFIED_CACHE_DEBUG
246f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
247f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Dumps the contents of this cache to standard error. Used for testing of
248f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * cache only.
249f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
250f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   void dumpContents() const;
251f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#endif
252f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
253f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
254f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Convenience method to get a value of type T from cache for a
255f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * particular locale with creationContext == NULL.
256f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * @param loc    the locale
257f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * @param ptr    On entry, must be NULL or included in the ref count
258f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *               of the object to which it points.
259f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *               On exit, fetched value stored here or is left
260f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *               unchanged on failure. Caller must call removeRef on
261f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *               ptr if set to a non NULL value.
262f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * @param status Any error returned here. May be set to a
263f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    *               warning value even if ptr is set.
264f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
265f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   template<typename T>
266f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   static void getByLocale(
267f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const Locale &loc, const T *&ptr, UErrorCode &status) {
268f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       const UnifiedCache *cache = getInstance(status);
269f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       if (U_FAILURE(status)) {
270f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           return;
271f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       }
272f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius       cache->get(LocaleCacheKey<T>(loc), ptr, status);
273f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   }
274f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
275f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#ifdef UNIFIED_CACHE_DEBUG
276f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
277f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Dumps the cache contents to stderr. For testing only.
278f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
279f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   static void dump();
280f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#endif
281f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
282f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
283f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Returns the number of keys in this cache. For testing only.
284f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
285f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   int32_t keyCount() const;
286f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
287f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   /**
288f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * Removes any values from cache that are not referenced outside
289f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    * the cache.
290f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    */
291f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   void flush() const;
292f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
293c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   /**
294c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * Configures at what point evcition of unused entries will begin.
295c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * Eviction is triggered whenever the number of unused entries exeeds
296c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * BOTH count AND (number of in-use items) * (percentageOfInUseItems / 100).
297c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * Once the number of unused entries drops below one of these,
298c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * eviction ceases. Because eviction happens incrementally,
299c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * the actual unused entry count may exceed both these numbers
300c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * from time to time.
301c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    *
302c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * A cache entry is defined as unused if it is not essential to guarantee
303c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * that for a given key X, the cache returns the same reference to the
304c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * same value as long as the client already holds a reference to that
305c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * value.
306c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    *
307c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * If this method is never called, the default settings are 1000 and 100%.
308c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    *
309c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * Although this method is thread-safe, it is designed to be called at
310c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * application startup. If it is called in the middle of execution, it
311c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * will have no immediate effect on the cache. However over time, the
312c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * cache will perform eviction slices in an attempt to honor the new
313c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * settings.
314c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    *
315c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * If a client already holds references to many different unique values
316c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * in the cache such that the number of those unique values far exeeds
317c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * "count" then the cache may not be able to maintain this maximum.
318c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * However, if this happens, the cache still guarantees that the number of
319c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * unused entries will remain only a small percentage of the total cache
320c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * size.
321c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    *
322c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * If the parameters passed are negative, setEvctionPolicy sets status to
323c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * U_ILLEGAL_ARGUMENT_ERROR.
324c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    */
325c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   void setEvictionPolicy(
326c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert           int32_t count, int32_t percentageOfInUseItems, UErrorCode &status);
327c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert
328c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert
329c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   /**
330c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * Returns how many entries have been auto evicted during the lifetime
331c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * of this cache. This only includes auto evicted entries, not
332c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * entries evicted because of a call to flush().
333c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    */
334c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   int64_t autoEvictedCount() const;
335c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert
336c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   /**
337c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * Returns the unused entry count in this cache. For testing only,
338c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    * Regular clients will not need this.
339c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert    */
340c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   int32_t unusedCount() const;
341c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert
342c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   virtual void incrementItemsInUse() const;
343c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   virtual void decrementItemsInUseWithLockingAndEviction() const;
344c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   virtual void decrementItemsInUse() const;
345f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   virtual ~UnifiedCache();
346f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius private:
347f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   UHashtable *fHashtable;
348c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   mutable int32_t fEvictPos;
349c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   mutable int32_t fItemsInUseCount;
350c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   int32_t fMaxUnused;
351c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   int32_t fMaxPercentageOfInUse;
352c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   mutable int64_t fAutoEvictedCount;
353f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   UnifiedCache(const UnifiedCache &other);
354f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   UnifiedCache &operator=(const UnifiedCache &other);
355f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   UBool _flush(UBool all) const;
356f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   void _get(
357f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const CacheKeyBase &key,
358f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const SharedObject *&value,
359f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const void *creationContext,
360f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           UErrorCode &status) const;
361f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   UBool _poll(
362f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const CacheKeyBase &key,
363f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const SharedObject *&value,
364f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           UErrorCode &status) const;
365f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   void _putNew(
366f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const CacheKeyBase &key,
367f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const SharedObject *value,
368f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const UErrorCode creationStatus,
369f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           UErrorCode &status) const;
370f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   void _putIfAbsentAndGet(
371f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const CacheKeyBase &key,
372f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const SharedObject *&value,
373f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           UErrorCode &status) const;
374c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   const UHashElement *_nextElement() const;
375c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   int32_t _computeCountOfItemsToEvict() const;
376c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   void _runEvictionSlice() const;
377c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   void _registerMaster(
378c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert        const CacheKeyBase *theKey, const SharedObject *value) const;
379c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   void _put(
380c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert           const UHashElement *element,
381c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert           const SharedObject *value,
382c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert           const UErrorCode status) const;
383f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#ifdef UNIFIED_CACHE_DEBUG
384f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   void _dumpContents() const;
385f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#endif
386c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   static void copyPtr(const SharedObject *src, const SharedObject *&dest);
387c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   static void clearPtr(const SharedObject *&ptr);
388f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   static void _fetch(
389f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const UHashElement *element,
390f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           const SharedObject *&value,
391f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius           UErrorCode &status);
392f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius   static UBool _inProgress(const UHashElement *element);
393c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   static UBool _inProgress(
394c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert           const SharedObject *theValue, UErrorCode creationStatus);
395c14898b482f76ecab9026615e2e4c6fe78358bdcFredrik Roubert   static UBool _isEvictable(const UHashElement *element);
396f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius};
397f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
398f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusU_NAMESPACE_END
399f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
400f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#endif
401