Project

General

Profile

Statistics
| Branch: | Revision:

root / src / envir / objectprinter.h @ 7c7d95e5

History | View | Annotate | Download (3.99 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  OBJECTPRINTER.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Levente Meszaros
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
#ifndef __OBJECTPRINTER_H
19
#define __OBJECTPRINTER_H
20
21
#include <vector>
22
#include <iostream>
23
#include "envirdefs.h"
24
25
NAMESPACE_BEGIN
26
27
class MatchExpression;
28
class cClassDescriptor;
29
class cObject;
30
31
/**
32
 *
33
 */
34
enum ObjectPrinterRecursionControl {
35
    SKIP,      // don't print this field
36
    RECURSE,   // print this field in detail by recursing down
37
    FULL_NAME, // print the full name only (applicable to cObject)
38
    FULL_PATH  // print the full Path only (applicable to cObject)
39
};
40
41
/**
42
 * This is function type that controls recursion during printing an object.
43
 * It will be asked what to do whenever recursion occurs at a compound object's field.
44
 *
45
 * Parameters: object that has the field, object's class descriptor, field index, field value,
46
 * parent objects collected during recursion, recursion level.
47
 */
48
typedef ObjectPrinterRecursionControl (*ObjectPrinterRecursionPredicate)(void *, cClassDescriptor *, int, void *, void **, int);
49
50
/**
51
 * A utility class to serialize an object in text form. It is currently used
52
 * to record message data into the event log file.
53
 */
54
class ENVIR_API ObjectPrinter
55
{
56
    protected:
57
        int indentSize;
58
        char buffer[1024];
59
        std::vector<MatchExpression*> objectMatchExpressions;
60
        std::vector<std::vector<MatchExpression*> > fieldNameMatchExpressionsList;
61
        ObjectPrinterRecursionPredicate recursionPredicate;
62
63
    public:
64
        /**
65
         * Accepts the parsed form of the pattern string. The two vectors
66
         * must be of the same size. The contained MatchExpression objects
67
         * will be deallocated by this ObjectPrinter.
68
         */
69
        ObjectPrinter(ObjectPrinterRecursionPredicate recursionPredicate,
70
                      const std::vector<MatchExpression*>& objectMatchExpressions,
71
                      const std::vector<std::vector<MatchExpression*> >& fieldNameMatchExpressionsList,
72
                      int indentSize=4);
73
74
        /**
75
         * Pattern syntax is that of the "eventlog-message-detail-pattern"
76
         * configuration entry -- see documentation there.
77
         *
78
         * Just some examples here:
79
         * "*":
80
         *     captures all fields of all messages
81
         * "*Msg | *Packet":
82
         *     captures all fields of classes named AnythingMsg or AnythingPacket
83
         * "*Frame:*Address,*Id":
84
         *     captures all fields named anythingAddress and anythingId from
85
         *     objects of any class named AnythingFrame
86
         * "MyMessage:declaredOn(MyMessage)":
87
         *     captures instances of MyMessage recording the fields
88
         *     declared on the MyMessage class
89
         * "*:(not declaredOn(cMessage) and not declaredOn(cNamedObject) and
90
         * not declaredOn(cObject))":
91
         *     records user-defined fields from all objects
92
         */
93
        ObjectPrinter(ObjectPrinterRecursionPredicate recursionPredicate=NULL, const char *pattern="*", int indentSize=4);
94
95
        /**
96
         * Destructor.
97
         */
98
        ~ObjectPrinter();
99
100
        void printObjectToStream(std::ostream& ostream, cObject *object);
101
102
        std::string printObjectToString(cObject *object);
103
104
    protected:
105
        void printIndent(std::ostream& ostream, int level);
106
        void printObjectToStream(std::ostream& ostream, void *object, cClassDescriptor *descriptor, void **objects, int level);
107
        bool matchesObjectField(cObject *object, int fieldIndex);
108
};
109
110
NAMESPACE_END
111
112
113
#endif