Skip to content

Commit 81b2a35

Browse files
committed
ModuleMixin: Changed resolveExpression to prefer watches if there are multiple objects with the same name.
In the EligibilityTimeMeter module the numTokens name refers to both a watch and a statistic.
1 parent 34d253d commit 81b2a35

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/inet/common/ModuleMixin.h

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
#ifndef __INET_MODULEMIXIN_H
99
#define __INET_MODULEMIXIN_H
1010

11-
#include <type_traits>
12-
1311
#include "inet/common/StringFormat.h"
1412

13+
#include <type_traits>
14+
#include <vector>
15+
1516
namespace inet {
1617

1718
/**
@@ -27,6 +28,24 @@ class INET_API ModuleMixin : public T, public StringFormat::IResolver
2728
{
2829
static_assert(std::is_base_of<cModule, T>::value, "Type parameter of ModuleMixin must be a subclass of cModule");
2930

31+
protected:
32+
class cCollectObjectsVisitor : public cVisitor
33+
{
34+
public:
35+
const char *name;
36+
std::vector<cObject*> objects;
37+
38+
public:
39+
cCollectObjectsVisitor(const char *name): name(name) { }
40+
41+
protected:
42+
virtual bool visit(cObject *object) override {
43+
if (object->isName(name))
44+
objects.push_back(object);
45+
return true;
46+
}
47+
};
48+
3049
protected:
3150
virtual void initialize() override { T::initialize(); }
3251
virtual void initialize(int stage) override { T::initialize(stage); }
@@ -52,9 +71,16 @@ class INET_API ModuleMixin : public T, public StringFormat::IResolver
5271

5372
virtual std::string resolveExpression(const char *expression) const override
5473
{
55-
cObject *obj = const_cast<ModuleMixin<T>*>(this)->findObject(expression, false);
56-
if (obj)
57-
return obj->str();
74+
cCollectObjectsVisitor visitor(expression);
75+
visitor.processChildrenOf(const_cast<ModuleMixin<T>*>(this));
76+
if (!visitor.objects.empty()) {
77+
if (visitor.objects.size() > 1) {
78+
std::stable_sort(visitor.objects.begin(), visitor.objects.end(), [] (const cObject *o1, const cObject *o2) {
79+
return dynamic_cast<const cWatchBase *>(o1) != nullptr && dynamic_cast<const cWatchBase *>(o2) == nullptr;
80+
});
81+
}
82+
return visitor.objects[0]->str();
83+
}
5884
else
5985
throw cRuntimeError("Unknown expression: %s", expression);
6086
}

0 commit comments

Comments
 (0)