0

私はsignals2を使用しています。サブスクライブされたスロットを持つビューとビューステート/ビューの関係をセットアップしようとしています。ただし、ハンドラー関数をトリガーできないようです。バインディングに何か問題がありますか?私はC ++を初めて使用するので、const/reference/dereferencerの誤用がある可能性があります。

マイ ステート マシン:

void State::setAppState( State::AppState pNewState )
{
    mCurrentState = pNewState;
    // this prints fine
    ci::app::console() <<"\nState::setState " << pNewState << "\n";
    (*mSignal)();
}

私のビューの基本クラスでは:

BaseView::BaseView( State& pState ): mState(pState)
{
    // register connection to state
    mConnection = mState.connect(boost::bind(&BaseView::stateChange, this));
}

// the use of const is right from their example in the doc.
// but i found i had to const_cast to get it to compile
// can i get rid of the 'this' and do it without const method? 
// edit: no (boost compile errors)
void BaseView::stateChange()  const
{
    int s = const_cast<State&>(mState).getAppState();
    // this does not print
    ci::app::console() << "\n>>>>> View Slot registered change " << s << "\n" ;
}

私の見解では、サブクラス:

AttractView::AttractView(State pState):BaseView(pState)
{
     // to pass the constructor param
}

メインアプリ:

mAttract = AttractView( mStateMachine );
mStateMachine.setAppState(State::AppState::Attract); //AppState is just an enum
4

1 に答える 1

0

問題は実際には初期化リストにあったと思います。

pState は BasiView コンストラクターで有効でしたが、適切にバインドされなかったか、シグナルが初期化される前にバインドされてバインドが消去されました。

 BaseView::BaseView( State& pState ): mState(pState)

追加の init/create 関数を追加すると、これが解消されます。

void BaseView::init(State& pState){
    mConnection = mState.connect( (boost::bind(&BaseView::stateChange, this)) );
}

...

mAttract.init( mStateMachine );
mStateMachine.setAppState(State::AppState::Attract);
于 2013-09-10T23:04:46.400 に答える