2004-02-28 20:44:04 +01:00
|
|
|
/*
|
|
|
|
* PROGRAM: Common class definition
|
|
|
|
* MODULE: object_array.h
|
|
|
|
* DESCRIPTION: half-static array of any objects,
|
|
|
|
* having MemoryPool'ed constructor.
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Initial
|
|
|
|
* Developer's Public License Version 1.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed AS IS,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing rights
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Alexander Peshkoff
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004 Alexander Peshkoff <peshkoff@mail.ru>
|
|
|
|
* and all contributors signed below.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FB_LIST_H
|
|
|
|
#define FB_LIST_H
|
|
|
|
|
|
|
|
#include "../common/classes/alloc.h"
|
|
|
|
#include "../common/classes/array.h"
|
|
|
|
|
|
|
|
namespace Firebird
|
|
|
|
{
|
|
|
|
template <typename T, int InlineCapacity = 8>
|
|
|
|
class ObjectsArray : private HalfStaticArray<T*, InlineCapacity>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
typedef HalfStaticArray<T*, InlineCapacity> inherited;
|
|
|
|
public:
|
|
|
|
class iterator {
|
|
|
|
friend class ObjectsArray<T, InlineCapacity>;
|
|
|
|
private:
|
|
|
|
ObjectsArray *lst;
|
|
|
|
int pos;
|
2004-02-29 06:49:03 +01:00
|
|
|
iterator(ObjectsArray *l, int p) : lst(l), pos(p){}
|
2004-02-28 20:44:04 +01:00
|
|
|
public:
|
|
|
|
iterator& operator++() {++pos; return (*this);}
|
|
|
|
iterator operator++(int) {iterator tmp = *this; ++pos;
|
|
|
|
return tmp;}
|
|
|
|
iterator& operator--() {--pos; return (*this);}
|
|
|
|
iterator operator--(int) {iterator tmp = *this; --pos;
|
|
|
|
return tmp;}
|
|
|
|
T* operator->() {
|
|
|
|
T* pointer = lst->getPointer(pos);
|
|
|
|
return pointer;
|
|
|
|
}
|
|
|
|
T& operator*() {
|
|
|
|
T* pointer = lst->getPointer(pos);
|
|
|
|
return *pointer;
|
|
|
|
}
|
2004-02-29 06:49:03 +01:00
|
|
|
bool operator!=(const iterator& v) const {return pos != v.pos;}
|
2004-02-28 20:44:04 +01:00
|
|
|
};
|
|
|
|
public:
|
|
|
|
void insert(int index, const T& item) {
|
|
|
|
T* data = FB_NEW(*pool) T(pool, item);
|
|
|
|
inherited::insert(index, data);
|
|
|
|
}
|
|
|
|
int add(const T& item) {
|
|
|
|
T* data = FB_NEW(*pool) T(pool, item);
|
|
|
|
return inherited::add(data);
|
|
|
|
};
|
|
|
|
void push(const T& item) {
|
|
|
|
add(item);
|
|
|
|
}
|
|
|
|
T pop() {
|
|
|
|
T* pntr = inherited::pop();
|
|
|
|
T rc = *pntr;
|
|
|
|
delete pntr;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
void remove(int index) {
|
|
|
|
delete getPointer(index);
|
|
|
|
inherited::remove(index);
|
|
|
|
}
|
|
|
|
void remove(iterator itr) {
|
|
|
|
fb_assert(itr.lst == this);
|
|
|
|
remove(itr.pos);
|
|
|
|
}
|
|
|
|
void shrink(int newCount) {
|
|
|
|
for (int i = newCount; i < getCount(); i++) {
|
|
|
|
delete getPointer(i);
|
|
|
|
}
|
|
|
|
inherited::shrink(newCount);
|
|
|
|
};
|
|
|
|
iterator begin() {
|
|
|
|
return iterator(this, 0);
|
|
|
|
}
|
|
|
|
iterator end() {
|
|
|
|
return iterator(this, getCount());
|
|
|
|
}
|
|
|
|
const T& operator[](int index) const {
|
|
|
|
return *getPointer(index);
|
|
|
|
}
|
|
|
|
const T* getPointer(int index) const {
|
|
|
|
return inherited::getElement(index);
|
|
|
|
}
|
|
|
|
T& operator[](int index) {
|
|
|
|
return *getPointer(index);
|
|
|
|
}
|
|
|
|
T* getPointer(int index) {
|
|
|
|
return inherited::getElement(index);
|
|
|
|
}
|
|
|
|
inline ObjectsArray(MemoryPool* p) :
|
|
|
|
HalfStaticArray<T*, InlineCapacity>(p) {}
|
|
|
|
~ObjectsArray() {
|
2004-03-07 08:58:55 +01:00
|
|
|
for (int i = 0; i < getCount(); i++) {
|
2004-02-28 20:44:04 +01:00
|
|
|
delete getPointer(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int getCount() const {return inherited::getCount();}
|
|
|
|
int getCapacity() const {return inherited::getCapacity();}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // FB_LIST_H
|