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

Subversion Repositories w11

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

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

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