genericopenlibs/cppstdlib/src/stdcpp_support.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2008-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 // Name        : stdcpp_support.cpp
       
    15 // Part of     : standard c++ library.
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <new>
       
    20 #include <stdcpp_support.h>
       
    21 #include <typeinfo>
       
    22 #include <string>
       
    23 #include <stdexcept>
       
    24 
       
    25 
       
    26 EXPORT_C void TranslateSymErrorToCppException(TInt aError)
       
    27 {
       
    28 	switch(aError)
       
    29 	{
       
    30 	case KErrNoMemory:
       
    31 		// This is about to use the emergency buffer!
       
    32 		__THROW(std::bad_alloc());
       
    33 	case KErrArgument:
       
    34 		__THROW(std::invalid_argument("Invalid argument"));
       
    35 	case KErrOverflow :
       
    36 		__THROW(std::overflow_error("Overflow error"));
       
    37 	case KErrUnderflow:
       
    38 		__THROW(std::underflow_error("Underflow error"));
       
    39 	default:
       
    40 		__THROW(Symbian_error(KErrGeneral));
       
    41 	}
       
    42 }
       
    43 
       
    44 EXPORT_C TInt TranslateCppExceptionToSymError(const std::exception& aThrow)
       
    45 {
       
    46 #ifndef __GCCXML__
       
    47 	const std::type_info& atype = typeid(aThrow);
       
    48 
       
    49 	if(atype == typeid (std::bad_alloc))
       
    50 		return KErrNoMemory;
       
    51 	else if(atype == typeid(std::invalid_argument))
       
    52 		return KErrArgument;
       
    53 	else if(atype == typeid(std::out_of_range))
       
    54 		// std::out_of_range is of type logic_error which by definition means that it is
       
    55 		// "presumably detectable before the program executes".
       
    56 		// std::out_of_range is used to report an argument is not within the expected range.
       
    57 		// The description of KErrArgument says an argument is out of range. Hence the mapping.
       
    58 		return KErrArgument;
       
    59 	else if(atype == typeid(std::overflow_error))
       
    60 		return KErrOverflow;
       
    61 	else if(atype == typeid(std::underflow_error))
       
    62 		return KErrUnderflow;
       
    63 	else
       
    64 		return KErrGeneral;
       
    65 #else
       
    66 	return KErrGeneral;
       
    67 #endif
       
    68 }
       
    69 
       
    70 #ifdef __WINSCW__
       
    71 /*
       
    72  * The StdC++ global new and delete operators defined here. They are done only for the emulator
       
    73  * as for EABI platforms, it is part of the runtime support(i.e., stdnew.dll).
       
    74  */
       
    75 #include <e32std.h>
       
    76 
       
    77 EXPORT_C const std::nothrow_t std::nothrow;
       
    78 
       
    79 EXPORT_C void* operator new(std::size_t a_size) __THROW(std::bad_alloc)
       
    80 	{
       
    81 	for (;;)
       
    82 		{
       
    83 		void* p = User::Alloc(a_size);
       
    84 
       
    85 		if (p)
       
    86 			{
       
    87 			return p;
       
    88 			}
       
    89 		std::new_handler nh_func ;
       
    90 		std::new_handler *ptr = reinterpret_cast<std::new_handler*>(Dll::Tls());
       
    91 		nh_func = (ptr)?(*ptr):NULL;
       
    92 
       
    93 		if (nh_func)
       
    94 			{
       
    95 			nh_func();
       
    96 			}
       
    97 		else
       
    98 			{
       
    99 			__THROW(std::bad_alloc());
       
   100 			}
       
   101 		}
       
   102 	}
       
   103 
       
   104 EXPORT_C void* operator new[](std::size_t a_size) __THROW(std::bad_alloc)
       
   105 	{
       
   106     return ::operator new(a_size);
       
   107 	}
       
   108 
       
   109 EXPORT_C void* operator new(std::size_t a_size, const std::nothrow_t&) __NO_THROW
       
   110 	{
       
   111 	for (;;)
       
   112 		{
       
   113 		void* p = User::Alloc(a_size);
       
   114 
       
   115 		if (p)
       
   116 			{
       
   117 			return p;
       
   118 			}
       
   119 
       
   120 		std::new_handler nh_func ;
       
   121 		std::new_handler *ptr = reinterpret_cast<std::new_handler*>(Dll::Tls());
       
   122 		nh_func = (ptr)?(*ptr):NULL;
       
   123 
       
   124 		if (nh_func)
       
   125 			{
       
   126 			try
       
   127 				{
       
   128 				nh_func();
       
   129 				}
       
   130 			catch (...)
       
   131 				{
       
   132 				return 0;
       
   133 				}
       
   134 			}
       
   135 		else
       
   136 			{
       
   137 			return 0;
       
   138 			}
       
   139 		}
       
   140 	}
       
   141 
       
   142 EXPORT_C void* operator new[](std::size_t a_size, const std::nothrow_t) __NO_THROW
       
   143 	{
       
   144     return ::operator new(a_size, std::nothrow);
       
   145 	}
       
   146 
       
   147 // Symbian-specific addition.
       
   148 EXPORT_C void* operator new(std::size_t a_size, std::size_t a_extra_size) __NO_THROW
       
   149 	{
       
   150     return ::operator new(a_size + a_extra_size, std::nothrow);
       
   151 	}
       
   152 
       
   153 EXPORT_C void operator delete(void* p) __NO_THROW
       
   154 	{
       
   155 	User::Free(p);
       
   156 	}
       
   157 
       
   158 EXPORT_C void operator delete(void* p, const std::nothrow_t&) __NO_THROW
       
   159 	{
       
   160 	User::Free(p);
       
   161 	}
       
   162 
       
   163 EXPORT_C void operator delete[](void* p) __NO_THROW
       
   164 	{
       
   165 	User::Free(p);
       
   166 	}
       
   167 
       
   168 EXPORT_C void operator delete[](void* p, const std::nothrow_t&) __NO_THROW
       
   169 	{
       
   170 	User::Free(p);
       
   171 	}
       
   172 
       
   173 // Symbian-specific addition.
       
   174 EXPORT_C void operator delete(void* p, std::size_t) __NO_THROW
       
   175 	{
       
   176 	User::Free(p);
       
   177 	}
       
   178 _LIT(K,"set_new_handler");
       
   179 
       
   180 
       
   181 using std::new_handler;
       
   182 
       
   183 EXPORT_C new_handler std::set_new_handler(new_handler a_new_nh) __NO_THROW
       
   184 	{
       
   185 	new_handler current_nh;
       
   186 	TAny* tls_word_p = Dll::Tls();
       
   187 
       
   188 	if (!tls_word_p)
       
   189 		{
       
   190 		// This is the first time we're called, so we need to set up the TLS.
       
   191 
       
   192 		tls_word_p = User::Alloc( sizeof(new_handler) );
       
   193 		if ( !tls_word_p )
       
   194 			{
       
   195 			User::Panic(K, KErrNoMemory);
       
   196 			}
       
   197 
       
   198 		Dll::SetTls(tls_word_p);
       
   199 		current_nh = 0;
       
   200 		}
       
   201 	else
       
   202 		{
       
   203 		// Get the currently installed new_handler function.
       
   204 		current_nh = *reinterpret_cast<new_handler*>(tls_word_p);
       
   205 		}
       
   206 	
       
   207 	// Set the new new_handler.
       
   208 	*reinterpret_cast<new_handler*>(tls_word_p) = a_new_nh;
       
   209 
       
   210 	return current_nh;
       
   211 	}
       
   212 
       
   213 #endif //__WINSCW__