michael@0: /* michael@0: ****************************************************************************** michael@0: * Copyright (C) 1997-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ****************************************************************************** michael@0: * file name: nfrlist.h michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * Modification history michael@0: * Date Name Comments michael@0: * 10/11/2001 Doug Ported from ICU4J michael@0: */ michael@0: michael@0: #ifndef NFRLIST_H michael@0: #define NFRLIST_H michael@0: michael@0: #include "unicode/rbnf.h" michael@0: michael@0: #if U_HAVE_RBNF michael@0: michael@0: #include "unicode/uobject.h" michael@0: #include "nfrule.h" michael@0: michael@0: #include "cmemory.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: // unsafe class for internal use only. assume memory allocations succeed, indexes are valid. michael@0: // should be a template, but we can't use them michael@0: michael@0: class NFRuleList : public UMemory { michael@0: protected: michael@0: NFRule** fStuff; michael@0: uint32_t fCount; michael@0: uint32_t fCapacity; michael@0: public: michael@0: NFRuleList(uint32_t capacity = 10) michael@0: : fStuff(capacity ? (NFRule**)uprv_malloc(capacity * sizeof(NFRule*)) : NULL) michael@0: , fCount(0) michael@0: , fCapacity(capacity) {} michael@0: ~NFRuleList() { michael@0: if (fStuff) { michael@0: for(uint32_t i = 0; i < fCount; ++i) { michael@0: delete fStuff[i]; michael@0: } michael@0: uprv_free(fStuff); michael@0: } michael@0: } michael@0: NFRule* operator[](uint32_t index) const { return fStuff != NULL ? fStuff[index] : NULL; } michael@0: NFRule* remove(uint32_t index) { michael@0: if (fStuff == NULL) { michael@0: return NULL; michael@0: } michael@0: NFRule* result = fStuff[index]; michael@0: fCount -= 1; michael@0: for (uint32_t i = index; i < fCount; ++i) { // assumes small arrays michael@0: fStuff[i] = fStuff[i+1]; michael@0: } michael@0: return result; michael@0: } michael@0: void add(NFRule* thing) { michael@0: if (fCount == fCapacity) { michael@0: fCapacity += 10; michael@0: fStuff = (NFRule**)uprv_realloc(fStuff, fCapacity * sizeof(NFRule*)); // assume success michael@0: } michael@0: if (fStuff != NULL) { michael@0: fStuff[fCount++] = thing; michael@0: } else { michael@0: fCapacity = 0; michael@0: fCount = 0; michael@0: } michael@0: } michael@0: uint32_t size() const { return fCount; } michael@0: NFRule* last() const { return (fCount > 0 && fStuff != NULL) ? fStuff[fCount-1] : NULL; } michael@0: NFRule** release() { michael@0: add(NULL); // ensure null termination michael@0: NFRule** result = fStuff; michael@0: fStuff = NULL; michael@0: fCount = 0; michael@0: fCapacity = 0; michael@0: return result; michael@0: } michael@0: void deleteAll() { michael@0: NFRule** tmp = NULL; michael@0: int32_t size = fCount; michael@0: if (size > 0) { michael@0: tmp = release(); michael@0: for (int32_t i = 0; i < size; i++) { michael@0: delete tmp[i]; michael@0: } michael@0: if (tmp) { michael@0: uprv_free(tmp); michael@0: } michael@0: } michael@0: } michael@0: michael@0: private: michael@0: NFRuleList(const NFRuleList &other); // forbid copying of this class michael@0: NFRuleList &operator=(const NFRuleList &other); // forbid copying of this class michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: /* U_HAVE_RBNF */ michael@0: #endif michael@0: michael@0: // NFRLIST_H michael@0: #endif