git-svn-id: svn://db.shs.com.ru/pip@806 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2019-06-23 11:21:46 +00:00
parent 3e36e6af8f
commit 8a2a9e1684
2 changed files with 31 additions and 35 deletions

View File

@@ -14,44 +14,52 @@
PIStringList PICollection::groups() { PIStringList PICollection::groups() {
PIStringList sl; PIStringList sl;
piForeachC (Group & g, *_groups) PIVector<PICollection::Group> & cg(_groups());
piForeachC (Group & g, cg)
sl << g.name; sl << g.name;
return sl; return sl;
} }
PIVector<const PIObject * > PICollection::groupElements(const PIString & group) { PIVector<const PIObject * > PICollection::groupElements(const PIString & group) {
piForeachC (Group & g, *_groups) PIVector<PICollection::Group> & cg(_groups());
piForeachC (Group & g, cg)
if (g.name == group) if (g.name == group)
return g.elements; return g.elements;
return PIVector<const PIObject * >(); return PIVector<const PIObject * >();
} }
void PICollection::addToGroup(const PIString & group, const PIObject * element) { bool PICollection::addToGroup(const PIString & group, const PIObject * element) {
//piCout << "add to" << group << element; //piCout << "add to" << group << element;
PIString n = element->className(); PIString n = PIStringAscii(element->className());
piForeach (Group & g, *_groups) PIVector<PICollection::Group> & cg(_groups());
piForeach (Group & g, cg)
if (g.name == group) { if (g.name == group) {
for (int i = 0; i < g.elements.size_s(); ++i) for (int i = 0; i < g.elements.size_s(); ++i)
if (PIString(g.elements[i]->className()) == n) if (PIString(g.elements[i]->className()) == n)
return; return false;
g.elements << element; g.elements << element;
//piCout << "new group" << group << ", ok"; //piCout << "new group" << group << ", ok";
return; return true;
} }
*_groups << Group(group); _groups() << Group(group);
_groups->back().elements << element; _groups().back().elements << element;
//piCout << "new group" << group << ", ok"; //piCout << "new group" << group << ", ok";
return true;
} }
PICollection::CollectionAdder::CollectionAdder(const PIString & group, const PIObject * element, const PIString & name) { PIVector<PICollection::Group> & PICollection::_groups() {
if (element == 0) return; 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); const_cast<PIObject * >(element)->setName(name);
PICollection::addToGroup(group, element); bool added = PICollection::addToGroup(group, element);
if (!added && own)
delete element;
} }
bool __PICollectionInitializer::_inited_(false);
PIVector<PICollection::Group> * PICollection::_groups;

View File

@@ -44,10 +44,10 @@
# define ADD_NEW_TO_COLLECTION(group, class) # define ADD_NEW_TO_COLLECTION(group, class)
#else #else
# define ADD_TO_COLLECTION(group, object) static PICollection::CollectionAdder __##group##_##__LINE__##_##adder##__(#group, object); # define ADD_TO_COLLECTION(group, object) static PICollection::CollectionAdder __##group##_##__LINE__##_##adder##__(#group, object, false);
# define ADD_TO_COLLECTION_WITH_NAME(group, object, name) static PICollection::CollectionAdder __##group##_##__LINE__##_##adder##__(#group, object, #name); # define ADD_TO_COLLECTION_WITH_NAME(group, object, name) static PICollection::CollectionAdder __##group##_##__LINE__##_##adder##__(#group, object, #name, false);
# define ADD_NEW_TO_COLLECTION(group, class) static PICollection::CollectionAdder __##group##_##class##_##adder##__(#group, new class()); # define ADD_NEW_TO_COLLECTION(group, class) static PICollection::CollectionAdder __##group##_##class##_##adder##__(#group, new class(), true);
# define ADD_NEW_TO_COLLECTION_WITH_NAME(group, class, name) static PICollection::CollectionAdder __##group##_##class##_##adder##__(#group, new class(), #name); # define ADD_NEW_TO_COLLECTION_WITH_NAME(group, class, name) static PICollection::CollectionAdder __##group##_##class##_##adder##__(#group, new class(), #name, true);
#endif #endif
class PIP_EXPORT PICollection class PIP_EXPORT PICollection
@@ -62,11 +62,11 @@ public:
//! \brief Returns all elements of group "group" //! \brief Returns all elements of group "group"
static PIVector<const PIObject * > groupElements(const PIString & group); static PIVector<const PIObject * > groupElements(const PIString & group);
static void addToGroup(const PIString & group, const PIObject * element); static bool addToGroup(const PIString & group, const PIObject * element);
class CollectionAdder { class CollectionAdder {
public: public:
CollectionAdder(const PIString & group, const PIObject * element, const PIString & name = PIString()); CollectionAdder(const PIString & group, const PIObject * element, const PIString & name = PIString(), bool own = false);
}; };
protected: protected:
@@ -77,20 +77,8 @@ protected:
PIVector<const PIObject * > elements; PIVector<const PIObject * > elements;
}; };
static PIVector<Group> * _groups; static PIVector<Group> & _groups();
}; };
class PIP_EXPORT __PICollectionInitializer {
public:
__PICollectionInitializer() {
if (_inited_) return;
_inited_ = true;
PICollection::_groups = new PIVector<PICollection::Group>();
}
static bool _inited_;
};
static __PICollectionInitializer __picollectioninitializer;
#endif // PICOLLECTION_H #endif // PICOLLECTION_H