2011-12-30 03:45:10 +01:00
|
|
|
|
/*
|
2025-06-07 23:59:57 +02:00
|
|
|
|
MetaUtils(Test) - check some simple type trait helpers
|
2011-12-30 03:45:10 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* 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.
2024-11-17 23:42:55 +01:00
|
|
|
|
Copyright (C)
|
|
|
|
|
|
2011, Hermann Vosseler <Ichthyostega@web.de>
|
2011-12-30 03:45:10 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* 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.
2024-11-17 23:42:55 +01:00
|
|
|
|
**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.
|
2011-12-30 03:45:10 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* 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.
2024-11-17 23:42:55 +01:00
|
|
|
|
* *****************************************************************/
|
2011-12-30 03:45:10 +01:00
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
|
/** @file meta-utils-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
|
** unit test \ref MetaUtils_test
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2011-12-30 03:45:10 +01:00
|
|
|
|
|
2024-11-10 00:40:20 +01:00
|
|
|
|
#include "lib/symbol.hpp"
|
2011-12-30 03:45:10 +01:00
|
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
|
#include "lib/meta/util.hpp"
|
|
|
|
|
|
#include "lib/meta/typelist.hpp"
|
2024-12-17 16:56:21 +01:00
|
|
|
|
#include "lib/hetero-data.hpp"
|
|
|
|
|
|
#include "lib/test/diagnostic-output.hpp"
|
2011-12-30 03:45:10 +01:00
|
|
|
|
|
|
|
|
|
|
#include <string>
|
2024-12-17 16:56:21 +01:00
|
|
|
|
#include <array>
|
|
|
|
|
|
#include <tuple>
|
2011-12-30 03:45:10 +01:00
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
namespace meta {
|
|
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
|
|
using std::string;
|
2024-12-17 16:56:21 +01:00
|
|
|
|
using std::array;
|
|
|
|
|
|
using std::tuple;
|
|
|
|
|
|
using std::pair;
|
2011-12-30 03:45:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
|
/*********************************************************************//**
|
2011-12-30 03:45:10 +01:00
|
|
|
|
* @test verify basic type trait and metaprogramming helpers.
|
|
|
|
|
|
* - marker types to tell which overload the compiler picks
|
|
|
|
|
|
* - simple trait to detect the possibility of a string conversion
|
2024-12-17 16:56:21 +01:00
|
|
|
|
* - trait to detect (possibly) structured types (»tuple-like«)
|
2011-12-30 03:45:10 +01:00
|
|
|
|
* - trait to detect a typelist type
|
|
|
|
|
|
*/
|
|
|
|
|
|
class MetaUtils_test : public Test
|
|
|
|
|
|
{
|
|
|
|
|
|
void
|
2025-06-07 23:59:57 +02:00
|
|
|
|
run (Arg)
|
2011-12-30 03:45:10 +01:00
|
|
|
|
{
|
2016-01-10 11:21:34 +01:00
|
|
|
|
verify_basicTypeProbing();
|
2016-01-10 00:31:13 +01:00
|
|
|
|
verify_genericTypeDisplay();
|
2011-12-30 03:45:10 +01:00
|
|
|
|
|
|
|
|
|
|
detect_stringConversion();
|
|
|
|
|
|
detect_typeList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-10 11:21:34 +01:00
|
|
|
|
/** @test demonstrate the basic type trait detection technique:
|
|
|
|
|
|
* - we have two overloads with differing return type
|
|
|
|
|
|
* - we form a function call expression
|
|
|
|
|
|
* - by investigating the return type,
|
|
|
|
|
|
* we can figure out which overload the compiler picks.
|
2011-12-30 03:45:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
void
|
2016-01-10 11:21:34 +01:00
|
|
|
|
verify_basicTypeProbing()
|
2011-12-30 03:45:10 +01:00
|
|
|
|
{
|
|
|
|
|
|
CHECK (sizeof(Yes_t) != sizeof (No_t));
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (sizeof(Yes_t) == sizeof (probe (1)));
|
|
|
|
|
|
CHECK (sizeof(Yes_t) == sizeof (probe (1L))); // conversion long -> int
|
|
|
|
|
|
CHECK (sizeof(Yes_t) == sizeof (probe ('a'))); // conversion char -> int
|
|
|
|
|
|
CHECK (sizeof(No_t) == sizeof (probe ("a"))); // char * can't be converted
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Yes_t probe (int);
|
|
|
|
|
|
static No_t probe (...);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-10 00:31:13 +01:00
|
|
|
|
void
|
|
|
|
|
|
verify_genericTypeDisplay()
|
|
|
|
|
|
{
|
|
|
|
|
|
cout << typeStr<SubString>() <<endl;
|
|
|
|
|
|
|
|
|
|
|
|
struct Lunatic
|
|
|
|
|
|
: Test
|
|
|
|
|
|
{
|
|
|
|
|
|
virtual void run (Arg) {}
|
|
|
|
|
|
}
|
|
|
|
|
|
lunatic;
|
|
|
|
|
|
cout << typeStr(lunatic) << endl;
|
|
|
|
|
|
cout << typeStr(&lunatic) << endl;
|
|
|
|
|
|
cout << typeStr((Test &)lunatic) << endl;
|
|
|
|
|
|
cout << typeStr((Test *) &lunatic) << endl;
|
|
|
|
|
|
cout << typeStr(&Lunatic::run) << endl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-30 03:45:10 +01:00
|
|
|
|
//-------------------------------------------------TEST-types--
|
|
|
|
|
|
class SubString : public string
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
SubString() : string("sublunar") { }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Something { };
|
|
|
|
|
|
|
|
|
|
|
|
struct SomehowStringy
|
|
|
|
|
|
{
|
|
|
|
|
|
operator string() { return "No such thing"; }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct SomehowSubtle
|
|
|
|
|
|
{
|
|
|
|
|
|
operator SubString() { return SubString(); }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class SomehowSubSub : public SomehowSubtle { };
|
|
|
|
|
|
//-------------------------------------------------TEST-types--
|
|
|
|
|
|
|
|
|
|
|
|
template<typename TY>
|
|
|
|
|
|
static bool
|
|
|
|
|
|
can_convert (TY const&)
|
|
|
|
|
|
{
|
2011-12-30 06:10:47 +01:00
|
|
|
|
return can_convertToString<TY>::value;
|
2011-12-30 03:45:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
detect_stringConversion()
|
|
|
|
|
|
{
|
|
|
|
|
|
CHECK ( can_convert (string("inline string")));
|
|
|
|
|
|
CHECK ( can_convert ("char literal"));
|
|
|
|
|
|
CHECK (!can_convert (23.34));
|
|
|
|
|
|
CHECK (!can_convert (23));
|
|
|
|
|
|
CHECK (!can_convert (1L));
|
|
|
|
|
|
|
|
|
|
|
|
string str("mhm");
|
|
|
|
|
|
string & str_ref (str);
|
|
|
|
|
|
string const& str_const_ref (str);
|
|
|
|
|
|
string * str_ptr = &str;
|
|
|
|
|
|
|
|
|
|
|
|
CHECK ( can_convert (str));
|
|
|
|
|
|
CHECK ( can_convert (str_ref));
|
|
|
|
|
|
CHECK ( can_convert (str_const_ref));
|
|
|
|
|
|
CHECK ( can_convert (*str_ptr));
|
|
|
|
|
|
CHECK (!can_convert (str_ptr));
|
|
|
|
|
|
|
|
|
|
|
|
SubString sub;
|
|
|
|
|
|
Something thing;
|
|
|
|
|
|
const SomehowStringy stringy = SomehowStringy();
|
|
|
|
|
|
SomehowSubSub subsub;
|
|
|
|
|
|
SubString const& subRef(subsub);
|
|
|
|
|
|
|
|
|
|
|
|
CHECK ( can_convert (sub));
|
|
|
|
|
|
CHECK (!can_convert (thing));
|
|
|
|
|
|
CHECK ( can_convert (stringy));
|
|
|
|
|
|
CHECK ( can_convert (subsub));
|
|
|
|
|
|
CHECK ( can_convert (subRef));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------TEST-types--
|
2025-06-07 18:04:59 +02:00
|
|
|
|
using TheList = Types<int
|
2025-06-07 01:07:36 +02:00
|
|
|
|
,uint
|
|
|
|
|
|
,int64_t
|
|
|
|
|
|
,uint64_t
|
|
|
|
|
|
>::List;
|
2011-12-30 03:45:10 +01:00
|
|
|
|
|
2025-06-07 01:07:36 +02:00
|
|
|
|
using EmptyList = Nil;
|
2011-12-30 03:45:10 +01:00
|
|
|
|
//-------------------------------------------------TEST-types--
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
detect_typeList()
|
|
|
|
|
|
{
|
|
|
|
|
|
CHECK ( is_Typelist<TheList>::value);
|
|
|
|
|
|
CHECK ( is_Typelist<EmptyList>::value);
|
|
|
|
|
|
CHECK (!is_Typelist<Something>::value);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
|
LAUNCHER (MetaUtils_test, "unit meta");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}}} // namespace lib::meta::test
|