1 /******************************************************************************
3 * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #ifndef CLASSCOMPILER_H
21 #define CLASSCOMPILER_H
41 enum FieldAccessorType
52 FieldAccessorType Type;
56 FieldAccessor(FieldAccessorType type, const std::string& accessor, bool pure)
57 : Type(type), Accessor(accessor), Pure(pure)
61 /* keep this in sync with lib/base/type.hpp */
71 FALoadDependency = 128,
74 FANoUserModify = 1024,
85 : IsName(false), ArrayRank(0)
88 inline std::string GetRealType(void) const
99 inline std::string GetArgumentType(void) const
101 std::string realType = GetRealType();
103 if (realType == "bool" || realType == "double" || realType == "int")
106 return "const " + realType + "&";
115 std::string AlternativeName;
116 std::string GetAccessor;
117 bool PureGetAccessor;
118 std::string SetAccessor;
119 bool PureSetAccessor;
120 std::string DefaultAccessor;
121 std::string TrackAccessor;
122 std::string NavigationName;
123 std::string NavigateAccessor;
124 bool PureNavigateAccessor;
127 : Attributes(0), PureGetAccessor(false), PureSetAccessor(false), PureNavigateAccessor(false)
130 inline std::string GetFriendlyName(void) const
132 if (!AlternativeName.empty())
133 return AlternativeName;
136 std::string name = Name;
138 for (size_t i = 0; i < name.size(); i++) {
139 if (name[i] == '_') {
145 name[i] = toupper(name[i]);
151 std::remove(name.begin(), name.end(), '_'),
155 /* TODO: figure out name */
169 std::string TypeBase;
171 std::vector<Field> Fields;
172 std::vector<std::string> LoadDependencies;
187 std::vector<Rule> Rules;
200 std::vector<Rule> Rules;
206 ClassCompiler(const std::string& path, std::istream& input, std::ostream& oimpl, std::ostream& oheader);
207 ~ClassCompiler(void);
211 std::string GetPath(void) const;
213 void InitializeScanner(void);
214 void DestroyScanner(void);
216 void *GetScanner(void);
218 size_t ReadInput(char *buffer, size_t max_size);
220 void HandleInclude(const std::string& path, const ClassDebugInfo& locp);
221 void HandleAngleInclude(const std::string& path, const ClassDebugInfo& locp);
222 void HandleImplInclude(const std::string& path, const ClassDebugInfo& locp);
223 void HandleAngleImplInclude(const std::string& path, const ClassDebugInfo& locp);
224 void HandleClass(const Klass& klass, const ClassDebugInfo& locp);
225 void HandleValidator(const Validator& validator, const ClassDebugInfo& locp);
226 void HandleNamespaceBegin(const std::string& name, const ClassDebugInfo& locp);
227 void HandleNamespaceEnd(const ClassDebugInfo& locp);
228 void HandleCode(const std::string& code, const ClassDebugInfo& locp);
229 void HandleLibrary(const std::string& library, const ClassDebugInfo& locp);
230 void HandleMissingValidators(void);
232 void CodeGenValidator(const std::string& name, const std::string& klass, const std::vector<Rule>& rules, const std::string& field, const FieldType& fieldType, ValidatorType validatorType);
233 void CodeGenValidatorSubrules(const std::string& name, const std::string& klass, const std::vector<Rule>& rules);
235 static void CompileFile(const std::string& inputpath, const std::string& implpath,
236 const std::string& headerpath);
237 static void CompileStream(const std::string& path, std::istream& input,
238 std::ostream& oimpl, std::ostream& oheader);
240 static void OptimizeStructLayout(std::vector<Field>& fields);
244 std::istream& m_Input;
245 std::ostream& m_Impl;
246 std::ostream& m_Header;
249 std::string m_Library;
251 std::map<std::pair<std::string, std::string>, Field> m_MissingValidators;
253 static unsigned long SDBM(const std::string& str, size_t len);
254 static std::string BaseName(const std::string& path);
255 static std::string FileNameToGuardName(const std::string& path);
260 #endif /* CLASSCOMPILER_H */