.. _program_listing_file__tmp_ws_src_smacc2_smacc2_include_smacc2_impl_smacc_state_impl.hpp: Program Listing for File smacc_state_impl.hpp ============================================= |exhale_lsh| :ref:`Return to documentation for file ` (``/tmp/ws/src/smacc2/smacc2/include/smacc2/impl/smacc_state_impl.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // Copyright 2021 RobosoftAI Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /***************************************************************************************************************** * * Authors: Pablo Inigo Blasco, Brett Aldrich * ******************************************************************************************************************/ #pragma once #include #include #include #include #include //#include #include #include #include namespace smacc2 { using namespace smacc2::introspection; #define THIS_STATE_NAME ((demangleSymbol(typeid(*this).name()).c_str())) template std::shared_ptr ISmaccState::configure(Args &&... args) { std::string orthogonalkey = demangledTypeName(); RCLCPP_INFO( getLogger(), "[%s] Configuring orthogonal: %s", THIS_STATE_NAME, orthogonalkey.c_str()); TOrthogonal * orthogonal = this->getOrthogonal(); if (orthogonal != nullptr) { auto clientBehavior = std::shared_ptr(new TBehavior(args...)); // is there an error here? are the // behavior constructor parameters right? orthogonal->addClientBehavior(clientBehavior); clientBehavior->template onOrthogonalAllocation(); return clientBehavior; } else { RCLCPP_ERROR( getLogger(), "[%s] Skipping client behavior creation in orthogonal [%s]. It does not exist.", THIS_STATE_NAME, orthogonalkey.c_str()); return nullptr; } } //------------------------------------------------------------------------------------------------------- template void ISmaccState::requiresComponent(SmaccComponentType *& storage) { this->getStateMachine().requiresComponent(storage); } //------------------------------------------------------------------------------------------------------- template void ISmaccState::requiresClient(SmaccClientType *& storage) { const char * sname = (demangleSymbol(typeid(*this).name()).c_str()); storage = nullptr; auto & orthogonals = this->getStateMachine().getOrthogonals(); for (auto & ortho : orthogonals) { ortho.second->requiresClient(storage); if (storage != nullptr) return; } RCLCPP_ERROR( getLogger(), "[%s] Client of type '%s' not found in any orthogonal of the current state machine. This may " "produce a segmentation fault if the returned reference is used.", sname, demangleSymbol().c_str()); } //------------------------------------------------------------------------------------------------------- template bool ISmaccState::getGlobalSMData(std::string name, T & ret) { return this->getStateMachine().getGlobalSMData(name, ret); } //------------------------------------------------------------------------------------------------------- // Store globally in this state machine. (By value parameter ) template void ISmaccState::setGlobalSMData(std::string name, T value) { this->getStateMachine().setGlobalSMData(name, value); } //------------------------------------------------------------------------------------------------------- template std::shared_ptr ISmaccState::createStateReactor(TEvArgs... args) { auto sr = std::make_shared(args...); // sb->initialize(this, mock); // sb->setOutputEvent(typelist()); stateReactors_.push_back(sr); return sr; } template std::shared_ptr ISmaccState::createEventGenerator(TEvArgs... args) { auto eg = std::make_shared(args...); eventGenerators_.push_back(eg); return eg; } // used to iterate on the source events list and fill the information of the stateReactorInfo structure template struct AddTEventTypeStateReactorInfo { smacc2::SmaccStateReactorInfo * srInfo_; AddTEventTypeStateReactorInfo(smacc2::SmaccStateReactorInfo * srInfo) : srInfo_(srInfo) {} template void operator()(T) { auto evinfo = std::make_shared(TypeInfo::getTypeInfoFromType()); srInfo_->sourceEventTypes.push_back(evinfo); EventLabel(evinfo->label); } }; // used to iterate on the source events list and fill the information of the stateReactorInfo structure // (is it required alreadyy having the AddTEventTypeStateReactorInfo?) template struct AddTEventTypeStateReactor { smacc2::StateReactor * sr_; AddTEventTypeStateReactor(smacc2::StateReactor * sr) : sr_(sr) {} template void operator()(T) { sr_->addInputEvent(); } }; template std::shared_ptr ISmaccState::createStateReactor(TEvArgs... args) { auto sr = std::make_shared(args...); sr->initialize(this); sr->template setOutputEvent(); using boost::mpl::_1; using wrappedList = typename boost::mpl::transform::type; AddTEventTypeStateReactor op(sr.get()); boost::mpl::for_each(op); stateReactors_.push_back(sr); return sr; } template TOrthogonal * ISmaccState::getOrthogonal() { return this->getStateMachine().getOrthogonal(); } template TClientBehavior * ISmaccState::getClientBehavior(int index) { return this->getStateMachine().getClientBehavior(index); } template TEventGenerator * ISmaccState::getEventGenerator() { TEventGenerator * ret = nullptr; for (auto & evg : this->eventGenerators_) { ret = dynamic_cast(evg.get()); if (ret != nullptr) break; } return ret; } template TStateReactor * ISmaccState::getStateReactor() { TStateReactor * ret = nullptr; for (auto & sr : this->eventGenerators_) { ret = dynamic_cast(sr.get()); if (ret != nullptr) break; } return ret; } // template // std::shared_ptr ISmaccState::createStateReactor(TEvArgs... args) // { // auto sb = std::make_shared(std::forward(args...)); // sb->initialize(this, typelist()); // sb->setOutputEvent(typelist()); // stateReactors_.push_back(sb); // return sb; // } //------------------------------------------------------------------------------------------------------- template void ISmaccState::postEvent(const EventType & ev) { getStateMachine().postEvent(ev); } template void ISmaccState::postEvent() { getStateMachine().postEvent(); } //------------------------------------------------------------------------------------------------------- template void ISmaccState::notifyTransition() { auto transitionType = TypeInfo::getTypeInfoFromType(); this->notifyTransitionFromTransitionTypeInfo(transitionType); } //------------------------------------------------------------------------------------------------------------------- } // namespace smacc2 // implementation depends on state definition #include #include