Skip to content

Commit

Permalink
added tool to pretty-print type names
Browse files Browse the repository at this point in the history
  • Loading branch information
lformaggia committed Jan 1, 2025
1 parent cd119e9 commit 1161a94
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions Examples/CONTENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!`
Expand Down
7 changes: 3 additions & 4 deletions Examples/src/Utilities/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tuple>` that returns the common tpe of all types contained in a tuple, and `for_each<Tuple F>` and `for_each2<Tuple, F>` 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<Tuple,F>` and `any_of<Tuple,F>`, 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<Tuple>` that returns the common tpe of all types contained in a tuple, and `for_each<Tuple F>` and `for_each2<Tuple, F>` 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<Tuple,F>` and `any_of<Tuple,F>`, 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/`.


Expand Down
12 changes: 12 additions & 0 deletions Examples/src/Utilities/test_type_name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "type_name.hpp"
#include <iostream>
int
main()
{
std::cout << apsc::type_name<int>() << std::endl;
std::cout << apsc::type_name<double>() << std::endl;
double *x = new double[10];
std::cout << apsc::type_name<decltype(x)>() << std::endl;
std::cout << apsc::type_name<std::string>() << std::endl;
return 0;
}
36 changes: 36 additions & 0 deletions Examples/src/Utilities/type_name.hpp
Original file line number Diff line number Diff line change
@@ -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 <string_view>
/*!
@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 <class T>
void foo(const T& variable)
{
std::cout << type_name<T>() << std::endl;
}
@endcode
*/
namespace apsc
{
template <class T>
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

0 comments on commit 1161a94

Please sign in to comment.