OpenCores
URL https://opencores.org/ocsvn/w11/w11/trunk

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.6/] [tools/] [src/] [librtcltools/] [RtclClassBase.cpp] - Blame information for rev 40

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 wfjm
// $Id: RtclClassBase.cpp 488 2013-02-16 18:49:47Z mueller $
2 10 wfjm
//
3 19 wfjm
// Copyright 2011-2013 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4 10 wfjm
//
5
// This program is free software; you may redistribute and/or modify it under
6
// the terms of the GNU General Public License as published by the Free
7
// Software Foundation, either version 2, or at your option any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
11
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
// for complete details.
13
// 
14
// Revision History: 
15
// Date         Rev Version  Comment
16 19 wfjm
// 2013-02-10   485   1.0.3  add static const defs
17
// 2013-01-13   474   1.0.2  TclClassCmd(): check for existing Rtclproxy names
18 10 wfjm
// 2011-03-05   366   1.0.1  use AppendResultNewLines() in exception catcher
19
// 2011-02-20   363   1.0    Initial version
20
// 2011-02-11   360   0.1    First draft
21
// ---------------------------------------------------------------------------
22
 
23
/*!
24
  \file
25 19 wfjm
  \version $Id: RtclClassBase.cpp 488 2013-02-16 18:49:47Z mueller $
26 10 wfjm
  \brief   Implemenation of RtclClassBase.
27
*/
28
 
29
#include <string.h>
30
 
31
#include <stdexcept>
32
 
33
#include "RtclClassBase.hpp"
34
#include "RtclContext.hpp"
35
#include "RtclOPtr.hpp"
36
#include "Rtcl.hpp"
37
 
38
using namespace std;
39
 
40
/*!
41
  \class Retro::RtclClassBase
42
  \brief FIXME_docs
43
*/
44
 
