Return-Path: Delivered-To: apmail-incubator-stdcxx-dev-archive@www.apache.org Received: (qmail 14465 invoked from network); 13 Jul 2006 16:03:12 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 13 Jul 2006 16:03:12 -0000 Received: (qmail 49179 invoked by uid 500); 13 Jul 2006 16:03:12 -0000 Delivered-To: apmail-incubator-stdcxx-dev-archive@incubator.apache.org Received: (qmail 49141 invoked by uid 500); 13 Jul 2006 16:03:11 -0000 Mailing-List: contact stdcxx-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: stdcxx-dev@incubator.apache.org Delivered-To: mailing list stdcxx-dev@incubator.apache.org Received: (qmail 49100 invoked by uid 99); 13 Jul 2006 16:03:11 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 13 Jul 2006 09:03:11 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [212.82.213.172] (HELO exkiv.kyiv.vdiweb.com) (212.82.213.172) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 13 Jul 2006 09:03:10 -0700 Received: from [10.11.37.44] ([10.11.37.44]) by exkiv.kyiv.vdiweb.com with Microsoft SMTPSVC(6.0.3790.1830); Thu, 13 Jul 2006 19:03:03 +0300 Message-ID: <44B66EAB.7080808@kyiv.vdiweb.com> Date: Thu, 13 Jul 2006 19:02:51 +0300 From: Farid Zaripov User-Agent: Thunderbird 1.5.0.4 (Windows/20060516) MIME-Version: 1.0 To: stdcxx-dev@incubator.apache.org Subject: RE: design of testuite exceptions (was: Re: svn commit: r418319 - /incubator/stdcxx/trunk/tests/strings/21.string.io.cpp) Content-Type: multipart/mixed; boundary="------------000401020201010008090204" X-OriginalArrivalTime: 13 Jul 2006 16:03:03.0712 (UTC) FILETIME=[D269BA00:01C6A695] X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N --------------000401020201010008090204 Content-Type: text/plain; charset=KOI8-R; format=flowed Content-Transfer-Encoding: 7bit Light update of the exception.cpp: ExceptionBase& ExceptionBase::operator= (const ExceptionBase& ex) : if strdup() is failed then copy string to the internal buf_ with truncate. Farid. > -----Original Message----- > From: Farid Zaripov [mailto:FaridZ@kyiv.vdiweb.com] > Sent: Wednesday, July 12, 2006 7:18 PM > To: stdcxx-dev@incubator.apache.org > Subject: RE: design of testuite exceptions (was: Re: svn > commit: r418319 - > /incubator/stdcxx/trunk/tests/strings/21.string.io.cpp) > > Martin, I implemented your latest notes about testsuite > exception (rw_exception.h, exception.cpp). The updated > version is attached. --------------000401020201010008090204 Content-Type: text/plain; name="exception.cpp" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="exception.cpp" /************************************************************************ * * exception.cpp - exceptions testsuite helpers * * $Id: $ * ************************************************************************ * * Copyright 2006 The Apache Software Foundation or its licensors, * as applicable. * * Copyright 2006 Rogue Wave Software. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * **************************************************************************/ // expand _TEST_EXPORT macros #define _RWSTD_TEST_SRC #include #include #include #include // for strncpy() #include // for free() #include // for size_t #include // for va_arg(), va_list #include // for std::bad_alloc /**************************************************************************/ Exception::~Exception () { } /**************************************************************************/ struct ExceptionBase { char buf_ [256]; char* what_; ExceptionBase (); ExceptionBase (const ExceptionBase&); ~ExceptionBase (); ExceptionBase& operator= (const ExceptionBase&); private: void free_ (); }; ExceptionBase::ExceptionBase () : what_ (buf_) { buf_ [0] = '\0'; } ExceptionBase::ExceptionBase (const ExceptionBase& ex) : what_ (buf_) { *this = ex; } ExceptionBase::~ExceptionBase () { free_ (); } ExceptionBase& ExceptionBase::operator= (const ExceptionBase& ex) { if (&ex != this) { free_ (); if (ex.buf_ != ex.what_) { what_ = strdup (ex.what_); if (!what_) { const size_t maxlen = sizeof (buf_) - 1; what_ = buf_; strncpy (buf_, ex.what_, maxlen); buf_ [maxlen] = '\0'; } } else strncpy (buf_, ex.buf_, sizeof (buf_)); } return *this; } void ExceptionBase::free_ () { if (buf_ != what_) { free (what_); what_ = buf_; } } /**************************************************************************/ struct BadAlloc : std::bad_alloc, ExceptionBase { ~BadAlloc () _THROWS (()) {} const char* what () const _THROWS (()) { return what_; } }; /**************************************************************************/ struct StreamException : Exception, ExceptionBase { StreamException () : Exception (ex_stream) { } const char* what () const { return what_; } }; /**************************************************************************/ _TEST_EXPORT int rw_vasnprintf (char**, size_t*, const char*, va_list); /**************************************************************************/ static int _rw_format (char** pbuf, size_t* pbufsize, const char* file, int line, const char* function, const char* fmt, va_list va) { const int nchars1 = function ? rw_asnprintf (pbuf, pbufsize, "\"%s\", %d, \"%s\" ", file, line, function) : rw_asnprintf (pbuf, pbufsize, "\"%s\", %d, ", file, line); if (0 > nchars1) return nchars1; char *tmpbuf = 0; size_t tmpsize = 0; const int nchars2 = rw_vasnprintf (&tmpbuf, &tmpsize, fmt, va); if (0 > nchars2) { free (tmpbuf); return nchars1; } const int nchars3 = rw_asnprintf (pbuf, pbufsize, "%{+}%s", tmpbuf); free (tmpbuf); return 0 > nchars3 ? nchars1 : nchars1 + nchars3; } // str should be allocated by malloc() call // str shouldn't be free after call _rw_init_exception() static void _rw_init_exception (ExceptionBase& ex, char* str, size_t nchars) { if (nchars < sizeof (ex.buf_)) { memcpy (ex.buf_, str, nchars); ex.buf_ [nchars] = '\0'; free (str); } else ex.what_ = str; } _TEST_EXPORT void rw_throw (ExceptionId exid, const char *file, int line, const char *function, const char *fmt, ...) { // format string char* buf = 0; size_t bufsize = 0; va_list va; va_start (va, fmt); const int nchars = _rw_format (&buf, &bufsize, file, line, function, fmt, va); va_end (va); switch (exid) { case ex_stream: { StreamException ex; _rw_init_exception (ex, buf, nchars); throw ex; } case ex_bad_alloc: { BadAlloc ex; _rw_init_exception (ex, buf, nchars); throw ex; } default: free (buf); throw ex_unknown; } } --------------000401020201010008090204--