From 7f44de24d7aa1356007cba70930b43062e12a77b Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sun, 19 Jul 2009 08:03:54 +0200 Subject: [PATCH] collecting some formatting & diagnostics utils here... --- src/lib/format.hpp | 96 ++++++++++++++++++++++++++++++++ src/lib/meta/trait.hpp | 51 +++++++++++++++++ tests/40components.tests | 10 ++++ tests/lib/Makefile.am | 1 + tests/lib/format-helper-test.cpp | 88 +++++++++++++++++++++++++++++ 5 files changed, 246 insertions(+) create mode 100644 src/lib/format.hpp create mode 100644 src/lib/meta/trait.hpp create mode 100644 tests/lib/format-helper-test.cpp diff --git a/src/lib/format.hpp b/src/lib/format.hpp new file mode 100644 index 000000000..db53affcd --- /dev/null +++ b/src/lib/format.hpp @@ -0,0 +1,96 @@ +/* + FORMAT.hpp - helpers for formatting and diagnostics + + Copyright (C) Lumiera.org + 2009, Hermann Vosseler + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + + +/** @file format.hpp + ** Collection of small helpers and convenience shortcuts for diagnostics & formatting. + ** + ** @todo we could add a facade to boost::format here, see Ticket #166 + ** + */ + + +#ifndef UTIL_FORMAT_H +#define UTIL_FORMAT_H + +//#include "lib/util.hpp" +#include "lib/meta/trait.hpp" +#include "include/symbol.hpp" + +#include +#include +#include +#include + + + +namespace util { + + using lumiera::typelist::can_ToString; + using lumiera::Symbol; + using boost::enable_if; + using boost::disable_if; + using std::string; + + + namespace { // we need to guard the string conversion + // to avoid a compiler error in case the type isn't convertible.... + + template + inline string + invoke_2string ( typename enable_if< can_ToString, + X >::type const& val) + { + return string(val); + } + + template + inline string + invoke_2string ( typename disable_if< can_ToString, + X >::type const&) + { + return ""; + } + } + + + + /** try to get an object converted to string */ + template + inline string + str ( TY const& val + , Symbol prefix="" ///< prefix to prepend in case conversion is possible + , Symbol fallback =0 /// < replacement text to show if string conversion fails + ) + { + if (can_ToString::value) + return string(prefix) + invoke_2string(val); + + else + return fallback? fallback + : string("«")+typeid(val).name()+"»"; + } + + +} // namespace util + +#endif /*UTIL_FORMAT_H*/ diff --git a/src/lib/meta/trait.hpp b/src/lib/meta/trait.hpp new file mode 100644 index 000000000..8e0348e9b --- /dev/null +++ b/src/lib/meta/trait.hpp @@ -0,0 +1,51 @@ +/* + TRAIT.hpp - type handling and type detection helpers + + Copyright (C) Lumiera.org + 2009, Hermann Vosseler + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + + +#ifndef LUMIERA_META_TRAIT_H +#define LUMIERA_META_TRAIT_H + + +#include "lib/meta/util.hpp" + +#include +#include + + +namespace lumiera { +namespace typelist { + + /** Trait template for detecting if a type can be converted to string. + * For example, this allows to write specialisations with the help of + * boost::enable_if + */ + template + struct can_ToString + { + enum { value = boost::is_convertible::value + }; + }; + + + +}} // namespace lumiera::typelist +#endif diff --git a/tests/40components.tests b/tests/40components.tests index 2dc20a375..2a76eae74 100644 --- a/tests/40components.tests +++ b/tests/40components.tests @@ -234,6 +234,16 @@ out: dtor ~TargetObj\(12\) successful END +TEST "formatting/diagnostics helpers" FormatHelper_test < +out: ^hey Joe! +out: ^he says: hey Joe! +return: 0 +END + + TEST "functional closure" FunctionClosure_test <-<2>-<3>- out: List2 :-<5>-<6>-<7>- diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am index 429abef00..1dae992cf 100644 --- a/tests/lib/Makefile.am +++ b/tests/lib/Makefile.am @@ -43,6 +43,7 @@ test_lib_SOURCES = \ $(testlib_srcdir)/exceptionerrortest.cpp \ $(testlib_srcdir)/factory-special-test.cpp \ $(testlib_srcdir)/factorytest.cpp \ + $(testlib_srcdir)/format-helper-test.cpp \ $(testlib_srcdir)/hash-indexed-test.cpp \ $(testlib_srcdir)/helloworldtest.cpp \ $(testlib_srcdir)/lifecycletest.cpp \ diff --git a/tests/lib/format-helper-test.cpp b/tests/lib/format-helper-test.cpp new file mode 100644 index 000000000..7b35d420a --- /dev/null +++ b/tests/lib/format-helper-test.cpp @@ -0,0 +1,88 @@ +/* + FormatHelper(Test) - validate formatting and diagnostics helpers + + Copyright (C) Lumiera.org + 2009, Hermann Vosseler + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +* *****************************************************/ + + +#include "lib/test/run.hpp" +#include "lib/format.hpp" +#include "lib/error.hpp" + +#include + +using std::cout; +using std::endl; + + +namespace util { +namespace test { + + class Reticent + { + }; + + class UnReticent + : public Reticent + { + public: + operator string() const { return "hey Joe!"; } + }; + + + + /************************************************* + * verifies the proper working of helper functions + * frequently used within the Lumiera testsuite. + * @see test-helper.hpp + */ + class FormatHelper_test : public Test + { + void + run (Arg) + { + check2String(); + } + + + /** @test verify the maybe-to-string conversion. */ + void + check2String () + { + std::cout << "Displaying some types....\n"; + + Reticent closeLipped; + UnReticent chatterer; + + cout << str (closeLipped) << endl; + cout << str (closeLipped, "he says: ", "") << endl; + + cout << str (chatterer) << endl; + cout << str (chatterer, "he says: ", "") << endl; + } + + + + }; + + LAUNCHER (FormatHelper_test, "unit common"); + + +}} // namespace util::test +