os/security/cryptoservices/filebasedcertificateandkeystores/source/generic/server/FSRunPackage.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2005-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 the License "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 
    19 
    20 
    21 /**
    22  @file
    23  @internalTechnology
    24 */
    25 
    26 #ifndef _FSRUNPACKAGE_H
    27 #define _FSRUNPACKAGE_H
    28 
    29 #include <e32base.h>
    30 
    31 // Disable WINS warning for symbol being too long (especially for template 
    32 // instantiations)
    33 #ifdef __WINS__
    34 #pragma warning(disable : 4786)
    35 #endif
    36 
    37 // The CRunPackage class allows a CActive derived object to "callback"
    38 // a member function with given parameters when the RunL function is called.
    39 //
    40 // It packages up the function call and the parameters into an object.
    41 // This saves you on having to have all these different member variables
    42 // to store parameters in an async call.
    43 //
    44 // The way this works is to create a member CRunPackage contained in
    45 // your class. Your RunL would simply be
    46 //
    47 // if (iRunPackage)
    48 //     {
    49 //     iRunPackage->ExecuteL();
    50 //     }
    51 //
    52 // And you set your iRunPackage by doing
    53 // iRunPackage = CRunPackage2<....>(this, function, param1, param2);
    54 //
    55 // In addition, CRunPackage encapsulates transient variables allocated on
    56 // the heap which must be destroyed when the CRunPackage object is destroyed.
    57 //
    58 // These objects can be added by calling the AddDeleteOnly, AddCloseDelete,
    59 // and AddReleaseOnly methods. When CRunPackage is destroyed, these objects
    60 // will be deleted.
    61 class CRunPackage : public CBase
    62 	{
    63 private:
    64 	// Private classes derived from CBase whose destructors release transient
    65 	// resources when the RunPackage is destroyed
    66 	template <class T>
    67 	class CDeleteOnly : public CBase
    68 		{
    69 	public:
    70 		CDeleteOnly(T* aPtr) : iPtr(aPtr) {}
    71 		~CDeleteOnly() { delete iPtr; }
    72 
    73 	private:
    74 		T* iPtr;
    75 		};
    76 
    77 	// Close and delete an object - this is required for pointers to R classes
    78 	template <class T>
    79 	class CCloseDelete : public CBase
    80 		{
    81 	public:
    82 		CCloseDelete(T* aPtr) : iPtr(aPtr) {}
    83 		~CCloseDelete() 
    84 			{
    85 			iPtr->Close();
    86 			delete iPtr; 
    87 			}
    88 
    89 	private:
    90 		T* iPtr;
    91 		};
    92 
    93 	// Release an object
    94 	template <class T>
    95 	class CReleaseOnly : public CBase
    96 		{
    97 	public:
    98 		CReleaseOnly(T* aPtr) : iPtr(aPtr) {}
    99 		~CReleaseOnly() { iPtr->Release(); }
   100 
   101 	private:
   102 		T* iPtr;
   103 		};
   104 
   105 public:
   106 	// when the destructor is invoked, if there is an iDeleteAction, its
   107 	// destructor will be invoked 
   108 	virtual ~CRunPackage();
   109 	virtual void ExecuteL() = 0;
   110 
   111 	// Operations to add delete operations
   112 	template <class T>
   113 	TInt AddDeleteOnly(T* aPtr)
   114 		{
   115 		return AddDeleteAction(new CDeleteOnly<T>(aPtr));
   116 		}
   117 
   118 	// adds a pointer which is to be closed and deleted
   119 	template <class T>
   120 	TInt AddCloseDelete(T* aPtr)
   121 		{
   122 		return AddDeleteAction(new CCloseDelete<T>(aPtr));
   123 		}
   124 
   125 	// adds a pointer which is to be released
   126 	template <class T>
   127 	TInt AddReleaseOnly(T* aPtr)
   128 		{
   129 		return AddDeleteAction(new CReleaseOnly<T>(aPtr));
   130 		}
   131 
   132 private:
   133 	// Adds a deleted action
   134 	TInt AddDeleteAction(CBase* aAction);
   135 
   136 private:
   137 	RPointerArray<CBase> iDeleteActions;
   138 	};
   139 
   140 // Implementation of RunPackage where callback takes no parameters
   141 template <class T>
   142 class CRunPackage0 : public CRunPackage
   143 	{
   144 public:
   145 	typedef void (T::*TRunFn)();
   146 
   147 	CRunPackage0(T& aParent, TRunFn aRunFn)
   148 		: iParent(aParent), iRunFn(aRunFn) {}
   149 
   150 	void ExecuteL() 
   151 		{ 
   152 		(iParent.*iRunFn)(); 
   153 		}
   154 
   155 private:
   156 	T& iParent;
   157 	TRunFn iRunFn;
   158 	};
   159 
   160 
   161 // implementation which takes one parameter
   162 template <class T, class P1>
   163 class CRunPackage1 : public CRunPackage
   164 	{
   165 public:
   166 	typedef void (T::*TRunFn)(P1);
   167 
   168 	CRunPackage1(T& aParent, TRunFn aRunFn, P1 aP1)
   169 		: iParent(aParent), iRunFn(aRunFn), iP1(aP1) {}
   170 
   171 	void ExecuteL() 
   172 		{ 
   173 		(iParent.*iRunFn)(iP1); 
   174 		}
   175 
   176 private:
   177 	T& iParent;
   178 	TRunFn iRunFn;
   179 	P1 iP1;
   180 	};
   181 
   182 // implementation which takes two parameters
   183 template <class T, class P1, class P2>
   184 class CRunPackage2 : public CRunPackage
   185 	{
   186 public:
   187 	typedef void (T::*TRunFn)(P1, P2);
   188 
   189 	CRunPackage2(T& aParent, TRunFn aRunFn, P1 aP1, P2 aP2)
   190 		: iParent(aParent), iRunFn(aRunFn), iP1(aP1), iP2(aP2) {}
   191 
   192 	void ExecuteL() 
   193 		{ 
   194 		(iParent.*iRunFn)(iP1, iP2); 
   195 		}
   196 
   197 private:
   198 	T& iParent;
   199 	TRunFn iRunFn;
   200 	P1 iP1;
   201 	P2 iP2;
   202 	};
   203 
   204 // implementation which takes three parameters
   205 template <class T, class P1, class P2, class P3>
   206 class CRunPackage3 : public CRunPackage
   207 	{
   208 public:
   209 	typedef void (T::*TRunFn)(P1, P2, P3);
   210 
   211 	CRunPackage3(T& aParent, TRunFn aRunFn, P1 aP1, P2 aP2, P3 aP3)
   212 		: iParent(aParent), iRunFn(aRunFn), iP1(aP1), iP2(aP2), iP3(aP3) {}
   213 
   214 	void ExecuteL() 
   215 		{ 
   216 		(iParent.*iRunFn)(iP1, iP2, iP3); 
   217 		}
   218 
   219 private:
   220 	T& iParent;
   221 	TRunFn iRunFn;
   222 	P1 iP1;
   223 	P2 iP2;
   224 	P3 iP3;
   225 	};
   226 
   227 // implementation which takes four parameters
   228 template <class T, class P1, class P2, class P3, class P4>
   229 class CRunPackage4 : public CRunPackage
   230 	{
   231 public:
   232 	typedef void (T::*TRunFn)(P1, P2, P3, P4);
   233 
   234 	CRunPackage4(T& aParent, TRunFn aRunFn, P1 aP1, P2 aP2, P3 aP3, P4 aP4)
   235 		: iParent(aParent), iRunFn(aRunFn), iP1(aP1), iP2(aP2), iP3(aP3), iP4(aP4) {}
   236 
   237 	void ExecuteL() 
   238 		{ 
   239 		(iParent.*iRunFn)(iP1, iP2, iP3, iP4);
   240 		}
   241 
   242 private:
   243 	T& iParent;
   244 	TRunFn iRunFn;
   245 	P1 iP1;
   246 	P2 iP2;
   247 	P3 iP3;
   248 	P4 iP4;
   249 	};
   250 
   251 // implementation which takes four parameters
   252 template <class T, class P1, class P2, class P3, class P4, class P5>
   253 class CRunPackage5 : public CRunPackage
   254 	{
   255 public:
   256 	typedef void (T::*TRunFn)(P1, P2, P3, P4, P5);
   257 
   258 	CRunPackage5(T& aParent, TRunFn aRunFn, P1 aP1, P2 aP2, P3 aP3, P4 aP4, P5 aP5)
   259 		: iParent(aParent), iRunFn(aRunFn), iP1(aP1), iP2(aP2), iP3(aP3), 
   260 		  iP4(aP4), iP5(aP5) {}
   261 
   262 	void ExecuteL() 
   263 		{ 
   264 		(iParent.*iRunFn)(iP1, iP2, iP3, iP4, iP5);
   265 		}
   266 
   267 private:
   268 	T& iParent;
   269 	TRunFn iRunFn;
   270 	P1 iP1;
   271 	P2 iP2;
   272 	P3 iP3;
   273 	P4 iP4;
   274 	P5 iP5;
   275 	};
   276 
   277 // implementation which takes four parameters
   278 template <class T, class P1, class P2, class P3, class P4, class P5, class P6>
   279 class CRunPackage6 : public CRunPackage
   280 	{
   281 public:
   282 	typedef void (T::*TRunFn)(P1, P2, P3, P4, P5, P6);
   283 
   284 	CRunPackage6(T& aParent, TRunFn aRunFn, P1 aP1, P2 aP2, P3 aP3, P4 aP4, P5 aP5, P6 aP6)
   285 		: iParent(aParent), iRunFn(aRunFn), iP1(aP1), iP2(aP2), iP3(aP3), 
   286 		  iP4(aP4), iP5(aP5), iP6(aP6) {}
   287 
   288 	void ExecuteL() 
   289 		{ 
   290 		(iParent.*iRunFn)(iP1, iP2, iP3, iP4, iP5, iP6);
   291 		}
   292 
   293 private:
   294 	T& iParent;
   295 	TRunFn iRunFn;
   296 	P1 iP1;
   297 	P2 iP2;
   298 	P3 iP3;
   299 	P4 iP4;
   300 	P5 iP5;
   301 	P6 iP6;
   302 	};
   303 
   304 
   305 #endif