datacommsserver/esockserver/ssock/ss_rmetaextensioncontainer.cpp
changeset 0 dfb7c4ff071f
child 73 5ebd530e523b
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     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 // ss_rmetaextensioncontainer.cpp 
       
    15 //
       
    16 
       
    17 
       
    18 #include "ss_rmetaextensioncontainer.h"
       
    19 #include "ss_cmetaextensioncontainer.h"
       
    20 
       
    21 using namespace ESock;
       
    22 
       
    23 _LIT(KMecPanic, "mecpanic");
       
    24 
       
    25 void PanicMec(TMecPanic aCode)
       
    26     {
       
    27     User::Panic(KMecPanic, aCode);
       
    28     }
       
    29 
       
    30 // ===========================================================
       
    31 // RMetaExtensionContainerC
       
    32 // ===========================================================
       
    33 
       
    34 EXPORT_C RMetaExtensionContainerC::RMetaExtensionContainerC()
       
    35     : iContainerImpl(NULL)
       
    36     {
       
    37     }
       
    38 
       
    39 EXPORT_C RMetaExtensionContainerC::~RMetaExtensionContainerC()
       
    40     {
       
    41     }
       
    42 
       
    43 EXPORT_C void RMetaExtensionContainerC::Open()
       
    44     {
       
    45     __ASSERT_ALWAYS(iContainerImpl, PanicMec(ENoImplementation));
       
    46     iContainerImpl->Open();
       
    47     }
       
    48 
       
    49 EXPORT_C void RMetaExtensionContainerC::Open(const RMetaExtensionContainerC& aSource)
       
    50     {
       
    51     __ASSERT_ALWAYS(&aSource != this, PanicMec(EInvalidSelfReference));
       
    52     __ASSERT_ALWAYS(aSource.Container(), PanicMec(ENoImplementation));
       
    53     __ASSERT_ALWAYS(!iContainerImpl, PanicMec(EAlreadyOpen));
       
    54     
       
    55     aSource.Container()->Open();
       
    56     iContainerImpl = aSource.Container();
       
    57     }
       
    58 
       
    59 EXPORT_C void RMetaExtensionContainerC::Close()
       
    60     {
       
    61     if (iContainerImpl)
       
    62         {
       
    63         iContainerImpl->Close();
       
    64         iContainerImpl = NULL;
       
    65         }
       
    66     }
       
    67 
       
    68 EXPORT_C const Meta::SMetaData* RMetaExtensionContainerC::FindExtension(const Meta::STypeId& aTypeId) const
       
    69     {
       
    70     __ASSERT_ALWAYS(iContainerImpl, PanicMec(ENoImplementation));
       
    71     return iContainerImpl->FindExtension(aTypeId);
       
    72     }
       
    73 
       
    74 
       
    75 EXPORT_C const Meta::SMetaData& RMetaExtensionContainerC::FindExtensionL(const Meta::STypeId& aTypeId) const
       
    76     {
       
    77     __ASSERT_ALWAYS(iContainerImpl, PanicMec(ENoImplementation));
       
    78     return iContainerImpl->FindExtensionL(aTypeId);
       
    79     }
       
    80 
       
    81 
       
    82 // ===========================================================
       
    83 // RMetaExtensionContainer
       
    84 // ===========================================================
       
    85 
       
    86 EXPORT_C RMetaExtensionContainer::RMetaExtensionContainer()
       
    87     : iIsLazyCreationDone(EFalse), iIsOpen(EFalse), iCurrentExtensionContainer(NULL)
       
    88     {
       
    89     }
       
    90 
       
    91 
       
    92 EXPORT_C void RMetaExtensionContainer::Open()
       
    93     {
       
    94     __ASSERT_ALWAYS(!iIsOpen, PanicMec(EAlreadyOpen));
       
    95     
       
    96     // Using lazy creation of the implementation so that Open() is infallible
       
    97     // This boolean ensures that Open() is still called before use of the container
       
    98     iIsOpen = ETrue;
       
    99     }
       
   100 
       
   101 
       
   102 EXPORT_C void RMetaExtensionContainer::Open(const RMetaExtensionContainerC& aBaseContainer)
       
   103     {
       
   104     __ASSERT_ALWAYS(!iIsOpen, PanicMec(EAlreadyOpen));
       
   105 	RMetaExtensionContainerC::Open(aBaseContainer);
       
   106     iIsOpen = ETrue;
       
   107     }
       
   108 
       
   109 
       
   110 EXPORT_C void RMetaExtensionContainer::Close()
       
   111     {
       
   112     if (iIsOpen)
       
   113         {
       
   114         if (AppendableExtensionContainer())
       
   115         	{
       
   116         	AppendableExtensionContainer()->Compact();
       
   117         	}
       
   118         iIsOpen = EFalse;
       
   119         }
       
   120     
       
   121     RMetaExtensionContainerC::Close();
       
   122     }
       
   123 
       
   124 
       
   125 void RMetaExtensionContainer::CreateExtensionContainerL()
       
   126     {
       
   127     // Don't call it unless it should be created
       
   128     __ASSERT_DEBUG(!AppendableExtensionContainer(), PanicMec(EIllegalState));
       
   129     
       
   130 	CMetaExtensionContainerImplBase* pendingBaseContainerImpl = NULL;
       
   131 	if (!iIsLazyCreationDone)
       
   132 		{
       
   133 		// Retrieve the pending base container implementation if there is one
       
   134 		pendingBaseContainerImpl = Container();
       
   135 		}
       
   136 	
       
   137     CMetaExtensionContainer* mecImpl = CMetaExtensionContainer::NewLC(pendingBaseContainerImpl);
       
   138     if (!iIsLazyCreationDone)
       
   139     	{
       
   140     	// If there is no proper container implementation this normal extension container
       
   141     	// will become it.
       
   142     	// Base class takes ownership of the new container implementation reference
       
   143     	SetContainer(mecImpl);
       
   144 
       
   145         if (pendingBaseContainerImpl)
       
   146             {
       
   147             // Remove the holding reference - the new container implementation has its own
       
   148             pendingBaseContainerImpl->Close();
       
   149             }
       
   150         
       
   151         iIsLazyCreationDone = ETrue;
       
   152         }
       
   153     else
       
   154     	{
       
   155     	// There is an existing container implementation. Given the fact that the
       
   156     	// CreateExtensionContainerL method has been called it must be of the
       
   157     	// container array type. This will own (the reference on) our new extension
       
   158     	// container.
       
   159     	__ASSERT_DEBUG(Container()->Type() == CMetaExtensionContainerImplBase::EMetaExtensionContainerArray, PanicMec(EIllegalState));
       
   160     	__ASSERT_DEBUG(!pendingBaseContainerImpl, PanicMec(EIllegalState));
       
   161     	User::LeaveIfError(AppendableContainerArray()->AppendContainer(*mecImpl));
       
   162     	}
       
   163     
       
   164 	__ASSERT_DEBUG(!iCurrentExtensionContainer, PanicMec(EIllegalState));
       
   165 	iCurrentExtensionContainer = mecImpl;
       
   166     CleanupStack::Pop(mecImpl);
       
   167     }
       
   168 
       
   169 
       
   170 void RMetaExtensionContainer::CreateContainerArrayL()
       
   171     {
       
   172     ASSERT(!AppendableContainerArray()); // Don't call it unless it should be created
       
   173     
       
   174 	CMetaExtensionContainerImplBase* pendingBaseContainerImpl = NULL;
       
   175 	if (!iIsLazyCreationDone)
       
   176 		{
       
   177 		// Retrieve the pending base container implementation if there is one
       
   178 		pendingBaseContainerImpl = Container();
       
   179 		}
       
   180 	
       
   181 	CMetaExtensionContainerArray* mecImpl = CMetaExtensionContainerArray::NewLC(pendingBaseContainerImpl);
       
   182     if (!iIsLazyCreationDone)
       
   183     	{
       
   184     	// If there is no proper container implementation this container array implementation
       
   185     	// will become it.
       
   186     	// Base class takes ownership of the new container implementation reference
       
   187     	SetContainer(mecImpl);
       
   188         
       
   189     	if (pendingBaseContainerImpl)
       
   190             {
       
   191             // Remove the holding reference - the new container implementation has its own
       
   192             pendingBaseContainerImpl->Close();
       
   193             }
       
   194     	
       
   195     	iIsLazyCreationDone = ETrue;
       
   196     	}
       
   197     else
       
   198     	{
       
   199     	// There is an existing container implementation. Given the fact that the
       
   200     	// CreateContainerArrayL method has been called it must be of the
       
   201     	// normal extension container type. This will be replaced with a container
       
   202     	// array implementation which will own (the reference on) the existing
       
   203     	// container.
       
   204     	__ASSERT_DEBUG(Container()->Type() == CMetaExtensionContainerImplBase::EMetaExtensionContainer, PanicMec(EIllegalState));
       
   205     	__ASSERT_DEBUG(!pendingBaseContainerImpl, PanicMec(EIllegalState));
       
   206     	__ASSERT_DEBUG(iCurrentExtensionContainer, PanicMec(EIllegalState));
       
   207     	User::LeaveIfError(mecImpl->AppendContainer(*iCurrentExtensionContainer));
       
   208 
       
   209     	// Close our reference on the extension container, the container array will open its own
       
   210     	iCurrentExtensionContainer->Close();
       
   211     	iCurrentExtensionContainer = NULL;
       
   212     	SetContainer(mecImpl);
       
   213     	}
       
   214 
       
   215     CleanupStack::Pop(mecImpl);
       
   216     }
       
   217 
       
   218 
       
   219 EXPORT_C const Meta::SMetaData* RMetaExtensionContainer::FindExtension(const Meta::STypeId& aTypeId) const
       
   220     {
       
   221     __ASSERT_ALWAYS(iIsOpen, PanicMec(ENoImplementation));
       
   222     if (Container())
       
   223     	{
       
   224     	return Container()->FindExtension(aTypeId);
       
   225     	}
       
   226     return NULL;
       
   227     }
       
   228 
       
   229 
       
   230 EXPORT_C const Meta::SMetaData& RMetaExtensionContainer::FindExtensionL(const Meta::STypeId& aTypeId) const
       
   231     {
       
   232     __ASSERT_ALWAYS(iIsOpen, PanicMec(ENoImplementation));
       
   233     if (!Container())
       
   234     	{
       
   235         User::Leave(KErrNotFound);
       
   236     	}
       
   237 	return Container()->FindExtensionL(aTypeId);
       
   238     }
       
   239 
       
   240 
       
   241 EXPORT_C TInt RMetaExtensionContainer::AppendExtension(const Meta::SMetaData* aExtension)
       
   242     {
       
   243     __ASSERT_ALWAYS(iIsOpen, PanicMec(ENoImplementation));
       
   244 
       
   245     if (!aExtension)
       
   246         {
       
   247         return KErrArgument;
       
   248         }
       
   249     
       
   250     CMetaExtensionContainer* mecImpl = AppendableExtensionContainer();
       
   251     if (!mecImpl)
       
   252         {
       
   253         // Do lazy creation
       
   254         TRAPD(err, CreateExtensionContainerL());
       
   255         if (err != KErrNone)
       
   256             {
       
   257             return err;
       
   258             }
       
   259         mecImpl = AppendableExtensionContainer();
       
   260         }
       
   261 
       
   262     return mecImpl->AppendExtension(aExtension);
       
   263     }
       
   264 
       
   265 
       
   266 EXPORT_C void RMetaExtensionContainer::AppendExtensionL(const Meta::SMetaData* aExtension)
       
   267     {
       
   268     __ASSERT_ALWAYS(iIsOpen, PanicMec(ENoImplementation));
       
   269 
       
   270     if (!aExtension)
       
   271         {
       
   272         User::Leave(KErrArgument);
       
   273         }
       
   274     
       
   275     CMetaExtensionContainer* mecImpl = AppendableExtensionContainer();
       
   276     if (!mecImpl)
       
   277         {
       
   278         // Do lazy creation
       
   279         CreateExtensionContainerL();
       
   280         mecImpl = AppendableExtensionContainer();
       
   281         }
       
   282 
       
   283     mecImpl->AppendExtensionL(aExtension);
       
   284     }
       
   285 
       
   286 
       
   287 EXPORT_C void RMetaExtensionContainer::AppendContainerL(const RMetaExtensionContainerC& aContainer)
       
   288 	{
       
   289     __ASSERT_ALWAYS(&aContainer != this, PanicMec(EInvalidSelfReference));
       
   290 	__ASSERT_ALWAYS(iIsOpen, PanicMec(ENoImplementation));
       
   291 	
       
   292 	CMetaExtensionContainerImplBase* containerImpl = static_cast<const RMetaExtensionContainer&>(aContainer).Container();
       
   293 	__ASSERT_ALWAYS(containerImpl, PanicMec(ENoImplementation));
       
   294 	__ASSERT_ALWAYS(containerImpl != Container(), PanicMec(EInvalidSelfReference));
       
   295 		
       
   296 	CMetaExtensionContainerArray* mecImpl = AppendableContainerArray();
       
   297 	if (!mecImpl)
       
   298 		{
       
   299 		// Do lazy creation
       
   300 		CreateContainerArrayL();
       
   301 		mecImpl = AppendableContainerArray();
       
   302 		}
       
   303 
       
   304     const RMetaExtensionContainer& containerIfc = static_cast<const RMetaExtensionContainer&>(aContainer);
       
   305 	User::LeaveIfError(mecImpl->AppendContainer(*containerIfc.Container()));
       
   306 	
       
   307 	// Null the current extension container pointer forcing a new extension container to be
       
   308 	// created and appended to the array after the one that has been added
       
   309 	iCurrentExtensionContainer = NULL;
       
   310 	}
       
   311 
       
   312 
       
   313 CMetaExtensionContainer* RMetaExtensionContainer::AppendableExtensionContainer() const
       
   314     {
       
   315 	return iIsLazyCreationDone ? iCurrentExtensionContainer : NULL;
       
   316     }
       
   317 
       
   318 
       
   319 CMetaExtensionContainerArray* RMetaExtensionContainer::AppendableContainerArray() const
       
   320     {
       
   321     if (iIsLazyCreationDone)
       
   322     	{
       
   323 		const CMetaExtensionContainerImplBase* mecImpl = Container();
       
   324 		if (mecImpl && mecImpl->Type() == CMetaExtensionContainerImplBase::EMetaExtensionContainerArray)
       
   325 			{
       
   326 			return const_cast<CMetaExtensionContainerArray*>(static_cast<const CMetaExtensionContainerArray*>(mecImpl));
       
   327 			}
       
   328     	}
       
   329     return NULL;
       
   330     }
       
   331 
       
   332