Return a pointer to the object identified by id. If we haven't yet loaded the object, use loader to load it. If loader fails to load it, record a NULL entry in the cache and return NULL -- further attempts to load will fail (even with a different loader) until DeleteUnusedObjects() is called. We delete the given loader.
| 58 | // with a different loader) until DeleteUnusedObjects() is called. |
| 59 | // We delete the given loader. |
| 60 | T *Get(STRING id, |
| 61 | TessResultCallback<T *> *loader) { |
| 62 | T *retval = NULL; |
| 63 | mu_.Lock(); |
| 64 | for (int i = 0; i < cache_.size(); i++) { |
| 65 | if (id == cache_[i].id) { |
| 66 | retval = cache_[i].object; |
| 67 | if (cache_[i].object != NULL) { |
| 68 | cache_[i].count++; |
| 69 | } |
| 70 | mu_.Unlock(); |
| 71 | delete loader; |
| 72 | return retval; |
| 73 | } |
| 74 | } |
| 75 | cache_.push_back(ReferenceCount()); |
| 76 | ReferenceCount &rc = cache_.back(); |
| 77 | rc.id = id; |
| 78 | retval = rc.object = loader->Run(); |
| 79 | rc.count = (retval != NULL) ? 1 : 0; |
| 80 | mu_.Unlock(); |
| 81 | return retval; |
| 82 | } |
| 83 | |
| 84 | // Decrement the count for t. |
| 85 | // Return whether we knew about the given pointer. |