From e67156ce6522142b0d81d4350797d57db608c63d Mon Sep 17 00:00:00 2001 From: Andreas Waidler Date: Mon, 15 Aug 2011 14:36:19 +0200 Subject: [PATCH] Modified DECLARE/DEFINE to expect seperate scope. Can now be used for inner classes (etc.), but requires non-global scope. --- libsex/declare.hxx | 18 +++++++++++++++--- libsex/define.hxx | 19 ++++++++++++++----- tests/UsageExamples.cxx | 18 +++++++++--------- tests/framework/ExampleException1.cxx | 4 ++-- tests/framework/ExampleException1.hxx | 10 +++++++++- tests/framework/ExampleException2.cxx | 2 +- tests/framework/ExampleException2.hxx | 7 ++++++- 7 files changed, 56 insertions(+), 22 deletions(-) diff --git a/libsex/declare.hxx b/libsex/declare.hxx index 365509f..2e84982 100644 --- a/libsex/declare.hxx +++ b/libsex/declare.hxx @@ -9,9 +9,21 @@ #include -/// Macro for declaring exception classes (.hxx). -#define LIBSEX_DECLARE(parent, name) \ -class name : public parent\ +/** + * Declares an exception inheriting from a supplied class. + * + * @note Due to technical limitations, it is not possible + * to use LIBSEX_DECLARE to declare a class in global + * scope. However, in a truly modular system that would be + * bad practice anyways, so I don't care. + * + * @param parent Parent class (incl. scope). + * @param scope Non-global scope of class to be defined. + * @param name Name of class to be defined. + * @see LIBSEX_DEFINE + */ +#define LIBSEX_DECLARE(parent, scope, name) \ +class scope::name : public parent\ {\ public:\ static const char* const TEMPLATE;\ diff --git a/libsex/define.hxx b/libsex/define.hxx index 35cabd2..416f3ec 100644 --- a/libsex/define.hxx +++ b/libsex/define.hxx @@ -7,16 +7,25 @@ #ifndef LIBSEX_DEFINE_HXX #define LIBSEX_DEFINE_HXX -/// Macro for defining exception classes (.cxx). -#define LIBSEX_DEFINE(parent, name, message) \ -const char* const name::TEMPLATE = message;\ +/** + * Defines an exception inheriting from a supplied parent. + * + * @param parent Parent class (incl. scope). + * @param scope Non-global scope of class to be defined. + * @param name Name of the class to be defined. + * @param message Error message template. + * @see LIBSEX_DECLARE + * @see snprintf + */ +#define LIBSEX_DEFINE(parent, scope, name, message) \ +const char* const scope::name::TEMPLATE = message;\ \ -name::name(const char* const errorMessage)\ +scope::name::name(const char* const errorMessage)\ : parent(errorMessage)\ {\ }\ \ -name::name(\ +scope::name::name(\ const char* const errorMessage,\ const libsex::Exception& previous)\ : parent(errorMessage, previous)\ diff --git a/tests/UsageExamples.cxx b/tests/UsageExamples.cxx index c204b83..4debcee 100644 --- a/tests/UsageExamples.cxx +++ b/tests/UsageExamples.cxx @@ -26,7 +26,7 @@ void tests::UsageExamples::testSimpleConstruction() // can pass an arbitrary string as message -- // the Exception subsystem itself does not depend // on message templates. - ExampleException2 e(msg.c_str()); + example::ExampleException2 e(msg.c_str()); // Turn description into an instance of std::string // (needed for comparing). @@ -52,8 +52,8 @@ void tests::UsageExamples::testSimpleThrowing() // Since ExampleException1's message // template does not expect any arguments, // we can use THROW_ARGLESS. - THROW_ARGLESS(ExampleException1); - } catch (ExampleException1& e) { + THROW_ARGLESS(example::ExampleException1); + } catch (example::ExampleException1& e) { // Check whether result is as expected. std::string exp(__FILE__); @@ -78,8 +78,8 @@ void tests::UsageExamples::testThrowing() // Fill the parameters into the message // template and throw an exception // containing the resulting string. - THROW(ExampleException2, "foobar", 42); - } catch (ExampleException2& e) { + THROW(example::ExampleException2, "foobar", 42); + } catch (example::ExampleException2& e) { // Check whether result is as expected. std::string exp(__FILE__); @@ -102,14 +102,14 @@ void tests::UsageExamples::testChaining() try { try { - THROW(ExampleException2, "foo", 21); - } catch (ExampleException2& e) { + THROW(example::ExampleException2, "foo", 21); + } catch (example::ExampleException2& e) { // Note that CHAIN expects the // variable name of the previous // exception to be "e"! - CHAIN(ExampleException2, "bar", 42); + CHAIN(example::ExampleException2, "bar", 42); } - } catch (ExampleException2& e) { + } catch (example::ExampleException2& e) { // Check whether result is as expected. std::string exp(__FILE__); diff --git a/tests/framework/ExampleException1.cxx b/tests/framework/ExampleException1.cxx index 57bb52b..57a05bf 100644 --- a/tests/framework/ExampleException1.cxx +++ b/tests/framework/ExampleException1.cxx @@ -9,6 +9,6 @@ // Define ExampleException1 and provide a message template. // If you want to localize your exceptions, you just have -// to localize the following line. +// to localize that line. -LIBSEX_DEFINE(libsex::Exception, ExampleException1, "Error!") +LIBSEX_DEFINE(libsex::Exception, example, ExampleException1, "Error!") diff --git a/tests/framework/ExampleException1.hxx b/tests/framework/ExampleException1.hxx index 112ca33..ac0ff25 100644 --- a/tests/framework/ExampleException1.hxx +++ b/tests/framework/ExampleException1.hxx @@ -10,7 +10,15 @@ #include #include + +namespace example +{ + // Forward declare to ensure the namespace exists. + class ExampleException1; +} + + // ExampleException1 inherits from libsex::Exception. -LIBSEX_DECLARE(libsex::Exception, ExampleException1) +LIBSEX_DECLARE(libsex::Exception, example, ExampleException1) #endif diff --git a/tests/framework/ExampleException2.cxx b/tests/framework/ExampleException2.cxx index a22cfce..ad439c6 100644 --- a/tests/framework/ExampleException2.cxx +++ b/tests/framework/ExampleException2.cxx @@ -12,4 +12,4 @@ // Define ExampleException2 and provide a message template // (see printf for a list and explanation of placeholders). -LIBSEX_DEFINE(libsex::Exception, ExampleException2, "%s is %d") +LIBSEX_DEFINE(libsex::Exception, example, ExampleException2, "%s is %d") diff --git a/tests/framework/ExampleException2.hxx b/tests/framework/ExampleException2.hxx index 0e26f0d..82b5bfd 100644 --- a/tests/framework/ExampleException2.hxx +++ b/tests/framework/ExampleException2.hxx @@ -14,7 +14,12 @@ #include #include +namespace example +{ + class ExampleException2; +} + // ExampleException2 inherits from libsex::Exception. -LIBSEX_DECLARE(libsex::Exception, ExampleException2) +LIBSEX_DECLARE(libsex::Exception, example, ExampleException2) #endif -- 2.11.4.GIT