datacommsserver/esockserver/inc/ss_refcountowner.h
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 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  @file
       
    18  @Internal
       
    19  @released
       
    20 WARNING: For internal use ONLY. Compatibility is not guaranteed in future releases.
       
    21 */
       
    22 
       
    23 #ifndef __SS_REFCOUNTOWNER_H__
       
    24 #define __SS_REFCOUNTOWNER_H__
       
    25 
       
    26 #include <e32base.h>
       
    27 #include <elements/nm_node.h>
       
    28 #include <comms-infras/es_event.h>
       
    29 
       
    30 
       
    31 class CParameterBundleBase;
       
    32 namespace ESock
       
    33 {
       
    34 
       
    35 class CRefCountOwnerBase : public CBase, protected Messages::ASimpleNodeIdBase
       
    36 /**
       
    37 Base container class useful for passing reference counted objects around the stack.
       
    38 When a node receives an object based on this, it calls Open to increment the access
       
    39 counter and ensure the object does go away while it's being used. When the node is
       
    40 finished with the object it calls Close on it. The object will then delete itself if
       
    41 it's access counter is zero.
       
    42 
       
    43 Objects of this type will delete themselves asynchronously.
       
    44 WARNING: For internal use ONLY. Compatibility is not guaranteed in future releases.
       
    45 */
       
    46     {
       
    47 public:
       
    48 	/**
       
    49 	Relinquish access to the object. Decrements and access counter and may trigger deletion of the point and of the object
       
    50 	*/
       
    51     IMPORT_C void Close();
       
    52 	/**
       
    53 	Gain access to the object. Increments the access counter
       
    54 	*/
       
    55     IMPORT_C void Open();
       
    56 
       
    57 protected:
       
    58 	/**	
       
    59 	@param aPtr pointer to the real object to be contained
       
    60 	 */
       
    61     IMPORT_C CRefCountOwnerBase(const TAny* aPtr);
       
    62 
       
    63     IMPORT_C ~CRefCountOwnerBase();
       
    64 
       
    65 	/**
       
    66 	Receive a message. CRefCountOwnerBase objects can only receive TCFDataClient::TStop, which will trigger deletion of the ptr and deletion of the object
       
    67 	*/
       
    68    	IMPORT_C virtual void ReceivedL(const Messages::TRuntimeCtxId& aSender, const Messages::TNodeId& aRecipient, Messages::TSignatureBase& aMessage);
       
    69 
       
    70     /**
       
    71 	Delete the object pointed to by iPtr. Derived classes must implement this.
       
    72 	 */
       
    73 	virtual void DeletePtr() = 0;
       
    74 
       
    75 protected:
       
    76 	/**
       
    77 	@param aPtr CObject to delete
       
    78 	*/
       
    79     static void DeletePtr(CObject* aPtr)
       
    80         {
       
    81         aPtr->Close();
       
    82         }
       
    83 
       
    84 	/**
       
    85 	@param aPtr CBase to delete
       
    86 	*/
       
    87     static void DeletePtr(const CBase* aPtr)
       
    88         {
       
    89         delete aPtr;
       
    90         }
       
    91 
       
    92 	/**
       
    93 	@param aPtr Pointer to a Meta::SMetaData to delete
       
    94 	*/
       
    95     static void DeletePtr(const Meta::SMetaData* aPtr)
       
    96         {
       
    97         delete aPtr;
       
    98         }
       
    99 
       
   100 protected:
       
   101 	/**
       
   102 	Pointer to the contained object
       
   103 	*/
       
   104     TAny*       iPtr;
       
   105 
       
   106 	/**
       
   107 	Sanity checking member. Set to true after first open. Prevents dead objects being used.
       
   108 	Only used in udeb, but not conditional to avoid any urel/udeb BC issues
       
   109 	*/
       
   110     TBool		iRefOpened; 
       
   111 
       
   112 private:
       
   113     TInt        iRefCount;
       
   114     };
       
   115 
       
   116 template <class T>
       
   117 class CRefCountOwner : public CRefCountOwnerBase
       
   118 /*
       
   119 Use this class as a wrapper for a pointer that you need to send
       
   120 (with a TCFMessage) and give away the ownership of (as with
       
   121 subconnection events). This class solves two problems:
       
   122 (1) having multiple recipients for the message (no recipient
       
   123     can determine whether it should destroy the pointer).
       
   124 (2) having recipients in a different thread than the
       
   125     sender. One mustn't dealocate memory alocated in a different
       
   126     thread.
       
   127 WARNING: For internal use ONLY. Compatibility is not guaranteed in future releases.
       
   128 */
       
   129     {
       
   130 public:
       
   131 	/**
       
   132 	@param Pointer to object of type T
       
   133 	*/
       
   134     CRefCountOwner(T* aPtr)
       
   135         : CRefCountOwnerBase(aPtr)
       
   136         {
       
   137         }
       
   138 
       
   139 public:
       
   140 	/**
       
   141 	@return pointer to the contained object, or NULL if it hasn't been set
       
   142 	*/
       
   143     T* Ptr()
       
   144         {
       
   145         return static_cast<T*>(iPtr);
       
   146         }
       
   147 
       
   148 	/**
       
   149 	@return pointer to the contained object
       
   150 	@leave KErrNotReady if the pointer hasn't been set
       
   151 	*/
       
   152     T* PtrL()
       
   153         {
       
   154         if(iPtr==NULL)
       
   155 	        {
       
   156 	        User::Leave(KErrNotReady);
       
   157 			}
       
   158         return static_cast<T*>(iPtr);
       
   159         }
       
   160 
       
   161 protected:
       
   162 	/**
       
   163 	Delete the pointer. 
       
   164 	 */
       
   165     virtual void DeletePtr()
       
   166         {
       
   167         CRefCountOwnerBase::DeletePtr(Ptr());
       
   168         }
       
   169     };
       
   170 
       
   171 
       
   172 /**
       
   173 Typed version of the CRefCountOwner specifically for posting sub connection notification events
       
   174 WARNING: For internal use ONLY. Compatibility is not guaranteed in future releases.
       
   175 */
       
   176 typedef CRefCountOwner<const CSubConNotificationEvent> CRefCountOwnedSubConNotification;
       
   177 
       
   178 typedef CRefCountOwner<const ESock::XPlaneEvent> CRefCountOwnedNotification;
       
   179 	
       
   180 /**
       
   181 Typed version of the CRefCountOwner specifically for posting parameter bundles
       
   182 WARNING: For internal use ONLY. Compatibility is not guaranteed in future releases.
       
   183 */
       
   184 typedef CRefCountOwner<const CParameterBundleBase> CRefCountOwnedParameterBundle;
       
   185 
       
   186 
       
   187 }
       
   188 
       
   189 
       
   190 
       
   191 #endif // __SS_REFCOUNTOWNER_H__
       
   192