2007-08-17 00:36:07 +02:00
|
|
|
/* try.cpp - for trying out some language features....
|
|
|
|
|
* scons will create the binary bin/try
|
|
|
|
|
*
|
2014-08-16 02:04:29 +02:00
|
|
|
*/
|
2007-08-17 00:36:07 +02:00
|
|
|
|
|
|
|
|
// 8/07 - how to control NOBUG??
|
2007-08-31 14:57:49 +02:00
|
|
|
// execute with NOBUG_LOG='ttt:TRACE' bin/try
|
2008-01-04 01:44:40 +01:00
|
|
|
// 1/08 - working out a static initialisation problem for Visitor (Tag creation)
|
2008-01-27 03:58:24 +01:00
|
|
|
// 1/08 - check 64bit longs
|
2008-04-05 05:57:54 +02:00
|
|
|
// 4/08 - comparison operators on shared_ptr<Asset>
|
2008-04-26 05:38:19 +02:00
|
|
|
// 4/08 - conversions on the value_type used for boost::any
|
2008-05-17 04:34:46 +02:00
|
|
|
// 5/08 - how to guard a downcasting access, so it is compiled in only if the involved types are convertible
|
2008-07-22 08:19:03 +02:00
|
|
|
// 7/08 - combining partial specialisation and subclasses
|
2008-10-26 03:21:33 +01:00
|
|
|
// 10/8 - abusing the STL containers to hold noncopyable values
|
2009-06-26 05:27:54 +02:00
|
|
|
// 6/09 - investigating how to build a mixin template providing an operator bool()
|
2009-12-31 01:21:45 +01:00
|
|
|
// 12/9 - tracking down a strange "warning: type qualifiers ignored on function return type"
|
2010-01-02 06:26:56 +01:00
|
|
|
// 1/10 - can we determine at compile time the presence of a certain function (for duck-typing)?
|
2010-04-09 07:44:31 +02:00
|
|
|
// 4/10 - pretty printing STL containers with python enabled GDB?
|
2011-01-08 03:30:10 +01:00
|
|
|
// 1/11 - exploring numeric limits
|
2011-01-20 13:21:14 +01:00
|
|
|
// 1/11 - integer floor and wrap operation(s)
|
2011-01-31 05:35:43 +01:00
|
|
|
// 1/11 - how to fetch the path of the own executable -- at least under Linux?
|
2011-10-09 14:52:58 +02:00
|
|
|
// 10/11 - simple demo using a pointer and a struct
|
2011-11-01 03:11:43 +01:00
|
|
|
// 11/11 - using the boost random number generator(s)
|
2011-12-30 03:45:10 +01:00
|
|
|
// 12/11 - how to detect if string conversion is possible?
|
2012-01-07 03:28:12 +01:00
|
|
|
// 1/12 - is partial application of member functions possible?
|
2014-05-09 00:49:15 +02:00
|
|
|
// 5/14 - c++11 transition: detect empty function object
|
2014-08-13 03:08:00 +02:00
|
|
|
// 7/14 - c++11 transition: std hash function vs. boost hash
|
2014-09-21 02:54:54 +02:00
|
|
|
// 9/14 - variadic templates and perfect forwarding
|
2014-11-22 03:31:59 +01:00
|
|
|
// 11/14 - pointer to member functions and name mangling
|
2015-08-12 02:31:41 +02:00
|
|
|
// 8/15 - Segfault when loading into GDB (on Debian/Jessie 64bit
|
2015-08-29 17:09:03 +02:00
|
|
|
// 8/15 - generalising the Variant::Visitor
|
2016-01-04 02:58:58 +01:00
|
|
|
// 1/16 - generic to-string conversion for ostream
|
2012-01-07 03:28:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @file try.cpp
|
2016-01-04 02:58:58 +01:00
|
|
|
** How to build generic string conversion into `ostream::operator<< `.
|
2016-01-05 03:32:24 +01:00
|
|
|
**
|
|
|
|
|
** This task is actually a conglomerate of several chores:
|
|
|
|
|
** - sanitise and segregate the type-traits usage
|
|
|
|
|
** - disentangle the existing toString conversion helper
|
|
|
|
|
** - extract a basic form from this helper, which can be placed
|
|
|
|
|
** into a header with minimal dependencies. After some consideration,
|
|
|
|
|
** I decided to allow `<typeinfo>` in this category, which allows us
|
|
|
|
|
** at least to show a type name as fallback
|
|
|
|
|
** - distill an essential version of `enable_if`, which can be inlined.
|
|
|
|
|
** This allows us to get rid of `boost::enable_if` finally.
|
|
|
|
|
** - build a sensible `operator string()` for our `lib::P` based on this
|
|
|
|
|
** - and _finally_, to come up with a templated version of the `ostream`
|
|
|
|
|
** inserter `operator<<`, which does not cause too much havoc when
|
|
|
|
|
** used by default. The greatest challenge here is to avoid ambiguous
|
|
|
|
|
** overloads, yet also to deal with references, `void` and arrays.
|
2014-08-16 02:04:29 +02:00
|
|
|
**
|
2016-01-05 03:32:24 +01:00
|
|
|
** \par policy
|
|
|
|
|
** What shall be expected from such 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`
|
2016-01-05 23:52:48 +01:00
|
|
|
** inserter; in a modern GUI application, there isn't much use for
|
2016-01-05 03:32:24 +01:00
|
|
|
** STDOUT and STDERR, beyond error messages and unit testing.
|
|
|
|
|
** Thus, we can strive at building a more convenient flavour
|
|
|
|
|
** here, which does indeed even shows the address of pointers.
|
|
|
|
|
**
|
2012-01-07 03:28:12 +01:00
|
|
|
*/
|
|
|
|
|
|
2015-08-29 17:09:03 +02:00
|
|
|
typedef unsigned int uint;
|
|
|
|
|
|
2016-01-04 02:58:58 +01:00
|
|
|
#include "lib/p.hpp"
|
|
|
|
|
#include "lib/diff/gen-node.hpp"
|
2015-08-29 17:09:03 +02:00
|
|
|
|
2016-01-04 04:01:34 +01:00
|
|
|
#include "lib/meta/util.hpp"
|
2016-01-04 22:21:09 +01:00
|
|
|
#include "lib/meta/trait.hpp"
|
2016-01-04 04:01:34 +01:00
|
|
|
|
2011-12-30 03:45:10 +01:00
|
|
|
#include <iostream>
|
2016-01-04 22:21:09 +01:00
|
|
|
#include <type_traits>
|
2016-01-04 02:58:58 +01:00
|
|
|
#include <utility>
|
2016-01-04 04:01:34 +01:00
|
|
|
#include <string>
|
2014-11-22 03:31:59 +01:00
|
|
|
|
2016-01-04 02:58:58 +01:00
|
|
|
using lib::diff::GenNode;
|
|
|
|
|
using lib::P;
|
2016-01-05 20:53:31 +01:00
|
|
|
using lib::meta::enable_if;
|
2016-01-05 23:34:53 +01:00
|
|
|
using lib::meta::typeStr;
|
2016-01-06 02:41:58 +01:00
|
|
|
using lib::meta::is_basically;
|
|
|
|
|
using lib::meta::can_lexical2string;
|
2016-01-04 04:01:34 +01:00
|
|
|
using lib::meta::can_convertToString;
|
2016-01-05 23:52:48 +01:00
|
|
|
using lib::meta::CustomStringConv;
|
2016-01-06 02:41:58 +01:00
|
|
|
using lib::meta::Strip;
|
2016-01-04 02:58:58 +01:00
|
|
|
|
2016-01-06 02:41:58 +01:00
|
|
|
using std::__and_;
|
|
|
|
|
using std::__not_;
|
2011-12-30 03:45:10 +01:00
|
|
|
using std::string;
|
2011-10-09 14:52:58 +02:00
|
|
|
using std::cout;
|
2011-11-01 03:11:43 +01:00
|
|
|
using std::endl;
|
2008-10-26 03:21:33 +01:00
|
|
|
|
2015-08-29 18:10:18 +02:00
|
|
|
|
2016-01-05 03:32:24 +01:00
|
|
|
|
|
|
|
|
|
2015-08-29 17:09:03 +02:00
|
|
|
|
2016-01-04 04:01:34 +01:00
|
|
|
|
|
|
|
|
|
2016-01-05 03:32:24 +01:00
|
|
|
///////////////////////////////shall go into the implementation of lib::P
|
2016-01-04 04:01:34 +01:00
|
|
|
template<typename X>
|
|
|
|
|
inline string
|
|
|
|
|
stringz (P<X> ptr)
|
|
|
|
|
{
|
|
|
|
|
if (not ptr)
|
2016-01-05 23:34:53 +01:00
|
|
|
return "⟂ P<"+typeStr(ptr.get())+">";
|
2016-01-04 04:01:34 +01:00
|
|
|
else
|
|
|
|
|
return CustomStringConv<X>::invoke (*ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-26 05:27:54 +02:00
|
|
|
|
2016-01-04 22:21:09 +01:00
|
|
|
|
|
|
|
|
/////////////////////////////////////////planned new ostream inclusion
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
template<typename X>
|
2016-01-05 03:32:24 +01:00
|
|
|
struct use_StringConversion4Stream
|
|
|
|
|
: __and_< std::is_class<typename Strip<X>::TypePlain>
|
|
|
|
|
,__not_<std::is_pointer<X>>
|
2016-01-04 22:21:09 +01:00
|
|
|
,__not_<can_lexical2string<X>>
|
|
|
|
|
>
|
|
|
|
|
{ };
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-05 03:32:24 +01:00
|
|
|
template<typename X, typename = enable_if <use_StringConversion4Stream<X>>>
|
2016-01-04 22:21:09 +01:00
|
|
|
std::ostream&
|
|
|
|
|
operator<< (std::ostream& os, X const& obj)
|
|
|
|
|
{
|
|
|
|
|
return os << CustomStringConv<X>::invoke (obj);
|
|
|
|
|
}
|
2016-01-05 03:32:24 +01:00
|
|
|
|
|
|
|
|
template<typename X, typename = enable_if <use_StringConversion4Stream<X>>>
|
|
|
|
|
std::ostream&
|
|
|
|
|
operator<< (std::ostream& os, X* ptr)
|
|
|
|
|
{
|
|
|
|
|
if (ptr)
|
|
|
|
|
return os << (void*)ptr << " ↗" << *ptr;
|
|
|
|
|
else
|
2016-01-05 23:34:53 +01:00
|
|
|
return os << "⟂ «" << typeStr<X>() << "»";
|
2016-01-05 03:32:24 +01:00
|
|
|
}
|
|
|
|
|
|
2016-01-04 22:21:09 +01:00
|
|
|
template<typename X>
|
|
|
|
|
std::ostream&
|
|
|
|
|
operator<< (std::ostream& os, P<X> const& ptr)
|
|
|
|
|
{
|
|
|
|
|
return os << stringz (ptr);
|
|
|
|
|
}
|
2016-01-05 03:32:24 +01:00
|
|
|
|
2016-01-04 22:21:09 +01:00
|
|
|
/////////////////////////////////////////planned new ostream inclusion
|
|
|
|
|
|
|
|
|
|
|
2016-01-05 03:32:24 +01:00
|
|
|
|
|
|
|
|
class Reticent
|
|
|
|
|
{
|
|
|
|
|
uint neigh_ = 42;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X, typename...ARGS>
|
|
|
|
|
inline P<X>
|
|
|
|
|
newP (ARGS&&... ctorArgs)
|
|
|
|
|
{
|
|
|
|
|
return P<X>{new X {std::forward<ARGS>(ctorArgs)...}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
using BasicallyString = is_basically<T, string>;
|
|
|
|
|
template<typename T>
|
|
|
|
|
using BasicallyChar = is_basically<typename std::remove_all_extents<T>::type, char>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
showTypes()
|
2011-10-09 14:52:58 +02:00
|
|
|
{
|
2015-08-29 17:09:03 +02:00
|
|
|
|
2016-01-04 22:21:09 +01:00
|
|
|
#define SHOW_CHECK(_EXPR_) cout << STRINGIFY(_EXPR_) << "\t : " << (_EXPR_::value? "Yes":"No") << endl;
|
2016-01-05 03:32:24 +01:00
|
|
|
#define ANALYSE(_TYPE_) \
|
|
|
|
|
cout << "Type: " STRINGIFY(_TYPE_) " ......"<<endl; \
|
|
|
|
|
SHOW_CHECK (BasicallyChar<_TYPE_>); \
|
|
|
|
|
SHOW_CHECK (BasicallyString<_TYPE_>); \
|
|
|
|
|
SHOW_CHECK (std::is_arithmetic<_TYPE_>);\
|
|
|
|
|
SHOW_CHECK (can_lexical2string<_TYPE_>); \
|
|
|
|
|
SHOW_CHECK (can_convertToString<_TYPE_>); \
|
|
|
|
|
SHOW_CHECK (use_StringConversion4Stream<_TYPE_>);
|
|
|
|
|
|
2016-01-04 22:21:09 +01:00
|
|
|
|
|
|
|
|
using CharLit = typeof("bla");
|
2016-01-05 03:32:24 +01:00
|
|
|
using CharPtr = typeof(const char*);
|
|
|
|
|
using GenNodePtr = typeof(GenNode*);
|
|
|
|
|
using GenNodeRef = typeof(GenNode&);
|
2016-01-04 22:21:09 +01:00
|
|
|
|
2016-01-05 03:32:24 +01:00
|
|
|
ANALYSE (string);
|
|
|
|
|
ANALYSE (CharLit);
|
|
|
|
|
ANALYSE (CharPtr)
|
|
|
|
|
ANALYSE (Reticent)
|
|
|
|
|
ANALYSE (P<Reticent>)
|
|
|
|
|
ANALYSE (GenNode)
|
|
|
|
|
ANALYSE (GenNodePtr)
|
|
|
|
|
ANALYSE (GenNodeRef)
|
|
|
|
|
ANALYSE (P<GenNode>)
|
|
|
|
|
cout << endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main (int, char**)
|
|
|
|
|
{
|
|
|
|
|
showTypes();
|
2016-01-04 22:21:09 +01:00
|
|
|
|
2016-01-05 03:32:24 +01:00
|
|
|
auto psss = newP<Reticent>();
|
|
|
|
|
auto gnng = newP<GenNode>("Hui", "Buh");
|
2016-01-04 22:21:09 +01:00
|
|
|
|
|
|
|
|
cout << "mauu..." << psss <<endl;
|
|
|
|
|
cout << "wauu..." << gnng <<endl;
|
|
|
|
|
|
2016-01-05 03:32:24 +01:00
|
|
|
cout << "mauuu.." << *psss <<endl;
|
2016-01-04 22:21:09 +01:00
|
|
|
cout << "wauuu.." << *gnng <<endl;
|
2016-01-05 03:32:24 +01:00
|
|
|
cout << "wauuup." << gnng.get() <<endl;
|
|
|
|
|
|
|
|
|
|
gnng.reset();
|
|
|
|
|
cout << "aauu..." << gnng <<endl;
|
|
|
|
|
cout << "aauu..." << gnng.get() <<endl;
|
2014-08-17 03:23:35 +02:00
|
|
|
|
2008-10-26 22:35:01 +01:00
|
|
|
cout << "\n.gulp.\n";
|
2007-08-17 00:36:07 +02:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|