45 19 wfjm
// all method definitions in namespace Retro
46
namespace Retro {
47
 
48 10 wfjm
//------------------------------------------+-----------------------------------
49 19 wfjm
// constants definitions
50
 
51
const int RtclClassBase::kOK;
52
const int RtclClassBase::kERR;
53
 
54
//------------------------------------------+-----------------------------------
55 10 wfjm
//! Default constructor
56
 
57
RtclClassBase::RtclClassBase(const std::string& type)
58
  : fType(type),
59
    fInterp(0)
60
{}
61
 
62
//------------------------------------------+-----------------------------------
63
//! Destructor
64
 
65
RtclClassBase::~RtclClassBase()
66
{
67
  if (fInterp) RtclContext::Find(fInterp).UnRegisterClass(this);
68
}
69
 
70
//------------------------------------------+-----------------------------------
71
//! FIXME_docs
72
 
73
void RtclClassBase::CreateClassCmd(Tcl_Interp* interp, const char* name)
74
{
75
  fInterp = interp;
76
  fCmdToken =
77
    Tcl_CreateObjCommand(interp, name, ThunkTclClassCmd, (ClientData) this,
78
                         (Tcl_CmdDeleteProc *) ThunkTclCmdDeleteProc);
79
  RtclContext::Find(interp).RegisterClass(this);
80
  Tcl_CreateExitHandler((Tcl_ExitProc*) ThunkTclExitProc, (ClientData) this);
81
  return;
82
}
83
 
84
//------------------------------------------+-----------------------------------
85
//! FIXME_docs
86
 
87 19 wfjm
int RtclClassBase::TclClassCmd(Tcl_Interp* interp, int objc,
88 10 wfjm
                                      Tcl_Obj* const objv[])
89
{
90 19 wfjm
  // no args -> lists existing proxies
91 10 wfjm
  if (objc == 1) {
92
    return ClassCmdList(interp);
93
  }
94
 
95 19 wfjm
  // 2nd arg -delete -> delete proxy
96 10 wfjm
  const char* name = Tcl_GetString(objv[1]);
97
  if (objc == 3 && strcmp(Tcl_GetString(objv[2]), "-delete")==0) {
98
    return ClassCmdDelete(interp, name);
99
  }
100 19 wfjm
 
101
  // check if proxy of given name already existing
102
  RtclProxyBase* pprox = RtclContext::Find(interp).FindProxy("",name);
103
  if (pprox) {
104
    Tcl_AppendResult(interp, "-E: command name '", name,
105
                     "' exists already as RtclProxy of type '",
106
                     pprox->Type().c_str(), "'", NULL);
107
    return kERR;
108
 
109
  }
110
 
111
  // finally create new proxy
112 10 wfjm
  return ClassCmdCreate(interp, objc, objv);
113
}
114
 
115
//------------------------------------------+-----------------------------------
116
//! FIXME_docs
117
 
118 19 wfjm
int RtclClassBase::ClassCmdList(Tcl_Interp* interp)
119 10 wfjm
{
120
  std::vector<RtclProxyBase*> list;
121
  RtclContext::Find(interp).ListProxy(list, Type());
122
  RtclOPtr rlist(Tcl_NewListObj(0, NULL));
123
 
124
  for (size_t i=0; i<list.size(); i++) {
125
    const char* cmdname = Tcl_GetCommandName(interp, list[i]->Token());
126
    RtclOPtr rval(Tcl_NewStringObj(cmdname, -1));
127
    if (Tcl_ListObjAppendElement(interp, rlist, rval) != kOK) return kERR;
128
  }
129
 
130
  Tcl_SetObjResult(interp, rlist);
131
 
132
  return kOK;
133
}
134
 
135
//------------------------------------------+-----------------------------------
136
//! FIXME_docs
137
 
138 19 wfjm
int RtclClassBase::ClassCmdDelete(Tcl_Interp* interp, const char* name)
139 10 wfjm
{
140
  Tcl_CmdInfo cinfo;
141
  if (Tcl_GetCommandInfo(interp, name, &cinfo) == 0) {
142 19 wfjm
    Tcl_AppendResult(interp, "-E: unknown command name '", name, "'", NULL);
143 10 wfjm
    return kERR;
144
  }
145
 
146
  RtclContext& cntx = RtclContext::Find(interp);
147
  if (!cntx.CheckProxy((RtclProxyBase*) cinfo.objClientData)) {
148 19 wfjm
    Tcl_AppendResult(interp, "-E: command '", name, "' is not a RtclProxy",
149 10 wfjm
                     NULL);
150
    return kERR;
151
  }
152
  if (!cntx.CheckProxy((RtclProxyBase*) cinfo.objClientData, Type())) {
153 19 wfjm
    Tcl_AppendResult(interp, "-E: command '", name,
154
                     "' is not a RtclProxy of type '",
155
                     Type().c_str(), "'", NULL);
156 10 wfjm
    return kERR;
157
  }
158
 
159
  int irc = Tcl_DeleteCommand(interp, name);
160 19 wfjm
  if (irc != kOK) Tcl_AppendResult(interp, "-E: failed to delete '", name,
161
                                   "'", NULL);
162 10 wfjm
  return irc;
163
}
164
 
165
//------------------------------------------+-----------------------------------
166
//! FIXME_docs
167
 
168
int RtclClassBase::ThunkTclClassCmd(ClientData cdata, Tcl_Interp* interp,
169
                                    int objc, Tcl_Obj* const objv[])
170
{
171
  if (!cdata) {
172
    Tcl_AppendResult(interp, "-E: BUG! ThunkTclClassCmd called with cdata == 0",
173
                     NULL);
174
    return kERR;
175
  }
176
 
177
  try {
178
    return ((RtclClassBase*) cdata)->TclClassCmd(interp, objc, objv);
179
  } catch (exception& e) {
180
    Rtcl::AppendResultNewLines(interp);
181 19 wfjm
    Tcl_AppendResult(interp, "-E: exception caught in ThunkTclClassCmd: '",
182
                     e.what(), "'", NULL);
183 10 wfjm
  }
184
  return kERR;
185
}
186
 
187
//------------------------------------------+-----------------------------------
188
//! FIXME_docs
189
 
190
void RtclClassBase::ThunkTclCmdDeleteProc(ClientData cdata)
191
{
192
  Tcl_DeleteExitHandler((Tcl_ExitProc*) ThunkTclExitProc, cdata);
193
  delete ((RtclClassBase*) cdata);
194
  return;
195
}
196
 
197
//------------------------------------------+-----------------------------------
198
//! FIXME_docs
199
 
200
void RtclClassBase::ThunkTclExitProc(ClientData cdata)
201
{
202
  delete ((RtclClassBase*) cdata);
203
  return;
204
}
205
 
206 19 wfjm
} // end namespace Retro

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.