66 lines
1.8 KiB
C++
Executable File
66 lines
1.8 KiB
C++
Executable File
#include "picollection.h"
|
|
|
|
|
|
/** \class PICollection
|
|
* \brief Interface to discover element groups
|
|
* \details
|
|
* \section PICollection_sec0 Synopsis
|
|
* This class has only static functions so no need to create instance of the
|
|
* %PICollection. This class provide macros to add some classes or existing
|
|
* objects to global collection and access to them from any place of the code.
|
|
* \snippet picollection.cpp main
|
|
* */
|
|
|
|
|
|
PIStringList PICollection::groups() {
|
|
PIStringList sl;
|
|
PIVector<PICollection::Group> & cg(_groups());
|
|
piForeachC (Group & g, cg)
|
|
sl << g.name;
|
|
return sl;
|
|
}
|
|
|
|
|
|
PIVector<const PIObject * > PICollection::groupElements(const PIString & group) {
|
|
PIVector<PICollection::Group> & cg(_groups());
|
|
piForeachC (Group & g, cg)
|
|
if (g.name == group)
|
|
return g.elements;
|
|
return PIVector<const PIObject * >();
|
|
}
|
|
|
|
|
|
bool PICollection::addToGroup(const PIString & group, const PIObject * element) {
|
|
//piCout << "add to" << group << element;
|
|
PIString n = PIStringAscii(element->className());
|
|
PIVector<PICollection::Group> & cg(_groups());
|
|
piForeach (Group & g, cg)
|
|
if (g.name == group) {
|
|
for (int i = 0; i < g.elements.size_s(); ++i)
|
|
if (PIString(g.elements[i]->className()) == n)
|
|
return false;
|
|
g.elements << element;
|
|
//piCout << "new group" << group << ", ok";
|
|
return true;
|
|
}
|
|
_groups() << Group(group);
|
|
_groups().back().elements << element;
|
|
//piCout << "new group" << group << ", ok";
|
|
return true;
|
|
}
|
|
|
|
|
|
PIVector<PICollection::Group> & PICollection::_groups() {
|
|
static PIVector<PICollection::Group> ret;
|
|
return ret;
|
|
}
|
|
|
|
|
|
PICollection::CollectionAdder::CollectionAdder(const PIString & group, const PIObject * element, const PIString & name, bool own) {
|
|
if (!element) return;
|
|
const_cast<PIObject * >(element)->setName(name);
|
|
bool added = PICollection::addToGroup(group, element);
|
|
if (!added && own)
|
|
delete element;
|
|
}
|