Project

General

Profile

Statistics
| Branch: | Revision:

root / src / tkenv / tkutil.h @ 3e29b8a0

History | View | Annotate | Download (4.84 KB)

1
//==========================================================================
2
//  TKUTIL.H - part of
3
//
4
//                     OMNeT++/OMNEST
5
//            Discrete System Simulation in C++
6
//
7
//==========================================================================
8

    
9
/*--------------------------------------------------------------*
10
  Copyright (C) 1992-2008 Andras Varga
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15

16
*--------------------------------------------------------------*/
17

    
18
#ifndef __TKUTIL_H
19
#define __TKUTIL_H
20

    
21
#define WIN32_LEAN_AND_MEAN
22
#include <tk.h>
23

    
24
#include "tkdefs.h"
25
#include "cownedobject.h"
26
#include "exception.h"
27

    
28
NAMESPACE_BEGIN
29

    
30
class cPar;
31
class cComponent;
32

    
33
//
34
// In some installations Tcl headers files have 'char*' without 'const char*'
35
// in arg lists -- we have to cast away 'const char*' from args in our Tcl calls.
36
//
37
#define TCLCONST(x)   const_cast<char*>(x)
38

    
39
// enable when debugging Tcl errors (causes stack trace to be printed)
40
//#define VERBOSE_TCL_ERRORS
41

    
42
//
43
// Print error message on console if Tcl code returns error
44
//
45
#ifdef _NDEBUG
46
#define CHK(tcl_eval_statement)   tcl_eval_statement
47
#elif defined VERBOSE_TCL_ERRORS
48
#define CHK(tcl_eval_statement)    \
49
  do { if (tcl_eval_statement==TCL_ERROR) \
50
        ::printf("Tcl error: %s#%d: %s\n\n",__FILE__,__LINE__, Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY)); \
51
  } while(0)
52
#else
53
#define CHK(tcl_eval_statement)    \
54
  do { if (tcl_eval_statement==TCL_ERROR) \
55
        ::printf("%s#%d: %s\n",__FILE__,__LINE__, Tcl_GetStringResult(interp)); \
56
  } while(0)
57
#endif
58

    
59
//
60
// Turns exceptions into Tcl errors
61
//
62
#define TRY(code) \
63
  try {code;} catch (std::exception& e) { \
64
      Tcl_SetResult(interp, TCLCONST(e.what()), TCL_VOLATILE); \
65
      return TCL_ERROR; \
66
  }
67

    
68
#define E_TRY   try {
69

    
70
#define E_CATCH \
71
  } catch (std::exception& e) { \
72
      Tcl_SetResult(interp, TCLCONST(e.what()), TCL_VOLATILE); \
73
      return TCL_ERROR; \
74
  }
75

    
76

    
77
/**
78
 * Quoting strings for Tcl_Eval
79
 */
80
class TKENV_API TclQuotedString
81
{
82
  private:
83
    char buf[80];  // used only if string is short, to spare alloc/dealloc
84
    char *quotedstr;
85
  public:
86
    TclQuotedString();
87
    TclQuotedString(const char *s);
88
    TclQuotedString(const char *s, int n);
89

    
90
    void set(const char *s);
91
    void set(const char *s, int n);
92

    
93
    ~TclQuotedString();
94
    const char *get() {return quotedstr;}
95
    operator const char *() {return quotedstr;}
96
};
97

    
98
//
99
// Utility functions:
100
//
101
class cCollectObjectsVisitor;
102

    
103
const char *getObjectShortTypeName(cObject *object);
104
const char *getObjectFullTypeName(cObject *object);
105

    
106
char *voidPtrToStr(void *ptr, char *buffer=NULL);
107
void *strToVoidPtr(const char *s);
108

    
109
inline char *ptrToStr(cObject *ptr, char *buffer=NULL) {return voidPtrToStr((void *)ptr, buffer);}
110
inline cObject *strToPtr(const char *s) {return (cObject *)strToVoidPtr(s);}
111

    
112
bool isAPL();
113

    
114
void setObjectListResult(Tcl_Interp *interp, cCollectObjectsVisitor *visitor);
115

    
116
void insertIntoInspectorListbox(Tcl_Interp *interp, const char *listbox, cObject *obj, bool fullpath);
117

    
118
void feedCollectionIntoInspectorListbox(cCollectObjectsVisitor *visitor, Tcl_Interp *interp, const char *listbox, bool fullpath);
119

    
120
int fillListboxWithChildObjects(cObject *object, Tcl_Interp *interp, const char *listbox, bool deep);
121

    
122
void inspectObjectByName(const char *fullpath, const char *classname, int insptype, const char *geometry);
123

    
124
void textWidget_insert(Tcl_Interp *interp, const char *textWidget, const char *text, const char *tags=NULL);
125

    
126
void textWidget_gotoEnd(Tcl_Interp *interp, const char *textWidget);
127

    
128
void textWidget_clear(Tcl_Interp *interp, const char *textWidget);
129

    
130
/**
131
 * Looks up the given parameter
132
 */
133
cPar *displayStringPar(const char *parname, cComponent *component, bool searchparent);
134

    
135
/**
136
 * Returns true if the given (partial or full) display string contains
137
 * module or channel parameter references ("$param" or "${param}")
138
 */
139
bool displayStringContainsParamRefs(const char *dispstr);
140

    
141
/**
142
 * If the given string is a single parameter reference in the syntax "$name" or
143
 * "${name}", returns the parameter, otherwise returns NULL.
144
 */
145
cPar *resolveDisplayStringParamRef(const char *dispstr, cComponent *component, bool searchparent);
146

    
147
/**
148
 * Substitutes module/channel param references into the given (partial)
149
 * display string. The returned string may be the same as the one passed in,
150
 * or may point into the buffer std::string.
151
 */
152
const char *substituteDisplayStringParamRefs(const char *dispstr, std::string& buffer, cComponent *component, bool searchparent);
153

    
154
bool resolveBoolDispStrArg(const char *arg, cComponent *component, bool defaultValue);
155
long resolveLongDispStrArg(const char *arg, cComponent *component, int defaultValue);
156
double resolveDoubleDispStrArg(const char *arg, cComponent *component, double defaultValue);
157

    
158
NAMESPACE_END
159

    
160

    
161
#endif
162

    
163

    
164