//------------------------------------------------------------------------------ // Process device input channels and components (default) //------------------------------------------------------------------------------ void IoDevice::processInputs(const double dt, IoData* const inData) { // ### Since we'll process all of the input adapters, our derived I/O device // classes should process their device inputs BEFORE calling this base // class functions. ### // process any input adapters if (adapters != nullptr) { List::Item* item = adapters->getFirstItem(); while (item != nullptr) { Pair* const pair = static_cast<Pair*>(item->getValue()); IoAdapter* const p = static_cast<IoAdapter*>(pair->object()); p->processInputs(dt, this, inData); item = item->getNext(); } } // process any input (sub)devices if (devices != nullptr) { List::Item* item = devices->getFirstItem(); while (item != nullptr) { Pair* const pair = static_cast<Pair*>(item->getValue()); IoDevice* const p = static_cast<IoDevice*>(pair->object()); p->processInputs(dt, inData); item = item->getNext(); } } }
//------------------------------------------------------------------------------ // shutdownNotification() -- We're shutting down //------------------------------------------------------------------------------ bool IoDevice::shutdownNotification() { // Shutdown our I/O adapters if (adapters != nullptr) { List::Item* item = adapters->getFirstItem(); while (item != nullptr) { Pair* const pair = static_cast<Pair*>(item->getValue()); IoAdapter* const p = static_cast<IoAdapter*>(pair->object()); p->event(SHUTDOWN_EVENT); item = item->getNext(); } } // Shutdown our I/O devices if (devices != nullptr) { List::Item* item = devices->getFirstItem(); while (item != nullptr) { Pair* const pair = static_cast<Pair*>(item->getValue()); IoDevice* const p = static_cast<IoDevice*>(pair->object()); p->event(SHUTDOWN_EVENT); item = item->getNext(); } } return BaseClass::shutdownNotification(); }
//------------------------------------------------------------------------------ // reset() -- Reset the I/O handler //------------------------------------------------------------------------------ void IoDevice::reset() { BaseClass::reset(); // Reset our I/O adapters if (adapters != 0) { List::Item* item = adapters->getFirstItem(); while (item != 0) { Pair* const pair = (Pair*) item->getValue(); IoAdapter* const p = (IoAdapter*) pair->object(); p->reset(); item = item->getNext(); } } // Reset our I/O devices if (devices != 0) { List::Item* item = devices->getFirstItem(); while (item != 0) { Pair* const pair = (Pair*) item->getValue(); IoDevice* const p = (IoDevice*) pair->object(); p->reset(); item = item->getNext(); } } }
//------------------------------------------------------------------------------ // Process device output channels and components (default) //------------------------------------------------------------------------------ void IoDevice::processOutputs(const double dt, const IoData* const outData) { // ### Since we'll process all of the output I/O adapters, our derived I/O // device classes should process their device outputs AFTER calling this // base class functions. ### // process our output (sub)devices if (devices != nullptr) { List::Item* item = devices->getFirstItem(); while (item != nullptr) { Pair* const pair = static_cast<Pair*>(item->getValue()); IoDevice* const p = static_cast<IoDevice*>(pair->object()); p->processOutputs(dt, outData); item = item->getNext(); } } // process any output adapters if (adapters != nullptr) { if (outData != nullptr) { List::Item* item = adapters->getFirstItem(); while (item != nullptr) { Pair* const pair = static_cast<Pair*>(item->getValue()); IoAdapter* const p = static_cast<IoAdapter*>(pair->object()); p->processOutputs(dt, outData, this); item = item->getNext(); } } } }
//------------------------------------------------------------------------------ // reset() -- Reset the I/O handler //------------------------------------------------------------------------------ void IoDevice::reset() { BaseClass::reset(); // Reset our I/O adapters if (adapters != nullptr) { List::Item* item = adapters->getFirstItem(); while (item != nullptr) { Pair* const pair = static_cast<Pair*>(item->getValue()); IoAdapter* const p = static_cast<IoAdapter*>(pair->object()); p->reset(); item = item->getNext(); } } // Reset our I/O devices if (devices != nullptr) { List::Item* item = devices->getFirstItem(); while (item != nullptr) { Pair* const pair = static_cast<Pair*>(item->getValue()); IoDevice* const p = static_cast<IoDevice*>(pair->object()); p->reset(); item = item->getNext(); } } }
// adapters: List of IoAdapter objects bool IoDevice::setSlotAdapters(PairStream* const list) { bool ok = true; if (list != 0) { // check to make sure all objects on the list are I/O adapters unsigned int cnt = 0; List::Item* item = list->getFirstItem(); while (item != 0) { cnt++; Pair* const pair = (Pair*) item->getValue(); ok = pair->object()->isClassType(typeid(IoAdapter)); if (ok) { ((IoAdapter*) pair->object())->container(this); } else { std::cerr << "IoDevice::setSlotAdapters(): Item number " << cnt; std::cerr << " on the list is a non-IoAdapter component!" << std::endl; } item = item->getNext(); } } if (ok) adapters = list; return ok; }
// devices: List of IoDevice objects bool IoDevice::setSlotDevices(PairStream* const list) { bool ok = true; if (list != nullptr) { // check to make sure all objects on the list are I/O Devices unsigned int cnt = 0; List::Item* item = list->getFirstItem(); while (item != nullptr) { cnt++; Pair* const pair = static_cast<Pair*>(item->getValue()); ok = pair->object()->isClassType(typeid(IoDevice)); if (ok) { static_cast<IoDevice*>(pair->object())->container(this); } else { std::cerr << "IoDevice::setSlotDevices(): Item number " << cnt; std::cerr << " on the list is a non-IoDevice component!" << std::endl; } item = item->getNext(); } } if (ok) devices = list; return ok; }
//------------------------------------------------------------------------------ // updateData() -- Update non-time critical (background) stuff here //------------------------------------------------------------------------------ void Component::updateData(const LCreal dt) { // Update all my children PairStream* subcomponents = getComponents(); if (subcomponents != 0) { if (selection != 0) { // When we've selected only one if (selected != 0) selected->updateData(dt); } else { // When we should update them all List::Item* item = subcomponents->getFirstItem(); while (item != 0) { Pair* pair = (Pair*)(item->getValue()); Component* obj = (Component*)( pair->object() ); if (obj != 0) obj->updateData(dt); item = item->getNext(); } } subcomponents->unref(); subcomponents = 0; } // Update our log file if (elog0 != 0) { elog0->updateData(dt); } }
//------------------------------------------------------------------------------ // reset() -- Reset parameters //------------------------------------------------------------------------------ void Component::reset() { PairStream* subcomponents = getComponents(); if (subcomponents != 0) { if (selection != 0) { // When we've selected only one if (selected != 0) selected->reset(); } else { // When we should reset them all List::Item* item = subcomponents->getFirstItem(); while (item != 0) { Pair* pair = (Pair*)(item->getValue()); Component* obj = (Component*)( pair->object() ); if (obj != 0) obj->reset(); item = item->getNext(); } } subcomponents->unref(); subcomponents = 0; } // Reset the log file if (elog0 != 0) { elog0->reset(); } }
//------------------------------------------------------------------------------ // Implementation of the output devices handler //------------------------------------------------------------------------------ void IoHandler::outputDevicesImp(const double dt) { // update the output data buffers before the output devices if (outData != nullptr) outData->processOutputs(); // process our I/O devices if (devices != nullptr) { List::Item* item = devices->getFirstItem(); while (item != nullptr) { Pair* const pair = static_cast<Pair* const>(item->getValue()); IoDevice* const p = static_cast<IoDevice* const>(pair->object()); p->processOutputs(dt, outData); item = item->getNext(); } } }
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; }
//------------------------------------------------------------------------------ // shutdownNotification() -- Default shutdown //------------------------------------------------------------------------------ bool Component::shutdownNotification() { // Tell all of our components PairStream* subcomponents = getComponents(); if (subcomponents != nullptr) { List::Item* item = subcomponents->getFirstItem(); while (item != nullptr) { const auto pair = static_cast<Pair*>(item->getValue()); const auto p = static_cast<Component*>(pair->object()); p->event(SHUTDOWN_EVENT); item = item->getNext(); } subcomponents->unref(); subcomponents = nullptr; } shutdown = true; return shutdown; }
//------------------------------------------------------------------------------ // shutdownNotification() -- Default shutdown //------------------------------------------------------------------------------ bool Component::shutdownNotification() { // Tell all of our components PairStream* subcomponents = getComponents(); if (subcomponents != 0) { List::Item* item = subcomponents->getFirstItem(); while (item != 0) { Pair* pair = (Pair*)(item->getValue()); Component* p = (Component*) pair->object(); p->event(SHUTDOWN_EVENT); item = item->getNext(); } subcomponents->unref(); subcomponents = 0; } shutdown = true; return shutdown; }
//------------------------------------------------------------------------------ // reset() -- Reset parameters //------------------------------------------------------------------------------ void Component::reset() { PairStream* subcomponents = getComponents(); if (subcomponents != nullptr) { if (selection != nullptr) { // When we've selected only one if (selected != nullptr) selected->reset(); } else { // When we should reset them all List::Item* item = subcomponents->getFirstItem(); while (item != nullptr) { const auto pair = static_cast<Pair*>(item->getValue()); const auto obj = static_cast<Component*>(pair->object()); obj->reset(); item = item->getNext(); } } subcomponents->unref(); subcomponents = nullptr; } }
//------------------------------------------------------------------------------ // reset() -- Reset the I/O handler //------------------------------------------------------------------------------ void IoHandler::reset() { BaseClass::reset(); // Reset our I/O devices if (devices != nullptr) { List::Item* item = devices->getFirstItem(); while (item != nullptr) { Pair* const pair = static_cast<Pair* const>(item->getValue()); IoDevice* const p = static_cast<IoDevice* const>(pair->object()); p->reset(); item = item->getNext(); } } // Initialize the networks if (!netInitialized && !netInitFailed) { netInitialized = initNetworks(); netInitFailed = !netInitialized; } clear(); }
//------------------------------------------------------------------------------ // updateTC() -- Update time critical stuff here //------------------------------------------------------------------------------ void Component::updateTC(const double dt) { // Update all my children PairStream* subcomponents = getComponents(); if (subcomponents != nullptr) { if (selection != nullptr) { // When we've selected only one if (selected != nullptr) selected->tcFrame(dt); } else { // When we should update them all List::Item* item = subcomponents->getFirstItem(); while (item != nullptr) { const auto pair = static_cast<Pair*>(item->getValue()); const auto obj = static_cast<Component*>( pair->object() ); obj->tcFrame(dt); item = item->getNext(); } } subcomponents->unref(); subcomponents = nullptr; } }
// ----------------------------------------------------------------- // reset() -- Resets the state machine // ----------------------------------------------------------------- void StateMachine::reset() { BaseClass::reset(); // Reset our state machine list 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->reset(); item = item->getNext(); } } // Goto our RESET state state = INVALID_STATE; substate = INVALID_STATE; stMach = nullptr; stMachName = nullptr; arg = nullptr; goTo(INIT_STATE); }
//------------------------------------------------------------------------------ // shutdownNotification() -- We're shutting down //------------------------------------------------------------------------------ bool IoHandler::shutdownNotification() { // Shutdown our I/O devices if (devices != nullptr) { List::Item* item = devices->getFirstItem(); while (item != nullptr) { Pair* const pair = static_cast<Pair* const>(item->getValue()); IoDevice* const p = static_cast<IoDevice* const>(pair->object()); p->event(SHUTDOWN_EVENT); item = item->getNext(); } } // Zero (unref) our thread object (of any). The thread's function has ref()'d // this object, so it won't be deleted until the thread terminates, which it // will based on our BaseClass::isShutdown() function. But at least we won't // mistakenly think that it's still around. if (thread != nullptr) { thread->terminate(); thread = nullptr; } return BaseClass::shutdownNotification(); }
//------------------------------------------------------------------------------ // 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(); } }
//------------------------------------------------------------------------------ // 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 {}; if (filter == typeid(Component)) { skipFilter = true; } // --- // Create a new list, copy (filter) the component pairs and set their container pointers // --- const auto newList = new PairStream(); if (list != nullptr) { // Add the (filtered) components to the new list and set their container List::Item* item = list->getFirstItem(); while (item != nullptr) { const auto pair = static_cast<Pair*>(item->getValue()); const auto cp = dynamic_cast<Component*>( pair->object() ); if ( cp != nullptr && cp != remove && (skipFilter || cp->isClassType(filter)) ) { newList->put(pair); cp->container(this); } else if ( cp != nullptr && cp == remove ) { cp->container(nullptr); } item = item->getNext(); } } // --- // Add the optional component // --- if (add != nullptr) { const auto cp = dynamic_cast<Component*>( add->object() ); if ( cp != nullptr && (skipFilter || cp->isClassType(filter)) ) { newList->put(add); cp->container(this); } } // --- // Swap lists // --- components = newList; newList->unref(); // --- // Anything selected? // --- if (selection != nullptr) { if (selection->isClassType(typeid(String))) { const auto str = new String(*(static_cast<String*>(selection))); select(str); str->unref(); } else { const auto num = new Integer((static_cast<Number*>(selection))->getInt()); select(num); num->unref(); } } if (oldList != nullptr) { oldList->unref(); } }