datasourceadaptation/gpsdatasourceadaptation/src/EPos_CPositionerRegistry.cpp
changeset 36 b47902b73a93
parent 0 9cfd9a3ee49c
equal deleted inserted replaced
35:a2efdd544abf 36:b47902b73a93
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <e32base.h>
       
    20 #include "EPos_CPositionerRegistry.h"
       
    21 #include "EPos_MPosStatusObserver.h"
       
    22 #include "EPos_CPosPsyExtension.h"
       
    23 
       
    24 // ================= MEMBER FUNCTIONS =======================
       
    25 
       
    26 // C++ default constructor
       
    27 CPositionerRegistry::CPositionerRegistry(
       
    28     MPosStatusObserver* aStatusObserver)
       
    29 :   iStatusObserver(aStatusObserver)
       
    30     {
       
    31     }
       
    32     
       
    33 // Destructor
       
    34 CPositionerRegistry::~CPositionerRegistry()
       
    35     {
       
    36     iPsyExtensions.ResetAndDestroy();
       
    37     }
       
    38 
       
    39 // Static Singleton constructor.
       
    40 CPositionerRegistry* CPositionerRegistry::InstanceL(
       
    41     MPosStatusObserver* aStatusObserver)
       
    42     {
       
    43     CPositionerRegistry* self;
       
    44     TAny* tlsptr = Dll::Tls();
       
    45 
       
    46 	if (tlsptr)
       
    47 		{
       
    48         // An instance already exists.
       
    49         self = reinterpret_cast<CPositionerRegistry*> (Dll::Tls());
       
    50 		}
       
    51     else
       
    52         {
       
    53         // Instance does not exist. Create one.
       
    54         self = new (ELeave) CPositionerRegistry(aStatusObserver);
       
    55 		Dll::SetTls(reinterpret_cast<TAny*> (self));
       
    56         }
       
    57 
       
    58 	self->iRefCount++;
       
    59 	return self;
       
    60     }
       
    61 
       
    62 // Singleton destructor
       
    63 void CPositionerRegistry::Release()
       
    64     {
       
    65 	if (--iRefCount == 0)
       
    66 		{
       
    67         Dll::SetTls(NULL);
       
    68 		delete this;
       
    69 		}
       
    70     }
       
    71 
       
    72 // ---------------------------------------------------------
       
    73 // CPositionerRegistry::RegisterPositionerL
       
    74 //
       
    75 // (other items were commented in a header).
       
    76 // ---------------------------------------------------------
       
    77 //
       
    78 CPosPsyExtension* CPositionerRegistry::RegisterPositionerL(TUid aImplementationUid)
       
    79     {
       
    80     TInt pos = Find(aImplementationUid);
       
    81     CPosPsyExtension* psyExtension = NULL;
       
    82     if (pos == KErrNotFound)
       
    83         {
       
    84         psyExtension = new (ELeave) CPosPsyExtension(
       
    85             aImplementationUid, iStatusObserver);
       
    86         CleanupStack::PushL(psyExtension);
       
    87         User::LeaveIfError(iPsyExtensions.Append(psyExtension));
       
    88         CleanupStack::Pop(psyExtension);
       
    89         }
       
    90     else
       
    91         {
       
    92         psyExtension = iPsyExtensions[pos];
       
    93         }
       
    94     psyExtension->RegisterPositioner();
       
    95     return psyExtension;
       
    96     }
       
    97 
       
    98 // ---------------------------------------------------------
       
    99 // CPositionerRegistry::DeregisterPositioner
       
   100 //
       
   101 // (other items were commented in a header).
       
   102 // ---------------------------------------------------------
       
   103 //
       
   104 void CPositionerRegistry::DeregisterPositioner(TUid aImplementationUid)
       
   105     {
       
   106     TInt pos = Find(aImplementationUid);
       
   107     if (pos != KErrNotFound)
       
   108         {
       
   109         CPosPsyExtension* psyExtension = iPsyExtensions[pos];
       
   110         if (psyExtension->UnregisterPositioner() == 0)
       
   111             {
       
   112             // There are no more positioners using this PSY
       
   113             delete psyExtension;
       
   114             iPsyExtensions.Remove(pos);
       
   115             }
       
   116         }
       
   117     }
       
   118 
       
   119 // ---------------------------------------------------------
       
   120 // CPositionerRegistry::Find
       
   121 //
       
   122 // (other items were commented in a header).
       
   123 // ---------------------------------------------------------
       
   124 //
       
   125 TInt CPositionerRegistry::Find(TUid aImplementationUid)
       
   126     {
       
   127     TInt count = iPsyExtensions.Count();
       
   128     for (TInt i = 0; i < count; i++)
       
   129         {
       
   130         if (iPsyExtensions[i]->iImplementationUid == aImplementationUid)
       
   131             {
       
   132             return i;
       
   133             }
       
   134         }
       
   135     return KErrNotFound;
       
   136     }
       
   137 
       
   138 //  End of File