Added class that creates a vector of objects from a list.

git-svn-id: http://voip.null.ro/svn/yate@4881 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
paulc 2012-02-10 11:22:54 +00:00
parent d2f2865e17
commit 28c07865cb
2 changed files with 275 additions and 3 deletions

View File

@ -39,9 +39,7 @@ ObjList::ObjList()
ObjList::~ObjList()
{
#ifdef XDEBUG
Debugger debug("ObjList::~ObjList()"," [%p]",this);
#endif
XDebug(DebugAll,"ObjList::~ObjList() [%p]",this);
clear();
}
@ -265,4 +263,134 @@ void ObjList::clear()
TelEngine::destruct(n);
}
ObjVector::ObjVector(unsigned int maxLen, bool autodelete)
: m_length(maxLen), m_objects(0), m_delete(autodelete)
{
XDebug(DebugAll,"ObjVector::ObjVector(%u,%s) [%p]",
maxLen,String::boolText(autodelete),this);
if (maxLen) {
m_objects = new GenObject*[maxLen];
for (unsigned int i = 0; i < maxLen; i++)
m_objects[i] = 0;
}
}
ObjVector::ObjVector(ObjList& list, bool move, unsigned int maxLen, bool autodelete)
: m_length(0), m_objects(0), m_delete(autodelete)
{
XDebug(DebugAll,"ObjVector::ObjVector(%p,%s,%u,%s) [%p]",
&list,String::boolText(move),maxLen,String::boolText(autodelete),this);
assign(list,move,maxLen);
}
ObjVector::~ObjVector()
{
XDebug(DebugAll,"ObjVector::~ObjVector() [%p]",this);
clear();
}
void* ObjVector::getObject(const String& name) const
{
if (name == YSTRING("ObjVector"))
return const_cast<ObjVector*>(this);
return GenObject::getObject(name);
}
unsigned int ObjVector::assign(ObjList& list, bool move, unsigned int maxLen)
{
if (!maxLen)
maxLen = list.count();
clear();
if (maxLen) {
m_objects = new GenObject*[maxLen];
ObjList* l = list.skipNull();
for (unsigned int i = 0; i < maxLen; i++) {
if (l) {
if (move) {
m_objects[i] = l->remove(false);
l = l->skipNull();
}
else {
m_objects[i] = l->get();
l = l->skipNext();
}
}
else
m_objects[i] = 0;
}
m_length = maxLen;
}
return maxLen;
}
unsigned int ObjVector::count() const
{
if (!m_objects)
return 0;
unsigned int c = 0;
for (unsigned int i = 0; i < m_length; i++)
if (m_objects[i])
c++;
return c;
}
int ObjVector::index(const GenObject* obj) const
{
if (!m_objects)
return -1;
for (unsigned int i = 0; i < m_length; i++)
if (m_objects[i] == obj)
return i;
return -1;
}
int ObjVector::index(const String& str) const
{
if (!m_objects)
return -1;
for (unsigned int i = 0; i < m_length; i++)
if (m_objects[i] && str.matches(m_objects[i]->toString()))
return i;
return -1;
}
GenObject* ObjVector::take(unsigned int index)
{
if (index >= m_length || !m_objects)
return 0;
GenObject* ret = m_objects[index];
m_objects[index] = 0;
return ret;
}
bool ObjVector::set(GenObject* obj, unsigned int index)
{
if (index >= m_length || !m_objects)
return false;
GenObject* old = m_objects[index];
if (old == obj)
return true;
m_objects[index] = obj;
if (m_delete)
TelEngine::destruct(old);
return true;
}
void ObjVector::clear()
{
#ifdef XDEBUG
Debugger debug("ObjVector::clear()"," [%p]",this);
#endif
GenObject** objs = m_objects;
unsigned int len = m_length;
m_length = 0;
m_objects = 0;
if (m_delete && objs) {
for (unsigned int i = 0; i < len; i++)
TelEngine::destruct(objs[i]);
}
delete objs;
}
/* vi: set ts=8 sw=4 sts=4 noet: */

