You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stdcxx.apache.org by an...@apache.org on 2006/07/19 10:46:49 UTC

svn commit: r423403 - in /incubator/stdcxx/trunk/tests: include/rw_exception.h src/exception.cpp

Author: antonp
Date: Wed Jul 19 01:46:46 2006
New Revision: 423403

URL: http://svn.apache.org/viewvc?rev=423403&view=rev
Log:
2006-07-19  Anton Pevtsov  <an...@moscow.vdiweb.com>

	STDCXX-3
	* rw_exception.h: New header with definition of the Exception class
	and the rw_throw() function
	* exception.cpp: Implementation of the Exception class 
	and the rw_throw() function.

Added:
    incubator/stdcxx/trunk/tests/include/rw_exception.h   (with props)
    incubator/stdcxx/trunk/tests/src/exception.cpp   (with props)

Added: incubator/stdcxx/trunk/tests/include/rw_exception.h
URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/tests/include/rw_exception.h?rev=423403&view=auto
==============================================================================
--- incubator/stdcxx/trunk/tests/include/rw_exception.h (added)
+++ incubator/stdcxx/trunk/tests/include/rw_exception.h Wed Jul 19 01:46:46 2006
@@ -0,0 +1,61 @@
+/************************************************************************
+ *
+ * rw_exception.h - defines a test driver exception
+ *
+ * $Id$
+ *
+ ***************************************************************************
+ *
+ * Copyright 2006 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * Copyright 2004-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.
+ * 
+ **************************************************************************/
+
+#ifndef RW_EXCEPTION_H_INCLUDED
+#define RW_EXCEPTION_H_INCLUDED
+
+#include <testdefs.h>
+
+enum ExceptionId
+{
+    ex_unknown = 0,
+    // custom exceptions, i.e. not derived for std::exception
+    ex_stream = 1,
+    ex_custom = 7,
+    // exceptions derived from std::exception
+    ex_bad_alloc = 8,
+    ex_std = (7 << 3)
+};
+
+
+struct _TEST_EXPORT Exception
+{
+    const ExceptionId id_;
+
+    Exception (ExceptionId id) : id_ (id) {}
+
+    virtual ~Exception ();
+
+    virtual const char* what () const = 0;
+};
+
+
+_TEST_EXPORT void
+rw_throw (ExceptionId exid, const char *file, int line,
+          const char *function, const char *fmt, ...);
+
+#endif   // RW_EXCEPTION_H_INCLUDED

Propchange: incubator/stdcxx/trunk/tests/include/rw_exception.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/stdcxx/trunk/tests/include/rw_exception.h
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/stdcxx/trunk/tests/src/exception.cpp
URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/tests/src/exception.cpp?rev=423403&view=auto
==============================================================================
--- incubator/stdcxx/trunk/tests/src/exception.cpp (added)
+++ incubator/stdcxx/trunk/tests/src/exception.cpp Wed Jul 19 01:46:46 2006
@@ -0,0 +1,285 @@
+/************************************************************************
+ *
+ * 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 <rw_exception.h>
+#include <driver.h>   
+#include <rw_printf.h>
+#include <string.h>         // for strncpy()
+#include <malloc.h>         // for free()
+#include <stddef.h>         // for size_t
+#include <stdarg.h>         // for va_arg(), va_list
+#include <new>              // for std::bad_alloc
+
+/**************************************************************************/
+
+_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);
+
+/**************************************************************************/
+
+Exception::~Exception ()
+{
+}
+
+/**************************************************************************/
+
+struct ExceptionBase
+{
+private:
+    struct ExceptionString;
+
+public:
+    char             buf_ [256];
+    ExceptionString* ex_str_;
+
+    ExceptionBase ();
+    ExceptionBase (const ExceptionBase&);
+
+    ~ExceptionBase ();
+
+    ExceptionBase& operator= (const ExceptionBase&);
+
+    void format (const char* file, int line, const char* function,
+        const char* fmt, va_list va);
+
+    const char* what () const;
+
+private:
+
+    struct ExceptionString
+    {
+        long  refs_;
+        char* str_;
+
+        void init (char* str)
+        {
+            refs_ = 1;
+            str_ = str;
+        }
+
+        void addref () { ++refs_; }
+
+        long release ()
+        {
+            if (0 == --refs_) {
+                free (str_);
+                str_ = 0;
+            }
+            return refs_;
+        }
+
+    private:
+        // not defined
+        ExceptionString (const ExceptionString&);
+        ExceptionString& operator= (const ExceptionString&);
+    };
+
+
+    void free_ ();
+};
+
+ExceptionBase::ExceptionBase () : ex_str_ (0)
+{
+    buf_ [0] = '\0';
+}
+
+ExceptionBase::ExceptionBase (const ExceptionBase& ex) : ex_str_ (0)
+{
+    *this = ex;
+}
+
+ExceptionBase::~ExceptionBase ()
+{
+    free_ ();
+}
+
+ExceptionBase& ExceptionBase::operator= (const ExceptionBase& ex)
+{
+    if (&ex != this) {
+
+        free_ ();
+
+        if (ex.ex_str_) {
+            ex_str_ = ex.ex_str_;
+            ex_str_->addref ();
+        }
+        else
+            strncpy (buf_, ex.buf_, sizeof (buf_));
+    }
+
+    return *this;
+}
+
+void ExceptionBase::format (const char* file, int line,
+                            const char* function, const char* fmt, va_list va)
+{
+    free_ ();
+
+    char* buf = 0;
+    size_t bufsize = 0;
+
+    const int nchars = _rw_format (&buf, &bufsize, file, line,
+        function, fmt, va);
+
+    if (0 > nchars)
+        return;
+
+    if (sizeof (buf_) <= nchars) {
+        // buf_ size is too small
+        ex_str_ = _RWSTD_STATIC_CAST (ExceptionString*,
+                                      malloc (sizeof (*ex_str_)));
+
+        if (ex_str_) {
+            ex_str_->init (buf);
+            return;
+        }
+    }
+
+    // buf_ size if enough or cannot allocate memory for the ex_str_
+    const size_t max_len = sizeof (buf_) - 1;
+    strncpy (buf_, buf, max_len);
+    buf_ [max_len] = '\0';
+    free (buf);
+}
+
+const char* ExceptionBase::what () const
+{
+    return ex_str_ && ex_str_->str_ ? ex_str_->str_ : buf_;
+}
+
+void ExceptionBase::free_ ()
+{
+    buf_ [0] = '\0';
+
+    if (ex_str_ && 0 == ex_str_->release ()) {
+        free (ex_str_);
+        ex_str_ = 0;
+    }
+}
+
+/**************************************************************************/
+
+struct BadAlloc : std::bad_alloc, ExceptionBase
+{
+    BadAlloc ()
+    {
+    }
+
+    BadAlloc (const ExceptionBase& ex) : ExceptionBase (ex)
+    {
+    }
+
+    ~BadAlloc () _THROWS (()) {}
+
+    const char* what () const _THROWS (())
+    {
+        const char* msg = ExceptionBase::what ();
+        return (msg && msg[0]) ? msg : std::bad_alloc::what ();
+    }
+};
+
+/**************************************************************************/
+
+struct StreamException : Exception, ExceptionBase
+{
+    StreamException () : Exception (ex_stream)
+    {
+    }
+
+    StreamException (const ExceptionBase& ex) :
+        Exception (ex_stream), ExceptionBase (ex)
+    {
+    }
+
+    const char* what () const
+    {
+        return ExceptionBase::what ();
+    }
+};
+
+/**************************************************************************/
+
+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;
+}
+
+_TEST_EXPORT void
+rw_throw (ExceptionId exid, const char *file, int line,
+          const char *function, const char *fmt, ...)
+{
+    va_list va;
+    va_start (va, fmt);
+
+    ExceptionBase base;
+    base.format (file, line, function, fmt, va);
+
+    va_end (va);
+
+    switch (exid) {
+
+    case ex_stream:
+        throw StreamException (base);
+
+    case ex_bad_alloc:
+        throw BadAlloc (base);
+
+    default:
+        throw ex_unknown;
+    }
+}

