Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 155f46f

Browse files
committed
Merge pull request #352 from dawagner/fix-showMapping-segfault
Fix a segfault in the showMapping command.
2 parents 3d38fee + 4d07de1 commit 155f46f

File tree

12 files changed

+123
-59
lines changed

12 files changed

+123
-59
lines changed

parameter/ComponentInstance.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,7 @@ bool CComponentInstance::hasMappingData() const
7373

7474
std::string CComponentInstance::getFormattedMapping() const
7575
{
76-
// Try myself first then associated component type
77-
std::string strValue = base::getFormattedMapping();
78-
if (_pComponentType) {
79-
80-
strValue += _pComponentType->getFormattedMapping();
81-
}
82-
83-
return strValue;
76+
return base::getFormattedMapping(_pComponentType);
8477
}
8578

8679
bool CComponentInstance::fromXml(const CXmlElement &xmlElement,

parameter/ComponentType.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,7 @@ bool CComponentType::hasMappingData() const
6565

6666
std::string CComponentType::getFormattedMapping() const
6767
{
68-
// Try myself first then associated component type
69-
std::string strValue = base::getFormattedMapping();
70-
if (_pExtendsComponentType) {
71-
72-
strValue += _pExtendsComponentType->getFormattedMapping();
73-
}
74-
75-
return strValue;
68+
return base::getFormattedMapping(_pExtendsComponentType);
7669
}
7770

7871
bool CComponentType::fromXml(const CXmlElement &xmlElement,

parameter/ConfigurableElement.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ class PARAMETER_EXPORT CConfigurableElement : public CElement
154154
* @return true if @p strKey is found in the object's mapping, false if not
155155
*/
156156
virtual bool getMappingData(const std::string &strKey, const std::string *&pStrValue) const = 0;
157+
/** Get the string representation of the mapping
158+
*
159+
* If applicable, amend values are applied to the leaf element.
160+
*/
161+
virtual std::string getFormattedMapping() const = 0;
157162

158163
// XML configuration settings parsing
159164
virtual bool serializeXmlSettings(

parameter/InstanceConfigurableElement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class PARAMETER_EXPORT CInstanceConfigurableElement : public CConfigurableElemen
6767
*
6868
* @return A std::string containing the formatted mapping
6969
*/
70-
std::string getFormattedMapping() const;
70+
std::string getFormattedMapping() const override;
7171

7272
// From CElement
7373
virtual std::string getKind() const;

parameter/Subsystem.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
using std::string;
4444
using std::list;
45-
using std::ostringstream;
4645

4746
CSubsystem::CSubsystem(const string &strName, core::log::Logger &logger)
4847
: base(strName), _pComponentLibrary(new CComponentLibrary),
@@ -186,38 +185,38 @@ string CSubsystem::formatMappingDataList(
186185
{
187186
// The list is parsed in reverse order because it has been filled from the leaf to the trunk
188187
// of the tree. When formatting the mapping, we want to start from the subsystem level
189-
ostringstream ossStream;
188+
std::list<string> mappings;
190189
list<const CConfigurableElement *>::const_reverse_iterator it;
191190
for (it = configurableElementPath.rbegin(); it != configurableElementPath.rend(); ++it) {
192191

193-
const CInstanceConfigurableElement *pInstanceConfigurableElement =
194-
static_cast<const CInstanceConfigurableElement *>(*it);
195-
196-
ossStream << pInstanceConfigurableElement->getFormattedMapping() << ", ";
192+
auto maybeMapping = (*it)->getFormattedMapping();
193+
if (not maybeMapping.empty()) {
194+
mappings.push_back(maybeMapping);
195+
}
197196
}
198-
return ossStream.str();
197+
198+
return utility::asString(mappings, ", ");
199199
}
200200

201201
// Find the CSubystemObject containing a specific CInstanceConfigurableElement
202202
const CSubsystemObject *CSubsystem::findSubsystemObjectFromConfigurableElement(
203203
const CInstanceConfigurableElement *pInstanceConfigurableElement) const
204204
{
205205

206-
const CSubsystemObject *pSubsystemObject = NULL;
207-
208206
list<CSubsystemObject *>::const_iterator it;
209207
for (it = _subsystemObjectList.begin(); it != _subsystemObjectList.end(); ++it) {
210208

211209
// Check if one of the SubsystemObjects is associated with a ConfigurableElement
212210
// corresponding to the expected one
213-
pSubsystemObject = *it;
211+
const CSubsystemObject *pSubsystemObject = *it;
212+
214213
if (pSubsystemObject->getConfigurableElement() == pInstanceConfigurableElement) {
215214

216-
break;
215+
return pSubsystemObject;
217216
}
218217
}
219218

220-
return pSubsystemObject;
219+
return nullptr;
221220
}
222221

223222
void CSubsystem::findSubsystemLevelMappingKeyValue(
@@ -277,14 +276,16 @@ string CSubsystem::getMapping(list<const CConfigurableElement *> &configurableEl
277276
// Get the first element, which is the element containing the amended mapping
278277
const CInstanceConfigurableElement *pInstanceConfigurableElement =
279278
static_cast<const CInstanceConfigurableElement *>(configurableElementPath.front());
280-
configurableElementPath.pop_front();
281-
// Now the list only contains elements whose mapping are related to the context
282279

283280
// Format context mapping data
284281
string strValue = formatMappingDataList(configurableElementPath);
285282

286283
// Print the mapping of the first node, which corresponds to a SubsystemObject
287-
strValue += getFormattedSubsystemMappingData(pInstanceConfigurableElement);
284+
auto subsystemObjectAmendedMapping =
285+
getFormattedSubsystemMappingData(pInstanceConfigurableElement);
286+
if (not subsystemObjectAmendedMapping.empty()) {
287+
strValue += ", " + subsystemObjectAmendedMapping;
288+
}
288289

289290
return strValue;
290291
}
@@ -330,6 +331,15 @@ bool CSubsystem::getMappingData(const std::string &strKey, const std::string *&p
330331
return false;
331332
}
332333

334+
// Returns the formatted mapping
335+
std::string CSubsystem::getFormattedMapping() const
336+
{
337+
if (!_pMappingData) {
338+
return "";
339+
}
340+
return _pMappingData->asString();
341+
}
342+
333343
// Mapping generic context handling
334344
bool CSubsystem::handleMappingContext(const CConfigurableElement *pConfigurableElement,
335345
CMappingContext &context, string &strError) const

parameter/Subsystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class PARAMETER_EXPORT CSubsystem : public CConfigurableElement, private IMapper
7676
virtual std::string getKind() const;
7777

7878
virtual bool getMappingData(const std::string &strKey, const std::string *&pStrValue) const;
79+
std::string getFormattedMapping() const override;
7980

8081
/**
8182
* Fetch mapping data of an element.

parameter/SystemClass.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ bool CSystemClass::getMappingData(const std::string & /*strKey*/,
8787
return false;
8888
}
8989

90+
string CSystemClass::getFormattedMapping() const
91+
{
92+
return "";
93+
}
94+
9095
bool CSystemClass::loadSubsystems(string &strError, const CSubsystemPlugins *pSubsystemPlugins,
9196
bool bVirtualSubsystemFallback)
9297
{

parameter/SystemClass.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class CSystemClass final : public CConfigurableElement
8484
virtual std::string getKind() const;
8585

8686
bool getMappingData(const std::string &strKey, const std::string *&pStrValue) const override;
87+
std::string getFormattedMapping() const override;
8788

8889
private:
8990
CSystemClass(const CSystemClass &);

parameter/TypeElement.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "MappingData.h"
3232
#include "Tokenizer.h"
3333
#include "InstanceConfigurableElement.h"
34+
#include "Utility.h"
35+
#include <list>
3436
#include <assert.h>
3537

3638
#define base CElement
@@ -141,6 +143,30 @@ CMappingData *CTypeElement::getMappingData()
141143
return _pMappingData;
142144
}
143145

146+
std::string CTypeElement::getFormattedMapping(const CTypeElement *predecessor) const
147+
{
148+
std::list<std::string> mappings;
149+
std::string mapping;
150+
151+
// Try predecessor type first, then myself (in order to have higher-level
152+
// mappings displayed first)
153+
if (predecessor) {
154+
mapping = predecessor->getFormattedMapping();
155+
if (not mapping.empty()) {
156+
mappings.push_back(mapping);
157+
}
158+
}
159+
160+
// Explicitly call the root implementation instead of calling it virtually
161+
// (otherwise, it will infinitely recurse).
162+
mapping = CTypeElement::getFormattedMapping();
163+
if (not mapping.empty()) {
164+
mappings.push_back(mapping);
165+
}
166+
167+
return utility::asString(mappings, ", ");
168+
}
169+
144170
std::string CTypeElement::getFormattedMapping() const
145171
{
146172
if (_pMappingData) {

parameter/TypeElement.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ class PARAMETER_EXPORT CTypeElement : public CElement
8484
protected:
8585
// Object creation
8686
virtual void populate(CElement *pElement) const;
87+
/** @Returns the mapping associated to the current type and its predecessor
88+
*
89+
* The meaning of predecessor depends on the TypeElement type: e.g. for a
90+
* component instance, the predecessor is the ComponentType; for a
91+
* ComponentType, the predecessor is its base type.
92+
*
93+
* The predecessor's mapping comes first, then the current type's mapping.
94+
*
95+
* @param[in] predecessor A pointer to the predecessor or NULL.
96+
*/
97+
std::string getFormattedMapping(const CTypeElement *predecessor) const;
8798

8899
private:
89100
CTypeElement(const CTypeElement &);

0 commit comments

Comments
 (0)