8
8
#ifndef __INET_MODULEMIXIN_H
9
9
#define __INET_MODULEMIXIN_H
10
10
11
- #include < type_traits>
12
-
13
11
#include " inet/common/StringFormat.h"
14
12
13
+ #include < type_traits>
14
+ #include < vector>
15
+
15
16
namespace inet {
16
17
17
18
/* *
@@ -27,6 +28,24 @@ class INET_API ModuleMixin : public T, public StringFormat::IResolver
27
28
{
28
29
static_assert (std::is_base_of<cModule, T>::value, " Type parameter of ModuleMixin must be a subclass of cModule" );
29
30
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
+
30
49
protected:
31
50
virtual void initialize () override { T::initialize (); }
32
51
virtual void initialize (int stage) override { T::initialize (stage); }
@@ -52,9 +71,16 @@ class INET_API ModuleMixin : public T, public StringFormat::IResolver
52
71
53
72
virtual std::string resolveExpression (const char *expression) const override
54
73
{
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
+ }
58
84
else
59
85
throw cRuntimeError (" Unknown expression: %s" , expression);
60
86
}
0 commit comments