MCPcopy Create free account
hub / github.com/Icinga/icinga2 / CodeGenValidator

Method CodeGenValidator

tools/mkclass/classcompiler.cpp:1106–1296  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1104}
1105
1106void ClassCompiler::CodeGenValidator(const std::string& name, const std::string& klass, const std::vector<Rule>& rules, const std::string& field, const FieldType& fieldType, ValidatorType validatorType)
1107{
1108 m_Impl << "static void TIValidate" << name << "(const intrusive_ptr<ObjectImpl<" << klass << "> >& object, ";
1109
1110 if (validatorType != ValidatorField)
1111 m_Impl << "const String& key, ";
1112
1113 m_Impl << fieldType.GetArgumentType() << " value, std::vector<String>& location, const ValidationUtils& utils)" << std::endl
1114 << "{" << std::endl;
1115
1116 if (validatorType == ValidatorField) {
1117 bool required = false;
1118
1119 for (const Rule& rule : rules) {
1120 if ((rule.Attributes & RARequired) && rule.Pattern == field) {
1121 required = true;
1122 break;
1123 }
1124 }
1125
1126 if (fieldType.GetRealType() != "int" && fieldType.GetRealType() != "double") {
1127 if (fieldType.GetRealType() == "Value" || fieldType.GetRealType() == "String")
1128 m_Impl << "\t" << "if (value.IsEmpty())" << std::endl;
1129 else
1130 m_Impl << "\t" << "if (!value)" << std::endl;
1131
1132 if (required)
1133 m_Impl << "BOOST_THROW_EXCEPTION(ValidationError(dynamic_cast<ConfigObject *>(this), location, \"This attribute must not be empty.\"));" << std::endl;
1134 else
1135 m_Impl << "\t\t" << "return;" << std::endl;
1136
1137 m_Impl << std::endl;
1138 }
1139 }
1140
1141 if (validatorType != ValidatorField)
1142 m_Impl << "\t" << "bool known_attribute = false;" << std::endl;
1143
1144 bool type_check = false;
1145 int i = 0;
1146
1147 for (const Rule& rule : rules) {
1148 if (rule.Attributes & RARequired)
1149 continue;
1150
1151 i++;
1152
1153 if (validatorType == ValidatorField && rule.Pattern != field)
1154 continue;
1155
1156 m_Impl << "\t" << "do {" << std::endl;
1157
1158 if (validatorType != ValidatorField) {
1159 if (rule.Pattern != "*") {
1160 if (rule.Pattern.find_first_of("*?") != std::string::npos)
1161 m_Impl << "\t\t" << "if (!Utility::Match(\"" << rule.Pattern << "\", key))" << std::endl;
1162 else
1163 m_Impl << "\t\t" << "if (key != \"" << rule.Pattern << "\")" << std::endl;

Callers

nothing calls this directly

Calls 3

GetArgumentTypeMethod · 0.80
GetRealTypeMethod · 0.80
emptyMethod · 0.45

Tested by

no test coverage detected