View File

@ -1248,6 +1248,150 @@ private:
bool m_delete;
};
/**
* Simple vector class that holds objects derived from GenObject
* @short A vector holding GenObjects
*/
class YATE_API ObjVector : public GenObject
{
YNOCOPY(ObjVector); // no automatic copies please
public:
/**
* Constructor of a zero capacity vector
* @param autodelete True to delete objects on destruct, false otherwise
*/
inline explicit ObjVector(bool autodelete = true)
: m_length(0), m_objects(0), m_delete(autodelete)
{ }
/**
* Constructor of an empty vector
* @param maxLen Maximum number of objects the vector can hold
* @param autodelete True to delete objects on destruct, false otherwise
*/
ObjVector(unsigned int maxLen, bool autodelete = true);
/**
* Constructor from an object list
* @param list List of objects to store in vector
* @param move True to move elements from list, false to just copy the pointer
* @param maxLen Maximum number of objects to put in vector, zero to put all
* @param autodelete True to delete objects on destruct, false otherwise
*/
ObjVector(ObjList& list, bool move = true, unsigned int maxLen = 0, bool autodelete = true);
/**
* Destroys the vector and the objects if automatic delete is set
*/
virtual ~ObjVector();
/**
* Get a pointer to a derived class given that class name
* @param name Name of the class we are asking for
* @return Pointer to the requested class or NULL if this object doesn't implement it
*/
virtual void* getObject(const String& name) const;
/**
* Get the capacity of the vector
* @return Number of items the vector can hold
*/
inline unsigned int length() const
{ return m_length; }
/**
* Get the number of non-null objects in the vector
* @return Count of items
*/
unsigned int count() const;
/**
* Get the object at a specific index in vector
* @param index Index of the object to retrieve
* @return Pointer to the object or NULL
*/
inline GenObject* at(int index) const
{ return (index >= 0 && index < (int)m_length) ? m_objects[index] : 0; }
/**
* Indexing operator with signed parameter
* @param index Index of the object to retrieve
* @return Pointer to the object or NULL
*/
inline GenObject* operator[](signed int index) const
{ return at(index); }
/**
* Indexing operator with unsigned parameter
* @param index Index of the object to retrieve
* @return Pointer to the object or NULL
*/
inline GenObject* operator[](unsigned int index) const
{ return at(index); }
/**
* Clear the vector and assign objects from a list
* @param list List of objects to store in vector
* @param move True to move elements from list, false to just copy the pointer
* @param maxLen Maximum number of objects to put in vector, zero to put all
* @return Capacity of the vector
*/
unsigned int assign(ObjList& list, bool move = true, unsigned int maxLen = 0);
/**
* Retrieve and remove an object from the vector
* @param index Index of the object to retrieve
* @return Pointer to the stored object, NULL for out of bound index
*/
GenObject* take(unsigned int index);
/**
* Store an object in the vector
* @param obj Object to store in vector
* @param index Index of the object to store
* @return True for success, false if index was out of bounds
*/
bool set(GenObject* obj, unsigned int index);
/**
* Get the position in vector of a GenObject by a pointer to it
* @param obj Pointer to the object to search for
* @return Index of object in vector, -1 if not found
*/
int index(const GenObject* obj) const;
/**
* Get the position in vector of the first GenObject with a given value
* @param str String value (toString) of the object to search for
* @return Index of object in vector, -1 if not found
*/
int index(const String& str) const;
/**
* Clear the vector and optionally delete all contained objects
*/
void clear();
/**
* Get the automatic delete flag
* @return True if will delete objects on destruct, false otherwise
*/
inline bool autoDelete()
{ return m_delete; }
/**
* Set the automatic delete flag
* @param autodelete True to delete objects on destruct, false otherwise
*/
inline void setDelete(bool autodelete)
{ m_delete = autodelete; }
private:
unsigned int m_length;
GenObject** m_objects;
bool m_delete;
};
/**
* A simple Array class derivated from RefObject
* It uses one ObjList to keep the pointers to other ObjList's.