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

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.61/] [tools/] [src/] [librtcltools/] [RtclProxyBase.cpp] - Blame information for rev 12

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

Line No. Rev Author Line
1 12 wfjm
// $Id: RtclProxyBase.cpp 401 2011-07-31 21:02:33Z mueller $
2 10 wfjm
//
3
// Copyright 2011- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4
//
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 12 wfjm
// 2011-07-31   401   1.2    add ctor(type,interp,name) for direct usage
17
// 2011-04-23   380   1.1    use boost/function instead of RmethDsc
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 12 wfjm
  \version $Id: RtclProxyBase.cpp 401 2011-07-31 21:02:33Z mueller $
26 10 wfjm
  \brief   Implemenation of RtclProxyBase.
27
*/
28
 
29
#include <stdexcept>
30
 
31
#include "RtclProxyBase.hpp"
32
#include "RtclContext.hpp"
33
#include "Rtcl.hpp"
34
 
35
using namespace std;
36
using namespace Retro;
37
 
38
/*!
39
  \class Retro::RtclProxyBase
40
  \brief FIXME_docs
41
*/
42
 
43
typedef std::pair<Retro::RtclProxyBase::mmap_it_t, bool>  mmap_ins_t;
44
 
45
//------------------------------------------+-----------------------------------
46
//! FIXME_docs
47
 
48
RtclProxyBase::RtclProxyBase(const std::string& type)
49
  : fType(type),
50
    fMapMeth(),
51
    fInterp(0)
52
{}
53
 
54
//------------------------------------------+-----------------------------------
55 12 wfjm
//! FIXME_docs
56
 
57
RtclProxyBase::RtclProxyBase(const std::string& type, Tcl_Interp* interp,
58
                             const char* name)
59
  : fType(type),
60
    fMapMeth(),
61
    fInterp(0)
62
{
63
  CreateObjectCmd(interp, name);
64
}
65
 
66
//------------------------------------------+-----------------------------------
67 10 wfjm
//! Destructor
68
 
69
RtclProxyBase::~RtclProxyBase()
70
{
71
  if (fInterp) RtclContext::Find(fInterp).UnRegisterProxy(this);
72
}
73
 
74
//------------------------------------------+-----------------------------------
75
//! FIXME_docs
76
 
77
int RtclProxyBase::ClassCmdConfig(Tcl_Interp* interp, int objc,
78
                                  Tcl_Obj* const objv[])
79
{
80
  if (objc > 2) {
81
    Tcl_AppendResult(interp, "-E: no configuration args supported", NULL);
82
    return TCL_ERROR;
83
  }
84
  return TCL_OK;
85
}
86
 
87
//------------------------------------------+-----------------------------------
88
//! FIXME_docs
89
 
90 12 wfjm
void RtclProxyBase::AddMeth(const std::string& name, const methfo_t& methfo)
91 10 wfjm
{
92 12 wfjm
  mmap_ins_t ret = fMapMeth.insert(mmap_val_t(name, methfo));
93 10 wfjm
  if (ret.second == false)                  // or use !(ret.second)
94
    throw logic_error(string("RtclProxyBase::AddMeth: duplicate name: ") +
95
                      name);
96
  return;
97
}
98
 
99
//------------------------------------------+-----------------------------------
100
//! FIXME_docs
101
 
102
void RtclProxyBase::CreateObjectCmd(Tcl_Interp* interp, const char* name)
103
{
104
  fInterp = interp;
105
  fCmdToken =
106
    Tcl_CreateObjCommand(interp, name, ThunkTclObjectCmd, (ClientData) this,
107
                         (Tcl_CmdDeleteProc *) ThunkTclCmdDeleteProc);
108
  RtclContext::Find(interp).RegisterProxy(this);
109
  Tcl_CreateExitHandler((Tcl_ExitProc*) ThunkTclExitProc, (ClientData) this);
110
  return;
111
}
112
 
113
//------------------------------------------+-----------------------------------
114
//! FIXME_docs
115
 
116
int RtclProxyBase::TclObjectCmd(Tcl_Interp* interp, int objc,
117
                                Tcl_Obj* const objv[])
