void poll_inserted() {
	const auto card_present_now = sdcIsCardInserted(&SDCD1);
	if( card_present_now != card_present ) {
		card_present = card_present_now;

		Status new_status { card_present ? Status::Present : Status::NotPresent };

		if( card_present ) {
			if( sdcConnect(&SDCD1) == CH_SUCCESS ) {
				if( mount() == FR_OK ) {
					new_status = Status::Mounted;
				} else {
					new_status = Status::MountError;
				}
			} else {
				new_status = Status::ConnectError;
			}
		} else {
			sdcDisconnect(&SDCD1);
		}

		status_ = new_status;
		status_signal.emit(status_);
	}
}
// profile time for emission with 4 slots, 2 in group 0 and 2 in group 1
static void benchSignalEmissionGroups()
{
	Signal<void ( void*, uint64_t )> sigIncrement;
	sigIncrement.connect( testCounterAdd2 );
	sigIncrement.connect( testCounterAdd2 );
	sigIncrement.connect( 1, testCounterAdd2 );
	sigIncrement.connect( 1, testCounterAdd2 );

	const uint64_t startCounter = TestCounter::get();
	const uint64_t benchStart = timestampBenchmark();

	uint64_t i;
	for( i = 0; i < 1000000; i++ )
		sigIncrement.emit( nullptr, 1 );

	const uint64_t benchDone = timestampBenchmark();
	const uint64_t endCounter = TestCounter::get();

	assert( endCounter - startCounter == ( i * 4 ) );

	cout << "OK" << endl;
	cout << "\tper emission: " << double( benchDone - benchStart ) / double( i ) << "ns"
	<< ", per slot: " << double( benchDone - benchStart ) / double( i * 4 ) << "ns"
	<< endl;
}
Exemple #3
0
int main()
{  
    Signal<void(int)> signal;
    signal.connect(anyFunc);

    signal.connect(
          [] (int num) {
             // TODO: Implement handler logic here.
          }
    );
    signal.emit( 0 );
    
    return 0;
}
void on_tick_second() {
	signal_tick_second.emit();
}
Exemple #5
0
		int main(int argc, char **argv) {
			Signal<int(int)> sig;

			Test("disconnect non existing");
			{
				bool res;
				res = sig.disconnect(0);
				ASSERT(!res);
			}

			Test("connect");
			size_t id = sig.connect(Util::dummy);

			Test("single connection: check result");
			const int input = 100;
			auto numVal = sig.emit(input);
			auto val = sig.results();
			ASSERT(numVal == val->size());
			ASSERT(val->size() == 1);
			ASSERT((*val)[0] == Util::dummy(input));

			Test("multiple connections: check results");
			size_t id2 = sig.connect(dummy2);
			size_t id3 = sig.connect(Util::dummy);
			size_t id4 = sig.connect(dummy2);
			numVal = sig.emit(input);
			val = sig.results();
			ASSERT(numVal == val->size());
			ASSERT(val->size() == 4);
			ASSERT((*val)[0] == Util::dummy(input));
			ASSERT((*val)[1] == dummy2(input));
			ASSERT((*val)[2] == Util::dummy(input));
			ASSERT((*val)[3] == dummy2(input));

			Test("multiple connections: ask results again");
			auto val2 = sig.results();
			ASSERT(numVal == val2->size());
			ASSERT(val2->size() == 4);
			ASSERT((*val2)[0] == Util::dummy(input));
			ASSERT((*val2)[1] == dummy2(input));
			ASSERT((*val2)[2] == Util::dummy(input));
			ASSERT((*val2)[3] == dummy2(input));

			Test("disconnections");
			{
				bool res;
				res = sig.disconnect(id2);
				ASSERT(res);

				res = sig.disconnect(id);
				ASSERT(res);

				//disconnect again
				res = sig.disconnect(id);
				ASSERT(!res);

				//wrong id
				res = sig.disconnect(id + 1);
				ASSERT(!res);
			}

			return 0;
		}
