PhoenixGenerator  3.4.0
Generate code from template files and projects in C++, Rust, Python and wrapped C++ to Python
Loading...
Searching...
No Matches
type_utils.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : LGPL-3.0-only
5****************************************/
6
7
8#include "type_utils.h"
9
11
14bool generator_typeIsList(const PString & type){
15 return type.isSameBegining("std::list");
16}
17
19
22PString generator_getListNestedType(const PString & type){
23 if(!generator_typeIsList(type)){return "";}
24 PString subType(type.substr(9lu));
25 return subType.eraseFirstLastChar("<> \t\n");
26}
27
29
32bool getIsSimpleType(const PString & varType){
33 PString restVarName(varType.replace("unsigned", "").eraseChar(" \n\t*&"));
34 return (restVarName == "char" || restVarName == "short" || restVarName == "int" || restVarName == "float" || restVarName == "double" || restVarName == "bool" || restVarName == "long" || restVarName == "longint" || restVarName == "size_t" || restVarName == "ssize_t" || restVarName == "void");
35}
36
38
41bool getIsPointerType(const PString & varType){
42 if(varType.size() == 0LU){return false;}
43 return varType.find('*') && varType.back() == '*';
44}
45
47
50PString getDefaultValueTypeInCpp(const PString & type){
51 if(type == "PString" || type == "std::string"){
52 return "\"\"";
53 }else if(type == "PPath"){
54 return "\"\"";
55 }else if(type == "char"){
56 return "0";
57 }else if(type == "unsigned char"){
58 return "0";
59 }else if(type == "int" || type == "short"){
60 return "0";
61 }else if(type == "unsigned int" || type == "unsigned short"){
62 return "0U";
63 }else if(type == "long int" || type == "long" || type == "ssize_t"){
64 return "0l";
65 }else if(type == "size_t" || type == "long unsigned int"){
66 return "0LU";
67 }else if(type == "bool"){
68 return "false";
69 }else if(type == "float"){
70 return "0.0f";
71 }else if(type == "double"){
72 return "0.0";
73 }else{
74 return "";
75 }
76}
77
79
82PString getDefaultValueTypeInPython(const PString & type){
83 if(type == "PString" || type == "std::string"){
84 return "\"\"";
85 }else if(type == "PPath"){
86 return "\"\"";
87 }else if(type == "char" || type == "unsigned char" || type == "int" || type == "short" || type == "unsigned int" || type == "unsigned short"){
88 return "0";
89 }else if(type == "long int" || type == "long" || type == "ssize_t" || type == "size_t" || type == "long unsigned int"){
90 return "0";
91 }else if(type == "bool"){
92 return "False";
93 }else if(type == "float" || type == "double"){
94 return "0.0";
95 }else{
96 return "";
97 }
98}
99
101
104PString getTestDefaultValueTypeInCpp(const PString & type){
105 if(type == "PString" || type == "std::string"){
106 return "\"Some string\"";
107 }else if(type == "PPath"){
108 return "PPath(\"Some/Path\")";
109 }else if(type == "char"){
110 return "8";
111 }else if(type == "unsigned char"){
112 return "32";
113 }else if(type == "int" || type == "short"){
114 return "42";
115 }else if(type == "unsigned int" || type == "unsigned short"){
116 return "32u";
117 }else if(type == "long int" || type == "long" || type == "ssize_t"){
118 return "328l";
119 }else if(type == "size_t" || type == "long unsigned int"){
120 return "3423420LU";
121 }else if(type == "bool"){
122 return "true";
123 }else if(type == "float"){
124 return "1.0f";
125 }else if(type == "double"){
126 return "1.0";
127 }else{
128 return "";
129 }
130}
131
133
136PString getTestDefaultValueTypeInPython(const PString & type){
137 if(type == "PString" || type == "std::string"){
138 return "\"Some string\"";
139 }else if(type == "PPath"){
140 return "\"Some/Path\"";
141 }else if(type == "char" || type == "unsigned char" || type == "int" || type == "short" || type == "unsigned int" || type == "unsigned short"){
142 return "42";
143 }else if(type == "long int" || type == "long" || type == "ssize_t" || type == "size_t" || type == "long unsigned int" || type == "time_t"){
144 return "328";
145 }else if(type == "bool"){
146 return "True";
147 }else if(type == "float" || type == "double"){
148 return "1.0";
149 }else{
150 return "";
151 }
152}
153
155
162PString makeVarType(const PString & varType, bool isSetter, bool isConst, bool isRef, bool isPtr){
163 PString varTypeName("");
164// std::cout << "makeVarType : varType = '" << varType << "'" << std::endl;
165 bool isSimpleType = getIsSimpleType(varType);
166 if(isSimpleType || isPtr){
167 if(isConst && !isSetter && (!isSimpleType || isPtr)){varTypeName += "const ";}
168 varTypeName += varType;
169 if(isRef && !isPtr){varTypeName += " &";}
170 }else{
171 if(isConst && !isPtr){varTypeName += "const ";}
172 varTypeName += varType + " &";
173 }
174 return varTypeName;
175}
176
177
178
179
PString getTestDefaultValueTypeInCpp(const PString &type)
Get the default value of a type in C++.
PString generator_getListNestedType(const PString &type)
Get the nested type inside a std::list.
PString getDefaultValueTypeInPython(const PString &type)
Get default value for a given type in Python.
bool generator_typeIsList(const PString &type)
Say if a given type is a std::list.
PString getTestDefaultValueTypeInPython(const PString &type)
Get default test value for a given type in Python.
bool getIsPointerType(const PString &varType)
Say if a given type is a pointer type (i.e. it ends with a '*').
PString makeVarType(const PString &varType, bool isSetter, bool isConst, bool isRef, bool isPtr)
Makes the var type by taking account of the type.
PString getDefaultValueTypeInCpp(const PString &type)
Get the default value of a type in C++.
bool getIsSimpleType(const PString &varType)
Check if the given type is a simple type.