📄 simarg.hh
字号:
#ifndef simarg_hh_included#define simarg_hh_included#include <tcl.h>#include "error.hh"#include "object.hh"// The Tcl interpreter.extern Tcl_Interp *sulima_tcl;// A script error as stored in the Tcl interpreter result.class ScriptError : public Error {private: Tcl_Obj *obj;public: ScriptError() : obj(Tcl_GetObjResult(sulima_tcl)) { Tcl_IncrRefCount(obj); } ~ScriptError(); const char *what() const;};// This file defines a simple wrapper around the Tcl function arguments. It// is here both for convinience and to hide details of the scripting engine.// A single argument to a callback function (and its return value.)class SimArg : public Object{ friend class SimArgs;private: Tcl_Obj *obj; // Construct a SimArg from a constant Tcl object. SimArg(Tcl_Obj *o) : obj(o) { Tcl_IncrRefCount(obj); }public: // Constructors, etc. SimArg(const SimArg &a) : obj(a.obj) { Tcl_IncrRefCount(obj); } ~SimArg() { Tcl_DecrRefCount(obj); } SimArg& operator=(const SimArg &a) { Tcl_DecrRefCount(obj); obj = a.obj; Tcl_IncrRefCount(obj); return *this; } SimArg(int x = 0) : obj(Tcl_NewIntObj(x)) { if (!obj) throw OutOfMemory(); Tcl_IncrRefCount(obj); } SimArg(long x) : obj(Tcl_NewLongObj(x)) { if (!obj) throw OutOfMemory(); Tcl_IncrRefCount(obj); } SimArg(bool x) : obj(Tcl_NewBooleanObj(x)) { if (!obj) throw OutOfMemory(); Tcl_IncrRefCount(obj); } SimArg(const char *s) : obj(Tcl_NewStringObj((char *)s, -1)) { if (!obj) throw OutOfMemory(); Tcl_IncrRefCount(obj); } // Return the internal object representation. Tcl_Obj *get_obj() const { return obj; } // Finaly, implicit conversions to strings, integers and booleans. operator int() const { int x; if (Tcl_GetIntFromObj(sulima_tcl, obj, &x) != TCL_OK) throw ScriptError(); return x; } operator long() const { long x; if (Tcl_GetLongFromObj(sulima_tcl, obj, &x) != TCL_OK) throw ScriptError(); return x; } operator bool() const { int x; if (Tcl_GetBooleanFromObj(sulima_tcl, obj, &x) != TCL_OK) throw ScriptError(); return x; } operator const char *() const { char *s = Tcl_GetStringFromObj(obj, NULL); if (!s) throw OutOfMemory(); return s; }};// A list of arguments to a callback function.class SimArgs {private: // Internal representation. int objc; Tcl_Obj *const *objv;public: // Constructors, etc. SimArgs() : objc(0), objv(0) { } SimArgs(int c, Tcl_Obj *const v[]) : objc(c), objv(v) { } // Return the array size. int length() const { return objc; } int size() const { return objc; } // Index the array. SimArg operator[](int i) const { if (i >= objc) throw Error("At least %d arguments expected.", i + 1); return SimArg(objv[i]); }};#endif // simarg_hh_included
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -