* Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
95 lines
3.2 KiB
C++
95 lines
3.2 KiB
C++
/*
|
||
FORMAT-COUT.hpp - use custom string conversions in stream output
|
||
|
||
Copyright (C)
|
||
2016, Hermann Vosseler <Ichthyostega@web.de>
|
||
|
||
**Lumiera** 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. See the file COPYING for further details.
|
||
|
||
*/
|
||
|
||
|
||
/** @file format-cout.hpp
|
||
** Automatically use custom string conversion in C++ stream output.
|
||
** This diagnostics facility allows just to dump any object into `cout` or `cerr`.
|
||
** Pointers will be detected, checked for NULL and printed as address, followed
|
||
** by the representation of the pointee. When the displayed entity defines an
|
||
** `operator string()`, this custom string conversion will be used (suppressing
|
||
** any exceptions, of course). As fallback, a simplified type string is printed.
|
||
**
|
||
** \par policy
|
||
** What shall be expected from a generic toString conversion?
|
||
** It should be _minimal_, it should be _transparent_ and it should
|
||
** always work and deliver a string, irrespective of the circumstances.
|
||
** By extension, this means that we do not want to differentiate much
|
||
** between values, references and pointers, which also means, we do
|
||
** not want to indicate pointers explicitly (just signal NULL, when
|
||
** encountered). The situation is slightly different for the `ostream`
|
||
** inserter; in a modern GUI application, there isn't much use for
|
||
** STDOUT and STDERR, beyond error messages and unit testing.
|
||
** Thus, we can strive at building a more convenient flavour
|
||
** here, which does indeed even show the address of pointers.
|
||
**
|
||
** @see FormatCOUT_test
|
||
** @see FormatHelper_test
|
||
** @see [generic string conversion helper](\ref util::toString() )
|
||
** @see [frontend for boost::format, printf-style](\ref format-string.hpp)
|
||
**
|
||
*/
|
||
|
||
|
||
#ifndef LIB_FORMAT_COUT_H
|
||
#define LIB_FORMAT_COUT_H
|
||
|
||
#include "lib/format-obj.hpp"
|
||
|
||
#include <string>
|
||
#include <iostream>
|
||
|
||
// make those generally visible
|
||
using std::cout;
|
||
using std::cerr;
|
||
using std::endl;
|
||
|
||
|
||
|
||
|
||
namespace std {
|
||
|
||
namespace { // toggle for the following generic overloads of operator<<
|
||
|
||
template<typename X>
|
||
using enable_StringConversion = lib::meta::enable_if< lib::meta::use_StringConversion4Stream<X>>;
|
||
}
|
||
|
||
|
||
/** generic overload to use custom string conversions in output */
|
||
template<typename _CharT, typename _Traits, typename X, typename = enable_StringConversion<X>>
|
||
inline basic_ostream<_CharT, _Traits>&
|
||
operator<< (basic_ostream<_CharT, _Traits>& os, X const& obj)
|
||
{
|
||
return os << util::StringConv<X>::invoke (obj);
|
||
}
|
||
|
||
|
||
/** generic overload to pretty-print any pointer in output
|
||
* @note possibly also invokes custom string conversion,
|
||
* in case the pointee defines one
|
||
*/
|
||
template<typename _CharT, typename _Traits, typename X, typename = enable_StringConversion<X>>
|
||
inline basic_ostream<_CharT, _Traits>&
|
||
operator<< (basic_ostream<_CharT, _Traits>& os, X const* ptr)
|
||
{
|
||
if (ptr)
|
||
return util::showAdr(os, ptr) << " ↗" << util::StringConv<X>::invoke (*ptr);
|
||
else
|
||
return os << "⟂ «" << lib::meta::typeStr<X>() << "»";
|
||
}
|
||
|
||
|
||
|
||
} // namespace std
|
||
#endif /*LIB_FORMAT_COUT_H*/
|