Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | ****************************************************************************** |
michael@0 | 3 | * Copyright (C) 1997-2012, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ****************************************************************************** |
michael@0 | 6 | * file name: nfrlist.h |
michael@0 | 7 | * encoding: US-ASCII |
michael@0 | 8 | * tab size: 8 (not used) |
michael@0 | 9 | * indentation:4 |
michael@0 | 10 | * |
michael@0 | 11 | * Modification history |
michael@0 | 12 | * Date Name Comments |
michael@0 | 13 | * 10/11/2001 Doug Ported from ICU4J |
michael@0 | 14 | */ |
michael@0 | 15 | |
michael@0 | 16 | #ifndef NFRLIST_H |
michael@0 | 17 | #define NFRLIST_H |
michael@0 | 18 | |
michael@0 | 19 | #include "unicode/rbnf.h" |
michael@0 | 20 | |
michael@0 | 21 | #if U_HAVE_RBNF |
michael@0 | 22 | |
michael@0 | 23 | #include "unicode/uobject.h" |
michael@0 | 24 | #include "nfrule.h" |
michael@0 | 25 | |
michael@0 | 26 | #include "cmemory.h" |
michael@0 | 27 | |
michael@0 | 28 | U_NAMESPACE_BEGIN |
michael@0 | 29 | |
michael@0 | 30 | // unsafe class for internal use only. assume memory allocations succeed, indexes are valid. |
michael@0 | 31 | // should be a template, but we can't use them |
michael@0 | 32 | |
michael@0 | 33 | class NFRuleList : public UMemory { |
michael@0 | 34 | protected: |
michael@0 | 35 | NFRule** fStuff; |
michael@0 | 36 | uint32_t fCount; |
michael@0 | 37 | uint32_t fCapacity; |
michael@0 | 38 | public: |
michael@0 | 39 | NFRuleList(uint32_t capacity = 10) |
michael@0 | 40 | : fStuff(capacity ? (NFRule**)uprv_malloc(capacity * sizeof(NFRule*)) : NULL) |
michael@0 | 41 | , fCount(0) |
michael@0 | 42 | , fCapacity(capacity) {} |
michael@0 | 43 | ~NFRuleList() { |
michael@0 | 44 | if (fStuff) { |
michael@0 | 45 | for(uint32_t i = 0; i < fCount; ++i) { |
michael@0 | 46 | delete fStuff[i]; |
michael@0 | 47 | } |
michael@0 | 48 | uprv_free(fStuff); |
michael@0 | 49 | } |
michael@0 | 50 | } |
michael@0 | 51 | NFRule* operator[](uint32_t index) const { return fStuff != NULL ? fStuff[index] : NULL; } |
michael@0 | 52 | NFRule* remove(uint32_t index) { |
michael@0 | 53 | if (fStuff == NULL) { |
michael@0 | 54 | return NULL; |
michael@0 | 55 | } |
michael@0 | 56 | NFRule* result = fStuff[index]; |
michael@0 | 57 | fCount -= 1; |
michael@0 | 58 | for (uint32_t i = index; i < fCount; ++i) { // assumes small arrays |
michael@0 | 59 | fStuff[i] = fStuff[i+1]; |
michael@0 | 60 | } |
michael@0 | 61 | return result; |
michael@0 | 62 | } |
michael@0 | 63 | void add(NFRule* thing) { |
michael@0 | 64 | if (fCount == fCapacity) { |
michael@0 | 65 | fCapacity += 10; |
michael@0 | 66 | fStuff = (NFRule**)uprv_realloc(fStuff, fCapacity * sizeof(NFRule*)); // assume success |
michael@0 | 67 | } |
michael@0 | 68 | if (fStuff != NULL) { |
michael@0 | 69 | fStuff[fCount++] = thing; |
michael@0 | 70 | } else { |
michael@0 | 71 | fCapacity = 0; |
michael@0 | 72 | fCount = 0; |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | uint32_t size() const { return fCount; } |
michael@0 | 76 | NFRule* last() const { return (fCount > 0 && fStuff != NULL) ? fStuff[fCount-1] : NULL; } |
michael@0 | 77 | NFRule** release() { |
michael@0 | 78 | add(NULL); // ensure null termination |
michael@0 | 79 | NFRule** result = fStuff; |
michael@0 | 80 | fStuff = NULL; |
michael@0 | 81 | fCount = 0; |
michael@0 | 82 | fCapacity = 0; |
michael@0 | 83 | return result; |
michael@0 | 84 | } |
michael@0 | 85 | void deleteAll() { |
michael@0 | 86 | NFRule** tmp = NULL; |
michael@0 | 87 | int32_t size = fCount; |
michael@0 | 88 | if (size > 0) { |
michael@0 | 89 | tmp = release(); |
michael@0 | 90 | for (int32_t i = 0; i < size; i++) { |
michael@0 | 91 | delete tmp[i]; |
michael@0 | 92 | } |
michael@0 | 93 | if (tmp) { |
michael@0 | 94 | uprv_free(tmp); |
michael@0 | 95 | } |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | private: |
michael@0 | 100 | NFRuleList(const NFRuleList &other); // forbid copying of this class |
michael@0 | 101 | NFRuleList &operator=(const NFRuleList &other); // forbid copying of this class |
michael@0 | 102 | }; |
michael@0 | 103 | |
michael@0 | 104 | U_NAMESPACE_END |
michael@0 | 105 | |
michael@0 | 106 | /* U_HAVE_RBNF */ |
michael@0 | 107 | #endif |
michael@0 | 108 | |
michael@0 | 109 | // NFRLIST_H |
michael@0 | 110 | #endif |