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