FRIBParallelanalysis  1.0
FrameworkforMPIParalleldataanalysisatFRIB
TCLObject.h
1 /*
2  This software is Copyright by the Board of Trustees of Michigan
3  State University (c) Copyright 2005.
4 
5  You may use this software under the terms of the GNU public license
6  (GPL). The terms of this license are described at:
7 
8  http://www.gnu.org/licenses/gpl.txt
9 
10  Author:
11  Ron Fox
12  NSCL
13  Michigan State University
14  East Lansing, MI 48824-1321
15 */
16 
17 
18 
19 // Class: CTCLObject //ANSI C++
20 //
21 
22 //
23 // Author:
24 // Ron Fox
25 // NSCL
26 // Michigan State University
27 // East Lansing, MI 48824-1321
28 // mailto: fox@nscl.msu.edu
29 //
30 // (c) Copyright NSCL 1999, All rights reserved TCLObject.h
31 //
32 
33 #ifndef TCLOBJECT_H //Required for current class
34 #define TCLOBJECT_H
35 
36 #ifndef TCLINTERPRETEROBJECT_H
37 #include "TCLInterpreterObject.h"
38 #endif
39 
40 #ifndef TCLLIST_H
41 #include "TCLList.h"
42 #endif
43 
44 #include <tcl.h>
45 #include <assert.h>
46 #include <string>
47 #include <vector>
48 
49 
51 {
52 private:
53 
54  Tcl_Obj* m_pObject; //Pointer to Tcl maintained object
55 
56 
57  // Constructors and other cannonical operations:
58 public:
59  // Default constructor:
60 
61  CTCLObject () : m_pObject(0)
62  {
63  m_pObject = Tcl_NewObj();
64  assert(m_pObject != 0);
65  Tcl_IncrRefCount(m_pObject);
66 
67  }
68  virtual ~CTCLObject ( ) // Destructor
69  {
70  Tcl_DecrRefCount(m_pObject);
71  }
72  // Construct for existing C TCL OBject:
73 
74  CTCLObject (Tcl_Obj* am_pObject)
75 
76  {
77 
78  m_pObject = am_pObject;
79  assert(m_pObject != 0);
80  Tcl_IncrRefCount(m_pObject); // Don't let caller kill us.
81  }
82 
83  //Copy constructor Since this is usually used for temps,
84  // we share the object with the caller:
85 
86  CTCLObject (const CTCLObject& aCTCLObject ) :
87  CTCLInterpreterObject(aCTCLObject)
88  {
89  m_pObject = aCTCLObject.m_pObject;
90  Tcl_IncrRefCount(m_pObject); // Will be decr. on destruction.
91  Bind(aCTCLObject.getInterpreter());
92  }
93 
94  // Operator= Assignment Operator
95 
96  CTCLObject& operator= (const CTCLObject& aCTCLObject);
97 
98  //Operator== Equality Operator (string comparison).
99 
100  int operator== (const CTCLObject& aCTCLObject) const;
101 
102 // Selectors:
103 
104 public:
105 
106  Tcl_Obj* getObject() // Don't ensure constness.
107  {
108  return m_pObject;
109  }
110  const Tcl_Obj* getObject() const
111  {
112  return m_pObject;
113  }
114 
115 // Mutators:
116 
117 protected:
118 
119  void setObject (Tcl_Obj* am_pObject)
120  {
121  Tcl_DecrRefCount(m_pObject); // Release prior object.
122  m_pObject = am_pObject;
123  Tcl_IncrRefCount(m_pObject); // Put the grab on the new one.
124  }
125 
126 public:
127 
128  // Assignments to this object
129 
130  CTCLObject& operator= (const std::string& rSource) ;
131  CTCLObject& operator= (const char* pSource) ;
132  CTCLObject& operator= (int nSource) ;
133  CTCLObject& operator= (const CTCLList& rList) ;
134  CTCLObject& operator= (double dSource) ;
135  CTCLObject& operator=(Tcl_Obj* rhs);
136  CTCLObject& operator=(Tcl_WideInt rhs);
137 
138  // Implcit conversions which access the object's ports.
139 
140  operator std::string () ;
141  operator int () ;
142  operator CTCLList () ;
143  operator double () ;
144 
145 
146  // Append operations which append the item to the object
147  // as a list element.
148 
149  CTCLObject& operator+= (const CTCLObject& rObject) ;
150  CTCLObject& operator+= (int nItem) ;
151  CTCLObject& operator+= (const std::string& rItem) ;
152  CTCLObject& operator+= (const char* pItem) ;
153  CTCLObject& operator+= (double Item) ;
154  CTCLObject& operator+= (Tcl_Obj* pObj);
155 
156  // Return a true copy of the object using Tcl_DuplicateObj
157 
158  CTCLObject clone () ;
159 
160  // Evaulate the object as a script returning the result std::string as
161  // an object. throws TCLException on error.
162 
163  CTCLObject operator() () ;
164 
165  // String based functions:
166 
167  CTCLObject getRange(int first, int last);
168 
169  // List based functions:
170 
171 
172  CTCLObject& concat(CTCLObject& rhs); // Concat lists.
173  std::vector<CTCLObject> getListElements();
174  CTCLObject& setList(std::vector<CTCLObject> elements);
175  int llength();
176  CTCLObject lindex(int index);
177  CTCLObject& lreplace(int first, int count, std::vector<CTCLObject> newElements);
178 
179 protected:
180  void DupIfMust() { // Duplicates object if needed for eval.
181  if(Tcl_IsShared(m_pObject)) {
182  Tcl_Obj* old = m_pObject;
183  m_pObject = Tcl_DuplicateObj(old);
184  Tcl_IncrRefCount(m_pObject);
185  Tcl_DecrRefCount(old);
186  }
187  }
188  void NewIfMust() { // Make new object if needed for replacement.
189  if(Tcl_IsShared(m_pObject)) {
190  Tcl_Obj* old = m_pObject;
191  m_pObject = Tcl_NewObj();
192  Tcl_DecrRefCount(old);
193  Tcl_IncrRefCount(m_pObject);
194  }
195  }
196 };
197 
198 #endif
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
CTCLObject & setList(std::vector< CTCLObject > elements)
Definition: TCLObject.cpp:516
CTCLObject & concat(CTCLObject &rhs)
Definition: TCLObject.cpp:457
Definition: TCLInterpreterObject.h:46
CTCLObject lindex(int index)
Definition: TCLObject.cpp:582
int llength()
Definition: TCLObject.cpp:553
Definition: TCLObject.h:50
Definition: TCLList.h:45
std::vector< CTCLObject > getListElements()
Definition: TCLObject.cpp:484
CTCLObject & lreplace(int first, int count, std::vector< CTCLObject > newElements)
Definition: TCLObject.cpp:625
CTCLObject getRange(int first, int last)
Definition: TCLObject.cpp:430