diff --git a/Examples/CONTENT.md b/Examples/CONTENT.md index cd6ff7c0..979d033c 100644 --- a/Examples/CONTENT.md +++ b/Examples/CONTENT.md @@ -87,6 +87,7 @@ since several examples make use of the utilities contained in this folder. * `Streams/redirect`. Straems can be redirected, also at run time!. Useful you you want to be able to switch from screen to file, or to a string. * `Streams/serialization`. A example of serializaion of an aggregate (more complex serialization that may be performed with specialised libraries like `boost::serialization` are not covered here). * `Streams/sstream`. String streams are a very useful tool. They are strings that can act as a stream (or viceversa). Here you find some example of usage. + * `Streams/format` An example of the use of the new `std::format` utility. It is a new feature in C++20 that allows you to format strings in a more readable and maintainable way. It is similar to `printf` but with a more modern syntax. * `Vector` Examples of use of `std::vector`, probably the most used container in C++. *`Vector/Basics` As the name says. *`Vector/Remove` What it means "removing" elements from a vector with `remove()`. It is not what you think!` diff --git a/Examples/src/Utilities/README.md b/Examples/src/Utilities/README.md index e7888a36..56d0675b 100644 --- a/Examples/src/Utilities/README.md +++ b/Examples/src/Utilities/README.md @@ -88,11 +88,10 @@ utilities of the Standard Library, but with a simpler interface. * `toString` Converts anything for which there is the `<<` streaming operator to a string. A use of `std::stringstream`. -* `tuple_utilities` Contains some utilities for tuples: `tuple_common_type_t` that returns the common tpe of all types contained in a tuple, and `for_each` and `for_each2` that apply (possibly in parallel) the function object `F` to all elements of the tuple. The first one returns a tuple with the result, the second one does not and is thus applicable also if `F` is a void function. `all_of` and `any_of`, that apply predicate `F` to all elements of a tuple. The first returns true if the predicate is true for all elements, the second if it is true for at least one element. - - - +* `tuple_utilities.hpp` Contains some utilities for tuples: `tuple_common_type_t` that returns the common tpe of all types contained in a tuple, and `for_each` and `for_each2` that apply (possibly in parallel) the function object `F` to all elements of the tuple. The first one returns a tuple with the result, the second one does not and is thus applicable also if `F` is a void function. `all_of` and `any_of`, that apply predicate `F` to all elements of a tuple. The first returns true if the predicate is true for all elements, the second if it is true for at least one element. +* `type_name.hpp` An utility to pretty-print the name of the type of a variable. It is useful for debugging. It is based on the `boost::core::demangle` function. + ** Note ** `Factory.hpp` and `Proxy.hpp` are in fact links to the same file in the folder `GenericFactory`. If the files are not present for some reason you may safely copy in `Utility/` the files in `GenericFactory/`. diff --git a/Examples/src/Utilities/test_type_name.cpp b/Examples/src/Utilities/test_type_name.cpp new file mode 100644 index 00000000..1d63babb --- /dev/null +++ b/Examples/src/Utilities/test_type_name.cpp @@ -0,0 +1,12 @@ +#include "type_name.hpp" +#include +int +main() +{ + std::cout << apsc::type_name() << std::endl; + std::cout << apsc::type_name() << std::endl; + double *x = new double[10]; + std::cout << apsc::type_name() << std::endl; + std::cout << apsc::type_name() << std::endl; + return 0; +} \ No newline at end of file diff --git a/Examples/src/Utilities/type_name.hpp b/Examples/src/Utilities/type_name.hpp new file mode 100644 index 00000000..23626171 --- /dev/null +++ b/Examples/src/Utilities/type_name.hpp @@ -0,0 +1,36 @@ +#pragma once +// code based on +// https://stackoverflow.com/questions/81870/is-it-possible-to-print-a-variables-type-in-standard-c +#include +/*! +@brief Get the type name of a variable +@tparam T Type of the variable +@return The type name of the variable +@note possible usage: +@code +template +void foo(const T& variable) +{ + std::cout << type_name() << std::endl; +} +@endcode +*/ +namespace apsc +{ +template +constexpr std::string_view +type_name() +{ +#ifdef __clang__ + std::string_view p = __PRETTY_FUNCTION__; + return std::string_view(p.data() + 34, p.size() - 34 - 1); +#elif defined(__GNUC__) + std::string_view p = __PRETTY_FUNCTION__; +#if __cplusplus < 201402 + return std::string_view(p.data() + 36, p.size() - 36 - 1); +#else + return std::string_view(p.data() + 49, p.find(';', 49) - 49); +#endif +#endif +} +} // namespace apsc \ No newline at end of file