typeName.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "typeName.H"
27 
28 #ifdef __GNUC__
29 
30 #include <cxxabi.h>
31 
32 #endif
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
36 template<>
37 Foam::string Foam::typeName<Foam::string>(const std::type_info& info)
38 {
39  #ifdef __GNUC__
40 
41  // Get the un-mangled name
42  int status;
43  char* nameCharStar =
44  abi::__cxa_demangle(info.name(), nullptr, nullptr, &status);
45  string nameString(nameCharStar);
46  std::free(nameCharStar);
47 
48  // Remove 'Foam::' namespace
49  nameString.replaceAll("Foam::", "");
50 
51  return nameString;
52 
53  #else
54 
55  return info.name();
56 
57  #endif
58 }
59 
60 
61 template<>
62 Foam::word Foam::typeName<Foam::word>(const std::type_info& info)
63 {
64  string nameString = typeName<string>(info);
65 
66  // Remove all white-space and camel-case as needed
67  size_t i0 = 0, i1 = 0;
68  bool camel = false;
69  while (nameString[i0] == ' ') ++ i0;
70  for (; i0 < nameString.size(); ++ i0)
71  {
72  if (nameString[i0] != ' ')
73  {
74  nameString[i1] = camel ? toupper(nameString[i0]) : nameString[i0];
75  i1 ++;
76  camel = false;
77  }
78  else if (i1 != 0 && isalnum(nameString[i1 - 1]))
79  {
80  camel = true;
81  }
82  }
83  nameString.resize(i1);
84 
85  return nameString;
86 }
87 
88 
89 // ************************************************************************* //
A class for handling character strings derived from std::string.
Definition: string.H:79
Template function which returns the un-mangled name of a given type. Useful for types which do not ha...
A class for handling words, derived from string.
Definition: word.H:63
string typeName< string >(const std::type_info &info)
Return the un-mangled name as a string, given the standard type info.