Propchange: incubator/stdcxx/trunk/tests/src/exception.cpp
------------------------------------------------------------------------------
    svn:keywords = Id



Re: svn commit: r423403 - in /incubator/stdcxx/trunk/tests: include/rw_exception.h src/exception.cpp

Posted by Martin Sebor <se...@roguewave.com>.
antonp@apache.org wrote:
> Author: antonp
> Date: Wed Jul 19 01:46:46 2006
> New Revision: 423403
> 
> URL: http://svn.apache.org/viewvc?rev=423403&view=rev
> Log:
> 2006-07-19  Anton Pevtsov  <an...@moscow.vdiweb.com>
> 
> 	STDCXX-3
> 	* rw_exception.h: New header with definition of the Exception class
> 	and the rw_throw() function
> 	* exception.cpp: Implementation of the Exception class 
> 	and the rw_throw() function.

Anton, please remember to mention Farid in the attribution of changes
that the two of you worked on together or that you commit for him. The
name and email address on the ChangeLog entry should be that of the
author or authors of the code; SVN will automatically fill in the
committer's name under Author.

Thanks
Martin

Re: svn commit: r423403 - in /incubator/stdcxx/trunk/tests: include/rw_exception.h src/exception.cpp

Posted by Martin Sebor <se...@roguewave.com>.
antonp@apache.org wrote:
> Author: antonp
> Date: Wed Jul 19 01:46:46 2006
> New Revision: 423403
> 
> URL: http://svn.apache.org/viewvc?rev=423403&view=rev
[...]
> ==============================================================================
> --- incubator/stdcxx/trunk/tests/include/rw_exception.h (added)
> +++ incubator/stdcxx/trunk/tests/include/rw_exception.h Wed Jul 19 01:46:46 2006
> @@ -0,0 +1,61 @@
> +/************************************************************************
> + *
> + * rw_exception.h - defines a test driver exception
> + *
> + * $Id$
> + *
> + ***************************************************************************
> + *
> + * Copyright 2006 The Apache Software Foundation or its licensors,
> + * as applicable.
> + *
> + * Copyright 2004-2006 Rogue Wave Software.
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The Rogue Wave copyright should not be in new files developed
at (or for) the ASF. According to Justin, the copyright can
stay in files that predate the donation of stdcxx to the ASF,
but it should be moved below the ASF text. Here's the new
policy: http://www.apache.org/legal/src-headers.html

I committed a couple of files with the new license statement
yesterday: http://svn.apache.org/viewvc?rev=423334&view=rev

(Since the files predate the stdcxx code drop to ASF Subversion
I retained the Rogue Wave copyright at the bottom of the notice.)

We will still need to go through all the files and make these
changes so this is not a big deal but it would be good to start
following this process in all new work.

Thanks
Martin