21 #ifndef CONFIGURATION_PARAMETERS_H
22 #define CONFIGURATION_PARAMETERS_H
28 #include <QStringList>
31 #include "parametersfileloadersaver.h"
32 #include "resourcesuser.h"
37 class ParameterSettable;
38 class ParameterSettableWithConfigureFunction;
39 class ConfigurationNode;
40 class FactoryObserver;
311 m_destructionResponsability(true)
318 FileFormat(
const FileFormat& other) :
319 formatName(other.formatName),
320 fileLoaderSaver(other.fileLoaderSaver),
321 extension(other.extension),
322 m_destructionResponsability(other.m_destructionResponsability)
326 other.m_destructionResponsability =
false;
332 FileFormat& operator=(
const FileFormat &other)
335 if (&other ==
this) {
339 formatName = other.formatName;
340 fileLoaderSaver = other.fileLoaderSaver;
341 extension = other.extension;
342 m_destructionResponsability = other.m_destructionResponsability;
346 other.m_destructionResponsability =
false;
357 if (m_destructionResponsability) {
358 delete fileLoaderSaver;
370 ParametersFileLoaderSaver *fileLoaderSaver;
384 mutable bool m_destructionResponsability;
397 static QMap<QString, FileFormat>& getFormatsMap();
409 static QMap<QString, QString>& getFileExtensionsMap();
419 ConfigurationParameters(
bool caseSensitive =
false);
429 ConfigurationParameters(
const ConfigurationParameters &other);
442 ConfigurationParameters& operator=(
const ConfigurationParameters &other);
447 ~ConfigurationParameters();
454 bool isCaseSensitive()
const;
468 QStringList getGroupsList(QString group)
const;
480 QStringList getGroupsWithPrefixList(QString group, QString prefix)
const;
492 QStringList getFilteredGroupsList(QString group, QRegExp filter)
const;
500 void createGroup(QString groupPath);
534 bool deleteGroup(QString groupPath);
573 bool renameGroup(QString oldGroupPath, QString newGroupName);
610 bool copyGroupTree(QString sourceGroup, QString destGroup);
622 QString fullpath = parentPath + GroupSeparator() + groupName;
623 createGroup( fullpath );
635 void createParameter(QString groupPath, QString parameter);
647 void createParameter(QString groupPath, QString parameter, QString value)
649 createParameter(groupPath, parameter);
650 setValue(groupPath + GroupSeparator() + parameter, value);
665 createParameter(groupPath, parameter);
666 setValue(groupPath + GroupSeparator() + parameter,
object);
676 void deleteParameter(QString groupPath, QString parameter);
721 bool startObjectParameters(QString groupPath, QString typeName,
ParameterSettable*
object);
750 QString getValue(QString path,
bool alsoMatchParents =
false)
const;
760 bool setValue(QString path, QString value);
778 QStringList getParametersList(QString group)
const;
790 QStringList getParametersWithPrefixList(QString group, QString prefix)
const;
802 QStringList getFilteredParametersList(QString group, QRegExp filter)
const;
821 void startRememberingGroupObjectAssociations();
828 void stopRememberingGroupObjectAssociations();
837 void resetGroupObjectAssociations();
870 template <
class TypeToCreate>
871 TypeToCreate* getObjectFromGroup(QString group,
bool configure =
true,
bool forceObjectCreation =
false);
911 template <
class TypeToCreate>
912 TypeToCreate* getObjectFromParameter(QString param,
bool alsoMatchParents =
false,
bool configure =
true,
bool forceObjectCreation =
false)
915 QString value = getValue(param, alsoMatchParents);
918 if (value.isEmpty()) {
921 return getObjectFromGroup<TypeToCreate>(value, configure, forceObjectCreation);
933 void updateObjectReferences();
947 bool loadParameters(QString filename,
bool keepOld =
false, QString format =
"");
965 bool saveParameters(QString filename, QString format =
"",
bool append =
false);
1006 QString formatFromFilenameExtension(QString filename)
const;
1031 bool setObjectFromGroupStatusToCreating(QString group);
1045 bool setObjectFromGroupStatusToCreatedNotConfigured(QString group,
ParameterSettable *
object);
1056 bool setObjectFromGroupStatusToConfiguring(QString group);
1068 bool setObjectFromGroupStatusToCreatingAndConfiguring(QString group);
1083 bool setObjectFromGroupStatusToCreatedAndConfigured(QString group,
ParameterSettable *
object = NULL);
1092 bool outsideCallsToGetObjectFromGroup()
const
1094 return (m_getObjectFromGroupRecursionLevel == 0);
1102 const std::auto_ptr<RealFactory> m_factory;
1107 std::auto_ptr<ConfigurationNode> m_root;
1115 unsigned int m_getObjectFromGroupRecursionLevel;
1125 struct ObjectAndRecursionLevel
1130 ParameterSettableWithConfigureFunction* object;
1135 unsigned int recursionLevel;
1148 QList<ObjectAndRecursionLevel> m_objectsToConfigure;
1155 bool m_dontForgetGroupObjectAssociations;
1164 QList<SimpleResourcesUser> m_resourcesUserPerRecursionLevel;
1170 template<
class T,
bool ConfigureInConstructor>
1187 #include "configurationnode.h"
1188 #include "parametersettable.h"
1189 #include "realfactory.h"
1194 template <
class TypeToCreate>
1201 class RecursionLevelRAII
1204 RecursionLevelRAII(
unsigned int *var, QList<ConfigurationParameters::ObjectAndRecursionLevel> *list, QList<SimpleResourcesUser> *resourcesUsers,
ConfigurationParameters ¶ms,
RealFactory& factory) :
1207 m_resourcesUsers(resourcesUsers),
1216 ~RecursionLevelRAII()
throw()
1221 void release(
bool doConfigure =
true)
1223 if (m_list != NULL) {
1224 while ((!m_list->isEmpty()) && (m_list->back().recursionLevel > (*m_var))) {
1227 m_list->back().object->configure(m_params, m_list->back().groupName +
GroupSeparator());
1229 m_factory.objectConfigured(m_list->back().object);
1235 if (m_var != NULL) {
1238 m_resourcesUsers->removeLast();
1243 unsigned int *m_var;
1244 QList<ObjectAndRecursionLevel> *m_list;
1245 QList<SimpleResourcesUser> *m_resourcesUsers;
1253 RecursionLevelRAII&
operator=(
const RecursionLevelRAII& other) {
return *
this; }
1265 if ((!m_dontForgetGroupObjectAssociations) && (m_getObjectFromGroupRecursionLevel == 0)) {
1272 if (m_getObjectFromGroupRecursionLevel == 0) {
1277 RecursionLevelRAII recursionLevelRAII(&m_getObjectFromGroupRecursionLevel, &m_objectsToConfigure, &m_resourcesUserPerRecursionLevel, *
this, *m_factory);
1280 TypeToCreate* retObj = NULL;
1282 std::auto_ptr<TypeToCreate> retObjRAII(NULL);
1289 bool actuallyConfigured =
true;
1290 switch (
object.objectStatus) {
1291 case ObjectNotCreated:
1293 retObj = m_factory->createFromParameter<TypeToCreate>(group, configure, &actuallyConfigured);
1294 retObjRAII.reset(retObj);
1296 case CreatingObject:
1299 if (forceObjectCreation) {
1305 case ObjectCreatedNotConfigured:
1308 if (forceObjectCreation) {
1312 retObj =
dynamic_cast<TypeToCreate *
>(
object.object);
1313 if (retObj == NULL) {
1327 m_factory->objectConfigured(o);
1331 case ConfiguringObject:
1334 if (forceObjectCreation) {
1342 retObj =
dynamic_cast<TypeToCreate *
>(
object.object);
1343 if (retObj == NULL) {
1348 case CreatingAndConfiguringObject:
1351 if (forceObjectCreation) {
1357 case ObjectCreatedAndConfigured:
1359 if (forceObjectCreation) {
1361 retObj = m_factory->createFromParameter<TypeToCreate>(group, configure, &actuallyConfigured);
1362 retObjRAII.reset(retObj);
1365 retObj =
dynamic_cast<TypeToCreate *
>(
object.object);
1366 if (retObj == NULL) {
1374 if (!actuallyConfigured) {
1375 ObjectAndRecursionLevel o;
1377 o.recursionLevel = m_getObjectFromGroupRecursionLevel;
1378 o.groupName = group;
1379 m_objectsToConfigure.push_back(o);
1384 recursionLevelRAII.release();
1391 if (m_factory->outsideCallsToCreate() && (m_getObjectFromGroupRecursionLevel == 0)) {
1392 m_factory->callPostConfigureInitializationForConfiguredObjects();
1395 if (m_objectsToConfigure.size() != 0) {
1396 qFatal(
"INTERNAL ERROR: The list of objects yet to be configured is not empty at the exit of the most external call to ConfigurationParameters::getObjectFromGroup(). The number of objects still in the list is %d", m_objectsToConfigure.size());
1400 retObjRAII.release();