Exemple #6
0
int main()
{
    Message::Ptr mm( new Message() );
    MsgA::Ptr ma( new MsgA() );
    MsgB::Ptr mb( new MsgB() );

    MyAction::Ptr action(new MyAction("myAction"));
    std::string op;

    cout << "Test Slot dynamic cast : ";

    op = "   Invoke action m_slotMsgM with mm";
    action->m_slotMsgM( mm );
    action->expectDynamicType( op, "Message" );
    action->expectStaticType( op, "Message" );

    op = "   Invoke action m_slotMsgM with ma";
    action->m_slotMsgM( ma );
    action->expectDynamicType( op, "MsgA" );
    action->expectStaticType( op, "Message" );

    op = "   Invoke action m_slotMsgM with mb";
    action->m_slotMsgM( mb );
    action->expectDynamicType( op, "MsgB" );
    action->expectStaticType( op, "Message" );

    op = "   Invoke action m_slotMsgA with mm";
    action->m_slotMsgA( mm );
    action->expectDynamicType( op, "" );
    action->expectStaticType( op, "" );

    op = "   Invoke action m_slotMsgA with ma";
    action->m_slotMsgA( ma );
    action->expectDynamicType( op, "MsgA" );
    action->expectStaticType( op, "MsgA" );

    op = "   Invoke action m_slotMsgA with mb";
    action->m_slotMsgA( mb );
    action->expectDynamicType( op, "MsgB" );
    action->expectStaticType( op, "MsgA" );

    op = "   Invoke action m_slotMsgB with mm";
    action->m_slotMsgB( mm );
    action->expectDynamicType( op, "" );
    action->expectStaticType( op, "" );

    op = "   Invoke action m_slotMsgB with ma";
    action->m_slotMsgB( ma );
    action->expectDynamicType( op, "" );
    action->expectStaticType( op, "" );

    op = "   Invoke action m_slotMsgB with mb";
    action->m_slotMsgB( mb );
    action->expectDynamicType( op, "MsgB" );
    action->expectStaticType( op, "MsgB" );
    cout << "Ok" << endl;


    cout << "Test Slot static cast  : ";

    op = "   Invoke action m_slotMsgM with mm";
    action->m_slotMsgM.getStaticCastFunction()( mm, nullptr );
    action->expectDynamicType( op, "Message" );
    action->expectStaticType( op, "Message" );

    op = "   Invoke action m_slotMsgM with ma";
    action->m_slotMsgM.getStaticCastFunction()( ma, nullptr );
    action->expectDynamicType( op, "MsgA" );
    action->expectStaticType( op, "Message" );

    op = "   Invoke action m_slotMsgM with mb";
    action->m_slotMsgM.getStaticCastFunction()( mb, nullptr );
    action->expectDynamicType( op, "MsgB" );
    action->expectStaticType( op, "Message" );

    //op = "   Invoke action m_slotMsgA with mm";
    //action->m_slotMsgA( mm ); is not a valid static cast

    op = "   Invoke action m_slotMsgA with ma";
    action->m_slotMsgA.getStaticCastFunction()( ma, nullptr );
    action->expectDynamicType( op, "MsgA" );
    action->expectStaticType( op, "MsgA" );

    op = "   Invoke action m_slotMsgA with mb";
    action->m_slotMsgA.getStaticCastFunction()( mb, nullptr );
    action->expectDynamicType( op, "MsgB" );
    action->expectStaticType( op, "MsgA" );

    //op = "   Invoke action m_slotMsgB with mm";
    //action->m_slotMsgB( mm ); is not a valid static cast

    //op = "   Invoke action m_slotMsgB with ma";
    //action->m_slotMsgB( ma ); is not a valid static cast

    op = "   Invoke action m_slotMsgB with mb";
    action->m_slotMsgB.getStaticCastFunction()( mb, nullptr );
    action->expectDynamicType( op, "MsgB" );
    action->expectStaticType( op, "MsgB" );
    cout << "Ok" << endl;


    // Note: Without links emission has no effect
    cout << "Test Signal type check : ";

    action->m_signalMsgM.emit( mm );
    action->m_signalMsgM.emit( ma );
    action->m_signalMsgM.emit( mb );

    // action->m_signalMsgA.emit( mm ); // Ok: static Error
    action->m_signalMsgA.emit( ma );
    action->m_signalMsgA.emit( mb );

    // action->m_signalMsgB.emit( mm ); // Ok: static Error
    // action->m_signalMsgB.emit( ma ); // Ok: static Error
    action->m_signalMsgB.emit( mb );

    cout << "Ok" << endl;

    cout << "Test connecting links  : ";

    try
    {
        if( Link::isConnected( "myAction::signalMsgM", "myAction::slotMsgM") )
        {
            cout << "Failed!" << endl;
            cout << "   Link myAction::signalMsgM -> myAction::slotMsgM exist." << endl;
            exit(1);
        }

        Link::connect( "myAction::signalMsgM", "myAction::slotMsgM");

        if( !Link::isConnected( "myAction::signalMsgM", "myAction::slotMsgM") )
        {
            cout << "Failed!" << endl;
            cout << "   Link myAction::signalMsgM -> myAction::slotMsgM not created." << endl;
            exit(1);
        }

        if( !Link::disconnect( "myAction::signalMsgM", "myAction::slotMsgM") )
        {
            cout << "Failed!" << endl;
            cout << "   Link myAction::signalMsgM -> myAction::slotMsgM not disconnected." << endl;
            exit(1);
        }

        if( Link::isConnected( "myAction::signalMsgM", "myAction::slotMsgM") )
        {
            cout << "Failed!" << endl;
            cout << "   Link myAction::signalMsgM -> myAction::slotMsgM exist." << endl;
            exit(1);
        }
        cout << "Ok" << endl;


        cout << "Test emit message      : ";

        Link::connect( "myAction::signalMsgM", "myAction::slotMsgM");
        op = "   Invoke action m_signalMsgM with mb";
        action->m_signalMsgM.emit( mb );
        while( Message::processNext() );
        action->expectDynamicType( op, "MsgB" );
        action->expectStaticType( op, "Message" );
        Link::disconnect( "myAction::signalMsgM", "myAction::slotMsgM");
        cout << "Ok" << endl;

        cout << "Test Action network    : ";

        Ping* ping = new Ping("Ping");
        Pong* pong = new Pong("Pong");
        Link::connect( "Ping::output", "Pong::input");
        Link::connect( "Pong::output", "Ping::input");

        if( !Link::isConnected("Ping::output", "Pong::input") )
        {
            cout << "Failed!" << endl;
            cout << "   Link Ping::output -> Pong::input not connected." << endl;
            exit(1);
        }
        if( !Link::isConnected("Pong::output", "Ping::input") )
        {
            cout << "Failed!" << endl;
            cout << "   Link Pong::output -> Ping::input not connected." << endl;
            exit(1);
        }

        Ball::Ptr ball( new Ball() );

        ping->start( ball, 15 );

        while( Message::processNext() );

        if( ball->pingCnt != ball->maxCount )
        {
            cout << "Failed!" << endl;
            cout << "   Ball ping counter is not " <<  ball->maxCount
                 << ". Found " << ball->pingCnt << endl;
            exit(1);
        }

        if( ball->pongCnt != ball->maxCount )
        {
            cout << "Failed!" << endl;
            cout << "   Ball pong counter is not " <<  ball->maxCount
                 <<". Found " << ball->pongCnt << endl;
            exit(1);
        }

        // Add a pong object instance duplicating the message ping transactions
        // Will double the ping pong transactions
        Pong* pong2 = new Pong("Pong2");
        Link::connect( "Ping::output", "Pong2::input");
        Link::connect( "Pong2::output", "Ping::input");

        ping->start( ball, 15 );
        while( Message::processNext() );

        if( ball->pingCnt != ball->maxCount )
        {
            cout << "Failed!" << endl;
            cout << "   Ball ping counter is not " <<  ball->maxCount
                 << ". Found " << ball->pingCnt << endl;
            exit(1);
        }

        if( ball->pongCnt != 2*ball->maxCount )
        {
            cout << "Failed!" << endl;
            cout << "   Ball pong counter is not " <<  2*ball->maxCount
                 <<". Found " << ball->pongCnt << endl;
            exit(1);
        }
        cout << "Ok" << endl;

        cout << "Test free functions    : ";

        SlotFunction<Ball,&catchBall> slotBall;
        Signal<Ball> signalBall;

        if( nbrBallCatched != 0 )
        {
            cout << "Failed!" << endl;
            cout << "   nbrBallCatched counter is not 0"
                 << ". Found " << nbrBallCatched << endl;
            exit(1);
        }

        // Establish a direct connecting between signal and slot
        Link::connect( &signalBall, &slotBall );

        while( Message::processNext() );
        signalBall.emit( ball );
        while( Message::processNext() );

        if( nbrBallCatched != 1 )
        {
            cout << "Failed!" << endl;
            cout << "   nbrBallCatched counter is not 1"
                 << ". Found " << nbrBallCatched << endl;
            exit(1);
        }

        Link::disconnect( &signalBall, &slotBall );

        // Retest using named Signal and slot
        slotBall.setName( "Ball slot" );
        signalBall.setName( "Ball signal" );
        Link::connect( "Ball signal", "Ball slot" );
        while( Message::processNext() );
        signalBall.emit( ball );
        while( Message::processNext() );
        if( nbrBallCatched != 2 )
        {
            cout << "Failed!" << endl;
            cout << "   nbrBallCatched counter is not 2"
                 << ". Found " << nbrBallCatched << endl;
            exit(1);
        }
        cout << "Ok" << endl;


        // destroy all objects
        Action::clearActions();
    }
    catch( std::exception& e )
    {
        cout << "Failed!" << endl;
        cout << "   Exception: " << e.what() << endl;
        exit(1);
    }
    catch( ... )
    {
        cout << "Failed!" << endl;
        cout << "   Unknown exception" << endl;
        exit(1);
    }


    {
        int nbr = 1000000;
        cout << "Timing for " << nbr << " MPO signal ping pong : ";
        cout.flush();
        class Timer {
            double m_start, m_stop;
        public:
            Timer() { start(); stop(); }
            double getTime()
            {
                struct timeval t;
                ::gettimeofday( &t, NULL );
                return double(t.tv_sec) + double(t.tv_usec)/1000000.;
            }

            void start() { m_start = getTime(); }
            void stop() { m_stop = getTime(); }
            double getDelta() { return m_stop - m_start; }
        } timer;

        Ping* ping = new Ping("Ping");
        Pong* pong = new Pong("Pong");
        Link::connect( "Ping::output", "Pong::input");
        Link::connect( "Pong::output", "Ping::input");

        Ball::Ptr ball( new Ball() );
        ping->start( ball, nbr );

        timer.start();
        while( Message::processNext() );
        timer.stop();

        cout << timer.getDelta() << " seconds " << endl;

    }




    return 0;
}