camerauis/cameraxui/cxengine/src/cxestatemachinebase.cpp
changeset 19 d9aefe59d544
child 21 fa6d9f75d6a6
equal deleted inserted replaced
3:8b2d6d0384b0 19:d9aefe59d544
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  */
       
    17 #include "cxestatemachinebase.h"
       
    18 #include "cxutils.h"
       
    19 #include "cxeerrormappingsymbian.h"
       
    20 #include "cxestate.h"
       
    21 
       
    22 CxeStateMachineBase::CxeStateMachineBase( const char* stateMachineName ) :
       
    23     mStateId(0), mName(stateMachineName)
       
    24 {
       
    25 }
       
    26 
       
    27 CxeStateMachineBase::~CxeStateMachineBase()
       
    28 {
       
    29     qDeleteAll(mStates);
       
    30     mStates.clear();
       
    31 }
       
    32 
       
    33 bool CxeStateMachineBase::addState( CxeState* state )
       
    34 {
       
    35     bool success = true;
       
    36 
       
    37     if (!state) {
       
    38         // given parameter is null pointer
       
    39         success = false;
       
    40     } else {
       
    41         // Make sure any of the existing stateId don't have overlapping bits.
       
    42         // We need to be able to use bitwise AND operator for stateIds.
       
    43         QList<int> stateIds = mStates.keys();
       
    44         foreach(int i, stateIds) {
       
    45             if ((i & state->stateId()) != 0) {
       
    46                 success = false;
       
    47                 break;
       
    48             }
       
    49         }
       
    50     }
       
    51 
       
    52     // if no errors
       
    53     if (success) {
       
    54         mStates[state->stateId()] = state; // state is now owned by the state machine
       
    55     }
       
    56 
       
    57     return success;
       
    58 }
       
    59 
       
    60 bool CxeStateMachineBase::setState( int stateId, int error )
       
    61 {
       
    62     bool success = true;
       
    63 
       
    64     if (stateId == mStateId) {
       
    65         // No transition, nothing needs to be done
       
    66         return success;
       
    67     }
       
    68 
       
    69     if (mStates.contains(stateId)) {
       
    70         // New state name known, print it
       
    71         CX_DEBUG(("%s::setState(%s, %d)", mName.toAscii().constData(),
       
    72                 mStates[stateId]->name().toAscii().constData(), error));
       
    73     } else {
       
    74         // Unknown state...
       
    75         CX_DEBUG(("%s::setState(UNKNOWN[%d], %d)", mName.toAscii().constData(),
       
    76                 stateId, error));
       
    77     }
       
    78 
       
    79     // State state machine does not use the error parameter itself.
       
    80     // It is only forwarded to handleStateChanged to be passed
       
    81     // on as a parameter in the stateChanged() signal.
       
    82 
       
    83     // setStateId itself will never return an error.
       
    84     // If an illegal state or state change is detected, it will panic the application (because it is a design error).
       
    85 
       
    86     success = verifyStateChange(stateId);
       
    87 
       
    88     if (success) {
       
    89         mStateId = stateId;
       
    90         handleStateChanged(mStateId, CxeErrorHandlingSymbian::map(error));
       
    91     }
       
    92 
       
    93     return success;
       
    94 }
       
    95 
       
    96 bool CxeStateMachineBase::setInitialState( int stateId )
       
    97 {
       
    98     bool success = mStates.contains(stateId);
       
    99 
       
   100     if (success) {
       
   101         // New state name known, print it
       
   102         CX_DEBUG(("%s::setInitialState(%s)", mName.toAscii().constData(),
       
   103                 mStates[stateId]->name().toAscii().constData()));
       
   104     } else {
       
   105         // Unknown state...
       
   106         CX_DEBUG(("%s::setInitialState(UNKNOWN[%d])", mName.toAscii().constData(),
       
   107                 stateId));
       
   108     }
       
   109 
       
   110     // check that state changes have not been made
       
   111     // and this is the one and only setInitialState call
       
   112     success = (success && mStateId == 0);
       
   113 
       
   114     if (success) {
       
   115         mStateId = stateId;
       
   116     } else {
       
   117         CX_DEBUG(("%s state has already been set", mName.toAscii().constData()));
       
   118     }
       
   119     return success;
       
   120 }
       
   121 
       
   122 int CxeStateMachineBase::stateId() const
       
   123 {
       
   124     return mStateId;
       
   125 }
       
   126 
       
   127 bool CxeStateMachineBase::verifyStateChange( int newStateId )
       
   128 {
       
   129     bool allowStateChange = false;
       
   130 
       
   131     // Check that current state can be found in the state chart
       
   132     if ( mStates.contains(stateId()) ) {
       
   133         // Check that current state allows change to the new state
       
   134         CxeState *currentState = mStates[stateId()];
       
   135         allowStateChange = (currentState->allowedNextStates() & newStateId);
       
   136     }
       
   137 
       
   138     return allowStateChange;
       
   139 }
       
   140 
       
   141 
       
   142 
       
   143