collecting some formatting & diagnostics utils here...
This commit is contained in:
parent
5784bd2819
commit
7f44de24d7
5 changed files with 246 additions and 0 deletions
96
src/lib/format.hpp
Normal file
96
src/lib/format.hpp
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
FORMAT.hpp - helpers for formatting and diagnostics
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2009, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
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 <string>
|
||||
#include <cstring>
|
||||
#include <typeinfo>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
|
||||
|
||||
|
||||
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<typename X>
|
||||
inline string
|
||||
invoke_2string ( typename enable_if< can_ToString<X>,
|
||||
X >::type const& val)
|
||||
{
|
||||
return string(val);
|
||||
}
|
||||
|
||||
template<typename X>
|
||||
inline string
|
||||
invoke_2string ( typename disable_if< can_ToString<X>,
|
||||
X >::type const&)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** try to get an object converted to string */
|
||||
template<typename TY>
|
||||
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<TY>::value)
|
||||
return string(prefix) + invoke_2string<TY>(val);
|
||||
|
||||
else
|
||||
return fallback? fallback
|
||||
: string("«")+typeid(val).name()+"»";
|
||||
}
|
||||
|
||||
|
||||
} // namespace util
|
||||
|
||||
#endif /*UTIL_FORMAT_H*/
|
||||
51
src/lib/meta/trait.hpp
Normal file
51
src/lib/meta/trait.hpp
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
TRAIT.hpp - type handling and type detection helpers
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2009, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
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 <boost/type_traits/is_convertible.hpp>
|
||||
#include <string>
|
||||
|
||||
|
||||
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 <typename TY>
|
||||
struct can_ToString
|
||||
{
|
||||
enum { value = boost::is_convertible<TY, std::string>::value
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
|
||||
}} // namespace lumiera::typelist
|
||||
#endif
|
||||
|
|
@ -234,6 +234,16 @@ out: dtor ~TargetObj\(12\) successful
|
|||
END
|
||||
|
||||
|
||||
TEST "formatting/diagnostics helpers" FormatHelper_test <<END
|
||||
out: Displaying some types....
|
||||
out: «..util.test.Reticent.»
|
||||
out: <no comment>
|
||||
out: ^hey Joe!
|
||||
out: ^he says: hey Joe!
|
||||
return: 0
|
||||
END
|
||||
|
||||
|
||||
TEST "functional closure" FunctionClosure_test <<END
|
||||
out: List1 :-<1>-<2>-<3>-
|
||||
out: List2 :-<5>-<6>-<7>-
|
||||
|
|
|
|||
|
|
@ -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 \
|
||||
|
|
|
|||
88
tests/lib/format-helper-test.cpp
Normal file
88
tests/lib/format-helper-test.cpp
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
FormatHelper(Test) - validate formatting and diagnostics helpers
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2009, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
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 <iostream>
|
||||
|
||||
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: ", "<no comment>") << endl;
|
||||
|
||||
cout << str (chatterer) << endl;
|
||||
cout << str (chatterer, "he says: ", "<no comment>") << endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
LAUNCHER (FormatHelper_test, "unit common");
|
||||
|
||||
|
||||
}} // namespace util::test
|
||||
|
||||
Loading…
Reference in a new issue