os/ossrv/genericopenlibs/openenvcore/ewsd/inc/pls.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/openenvcore/ewsd/inc/pls.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,120 @@
     1.4 +/*
     1.5 +* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:
    1.18 +* Name        : pls.h
    1.19 +* Part of     : Client API for ewsd library
    1.20 +* Contains the client API for using the emulator WSD library
    1.21 +*
    1.22 +*/
    1.23 +
    1.24 +
    1.25 + 
    1.26 +#ifndef __PLS_H__
    1.27 +#define __PLS_H__
    1.28 +
    1.29 +#ifdef __WINSCW__
    1.30 + 
    1.31 +#include <ewsd.h>
    1.32 +
    1.33 +// Panic strings
    1.34 +_LIT(KVirtualAllocFailure, "WSD VirtualAlloc() failed");
    1.35 +_LIT(KPLSInitFailed, "WSD PLS init failed");
    1.36 +_LIT(KWsdArrayFull, "WSD process or lib array full");	
    1.37 +
    1.38 +/**  
    1.39 +A templated function that is used by a library (DLL) that requires to use
    1.40 +WSD on the emulator.
    1.41 +The function returns the PLS (Process Local Storage) object of the specified library, 
    1.42 +for the current process. If the PLS object doesn't yet exist then it is allocated, 
    1.43 +initialised, stored and returned.
    1.44 +The template type T is the type of the PLS object, and is supplied by the caller.
    1.45 +
    1.46 +Takes as a parameter the TUid of the library DLL whose PLS is to be returned for the 
    1.47 +current process. It also takes as a parameter a pointer to a (non-leaving, non-panicing) 
    1.48 +initialisation function defined by the caller which takes a pointer to T (i.e. the 
    1.49 +PLS object) as a parameter and returns one of the system wide error codes as a TInt.
    1.50 +This parameter is optional but it should be used when necessary to ensure that if Pls() 
    1.51 +requires to create a PLS object then the object is completely initialised on its return. 
    1.52 +The initialisation function is called after the PLS object has been allocated and its 
    1.53 +constructor called if it is an instance of a class - neither the constructor nor the 
    1.54 +initialisation function should call Pls().
    1.55 +
    1.56 +Returns a pointer to the PLS object					
    1.57 +*/
    1.58 +template <typename T>
    1.59 +T* Pls(const TUid& aLibraryUid, TInt (*aInitFunc)(T*) = 0)
    1.60 +	{
    1.61 +	// Fetch the PLS, if it has been set
    1.62 +	T* p = (T*) CheckPls(aLibraryUid);
    1.63 +	if (p)
    1.64 +		{
    1.65 +		return p;
    1.66 +		}
    1.67 +	
    1.68 +	// Obtain ownership of the mutex
    1.69 +	TAny* mutexHandle = ObtainPlsMutex(); 
    1.70 +			
    1.71 +	// Check we haven't obtained the mutex from 
    1.72 +	// another thread that has just set the same PLS!
    1.73 +	p = (T*) CheckPls(aLibraryUid);
    1.74 +	if (p) 
    1.75 +		{
    1.76 +		ReleasePlsMutex(mutexHandle);				
    1.77 +		return p;
    1.78 +		}
    1.79 +	
    1.80 +	// Allocate the memory for the PLS object
    1.81 +	p = (T*) AllocatePls(sizeof(T));
    1.82 +	if (!p)
    1.83 +		{
    1.84 +		ReleasePlsMutex(mutexHandle);
    1.85 +		User::Panic(KVirtualAllocFailure, KErrNoMemory);
    1.86 +		}
    1.87 +			
    1.88 +	// Do a placement new to construct the PLS object in the allocated memory
    1.89 +	p = new (p) T;
    1.90 +	
    1.91 +	// Call the initialisation function (if one is provided)
    1.92 +	// to complete initialisation of the PLS object
    1.93 +	if (aInitFunc)
    1.94 +		{
    1.95 +		 if (((*aInitFunc)(p)) != KErrNone) 
    1.96 +		 	{
    1.97 +		 	 FreePls(p);			
    1.98 +			 ReleasePlsMutex(mutexHandle);
    1.99 +			 User::Panic(KPLSInitFailed, KErrGeneral);
   1.100 +		 	}
   1.101 +		}
   1.102 +		
   1.103 +	// Finally, call SetPls() to store the PLS object.
   1.104 +	// NOTE: This step is last to ensure that a PLS object returned by 
   1.105 +	// CheckPls() is completely constructed/initialised. This is important 
   1.106 +	// to handle the scenario in which the thread that is creating the PLS 
   1.107 +	// object is interrupted by another call to Pls() by another thread
   1.108 +	if (SetPls(p, aLibraryUid) != KErrNone)
   1.109 +		{
   1.110 +		// SetPls() failed due to a size limit being reached in the wsdArray
   1.111 +		FreePls(p);
   1.112 +		ReleasePlsMutex(mutexHandle);
   1.113 +		User::Panic(KWsdArrayFull, KErrNoMemory);				
   1.114 +		}
   1.115 +
   1.116 +	ReleasePlsMutex(mutexHandle);		
   1.117 +	return p;
   1.118 +	}
   1.119 +
   1.120 +#endif // __WINSCW__
   1.121 +
   1.122 +#endif // __PLS_H__
   1.123 +