INET Framework for OMNeT++/OMNEST
inet::MatchableObject Class Reference

Wrapper around a cObject to make it matchable with cMatchExpression. More...

#include <MatchableObject.h>

Inheritance diagram for inet::MatchableObject:

Public Types

enum  Attribute { ATTRIBUTE_FULLNAME, ATTRIBUTE_FULLPATH, ATTRIBUTE_CLASSNAME }
 

Public Member Functions

 MatchableObject (Attribute defaultAttribute=ATTRIBUTE_FULLPATH, const cObject *object=nullptr)
 
void setObject (const cObject *object)
 
void setDefaultAttribute (Attribute defaultAttribute)
 
virtual const char * getAsString () const override
 
virtual const char * getAsString (const char *attribute) const override
 

Static Protected Member Functions

static void splitIndex (char *indexedName, int &index)
 
static bool findDescriptorField (cClassDescriptor *classDescriptor, const char *attribute, int &fieldId, int &index)
 

Protected Attributes

Attribute defaultAttribute
 
const cObject * object
 
cClassDescriptor * classDescriptor
 
std::string attributeValue
 

Detailed Description

Wrapper around a cObject to make it matchable with cMatchExpression.

Member Enumeration Documentation

◆ Attribute

Enumerator
ATTRIBUTE_FULLNAME 
ATTRIBUTE_FULLPATH 
ATTRIBUTE_CLASSNAME 

Constructor & Destructor Documentation

◆ MatchableObject()

inet::MatchableObject::MatchableObject ( Attribute  defaultAttribute = ATTRIBUTE_FULLPATH,
const cObject *  object = nullptr 
)
13 {
15  this->object = object;
16  classDescriptor = nullptr;
17 }

Member Function Documentation

◆ findDescriptorField()

bool inet::MatchableObject::findDescriptorField ( cClassDescriptor *  classDescriptor,
const char *  attribute,
int &  fieldId,
int &  index 
)
staticprotected
52 {
53  // attribute may be in the form "fieldName[index]"; split the two
54  char *fieldNameBuf = new char[strlen(attribute) + 1];
55  strcpy(fieldNameBuf, attribute);
56  splitIndex(fieldNameBuf, index);
57 
58  // find field by name
59  fieldId = classDescriptor->findField(fieldNameBuf);
60  delete[] fieldNameBuf;
61  return fieldId != -1;
62 }

Referenced by getAsString().

◆ getAsString() [1/2]

const char * inet::MatchableObject::getAsString ( ) const
overridevirtual
26 {
27  switch (defaultAttribute) {
28  case ATTRIBUTE_FULLPATH: attributeValue = object->getFullPath(); return attributeValue.c_str();
29  case ATTRIBUTE_FULLNAME: return object->getFullName();
30  case ATTRIBUTE_CLASSNAME: return object->getClassName();
31  default: throw cRuntimeError("Unknown setting for default attribute");
32  }
33 }

◆ getAsString() [2/2]

const char * inet::MatchableObject::getAsString ( const char *  attribute) const
overridevirtual
65 {
66  if (!classDescriptor) {
67  classDescriptor = object->getDescriptor();
68  if (!classDescriptor)
69  return nullptr;
70  }
71 
72 /*FIXME
73  // start tokenizing the path
74  cStringTokenizer tokenizer(attribute, ".");
75  const char *token;
76  void *currentObj = obj;
77  cClassDescriptor *currentDesc = desc;
78  int currentFieldId = id
79  while ((token = tokenizer.nextToken())!=nullptr)
80  {
81  bool found = findDescriptorField(d, token, fid, index);
82  if (!found) return nullptr;
83  }
84 */
85 
86  int fieldId;
87  int index;
88  bool found = findDescriptorField(classDescriptor, attribute, fieldId, index);
89  if (!found)
90  return nullptr;
91 
92  attributeValue = classDescriptor->getFieldValueAsString(toAnyPtr(object), fieldId, index);
93  return attributeValue.c_str();
94 }

◆ setDefaultAttribute()

void inet::MatchableObject::setDefaultAttribute ( Attribute  defaultAttribute)
inline

◆ setObject()

void inet::MatchableObject::setObject ( const cObject *  object)
20 {
21  this->object = object;
22  classDescriptor = nullptr;
23 }

◆ splitIndex()

void inet::MatchableObject::splitIndex ( char *  indexedName,
int &  index 
)
staticprotected
36 {
37  index = 0;
38  char *startbracket = strchr(indexedName, '[');
39  if (startbracket) {
40  char *lastcharp = indexedName + strlen(indexedName) - 1;
41  if (*lastcharp != ']')
42  throw cRuntimeError("Unmatched '['");
43  *startbracket = '\0';
44  char *end;
45  index = strtol(startbracket + 1, &end, 10);
46  if (end != lastcharp)
47  throw cRuntimeError("Brackets [] must contain numeric index");
48  }
49 }

Referenced by findDescriptorField().

Member Data Documentation

◆ attributeValue

std::string inet::MatchableObject::attributeValue
mutableprotected

Referenced by getAsString().

◆ classDescriptor

cClassDescriptor* inet::MatchableObject::classDescriptor
mutableprotected

◆ defaultAttribute

Attribute inet::MatchableObject::defaultAttribute
protected

Referenced by getAsString(), and MatchableObject().

◆ object

const cObject* inet::MatchableObject::object
protected

Referenced by MatchableObject(), and setObject().


The documentation for this class was generated from the following files:
inet::MatchableObject::ATTRIBUTE_FULLPATH
@ ATTRIBUTE_FULLPATH
Definition: MatchableObject.h:23
inet::MatchableObject::attributeValue
std::string attributeValue
Definition: MatchableObject.h:31
inet::MatchableObject::object
const cObject * object
Definition: MatchableObject.h:29
inet::MatchableObject::classDescriptor
cClassDescriptor * classDescriptor
Definition: MatchableObject.h:30
inet::MatchableObject::ATTRIBUTE_CLASSNAME
@ ATTRIBUTE_CLASSNAME
Definition: MatchableObject.h:24
inet::MatchableObject::defaultAttribute
Attribute defaultAttribute
Definition: MatchableObject.h:28
inet::MatchableObject::splitIndex
static void splitIndex(char *indexedName, int &index)
Definition: MatchableObject.cc:35
omnetpp::toAnyPtr
any_ptr toAnyPtr(const inet::ClockTime *p)
Definition: common/ClockTime.h:351
inet::MatchableObject::findDescriptorField
static bool findDescriptorField(cClassDescriptor *classDescriptor, const char *attribute, int &fieldId, int &index)
Definition: MatchableObject.cc:51
inet::MatchableObject::ATTRIBUTE_FULLNAME
@ ATTRIBUTE_FULLNAME
Definition: MatchableObject.h:22