27 #include <SimoxUtility/algorithm/string/string_tools.h>
28 #include <boost/format.hpp>
29 #include <boost/regex.hpp>
36 #include "../GroupXmlReader.h"
49 std::tie(cppClass, cppClassBase) = BuildClass(namespaces, proxies, groupName, variantInfo, usedVariantTypes);
51 cppClass->writeCpp(writer);
53 cppClassBase->writeCpp(writerBase);
54 return std::make_tuple(writer->getString(), writerBase->getString());
57 std::tuple<CppClassPtr, CppClassPtr> XmlContextBaseClassGenerator::BuildClass(std::vector<std::string> namespaces, std::vector<std::string> proxies, std::string groupName,
VariantInfoPtr variantInfo,
const std::set<std::string>& usedVariantTypes)
59 std::string className = groupName +
"StatechartContext";
61 for (std::string& ns : namespaces)
63 ns = simox::alg::replace_all(ns,
"-",
"_");
69 cppImplClass->addInherit(
"virtual public XMLStatechartContext");
70 cppImplClass->addInherit(
"public " + cppBaseClass->getName());
72 cppImplClass->addInclude(
"<ArmarXCore/core/Component.h>");
73 cppImplClass->addInclude(
"<ArmarXCore/core/system/ImportExportComponent.h>");
74 cppImplClass->addInclude(
"<ArmarXCore/statechart/StatechartContext.h>");
75 cppImplClass->addInclude(
"\"" + className +
"Base.generated.h\"");
76 cppBaseClass->addInclude(
"<ArmarXCore/core/system/ImportExportComponent.h>");
78 std::vector<VariantInfo::ProxyEntryPtr> proxyEntries;
80 for (std::string proxyId : proxies)
86 proxyEntries.push_back(p);
90 CppClassPtr propertiesClass = cppImplClass->addInnerClass(
"PropertyDefinitions");
91 propertiesClass->addInherit(
"public StatechartContextPropertyDefinitions");
93 CppCtorPtr propertiesCtor = propertiesClass->addCtor(
"std::string prefix");
94 propertiesCtor->addInitListEntry(
"StatechartContextPropertyDefinitions",
"prefix");
96 CppMethodPtr getDefaultName = cppImplClass->addPublicMethod(
"std::string getDefaultName() const override");
97 CppMethodPtr onInit = cppImplClass->addPublicMethod(
"void onInitStatechartContext() override");
98 CppMethodPtr onConnect = cppImplClass->addPublicMethod(
"void onConnectStatechartContext() override");
100 getDefaultName->addLine(fmt(
"return \"%s\";", className));
104 std::string description = fmt(
"Name of the %s that should be used", simox::alg::to_lower(magicNameSplit(p->getPropertyName())));
106 if (p->getPropertyIsOptional())
108 propertiesCtor->addLine(fmt(
"defineOptionalProperty<std::string>(\"%s\", \"%s\", \"%s\");", p->getPropertyName(), p->getPropertyDefaultValue(), description));
112 propertiesCtor->addLine(fmt(
"defineRequiredProperty<std::string>(\"%s\", \"%s\");", p->getPropertyName(), description));
118 onInit->addLine(fmt(
"usingProxy(getProperty<std::string>(\"%s\").getValue());", p->getPropertyName()));
119 onConnect->addLine(fmt(
"%s = getProxy<%s>(getProperty<std::string>(\"%s\").getValue());", p->getMemberName(), p->getTypeName(), p->getPropertyName()));
123 onInit->addLine(fmt(
"offeringTopic(getProperty<std::string>(\"%s\").getValue());", p->getPropertyName()));
124 onConnect->addLine(fmt(
"%s = getTopic<%s>(getProperty<std::string>(\"%s\").getValue());", p->getMemberName(), p->getTypeName(), p->getPropertyName()));
128 throw LocalException(
"Not supported ProxyType");
132 cppImplClass->addPrivateField(f);
133 std::string proxyFuncVirtual = fmt(
"virtual const %s& %s() const = 0;", p->getTypeName(), p->getGetterName());
134 std::string proxyFuncOverride = fmt(
"const %s& %s() const override", p->getTypeName(), p->getGetterName());
135 CppMethodPtr virtualGetProxy = cppBaseClass->addPublicMethod(proxyFuncVirtual);
136 CppMethodPtr getProxy = cppImplClass->addPublicMethod(proxyFuncOverride);
137 getProxy->addLine(fmt(
"return %s;", p->getMemberName()));
138 getProxy->setCompact(
true);
139 getProxy->setEnforceBlockGeneration(
true);
140 cppBaseClass->addInclude(fmt(
"<%s>", p->getIncludePath()));
147 for (std::string member : p->getMembers())
150 cppImplClass->addPrivateField(f);
153 for (std::string include : p->getIncludes())
155 cppBaseClass->addInclude(fmt(
"<%s>", include));
158 for (std::string oni : p->getOnInit())
160 onInit->addLine(oni);
163 for (std::string onc : p->getOnConnect())
165 onConnect->addLine(onc);
168 for (std::pair<std::string, std::string> method : p->getMethods())
170 cppImplClass->addPublicMethod(method.first +
" override")->addLine(method.second);
171 cppBaseClass->addPublicMethod(
"virtual " + method.first +
" = 0;");
175 const std::string baseVirtualDtor = fmt(
"virtual ~%sBase() = default;", className);
176 cppBaseClass->addPublicMethod(baseVirtualDtor);
179 CppMethodPtr createPropertyDefinitions = cppImplClass->addPublicMethod(
"PropertyDefinitionsPtr createPropertyDefinitions() override");
180 createPropertyDefinitions->addLine(fmt(
"return PropertyDefinitionsPtr(new %s::PropertyDefinitions(getConfigIdentifier()));", className));
182 if (usedVariantTypes.size() > 0)
186 forceLibLoading->addLine(
"// Do not call this method.");
187 forceLibLoading->addLine(
"// The sole purpose of this method is to force the compiler/linker to include all libraries.");
190 std::set<std::string> includes;
191 for (std::string innerNonBasicVariantDataType : usedVariantTypes)
196 for (
auto& include : lib->getFactoryIncludes())
198 includes.insert(include);
200 for (
auto& include : lib->getVariantIncludes(innerNonBasicVariantDataType))
202 includes.insert(include);
204 std::string implementationType = variantInfo->getDataTypeName(innerNonBasicVariantDataType);
205 forceLibLoading->addLine(fmt(
"armarx::GenericFactory< %s, %s> ().create(%s::ice_staticId());", implementationType, implementationType, implementationType));
208 for (
auto& include : includes)
210 cppImplClass->addInclude(
"<" + include +
">");
213 cppImplClass->addPublicMethod(forceLibLoading);
216 return std::make_tuple(cppImplClass, cppBaseClass);
219 std::string XmlContextBaseClassGenerator::magicNameSplit(std::string name)
223 name = name.substr(0, name.size() - 4);
226 boost::regex re(
"([A-Z][a-z])");
227 name = boost::regex_replace(name, re,
" \\1");
228 name = simox::alg::to_lower(name);
229 simox::alg::trim(name);
233 std::string XmlContextBaseClassGenerator::fmt(
const std::string& fmt,
const std::string& arg1)
235 return str(boost::format(fmt) % arg1);
238 std::string XmlContextBaseClassGenerator::fmt(
const std::string& fmt,
const std::string& arg1,
const std::string& arg2)
240 return str(boost::format(fmt) % arg1 % arg2);
243 std::string XmlContextBaseClassGenerator::fmt(
const std::string& fmt,
const std::string& arg1,
const std::string& arg2,
const std::string& arg3)
245 return str(boost::format(fmt) % arg1 % arg2 % arg3);