Develop Biology
The language of life
TypeName.h
Go to the documentation of this file.
1/*
2 * This file is a part of the Biology project by eons LLC.
3 * Biology (aka Develop Biology) is a framework for approaching software
4 * development from a natural sciences perspective.
5 *
6 * Copyright (C) 2021 Séon O'Shannon & eons LLC
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <string>
25
26//Modified from: https://stackoverflow.com/questions/35941045/can-i-obtain-c-type-names-in-a-constexpr-way
27namespace bio {
28
34template < typename T >
35const std::string TypeName();
36
40template <>
41inline const std::string TypeName< void >()
42{
43 return "void";
44}
45
49typedef void TypeNameProber;
50
57template < typename T >
58static const std::string WrappedTypeName()
59{
60 #ifdef __clang__
61 return __PRETTY_FUNCTION__;
62 #elif defined(__GNUC__)
63 return __PRETTY_FUNCTION__;
64 #elif defined(_MSC_VER)
65 return __FUNCSIG__;
66 #else
67 #error "Unsupported compiler"
68 #endif
69}
70
75static const std::size_t WrappedTypeNamePrefixLength()
76{
77 return WrappedTypeName< TypeNameProber >().find(TypeName< TypeNameProber >());
78}
79
84static const std::size_t WrappedTypeNameSuffixLength()
85{
86 return WrappedTypeName< TypeNameProber >().length() - WrappedTypeNamePrefixLength() - TypeName< TypeNameProber >().length();
87}
88
94template < typename T >
95static const std::string TypeName()
96{
97 static const std::string wrappedName = WrappedTypeName< T >();
98 static const std::size_t prefixLength = WrappedTypeNamePrefixLength();
99 static const std::size_t suffixLength = WrappedTypeNameSuffixLength();
100 static const std::size_t typeNameLength = wrappedName.length() - prefixLength - suffixLength;
101 return wrappedName.substr(
102 prefixLength,
103 typeNameLength
104 );
105}
106
113template < typename T >
114const std::string TypeName(const T t)
115{
116 return TypeName< T >();
117}
118
119} //bio namespace
Definition: Cell.h:31
const std::string TypeName()
static const std::size_t WrappedTypeNamePrefixLength()
Definition: TypeName.h:75
static const std::size_t WrappedTypeNameSuffixLength()
Definition: TypeName.h:84
const std::string TypeName< void >()
Definition: TypeName.h:41
static const std::string WrappedTypeName()
Definition: TypeName.h:58
void TypeNameProber
Definition: TypeName.h:49