openMSX
TclObject.hh
Go to the documentation of this file.
1 #ifndef TCLOBJECT_HH
2 #define TCLOBJECT_HH
3 
4 #include "string_ref.hh"
5 #include "openmsx.hh"
6 #include <iterator>
7 
8 struct Tcl_Interp;
9 struct Tcl_Obj;
10 
11 namespace openmsx {
12 
13 class Interpreter;
14 
15 class TclObject
16 {
17 public:
18  TclObject(Tcl_Interp* interp, Tcl_Obj* object);
19  TclObject(Tcl_Interp* interp, string_ref value);
20  explicit TclObject(string_ref value);
21  explicit TclObject(Tcl_Interp* interp);
22  explicit TclObject(Interpreter& interp);
23  TclObject(const TclObject& object);
24  TclObject();
25  ~TclObject();
26 
27  // assignment operator so we can use vector<TclObject>
28  TclObject& operator=(const TclObject& other);
29 
30  // get associated interpreter
31  Tcl_Interp* getInterpreter() const;
32  // get underlying Tcl_Obj
33  Tcl_Obj* getTclObject();
34 
35  // value setters
36  void setString(string_ref value);
37  void setInt(int value);
38  void setBoolean(bool value);
39  void setDouble(double value);
40  void setBinary(byte* buf, unsigned length);
41  void addListElement(string_ref element);
42  void addListElement(int value);
43  void addListElement(double value);
44  void addListElement(const TclObject& element);
45  template <typename ITER> void addListElements(ITER begin, ITER end);
46  template <typename CONT> void addListElements(const CONT& container);
47 
48  // value getters
49  string_ref getString() const;
50  int getInt() const;
51  bool getBoolean() const;
52  double getDouble() const;
53  const byte* getBinary(unsigned& length) const;
54  unsigned getListLength() const;
55  TclObject getListIndex(unsigned index) const;
56  TclObject getDictValue(const TclObject& key) const;
57 
58  // expressions
59  bool evalBool() const;
60 
68  void checkExpression() const;
69  void checkCommand() const;
70 
78  std::string executeCommand(bool compile = false);
79 
80 private:
81  void init(Tcl_Obj* obj_);
82  void throwException() const;
83  void addListElement(Tcl_Obj* element);
84  void parse(const char* str, int len, bool expression) const;
85 
86  Tcl_Interp* interp;
87  Tcl_Obj* obj;
88 };
89 
90 template <typename ITER>
91 void TclObject::addListElements(ITER begin, ITER end)
92 {
93  for (ITER it = begin; it != end; ++it) {
94  addListElement(*it);
95  }
96 }
97 
98 template <typename CONT>
99 void TclObject::addListElements(const CONT& container)
100 {
101  addListElements(std::begin(container), std::end(container));
102 }
103 
104 } // namespace openmsx
105 
106 #endif