symbianunittestfw/sutfw/sutfwexamples/sutfwkernelexample/src/racecar.cpp
branchRCL_3
changeset 3 9397a16b6eb8
parent 1 6edeef394eb7
equal deleted inserted replaced
1:6edeef394eb7 3:9397a16b6eb8
     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 
       
    18 #include "racecar.h"
       
    19 
       
    20 // -----------------------------------------------------------------------------
       
    21 //
       
    22 // -----------------------------------------------------------------------------
       
    23 //
       
    24 DRaceCar::DRaceCar( const TDesC8& aColor, TUint aFuelTankSize )
       
    25  : iFuelTankSize( aFuelTankSize )
       
    26     {
       
    27     iColor = HBuf8::New(aColor);
       
    28     }
       
    29 
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 DRaceCar::~DRaceCar()
       
    36     {
       
    37     delete iColor;
       
    38     } 
       
    39     
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 TUint DRaceCar::FuelTankSize() const
       
    45     {
       
    46     return iFuelTankSize;
       
    47     }
       
    48     
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 // -----------------------------------------------------------------------------
       
    52 //    
       
    53 TUint DRaceCar::FuelLeft() const
       
    54     {
       
    55     return iFuel;
       
    56     }
       
    57     
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 TUint DRaceCar::AddFuel( TUint aFuel )
       
    63     {
       
    64     TUint extraFuel = 0;
       
    65     if ( aFuel > iFuelTankSize - iFuel )
       
    66         {
       
    67         extraFuel = aFuel- ( iFuelTankSize - iFuel );
       
    68         iFuel = iFuelTankSize;
       
    69         }
       
    70     else
       
    71         {
       
    72         iFuel += aFuel;
       
    73         }
       
    74     return extraFuel;
       
    75     }
       
    76     
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 // -----------------------------------------------------------------------------
       
    80 //    
       
    81 TInt DRaceCar::Steer( TInt aAngle )
       
    82     {
       
    83     if ( ( iDirection + aAngle ) < -KMaxTyreAngle || 
       
    84          ( iDirection + aAngle ) > KMaxTyreAngle )
       
    85         {
       
    86         return KErrArgument;
       
    87         }
       
    88     iDirection += aAngle;
       
    89     return KErrNone;
       
    90     }    
       
    91     
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 TInt DRaceCar::Direction() const
       
    97     {
       
    98     return iDirection;
       
    99     }
       
   100     
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 TInt DRaceCar::Accelerate( TUint aForce )
       
   106     {
       
   107     if ( iFuel == 0 )
       
   108         {
       
   109         return KErrGeneral;
       
   110         }
       
   111     else
       
   112         {
       
   113         iFuel--;
       
   114         iSpeed += aForce;
       
   115         }
       
   116     return KErrNone;
       
   117     }
       
   118     
       
   119 // -----------------------------------------------------------------------------
       
   120 //
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 TUint DRaceCar::Speed() const
       
   124     {
       
   125     return iSpeed;
       
   126     }
       
   127     
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 void DRaceCar::Brake( TUint aForce )
       
   133     {
       
   134     if ( iSpeed < aForce )
       
   135         {
       
   136         iSpeed = 0;
       
   137         }
       
   138     else
       
   139         {
       
   140         iSpeed -= aForce;
       
   141         }
       
   142     }
       
   143 
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 const TDesC8& DRaceCar::Color() const
       
   149     {
       
   150     return *iColor;
       
   151     }
       
   152     
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 void DRaceCar::ChangeColorL( const TDesC8& aNewColor )
       
   158     {
       
   159     HBuf* tmpColor = HBuf8::New(aNewColor);
       
   160     delete iColor;
       
   161     iColor = tmpColor;
       
   162     }