118
{
119 12 wfjm
  mmap_cit_t it_match;
120 10 wfjm
 
121
  if (objc == 1) {                             // no args
122
    mmap_cit_t it = fMapMeth.find("$default"); // default method registered ?
123
    if (it == fMapMeth.end()) {                // if not, complain
124
      Tcl_WrongNumArgs(interp, 1, objv, "option ?args?");
125
      return TCL_ERROR;
126
    }
127 12 wfjm
    it_match = it;
128 10 wfjm
 
129
  } else {                                     // at least method name given 
130
    string name(Tcl_GetString(objv[1]));
131
    mmap_cit_t it = fMapMeth.lower_bound(name);
132
 
133
    // no leading substring match
134
    if (it==fMapMeth.end() || name!=it->first.substr(0,name.length())) {
135
      Tcl_AppendResult(interp, "-E: bad option \"", Tcl_GetString(objv[1]),
136
                       "\": must be ", NULL);
137
      const char* delim = "";
138
      for (mmap_cit_t it1=fMapMeth.begin(); it1!=fMapMeth.end(); it1++) {
139
        if (it1->first.c_str()[0] != '$') {
140
          Tcl_AppendResult(interp, delim, it1->first.c_str(), NULL);
141
          delim = ",";
142
        }
143
      }
144
      return TCL_ERROR;
145
    }
146
 
147 12 wfjm
    it_match = it;
148 10 wfjm
 
149
    // check for ambiguous substring match
150
    if (name != it->first) {
151
      mmap_cit_t it1 = it;
152
      it1++;
153
      if (it1!=fMapMeth.end() && name==it1->first.substr(0,name.length())) {
154
        Tcl_AppendResult(interp, "-E: ambiguous option \"",
155
                         Tcl_GetString(objv[1]), "\": must be ", NULL);
156
        const char* delim = "";
157
        for (it1=it; it1!=fMapMeth.end() &&
158
               name==it1->first.substr(0,name.length()); it1++) {
159
          Tcl_AppendResult(interp, delim, it1->first.c_str(), NULL);
160
          delim = ",";
161
        }
162
        return TCL_ERROR;
163
      }
164
    }
165
  }
166
 
167
  RtclArgs  args(interp, objc, objv, 2);
168 12 wfjm
  return (it_match->second)(args);
169 10 wfjm
}
170
 
171
//------------------------------------------+-----------------------------------
172
//! FIXME_docs
173
 
174
int RtclProxyBase::ThunkTclObjectCmd(ClientData cdata, Tcl_Interp* interp,
175
                                     int objc, Tcl_Obj* const objv[])
176
{
177
  if (!cdata) {
178
    Tcl_AppendResult(interp, "-E: BUG! ThunkTclObjectCmd called with cdata==0",
179
                     NULL);
180
    return TCL_ERROR;
181
  }
182
 
183
  try {
184
    return ((RtclProxyBase*) cdata)->TclObjectCmd(interp, objc, objv);
185
  } catch (exception& e) {
186
    Rtcl::AppendResultNewLines(interp);
187
    Tcl_AppendResult(interp, "-E: exception caught \"", e.what(), "\"", NULL);
188
  }
189
  return TCL_ERROR;
190
}
191
 
192
//------------------------------------------+-----------------------------------
193
//! FIXME_docs
194
 
195
void RtclProxyBase::ThunkTclCmdDeleteProc(ClientData cdata)
196
{
197
  Tcl_DeleteExitHandler((Tcl_ExitProc*) ThunkTclExitProc, cdata);
198
  delete ((RtclProxyBase*) cdata);
199
  return;
200
}
201
 
202
//------------------------------------------+-----------------------------------
203
//! FIXME_docs
204
 
205
void RtclProxyBase::ThunkTclExitProc(ClientData cdata)
206
{
207
  delete ((RtclProxyBase*) cdata);
208
  return;
209
}
210
 
211
//------------------------------------------+-----------------------------------
212
#if (defined(Retro_NoInline) || defined(Retro_RtclProxyBase_NoInline))
213
#define inline
214
#include "RtclProxyBase.ipp"
215
#undef  inline
216
#endif

powered by: WebSVN 2.1.0

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