Statistics
| Branch: | Revision:

root / src / envir / matchableobject.cc @ master

History | View | Annotate | Download (3.05 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  MATCHABLEOBJECT.CC - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17
18
#include "matchableobject.h"
19
#include "cclassdescriptor.h"
20
21
USING_NAMESPACE
22
23
MatchableObjectAdapter::MatchableObjectAdapter(DefaultAttribute attr, cObject *obj)
24
{
25
    this->attr = attr;
26
    this->obj = obj;
27
    desc = NULL;
28
}
29
30
void MatchableObjectAdapter::setObject(cObject *obj)
31
{
32
    this->obj = obj;
33
    desc = NULL;
34
}
35
36
const char *MatchableObjectAdapter::getDefaultAttribute() const
37
{
38
    switch (attr)
39
    {
40
        case FULLPATH:  tmp = obj->getFullPath(); return tmp.c_str();
41
        case FULLNAME:  return obj->getFullName();
42
        case CLASSNAME: return obj->getClassName();
43
        default: throw opp_runtime_error("unknown setting for default attribute");
44
    }
45
}
46
47
void MatchableObjectAdapter::splitIndex(char *fieldname, int& index)
48
{
49
    index = 0;
50
    char *startbracket = strchr(fieldname, '[');
51
    if (startbracket)
52
    {
53
        char *lastcharp = fieldname + strlen(fieldname) - 1;
54
        if (*lastcharp != ']')
55
            throw opp_runtime_error("unmatched '['");
56
        *startbracket = '\0';
57
        char *end;
58
        index = strtol(startbracket+1, &end, 10);
59
        if (end!=lastcharp)
60
            throw opp_runtime_error("brackets [] must contain numeric index");
61
    }
62
}
63
64
bool MatchableObjectAdapter::findDescriptorField(cClassDescriptor *desc, cObject *obj, char *fieldname, int& fieldId, int& index)
65
{
66
    // chop off possible bracketed index from field name
67
    splitIndex(fieldname, index);
68
69
    // find field by name
70
    int n = desc->getFieldCount(obj);
71
    for (int i=0; i<n; i++)
72
        if (!strcmp(fieldname, desc->getFieldName(obj, i)))
73
            {fieldId = i; return true;}
74
    return false;
75
}
76
77
const char *MatchableObjectAdapter::getAttribute(const char *name) const
78
{
79
    if (!desc)
80
    {
81
        desc = obj->getDescriptor();
82
        if (!desc)
83
            return NULL;
84
    }
85
86
/*FIXME TBD
87
    // start tokenizing the path
88
    cStringTokenizer tokenizer(name, ".");
89
    const char *token;
90
    void *currentObj = obj;
91
    cClassDescriptor *currentDesc = desc;
92
    int currentFieldId = id
93
    while ((token = tokenizer.nextToken())!=NULL)
94
    {
95
        bool found = findDescriptorField(d, o, token, fid, index);
96
        if (!found) return NULL;
97
    }
98
*/
99
100
    int fieldId;
101
    int index;
102
    char *name2 = new char[strlen(name)+1];
103
    strcpy(name2, name);
104
    bool found = findDescriptorField(desc, obj, name2, fieldId, index);
105
    if (!found)
106
        return NULL;
107
108
    tmp = desc->getFieldAsString(obj, fieldId, index);
109
    return tmp.c_str();
110
}