// setSlotComponent() -- Sets a single component bool Component::setSlotComponent(Component* const single) { // When a only one component ... make it a PairStream PairStream* tmp = new PairStream(); tmp->put( new Pair("1", single) ); // Process the new components list and swap processComponents(tmp, typeid(Component)); tmp->unref(); return true; }
bool StateMachine::setSlotStateMachines(const PairStream* const msg) { // First remove the old list; and make sure we tell the old stMachList // that we're no longer their container. if (stMachList != nullptr) { List::Item* item = stMachList->getFirstItem(); while (item != nullptr) { Pair* p = static_cast<Pair*>(item->getValue()); Component* q = static_cast<Component*>(p->object()); q->container(nullptr); item = item->getNext(); } stMachList = nullptr; } // Build a new list containing only StateMachine class (or derived) objects if (msg != nullptr) { PairStream* newList = new PairStream(); // For each object in the list; if it's a StateMachine (or derived from) then // clone the object and add it to the new list. const List::Item* item = msg->getFirstItem(); while (item != nullptr) { const Pair* p = static_cast<const Pair*>(item->getValue()); const StateMachine* q = dynamic_cast<const StateMachine*>(p->object()); if (q != nullptr) { Pair* cp = static_cast<Pair*>(p->clone()); StateMachine* cq = static_cast<StateMachine*>(cp->object()); cq->container(this); newList->put(cp); } else { std::cerr << "StateMachine::setSlotStateMachines(): " << *p->slot() << " is not a StateMachine!" << std::endl; } item = item->getNext(); } // Set the pointer to the new stMach list stMachList = newList; } return true; }
//------------------------------------------------------------------------------ // processComponents() -- process our new components list; // -- Add the components from the input list, 'list', to a new list, 'newList' // make sure they are all of type Component (or derived from it) // tell them that we are their container // -- Add an optional component to the end of the new list // -- Swap our 'components' list with the new list, newList // -- Handle selections. //------------------------------------------------------------------------------ void Component::processComponents( PairStream* const list, const std::type_info& filter, Pair* const add, Component* const remove ) { PairStream* oldList = components.getRefPtr(); // --- // Our dynamic_cast (see below) already filters on the Component class // --- bool skipFilter = false; if (&filter == 0) skipFilter = true; else if (filter == typeid(Component)) skipFilter = true; // --- // Create a new list, copy (filter) the component pairs and set their container pointers // --- PairStream* newList = new PairStream(); if (list != 0) { // Add the (filtered) components to the new list and set their container List::Item* item = list->getFirstItem(); while (item != 0) { Pair* pair = (Pair*) item->getValue(); Component* cp = dynamic_cast<Component*>( pair->object() ); if ( cp != 0 && cp != remove && (skipFilter || cp->isClassType(filter)) ) { newList->put(pair); cp->container(this); } else if ( cp != 0 && cp == remove ) { cp->container(0); } item = item->getNext(); } } // --- // Add the optional component // --- if (add != 0) { Component* cp = dynamic_cast<Component*>( add->object() ); if ( cp != 0 && (skipFilter || cp->isClassType(filter)) ) { newList->put(add); cp->container(this); } } // --- // Swap lists // --- components = newList; newList->unref(); // --- // Anything selected? // --- if (selection != 0) { if (selection->isClassType(typeid(String))) { String str(*((String*)selection)); select(&str); } else { Integer num(((Number*)selection)->getInt()); select(&num); } } if (oldList != 0) { oldList->unref(); } }