NEURON
ocpointer.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 /*
3  provide a pointer to the interpreter
4  p = new Pointer(string) or p = new Pointer(&var)
5  val = p.val
6  p.val = val
7  &p.val can be an argument
8  Optional second arg can be a statement containing $1 for generalized
9  assignment. It will be executed (and p.val assigned) when
10  p.assign(val)
11 */
12 #include <InterViews/resource.h>
13 #include <InterViews/observe.h>
14 #include <string.h>
15 #include "classreg.h"
16 #include "oc2iv.h"
17 #include "ocpointer.h"
18 #include "parse.hpp"
19 #include "ocnotify.h"
20 
21 #if HAVE_IV
22 #include "ivoc.h"
23 #endif
24 
25 extern "C" {
26 extern void hoc_free_list(Symlist**);
27 extern Symbol* hoc_parse_stmt(const char*, Symlist**);
28 extern void hoc_run_stmt(Symbol*);
29 } // extern "C"
30 
31 OcPointer::OcPointer(const char* st, double* d) : Observer() {
32  sti_ = NULL;
33  s_ = new char[strlen(st)+1];
34  strcpy(s_, st);
35  p_ = d;
36  valid_ = true;
38 }
39 
41  if (sti_) {
42  delete sti_;
43  }
44  delete [] s_;
46 }
47 
49  valid_ = false;
50 }
51 
52 void OcPointer::assign(double x) {
53  assert(valid_);
54  *p_ = x;
55  if (sti_) {
56  sti_->play_one(x);
57  }
58 }
59 
60 static double assign(void* v) {
61  OcPointer* ocp = (OcPointer*)v;
62  if (!ocp->valid_) {
63  hoc_execerror("Pointer points to freed address:", ocp->s_);
64  }
65  ocp->assign(*getarg(1));
66  return *ocp->p_;
67 }
68 
69 static const char** pname(void* v) {
70  OcPointer* ocp = (OcPointer*)v;
71  return (const char**)&ocp->s_;
72 }
73 
74 static Member_func members[] = {
75  "val", 0, // will be changed below
76  "assign", assign, // will call assign_stmt if it exists
77  0, 0
78 };
79 
81  "s", pname,
82  0, 0
83 };
84 
85 
86 static void* cons(Object*) {
87  double* p;
88  const char* s;
89  if (hoc_is_pdouble_arg(1)) {
90  p = hoc_pgetarg(1);
91  s = "unknown";
92  }else{
93  s = gargstr(1);
94  ParseTopLevel ptl;
95  p = hoc_val_pointer(s);
96  }
97  if (!p) { hoc_execerror("Pointer constructor failed", 0); }
98  OcPointer* ocp = new OcPointer(s, p);
99  if (ifarg(2)) {
100  ocp->sti_ = new StmtInfo(gargstr(2));
101  }
102  return (void*)ocp;
103 }
104 
105 static void destruct(void* v) {
106  delete (OcPointer*)v;
107 }
108 
109 static void steer_val(void* v) {
110  OcPointer* ocp = (OcPointer*)v;
111  hoc_spop();
112  if (!ocp->valid_) {
113  hoc_execerror("Pointer points to freed address:", ocp->s_);
114  }
115  hoc_pushpx(ocp->p_);
116 }
117 
119  class2oc("Pointer", cons, destruct, members, NULL, NULL, s_memb);
120  // now make the val variable an actual double
121  Symbol* sv = hoc_lookup("Pointer");
122  Symbol* sx = hoc_table_lookup("val", sv->u.ctemplate->symtable);
123  sx->type = VAR;
124  sx->arayinfo = NULL;
125  sv->u.ctemplate->steer = steer_val;
126 }
127 
128 StmtInfo::StmtInfo(const char* s) {
129  stmt_ = new CopyString(s);
130  parse();
131 }
132 
134  delete stmt_;
135  hoc_free_list(&symlist_);
136 }
137 
138 
140  char buf[256], *d;
141  const char* s;
142  symlist_ = NULL;
143  ParseTopLevel ptl;
144  bool see_arg = false;
145  for (s=stmt_->string(), d = buf; *s; ++s, ++d) {
146  if (*s == '$' && s[1] == '1') {
147  strcpy(d, "hoc_ac_");
148  s++;
149  d+=6;
150  see_arg = true;
151  }else{
152  *d = *s;
153  }
154  }
155  if (!see_arg) {
156  strcpy(d, "=hoc_ac_");
157  d+=8;
158  }
159  *d = '\0';
160  symstmt_ = hoc_parse_stmt(buf, &symlist_);
161 }
162 
163 void StmtInfo::play_one(double val) {
164  ParseTopLevel ptl;
165  hoc_ac_ = val;
166  hoc_run_stmt(symstmt_);
167 }
void assign(double)
Definition: ocpointer.cpp:52
static Member_ret_str_func s_memb[]
Definition: ocpointer.cpp:80
Definition: hocdec.h:84
#define assert(ex)
Definition: hocassrt.h:26
virtual void update(Observable *)
Definition: ocpointer.cpp:48
void hoc_free_list(Symlist **)
short type
Definition: model.h:58
StmtInfo * sti_
Definition: ocpointer.h:17
Symbol * hoc_parse_stmt(const char *, Symlist **)
Definition: code2.cpp:664
Symbol * hoc_lookup(const char *)
double * p_
Definition: ocpointer.h:15
Symlist * symtable
Definition: hocdec.h:196
size_t p
static void steer_val(void *v)
Definition: ocpointer.cpp:109
#define v
Definition: md1redef.h:4
void hoc_run_stmt(Symbol *)
Definition: code2.cpp:657
#define gargstr
Definition: hocdec.h:14
double * hoc_pgetarg(int narg)
Definition: code.cpp:1604
OcPointer(const char *, double *)
Definition: ocpointer.cpp:31
void play_one(double)
Definition: ocpointer.cpp:163
void(* steer)(void *)
Definition: hocdec.h:208
virtual ~StmtInfo()
Definition: ocpointer.cpp:133
int hoc_is_pdouble_arg(int narg)
Definition: code.cpp:737
Symbol * hoc_spop(void)
_CONST char * s
Definition: system.cpp:74
void parse()
Definition: ocpointer.cpp:139
void class2oc(const char *, void *(*cons)(Object *), void(*destruct)(void *), Member_func *, int(*checkpoint)(void **), Member_ret_obj_func *, Member_ret_str_func *)
Definition: hoc_oop.cpp:1581
int val
Definition: dll.cpp:167
Arrayinfo * arayinfo
Definition: hocdec.h:158
#define CopyString
Definition: _defines.h:2
void hoc_execerror(const char *, const char *)
Definition: hoc.cpp:741
Definition: model.h:57
static void * cons(Object *)
Definition: ocpointer.cpp:86
int ifarg(int)
Definition: code.cpp:1562
static Member_func members[]
Definition: ocpointer.cpp:74
virtual ~OcPointer()
Definition: ocpointer.cpp:40
void nrn_notify_when_double_freed(double *p, Observer *ob)
Definition: ivoc.cpp:63
Definition: hocdec.h:226
HocStruct cTemplate * ctemplate
Definition: hocdec.h:151
#define getarg
Definition: hocdec.h:15
Symbol * hoc_table_lookup(const char *, Symlist *)
Definition: symbol.cpp:60
double * hoc_val_pointer(const char *s)
Definition: code2.cpp:699
StmtInfo(const char *)
Definition: ocpointer.cpp:128
char * s_
Definition: ocpointer.h:16
void nrn_notify_pointer_disconnect(Observer *ob)
Definition: ivoc.cpp:72
char buf[512]
Definition: init.cpp:13
void OcPointer_reg()
Definition: ocpointer.cpp:118
void hoc_pushpx(double *d)
Definition: code.cpp:702
static const char ** pname(void *v)
Definition: ocpointer.cpp:69
double hoc_ac_
Definition: hoc_init.cpp:261
union Symbol::@18 u
return NULL
Definition: cabcode.cpp:461
bool valid_
Definition: ocpointer.h:18
static void destruct(void *v)
Definition: ocpointer.cpp:105