1 /* 2 * Copyright 2009-2013, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <package/hpkg/Strings.h> 8 9 10 namespace BPackageKit { 11 12 namespace BHPKG { 13 14 namespace BPrivate { 15 16 17 StringCache::StringCache() 18 { 19 } 20 21 22 StringCache::~StringCache() 23 { 24 CachedString* cachedString = Clear(true); 25 while (cachedString != NULL) { 26 CachedString* next = cachedString->next; 27 delete cachedString; 28 cachedString = next; 29 } 30 } 31 32 33 CachedString* 34 StringCache::Get(const char* value) 35 { 36 CachedString* string = Lookup(value); 37 if (string != NULL) { 38 string->usageCount++; 39 return string; 40 } 41 42 string = new CachedString; 43 if (!string->Init(value)) { 44 delete string; 45 throw std::bad_alloc(); 46 } 47 48 Insert(string); 49 return string; 50 } 51 52 53 void 54 StringCache::Put(CachedString* string) 55 { 56 if (string != NULL) { 57 if (--string->usageCount == 0) { 58 Remove(string); 59 delete string; 60 } 61 } 62 } 63 64 65 } // namespace BPrivate 66 67 } // namespace BHPKG 68 69 } // namespace BPackageKit 70