Make BObjectList<> publically available:

* cleaned up ObjectList.h
* switched several uses of new() to new(std::nothrow)
* moved ugly AsBList() hack into BObjectList<>::Private class and
  adjusted all callers accordingly


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40252 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Oliver Tappe
2011-01-20 12:09:16 +00:00
parent 260f2d0994
commit 915a7b8c24
10 changed files with 373 additions and 257 deletions
@@ -31,45 +31,24 @@ of Be Incorporated in the United States and other countries. Other brand product
names are registered trademarks or trademarks of their respective holders. names are registered trademarks or trademarks of their respective holders.
All rights reserved. All rights reserved.
*/ */
#ifndef _OBJECT_LIST_H
#define _OBJECT_LIST_H
#include <new>
#include <List.h>
#include <SupportDefs.h>
/****************************************************************************
** WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING **
** **
** DANGER, WILL ROBINSON! **
** **
** The interfaces contained here are part of BeOS's **
** **
** >> PRIVATE NOT FOR PUBLIC USE << **
** **
** implementation. **
** **
** These interfaces WILL CHANGE in future releases. **
** If you use them, your app WILL BREAK at some future time. **
** **
** (And yes, this does mean that binaries built from OpenTracker will not **
** be compatible with some future releases of the OS. When that happens, **
** we will provide an updated version of this file to keep compatibility.) **
** **
** WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING **
****************************************************************************/
// //
// ObjectList is a wrapper around BList that adds type safety, // ObjectList is a wrapper around BList that adds type safety,
// optional object ownership, search, insert operations, etc. // optional object ownership, search, insert operations, etc.
// //
#ifndef __OBJECT_LIST__
#define __OBJECT_LIST__
#ifndef _BE_H
#include <List.h>
#endif
#include <SupportDefs.h>
template<class T> class BObjectList; template<class T> class BObjectList;
template<class T> template<class T>
struct UnaryPredicate { struct UnaryPredicate {
@@ -84,6 +63,7 @@ private:
friend class BObjectList<T>; friend class BObjectList<T>;
}; };
template<class T> template<class T>
int int
UnaryPredicate<T>::_unary_predicate_glue(const void *item, void *context) UnaryPredicate<T>::_unary_predicate_glue(const void *item, void *context)
@@ -111,10 +91,12 @@ public:
void HSortItems(GenericCompareFunctionWithState, void *state); void HSortItems(GenericCompareFunctionWithState, void *state);
void *BinarySearch(const void *, GenericCompareFunction) const; void *BinarySearch(const void *, GenericCompareFunction) const;
void *BinarySearch(const void *, GenericCompareFunctionWithState, void *state) const; void *BinarySearch(const void *, GenericCompareFunctionWithState,
void *state) const;
int32 BinarySearchIndex(const void *, GenericCompareFunction) const; int32 BinarySearchIndex(const void *, GenericCompareFunction) const;
int32 BinarySearchIndex(const void *, GenericCompareFunctionWithState, void *state) const; int32 BinarySearchIndex(const void *, GenericCompareFunctionWithState,
void *state) const;
int32 BinarySearchIndexByPredicate(const void *, UnaryPredicateGlue) const; int32 BinarySearchIndexByPredicate(const void *, UnaryPredicateGlue) const;
bool Owning() const; bool Owning() const;
@@ -125,6 +107,7 @@ protected:
}; };
template<class T> template<class T>
class BObjectList : private _PointerList_ { class BObjectList : private _PointerList_ {
public: public:
@@ -133,22 +116,22 @@ public:
typedef T* (*EachFunction)(T*, void*); typedef T* (*EachFunction)(T*, void*);
typedef const T* (*ConstEachFunction)(const T*, void*); typedef const T* (*ConstEachFunction)(const T*, void*);
typedef int (*CompareFunction)(const T*, const T*); typedef int (*CompareFunction)(const T*, const T*);
typedef int (* CompareFunctionWithState)(const T *, const T *, void *state); typedef int (*CompareFunctionWithState)(const T*, const T*,
void* state);
BObjectList(int32 itemsPerBlock = 20, bool owning = false); BObjectList(int32 itemsPerBlock = 20,
bool owning = false);
BObjectList(const BObjectList& list); BObjectList(const BObjectList& list);
// clones list; if list is owning, makes copies of all // clones list; if list is owning, makes
// the items // copies of all the items
virtual ~BObjectList(); virtual ~BObjectList();
BObjectList& operator=(const BObjectList& list); BObjectList& operator=(const BObjectList& list);
// clones list; if list is owning, makes copies of all // clones list; if list is owning, makes
// the items // copies of all the items
// adding and removing // adding and removing
// ToDo:
// change Add calls to return const item
bool AddItem(T*); bool AddItem(T*);
bool AddItem(T*, int32); bool AddItem(T*, int32);
bool AddList(BObjectList*); bool AddList(BObjectList*);
@@ -165,10 +148,12 @@ public:
T* ItemAt(int32) const; T* ItemAt(int32) const;
bool ReplaceItem(int32 index, T*); bool ReplaceItem(int32 index, T*);
// if list is owning, deletes the item at <index> first // if list is owning, deletes the item
// at <index> first
T* SwapWithItem(int32 index, T* newItem); T* SwapWithItem(int32 index, T* newItem);
// same as ReplaceItem, except does not delete old item at <index>, // same as ReplaceItem, except does not
// returns it instead // delete old item at <index>, returns it
// instead
T* FirstItem() const; T* FirstItem() const;
T* LastItem() const; T* LastItem() const;
@@ -183,97 +168,123 @@ public:
const T* EachElement(ConstEachFunction, void*) const; const T* EachElement(ConstEachFunction, void*) const;
void SortItems(CompareFunction); void SortItems(CompareFunction);
void SortItems(CompareFunctionWithState, void *state); void SortItems(CompareFunctionWithState,
void* state);
void HSortItems(CompareFunction); void HSortItems(CompareFunction);
void HSortItems(CompareFunctionWithState, void *state); void HSortItems(CompareFunctionWithState,
void* state);
// linear search, returns first item that matches predicate // linear search, returns first item that
// matches predicate
const T* FindIf(const UnaryPredicate<T>&) const; const T* FindIf(const UnaryPredicate<T>&) const;
T* FindIf(const UnaryPredicate<T>&); T* FindIf(const UnaryPredicate<T>&);
// list must be sorted with CompareFunction for these to work // list must be sorted with CompareFunction for
// these to work
T* BinarySearch(const T&, CompareFunction) const; T* BinarySearch(const T&, CompareFunction) const;
T *BinarySearch(const T &, CompareFunctionWithState, void *state) const; T* BinarySearch(const T&, CompareFunctionWithState,
void* state) const;
template<typename Key>
T *BinarySearchByKey(const Key &key, int (*compare)(const Key *, const T *))
const;
template<typename Key> template<typename Key>
T* BinarySearchByKey(const Key& key, T* BinarySearchByKey(const Key& key,
int (*compare)(const Key *, const T *, void *), void *state) const; int (*compare)(const Key*, const T*)) const;
int32 BinarySearchIndex(const T &item, CompareFunction compare) const; template<typename Key>
int32 BinarySearchIndex(const T &item, CompareFunctionWithState compare, T* BinarySearchByKey(const Key& key,
int (*compare)(const Key*, const T*, void*),
void* state) const;
int32 BinarySearchIndex(const T&item,
CompareFunction compare) const;
int32 BinarySearchIndex(const T&item,
CompareFunctionWithState compare,
void* state) const; void* state) const;
template<typename Key> template<typename Key>
int32 BinarySearchIndexByKey(const Key& key, int32 BinarySearchIndexByKey(const Key& key,
int (*compare)(const Key*, const T*)) const; int (*compare)(const Key*, const T*)) const;
// Binary insertion - list must be sorted with CompareFunction for // Binary insertion - list must be sorted with
// these to work // CompareFunction for these to work
// simple insert // simple insert
bool BinaryInsert(T*, CompareFunction); bool BinaryInsert(T*, CompareFunction);
bool BinaryInsert(T *, CompareFunctionWithState, void *state); bool BinaryInsert(T*, CompareFunctionWithState,
void* state);
bool BinaryInsert(T*, const UnaryPredicate<T>&); bool BinaryInsert(T*, const UnaryPredicate<T>&);
// unique insert, returns false if item already in list // unique insert, returns false if item already
// in list
bool BinaryInsertUnique(T*, CompareFunction); bool BinaryInsertUnique(T*, CompareFunction);
bool BinaryInsertUnique(T *, CompareFunctionWithState, void *state); bool BinaryInsertUnique(T*, CompareFunctionWithState,
bool BinaryInsertUnique(T *, const UnaryPredicate<T> &); void* state);
bool BinaryInsertUnique(T*,
const UnaryPredicate<T>&);
// insert a copy of the item, returns new inserted item // insert a copy of the item, returns new
T *BinaryInsertCopy(const T &copyThis, CompareFunction); // inserted item
T *BinaryInsertCopy(const T &copyThis, CompareFunctionWithState, void *state); T* BinaryInsertCopy(const T& copyThis,
CompareFunction);
T* BinaryInsertCopy(const T& copyThis,
CompareFunctionWithState, void* state);
// insert a copy of the item if not in list already // insert a copy of the item if not in list
// returns new inserted item or existing item in case of a conflict // already returns new inserted item or existing
T *BinaryInsertCopyUnique(const T &copyThis, CompareFunction); // item in case of a conflict
T *BinaryInsertCopyUnique(const T &copyThis, CompareFunctionWithState, void *state); T* BinaryInsertCopyUnique(const T& copyThis,
CompareFunction);
T* BinaryInsertCopyUnique(const T& copyThis,
CompareFunctionWithState, void* state);
int32 FindBinaryInsertionIndex(const UnaryPredicate<T> &, bool *alreadyInList = 0) const; int32 FindBinaryInsertionIndex(
// returns either the index into which a new item should be inserted const UnaryPredicate<T>&,
// or index of an existing item that matches the predicate bool* alreadyInList = 0) const;
// returns either the index into which a new
// item should be inserted or index of an
// existing item that matches the predicate
// deprecated API, will go away class Private;
BList *AsBList()
{ return this; }
const BList *AsBList() const
{ return this; }
private: private:
void SetItem(int32, T *); friend class Private;
void _SetItem(int32, T*);
}; };
template<class Item, class Result, class Param1> template<class Item, class Result, class Param1>
Result Result
WhileEachListItem(BObjectList<Item> *list, Result (Item::*func)(Param1), Param1 p1) WhileEachListItem(BObjectList<Item>* list, Result (Item::*func)(Param1),
Param1 p1)
{ {
Result result = 0; Result result = 0;
int32 count = list->CountItems(); int32 count = list->CountItems();
for (int32 index = 0; index < count; index++) for (int32 index = 0; index < count; index++) {
if ((result = (list->ItemAt(index)->*func)(p1)) != 0) if ((result = (list->ItemAt(index)->*func)(p1)) != 0)
break; break;
}
return result; return result;
} }
template<class Item, class Result, class Param1> template<class Item, class Result, class Param1>
Result Result
WhileEachListItem(BObjectList<Item> *list, Result (*func)(Item *, Param1), Param1 p1) WhileEachListItem(BObjectList<Item>* list, Result (*func)(Item*, Param1),
Param1 p1)
{ {
Result result = 0; Result result = 0;
int32 count = list->CountItems(); int32 count = list->CountItems();
for (int32 index = 0; index < count; index++) for (int32 index = 0; index < count; index++) {
if ((result = (*func)(list->ItemAt(index), p1)) != 0) if ((result = (*func)(list->ItemAt(index), p1)) != 0)
break; break;
}
return result; return result;
} }
template<class Item, class Result, class Param1, class Param2> template<class Item, class Result, class Param1, class Param2>
Result Result
WhileEachListItem(BObjectList<Item>* list, Result (Item::*func)(Param1, Param2), WhileEachListItem(BObjectList<Item>* list, Result (Item::*func)(Param1, Param2),
@@ -282,43 +293,51 @@ WhileEachListItem(BObjectList<Item> *list, Result (Item::*func)(Param1, Param2),
Result result = 0; Result result = 0;
int32 count = list->CountItems(); int32 count = list->CountItems();
for (int32 index = 0; index < count; index++) for (int32 index = 0; index < count; index++) {
if ((result = (list->ItemAt(index)->*func)(p1, p2)) != 0) if ((result = (list->ItemAt(index)->*func)(p1, p2)) != 0)
break; break;
}
return result; return result;
} }
template<class Item, class Result, class Param1, class Param2> template<class Item, class Result, class Param1, class Param2>
Result Result
WhileEachListItem(BObjectList<Item> *list, Result (*func)(Item *, Param1, Param2), WhileEachListItem(BObjectList<Item>* list,
Param1 p1, Param2 p2) Result (*func)(Item*, Param1, Param2), Param1 p1, Param2 p2)
{ {
Result result = 0; Result result = 0;
int32 count = list->CountItems(); int32 count = list->CountItems();
for (int32 index = 0; index < count; index++) for (int32 index = 0; index < count; index++) {
if ((result = (*func)(list->ItemAt(index), p1, p2)) != 0) if ((result = (*func)(list->ItemAt(index), p1, p2)) != 0)
break; break;
}
return result; return result;
} }
template<class Item, class Result, class Param1, class Param2, class Param3, class Param4>
template<class Item, class Result, class Param1, class Param2, class Param3,
class Param4>
Result Result
WhileEachListItem(BObjectList<Item> *list, Result (*func)(Item *, Param1, Param2, WhileEachListItem(BObjectList<Item>* list,
Param3, Param4), Param1 p1, Param2 p2, Param3 p3, Param4 p4) Result (*func)(Item*, Param1, Param2, Param3, Param4), Param1 p1, Param2 p2,
Param3 p3, Param4 p4)
{ {
Result result = 0; Result result = 0;
int32 count = list->CountItems(); int32 count = list->CountItems();
for (int32 index = 0; index < count; index++) for (int32 index = 0; index < count; index++) {
if ((result = (*func)(list->ItemAt(index), p1, p2, p3, p4)) != 0) if ((result = (*func)(list->ItemAt(index), p1, p2, p3, p4)) != 0)
break; break;
}
return result; return result;
} }
template<class Item, class Result> template<class Item, class Result>
void void
EachListItemIgnoreResult(BObjectList<Item>* list, Result (Item::*func)()) EachListItemIgnoreResult(BObjectList<Item>* list, Result (Item::*func)())
@@ -328,6 +347,7 @@ EachListItemIgnoreResult(BObjectList<Item> *list, Result (Item::*func)())
(list->ItemAt(index)->*func)(); (list->ItemAt(index)->*func)();
} }
template<class Item, class Param1> template<class Item, class Param1>
void void
EachListItem(BObjectList<Item>* list, void (*func)(Item*, Param1), Param1 p1) EachListItem(BObjectList<Item>* list, void (*func)(Item*, Param1), Param1 p1)
@@ -337,6 +357,7 @@ EachListItem(BObjectList<Item> *list, void (*func)(Item *, Param1), Param1 p1)
(func)(list->ItemAt(index), p1); (func)(list->ItemAt(index), p1);
} }
template<class Item, class Param1, class Param2> template<class Item, class Param1, class Param2>
void void
EachListItem(BObjectList<Item>* list, void (Item::*func)(Param1, Param2), EachListItem(BObjectList<Item>* list, void (Item::*func)(Param1, Param2),
@@ -347,6 +368,7 @@ EachListItem(BObjectList<Item> *list, void (Item::*func)(Param1, Param2),
(list->ItemAt(index)->*func)(p1, p2); (list->ItemAt(index)->*func)(p1, p2);
} }
template<class Item, class Param1, class Param2> template<class Item, class Param1, class Param2>
void void
EachListItem(BObjectList<Item>* list, void (*func)(Item*,Param1, Param2), EachListItem(BObjectList<Item>* list, void (*func)(Item*,Param1, Param2),
@@ -357,10 +379,11 @@ EachListItem(BObjectList<Item> *list, void (*func)(Item *,Param1, Param2),
(func)(list->ItemAt(index), p1, p2); (func)(list->ItemAt(index), p1, p2);
} }
template<class Item, class Param1, class Param2, class Param3> template<class Item, class Param1, class Param2, class Param3>
void void
EachListItem(BObjectList<Item> *list, void (*func)(Item *,Param1, Param2, EachListItem(BObjectList<Item>* list,
Param3), Param1 p1, Param2 p2, Param3 p3) void (*func)(Item*,Param1, Param2, Param3), Param1 p1, Param2 p2, Param3 p3)
{ {
int32 count = list->CountItems(); int32 count = list->CountItems();
for (int32 index = 0; index < count; index++) for (int32 index = 0; index < count; index++)
@@ -370,31 +393,38 @@ EachListItem(BObjectList<Item> *list, void (*func)(Item *,Param1, Param2,
template<class Item, class Param1, class Param2, class Param3, class Param4> template<class Item, class Param1, class Param2, class Param3, class Param4>
void void
EachListItem(BObjectList<Item> *list, void (*func)(Item *,Param1, Param2, EachListItem(BObjectList<Item>* list,
Param3, Param4), Param1 p1, Param2 p2, Param3 p3, Param4 p4) void (*func)(Item*,Param1, Param2, Param3, Param4), Param1 p1, Param2 p2,
Param3 p3, Param4 p4)
{ {
int32 count = list->CountItems(); int32 count = list->CountItems();
for (int32 index = 0; index < count; index++) for (int32 index = 0; index < count; index++)
(func)(list->ItemAt(index), p1, p2, p3, p4); (func)(list->ItemAt(index), p1, p2, p3, p4);
} }
// inline code // inline code
inline bool inline bool
_PointerList_::Owning() const _PointerList_::Owning() const
{ {
return owning; return owning;
} }
template<class T> template<class T>
BObjectList<T>::BObjectList(int32 itemsPerBlock, bool owning) BObjectList<T>::BObjectList(int32 itemsPerBlock, bool owning)
: _PointerList_(itemsPerBlock, owning) :
_PointerList_(itemsPerBlock, owning)
{ {
} }
template<class T> template<class T>
BObjectList<T>::BObjectList(const BObjectList<T> &list) BObjectList<T>::BObjectList(const BObjectList<T> &list)
: _PointerList_(list) :
_PointerList_(list)
{ {
owning = list.owning; owning = list.owning;
if (owning) { if (owning) {
@@ -403,19 +433,22 @@ BObjectList<T>::BObjectList(const BObjectList<T> &list)
for (int32 index = 0; index < count; index++) { for (int32 index = 0; index < count; index++) {
T* item = list.ItemAt(index); T* item = list.ItemAt(index);
if (item) if (item)
item = new T(*item); item = new (std::nothrow) T(*item);
SetItem(index, item); _SetItem(index, item);
} }
} }
} }
template<class T> template<class T>
BObjectList<T>::~BObjectList() BObjectList<T>::~BObjectList()
{ {
if (Owning()) if (Owning()) {
// have to nuke elements first // have to nuke elements first
MakeEmpty(); MakeEmpty();
} }
}
template<class T> template<class T>
BObjectList<T>& BObjectList<T>&
@@ -429,13 +462,14 @@ BObjectList<T>::operator=(const BObjectList<T> &list)
for (int32 index = 0; index < count; index++) { for (int32 index = 0; index < count; index++) {
T* item = list.ItemAt(index); T* item = list.ItemAt(index);
if (item) if (item)
item = new T(*item); item = new (std::nothrow) T(*item);
SetItem(index, item); _SetItem(index, item);
} }
} }
return result; return result;
} }
template<class T> template<class T>
bool bool
BObjectList<T>::AddItem(T* item) BObjectList<T>::AddItem(T* item)
@@ -444,6 +478,7 @@ BObjectList<T>::AddItem(T *item)
return _PointerList_::AddItem((void*)item); return _PointerList_::AddItem((void*)item);
} }
template<class T> template<class T>
bool bool
BObjectList<T>::AddItem(T* item, int32 atIndex) BObjectList<T>::AddItem(T* item, int32 atIndex)
@@ -451,6 +486,7 @@ BObjectList<T>::AddItem(T *item, int32 atIndex)
return _PointerList_::AddItem((void*)item, atIndex); return _PointerList_::AddItem((void*)item, atIndex);
} }
template<class T> template<class T>
bool bool
BObjectList<T>::AddList(BObjectList<T>* newItems) BObjectList<T>::AddList(BObjectList<T>* newItems)
@@ -458,6 +494,7 @@ BObjectList<T>::AddList(BObjectList<T> *newItems)
return _PointerList_::AddList(newItems); return _PointerList_::AddList(newItems);
} }
template<class T> template<class T>
bool bool
BObjectList<T>::AddList(BObjectList<T>* newItems, int32 atIndex) BObjectList<T>::AddList(BObjectList<T>* newItems, int32 atIndex)
@@ -478,6 +515,7 @@ BObjectList<T>::RemoveItem(T *item, bool deleteIfOwning)
return result; return result;
} }
template<class T> template<class T>
T* T*
BObjectList<T>::RemoveItemAt(int32 index) BObjectList<T>::RemoveItemAt(int32 index)
@@ -485,6 +523,7 @@ BObjectList<T>::RemoveItemAt(int32 index)
return (T*)_PointerList_::RemoveItem(index); return (T*)_PointerList_::RemoveItem(index);
} }
template<class T> template<class T>
inline T* inline T*
BObjectList<T>::ItemAt(int32 index) const BObjectList<T>::ItemAt(int32 index) const
@@ -492,6 +531,7 @@ BObjectList<T>::ItemAt(int32 index) const
return (T*)_PointerList_::ItemAt(index); return (T*)_PointerList_::ItemAt(index);
} }
template<class T> template<class T>
bool bool
BObjectList<T>::ReplaceItem(int32 index, T* item) BObjectList<T>::ReplaceItem(int32 index, T* item)
@@ -501,6 +541,7 @@ BObjectList<T>::ReplaceItem(int32 index, T *item)
return _PointerList_::ReplaceItem(index, (void*)item); return _PointerList_::ReplaceItem(index, (void*)item);
} }
template<class T> template<class T>
T* T*
BObjectList<T>::SwapWithItem(int32 index, T* newItem) BObjectList<T>::SwapWithItem(int32 index, T* newItem)
@@ -510,13 +551,15 @@ BObjectList<T>::SwapWithItem(int32 index, T *newItem)
return result; return result;
} }
template<class T> template<class T>
void void
BObjectList<T>::SetItem(int32 index, T *newItem) BObjectList<T>::_SetItem(int32 index, T* newItem)
{ {
_PointerList_::ReplaceItem(index, (void*)newItem); _PointerList_::ReplaceItem(index, (void*)newItem);
} }
template<class T> template<class T>
int32 int32
BObjectList<T>::IndexOf(const T* item) const BObjectList<T>::IndexOf(const T* item) const
@@ -524,6 +567,7 @@ BObjectList<T>::IndexOf(const T *item) const
return _PointerList_::IndexOf((void*)item); return _PointerList_::IndexOf((void*)item);
} }
template<class T> template<class T>
T* T*
BObjectList<T>::FirstItem() const BObjectList<T>::FirstItem() const
@@ -531,6 +575,7 @@ BObjectList<T>::FirstItem() const
return (T*)_PointerList_::FirstItem(); return (T*)_PointerList_::FirstItem();
} }
template<class T> template<class T>
T* T*
BObjectList<T>::LastItem() const BObjectList<T>::LastItem() const
@@ -538,6 +583,7 @@ BObjectList<T>::LastItem() const
return (T*)_PointerList_::LastItem(); return (T*)_PointerList_::LastItem();
} }
template<class T> template<class T>
bool bool
BObjectList<T>::HasItem(const T* item) const BObjectList<T>::HasItem(const T* item) const
@@ -545,6 +591,7 @@ BObjectList<T>::HasItem(const T *item) const
return _PointerList_::HasItem((void*)item); return _PointerList_::HasItem((void*)item);
} }
template<class T> template<class T>
bool bool
BObjectList<T>::IsEmpty() const BObjectList<T>::IsEmpty() const
@@ -552,6 +599,7 @@ BObjectList<T>::IsEmpty() const
return _PointerList_::IsEmpty(); return _PointerList_::IsEmpty();
} }
template<class T> template<class T>
int32 int32
BObjectList<T>::CountItems() const BObjectList<T>::CountItems() const
@@ -559,6 +607,7 @@ BObjectList<T>::CountItems() const
return _PointerList_::CountItems(); return _PointerList_::CountItems();
} }
template<class T> template<class T>
void void
BObjectList<T>::MakeEmpty(bool deleteIfOwning) BObjectList<T>::MakeEmpty(bool deleteIfOwning)
@@ -571,6 +620,7 @@ BObjectList<T>::MakeEmpty(bool deleteIfOwning)
_PointerList_::MakeEmpty(); _PointerList_::MakeEmpty();
} }
template<class T> template<class T>
T* T*
BObjectList<T>::EachElement(EachFunction func, void* params) BObjectList<T>::EachElement(EachFunction func, void* params)
@@ -593,9 +643,10 @@ const T *
BObjectList<T>::FindIf(const UnaryPredicate<T>& predicate) const BObjectList<T>::FindIf(const UnaryPredicate<T>& predicate) const
{ {
int32 count = CountItems(); int32 count = CountItems();
for (int32 index = 0; index < count; index++) for (int32 index = 0; index < count; index++) {
if (predicate.operator()(ItemAt(index)) == 0) if (predicate.operator()(ItemAt(index)) == 0)
return ItemAt(index); return ItemAt(index);
}
return 0; return 0;
} }
@@ -604,9 +655,10 @@ T *
BObjectList<T>::FindIf(const UnaryPredicate<T>& predicate) BObjectList<T>::FindIf(const UnaryPredicate<T>& predicate)
{ {
int32 count = CountItems(); int32 count = CountItems();
for (int32 index = 0; index < count; index++) for (int32 index = 0; index < count; index++) {
if (predicate.operator()(ItemAt(index)) == 0) if (predicate.operator()(ItemAt(index)) == 0)
return ItemAt(index); return ItemAt(index);
}
return 0; return 0;
} }
@@ -618,6 +670,7 @@ BObjectList<T>::SortItems(CompareFunction function)
_PointerList_::SortItems((GenericCompareFunction)function); _PointerList_::SortItems((GenericCompareFunction)function);
} }
template<class T> template<class T>
void void
BObjectList<T>::SortItems(CompareFunctionWithState function, void* state) BObjectList<T>::SortItems(CompareFunctionWithState function, void* state)
@@ -625,6 +678,7 @@ BObjectList<T>::SortItems(CompareFunctionWithState function, void *state)
_PointerList_::SortItems((GenericCompareFunctionWithState)function, state); _PointerList_::SortItems((GenericCompareFunctionWithState)function, state);
} }
template<class T> template<class T>
void void
BObjectList<T>::HSortItems(CompareFunction function) BObjectList<T>::HSortItems(CompareFunction function)
@@ -632,6 +686,7 @@ BObjectList<T>::HSortItems(CompareFunction function)
_PointerList_::HSortItems((GenericCompareFunction)function); _PointerList_::HSortItems((GenericCompareFunction)function);
} }
template<class T> template<class T>
void void
BObjectList<T>::HSortItems(CompareFunctionWithState function, void* state) BObjectList<T>::HSortItems(CompareFunctionWithState function, void* state)
@@ -639,17 +694,18 @@ BObjectList<T>::HSortItems(CompareFunctionWithState function, void *state)
_PointerList_::HSortItems((GenericCompareFunctionWithState)function, state); _PointerList_::HSortItems((GenericCompareFunctionWithState)function, state);
} }
template<class T> template<class T>
T* T*
BObjectList<T>::BinarySearch(const T& key, CompareFunction func) const BObjectList<T>::BinarySearch(const T& key, CompareFunction func) const
{ {
return (T*)_PointerList_::BinarySearch(&key, return (T*)_PointerList_::BinarySearch(&key, (GenericCompareFunction)func);
(GenericCompareFunction)func);
} }
template<class T> template<class T>
T* T*
BObjectList<T>::BinarySearch(const T &key, CompareFunctionWithState func, void *state) const BObjectList<T>::BinarySearch(const T& key, CompareFunctionWithState func,
void* state) const
{ {
return (T*)_PointerList_::BinarySearch(&key, return (T*)_PointerList_::BinarySearch(&key,
(GenericCompareFunctionWithState)func, state); (GenericCompareFunctionWithState)func, state);
@@ -722,9 +778,11 @@ BObjectList<T>::BinaryInsert(T *item, CompareFunction func)
return AddItem(item, -index - 1); return AddItem(item, -index - 1);
} }
template<class T> template<class T>
bool bool
BObjectList<T>::BinaryInsert(T *item, CompareFunctionWithState func, void *state) BObjectList<T>::BinaryInsert(T* item, CompareFunctionWithState func,
void* state)
{ {
int32 index = _PointerList_::BinarySearchIndex(item, int32 index = _PointerList_::BinarySearchIndex(item,
(GenericCompareFunctionWithState)func, state); (GenericCompareFunctionWithState)func, state);
@@ -736,6 +794,7 @@ BObjectList<T>::BinaryInsert(T *item, CompareFunctionWithState func, void *state
return AddItem(item, -index - 1); return AddItem(item, -index - 1);
} }
template<class T> template<class T>
bool bool
BObjectList<T>::BinaryInsertUnique(T* item, CompareFunction func) BObjectList<T>::BinaryInsertUnique(T* item, CompareFunction func)
@@ -748,9 +807,11 @@ BObjectList<T>::BinaryInsertUnique(T *item, CompareFunction func)
return AddItem(item, -index - 1); return AddItem(item, -index - 1);
} }
template<class T> template<class T>
bool bool
BObjectList<T>::BinaryInsertUnique(T *item, CompareFunctionWithState func, void *state) BObjectList<T>::BinaryInsertUnique(T* item, CompareFunctionWithState func,
void* state)
{ {
int32 index = _PointerList_::BinarySearchIndex(item, int32 index = _PointerList_::BinarySearchIndex(item,
(GenericCompareFunctionWithState)func, state); (GenericCompareFunctionWithState)func, state);
@@ -773,14 +834,16 @@ BObjectList<T>::BinaryInsertCopy(const T &copyThis, CompareFunction func)
else else
index = -index - 1; index = -index - 1;
T *newItem = new T(copyThis); T* newItem = new (std::nothrow) T(copyThis);
AddItem(newItem, index); AddItem(newItem, index);
return newItem; return newItem;
} }
template<class T> template<class T>
T* T*
BObjectList<T>::BinaryInsertCopy(const T &copyThis, CompareFunctionWithState func, void *state) BObjectList<T>::BinaryInsertCopy(const T& copyThis,
CompareFunctionWithState func, void* state)
{ {
int32 index = _PointerList_::BinarySearchIndex(&copyThis, int32 index = _PointerList_::BinarySearchIndex(&copyThis,
(GenericCompareFunctionWithState)func, state); (GenericCompareFunctionWithState)func, state);
@@ -790,11 +853,12 @@ BObjectList<T>::BinaryInsertCopy(const T &copyThis, CompareFunctionWithState fun
else else
index = -index - 1; index = -index - 1;
T *newItem = new T(copyThis); T* newItem = new (std::nothrow) T(copyThis);
AddItem(newItem, index); AddItem(newItem, index);
return newItem; return newItem;
} }
template<class T> template<class T>
T* T*
BObjectList<T>::BinaryInsertCopyUnique(const T& copyThis, CompareFunction func) BObjectList<T>::BinaryInsertCopyUnique(const T& copyThis, CompareFunction func)
@@ -805,15 +869,16 @@ BObjectList<T>::BinaryInsertCopyUnique(const T &copyThis, CompareFunction func)
return ItemAt(index); return ItemAt(index);
index = -index - 1; index = -index - 1;
T *newItem = new T(copyThis); T* newItem = new (std::nothrow) T(copyThis);
AddItem(newItem, index); AddItem(newItem, index);
return newItem; return newItem;
} }
template<class T> template<class T>
T* T*
BObjectList<T>::BinaryInsertCopyUnique(const T &copyThis, CompareFunctionWithState func, BObjectList<T>::BinaryInsertCopyUnique(const T& copyThis,
void *state) CompareFunctionWithState func, void* state)
{ {
int32 index = _PointerList_::BinarySearchIndex(&copyThis, int32 index = _PointerList_::BinarySearchIndex(&copyThis,
(GenericCompareFunctionWithState)func, state); (GenericCompareFunctionWithState)func, state);
@@ -821,15 +886,16 @@ BObjectList<T>::BinaryInsertCopyUnique(const T &copyThis, CompareFunctionWithSta
return ItemAt(index); return ItemAt(index);
index = -index - 1; index = -index - 1;
T *newItem = new T(copyThis); T* newItem = new (std::nothrow) T(copyThis);
AddItem(newItem, index); AddItem(newItem, index);
return newItem; return newItem;
} }
template<class T> template<class T>
int32 int32
BObjectList<T>::FindBinaryInsertionIndex(const UnaryPredicate<T> &pred, bool *alreadyInList) BObjectList<T>::FindBinaryInsertionIndex(const UnaryPredicate<T>& pred,
const bool* alreadyInList) const
{ {
int32 index = _PointerList_::BinarySearchIndexByPredicate(&pred, int32 index = _PointerList_::BinarySearchIndexByPredicate(&pred,
(UnaryPredicateGlue)&UnaryPredicate<T>::_unary_predicate_glue); (UnaryPredicateGlue)&UnaryPredicate<T>::_unary_predicate_glue);
@@ -843,6 +909,7 @@ BObjectList<T>::FindBinaryInsertionIndex(const UnaryPredicate<T> &pred, bool *al
return index; return index;
} }
template<class T> template<class T>
bool bool
BObjectList<T>::BinaryInsert(T* item, const UnaryPredicate<T>& pred) BObjectList<T>::BinaryInsert(T* item, const UnaryPredicate<T>& pred)
@@ -850,6 +917,7 @@ BObjectList<T>::BinaryInsert(T *item, const UnaryPredicate<T> &pred)
return AddItem(item, FindBinaryInsertionIndex(pred)); return AddItem(item, FindBinaryInsertionIndex(pred));
} }
template<class T> template<class T>
bool bool
BObjectList<T>::BinaryInsertUnique(T* item, const UnaryPredicate<T>& pred) BObjectList<T>::BinaryInsertUnique(T* item, const UnaryPredicate<T>& pred)
@@ -863,4 +931,5 @@ BObjectList<T>::BinaryInsertUnique(T *item, const UnaryPredicate<T> &pred)
return true; return true;
} }
#endif /* __OBJECT_LIST__ */
#endif /* _OBJECT_LIST_H */
@@ -0,0 +1,38 @@
/*
* Copyright 2011, Oliver Tappe <[email protected]>
* Distributed under the terms of the MIT License.
*/
#ifndef _OBJECT_LIST_PRIVATE_H
#define _OBJECT_LIST_PRIVATE_H
#include <ObjectList.h>
template<class T>
class BObjectList<T>::Private {
public:
Private(BObjectList<T>* objectList)
:
fObjectList(objectList)
{
}
BList*
AsBList()
{
return fObjectList;
}
const BList*
AsBList() const
{
return fObjectList;
}
private:
BObjectList<T>* fObjectList;
};
#endif // _OBJECT_LIST_PRIVATE_H
+4 -1
View File
@@ -64,6 +64,8 @@ All rights reserved.
#include <String.h> #include <String.h>
#include <Window.h> #include <Window.h>
#include <ObjectListPrivate.h>
#include "ColorTools.h" #include "ColorTools.h"
#include "ObjectList.h" #include "ObjectList.h"
@@ -4520,7 +4522,8 @@ OutlineView::SortList(BRowContainer* list, bool isVisible)
{ {
if (list) { if (list) {
// Shellsort // Shellsort
BRow** items = (BRow**) list->AsBList()->Items(); BRow** items
= (BRow**) BObjectList<BRow>::Private(list).AsBList()->Items();
int32 numItems = list->CountItems(); int32 numItems = list->CountItems();
int h; int h;
for (h = 1; h < numItems / 9; h = 3 * h + 1) for (h = 1; h < numItems / 9; h = 3 * h + 1)
+1 -1
View File
@@ -25,7 +25,7 @@ if ! $(TARGET_PLATFORM_HAIKU_COMPATIBLE) {
SetSubDirSupportedPlatforms haiku libbe_test ; SetSubDirSupportedPlatforms haiku libbe_test ;
UseLibraryHeaders agg icon ; UseLibraryHeaders agg icon ;
UsePrivateHeaders app input print interface libbe shared tracker ; UsePrivateHeaders app input print interface libbe shared support tracker ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) textview_support ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) textview_support ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) layouter ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) layouter ] ;
+1 -1
View File
@@ -3,7 +3,7 @@ SubDir HAIKU_TOP src kits tracker ;
SetSubDirSupportedPlatformsBeOSCompatible ; SetSubDirSupportedPlatformsBeOSCompatible ;
AddSubDirSupportedPlatforms libbe_test ; AddSubDirSupportedPlatforms libbe_test ;
UsePrivateHeaders interface mount shared storage tracker ; UsePrivateHeaders interface mount shared storage support tracker ;
UseLibraryHeaders icon ; UseLibraryHeaders icon ;
+6 -2
View File
@@ -68,6 +68,8 @@ All rights reserved.
#include <Volume.h> #include <Volume.h>
#include <Window.h> #include <Window.h>
#include <ObjectListPrivate.h>
#include "Attributes.h" #include "Attributes.h"
#include "AttributeStream.h" #include "AttributeStream.h"
#include "AutoLock.h" #include "AutoLock.h"
@@ -8776,10 +8778,12 @@ BPoseView::SortPoses()
PRINT(("===================\n")); PRINT(("===================\n"));
#endif #endif
BPose **poses = reinterpret_cast<BPose **>(fPoseList->AsBList()->Items()); BPose **poses = reinterpret_cast<BPose **>(
PoseList::Private(fPoseList).AsBList()->Items());
std::stable_sort(poses, &poses[fPoseList->CountItems()], PoseComparator(this)); std::stable_sort(poses, &poses[fPoseList->CountItems()], PoseComparator(this));
if (fFiltering) { if (fFiltering) {
poses = reinterpret_cast<BPose **>(fFilteredPoseList->AsBList()->Items()); poses = reinterpret_cast<BPose **>(
PoseList::Private(fFilteredPoseList).AsBList()->Items());
std::stable_sort(poses, &poses[fPoseList->CountItems()], std::stable_sort(poses, &poses[fPoseList->CountItems()],
PoseComparator(this)); PoseComparator(this));
} }
+1 -1
View File
@@ -1,7 +1,7 @@
SubDir HAIKU_TOP src servers app ; SubDir HAIKU_TOP src servers app ;
UseLibraryHeaders agg ; UseLibraryHeaders agg ;
UsePrivateHeaders app graphics input interface kernel shared storage ; UsePrivateHeaders app graphics input interface kernel shared storage support ;
UseHeaders [ FDirName $(HAIKU_TOP) src servers app drawing ] ; UseHeaders [ FDirName $(HAIKU_TOP) src servers app drawing ] ;
UseHeaders [ FDirName $(HAIKU_TOP) src servers app drawing Painter ] ; UseHeaders [ FDirName $(HAIKU_TOP) src servers app drawing Painter ] ;
+2 -1
View File
@@ -25,6 +25,7 @@
#include <LinkReceiver.h> #include <LinkReceiver.h>
#include <OffsetFile.h> #include <OffsetFile.h>
#include <ObjectListPrivate.h>
#include <PicturePlayer.h> #include <PicturePlayer.h>
#include <PictureProtocol.h> #include <PictureProtocol.h>
#include <PortLink.h> #include <PortLink.h>
@@ -1071,7 +1072,7 @@ ServerPicture::Play(View* view)
return; return;
BPrivate::PicturePlayer player(mallocIO->Buffer(), BPrivate::PicturePlayer player(mallocIO->Buffer(),
mallocIO->BufferLength(), fPictures->AsBList()); mallocIO->BufferLength(), PictureList::Private(fPictures).AsBList());
player.Play(const_cast<void**>(kTableEntries), player.Play(const_cast<void**>(kTableEntries),
sizeof(kTableEntries) / sizeof(void*), view); sizeof(kTableEntries) / sizeof(void*), view);
} }
+2 -1
View File
@@ -14,6 +14,7 @@
#include <AppServerLink.h> #include <AppServerLink.h>
#include <MessagePrivate.h> #include <MessagePrivate.h>
#include <ObjectListPrivate.h>
#include <Autolock.h> #include <Autolock.h>
#include <Deskbar.h> #include <Deskbar.h>
@@ -1729,7 +1730,7 @@ InputServer::_FilterEvent(BInputServerFilter* filter, EventList& events,
if (result == B_DISPATCH_MESSAGE) { if (result == B_DISPATCH_MESSAGE) {
EventList addedEvents; EventList addedEvents;
addedEvents.AsBList()->AddList(&newEvents); EventList::Private(&addedEvents).AsBList()->AddList(&newEvents);
_SanitizeEvents(addedEvents); _SanitizeEvents(addedEvents);
// add the new events - but don't methodize them again // add the new events - but don't methodize them again
events.AddList(&addedEvents, index); events.AddList(&addedEvents, index);
+1 -1
View File
@@ -25,7 +25,7 @@ SEARCH on US-International.keymap += [ FDirName $(HAIKU_TOP) src data keymaps ]
AddResources input_server : input_server.rdef ; AddResources input_server : input_server.rdef ;
UsePrivateHeaders app input interface shared storage ; UsePrivateHeaders app input interface shared storage support ;
UsePrivateSystemHeaders ; UsePrivateSystemHeaders ;
if $(TARGET_PLATFORM) != haiku { if $(TARGET_PLATFORM) != haiku {