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)
32  : Observer() {
33  sti_ = NULL;
34  s_ = new char[strlen(st) + 1];
35  strcpy(s_, st);
36  p_ = d;
37  valid_ = true;
39 }
40 
42  if (sti_) {
43  delete sti_;
44  }
45  delete[] s_;
47 }
48 
50  valid_ = false;
51 }
52 
53 void OcPointer::assign(double x) {
54  assert(valid_);
55  *p_ = x;
56  if (sti_) {
57  sti_->play_one(x);
58  }
59 }
60 
61 static double assign(void* v) {
62  OcPointer* ocp = (OcPointer*) v;
63  if (!ocp->valid_) {
64  hoc_execerror("Pointer points to freed address:", ocp->s_);
65  }
66  ocp->assign(*getarg(1));
67  return *ocp->p_;
68 }
69 
70 static const char** pname(void* v) {
71  OcPointer* ocp = (OcPointer*) v;
72  return (const char**) &ocp->s_;
73 }
74 
75 static Member_func members[] = {"val",
76  0, // will be changed below
77  "assign",
78  assign, // will call assign_stmt if it exists
79  0,
80  0};
81 
82 static Member_ret_str_func s_memb[] = {"s", pname, 0, 0};
83 
84 
85 static void* cons(Object*) {
86  double* p;
87  const char* s;
88  if (hoc_is_pdouble_arg(1)) {
89  p = hoc_pgetarg(1);
90  s = "unknown";
91  } else {
92  s = gargstr(1);
93  ParseTopLevel ptl;
94  p = hoc_val_pointer(s);
95  }
96  if (!p) {
97  hoc_execerror("Pointer constructor failed", 0);
98  }
99  OcPointer* ocp = new OcPointer(s, p);
100  if (ifarg(2)) {
101  ocp->sti_ = new StmtInfo(gargstr(2));
102  }
103  return (void*) ocp;
104 }
105 
106 static void destruct(void* v) {
107  delete (OcPointer*) v;
108 }
109 
110 static void steer_val(void* v) {
111  OcPointer* ocp = (OcPointer*) v;
112  hoc_spop();
113  if (!ocp->valid_) {
114  hoc_execerror("Pointer points to freed address:", ocp->s_);
115  }
116  hoc_pushpx(ocp->p_);
117 }
118 
120  class2oc("Pointer", cons, destruct, members, NULL, NULL, s_memb);
121  // now make the val variable an actual double
122  Symbol* sv = hoc_lookup("Pointer");
123  Symbol* sx = hoc_table_lookup("val", sv->u.ctemplate->symtable);
124  sx->type = VAR;
125  sx->arayinfo = NULL;
126  sv->u.ctemplate->steer = steer_val;
127 }
128 
129 StmtInfo::StmtInfo(const char* s) {
130  stmt_ = new CopyString(s);
131  parse();
132 }
133 
135  delete stmt_;
137 }
138 
139 
141  char buf[256], *d;
142  const char* s;
143  symlist_ = NULL;
144  ParseTopLevel ptl;
145  bool see_arg = false;
146  for (s = stmt_->string(), d = buf; *s; ++s, ++d) {
147  if (*s == '$' && s[1] == '1') {
148  strcpy(d, "hoc_ac_");
149  s++;
150  d += 6;
151  see_arg = true;
152  } else {
153  *d = *s;
154  }
155  }
156  if (!see_arg) {
157  strcpy(d, "=hoc_ac_");
158  d += 8;
159  }
160  *d = '\0';
162 }
163 
164 void StmtInfo::play_one(double val) {
165  ParseTopLevel ptl;
166  hoc_ac_ = val;
168 }
#define CopyString
Definition: _defines.h:2
bool valid_
Definition: ocpointer.h:18
void assign(double)
Definition: ocpointer.cpp:53
OcPointer(const char *, double *)
Definition: ocpointer.cpp:31
virtual ~OcPointer()
Definition: ocpointer.cpp:41
virtual void update(Observable *)
Definition: ocpointer.cpp:49
double * p_
Definition: ocpointer.h:15
StmtInfo * sti_
Definition: ocpointer.h:17
char * s_
Definition: ocpointer.h:16
StmtInfo(const char *)
Definition: ocpointer.cpp:129
void parse()
Definition: ocpointer.cpp:140
CopyString * stmt_
Definition: ocpointer.h:27
void play_one(double)
Definition: ocpointer.cpp:164
Symbol * symstmt_
Definition: ocpointer.h:29
Symlist * symlist_
Definition: ocpointer.h:28
virtual ~StmtInfo()
Definition: ocpointer.cpp:134
const char * string() const
Definition: string.h:139
Symbol * hoc_table_lookup(const char *, Symlist *)
Definition: symbol.cpp:61
void hoc_execerror(const char *, const char *)
Definition: hoc.cpp:754
char buf[512]
Definition: init.cpp:13
void hoc_pushpx(double *d)
Definition: code.cpp:716
double * hoc_val_pointer(const char *s)
Definition: code2.cpp:727
double hoc_ac_
Definition: hoc_init.cpp:397
Symbol * hoc_lookup(const char *)
int hoc_is_pdouble_arg(int narg)
Definition: code.cpp:748
double * hoc_pgetarg(int narg)
Definition: code.cpp:1623
Symbol * hoc_spop(void)
#define assert(ex)
Definition: hocassrt.h:32
#define getarg
Definition: hocdec.h:15
#define gargstr
Definition: hocdec.h:14
void nrn_notify_when_double_freed(double *p, Observer *ob)
Definition: ivoc.cpp:61
void nrn_notify_pointer_disconnect(Observer *ob)
Definition: ivoc.cpp:70
int ifarg(int)
Definition: code.cpp:1581
#define v
Definition: md1redef.h:4
size_t p
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:1560
void hoc_run_stmt(Symbol *)
Definition: code2.cpp:685
static Member_func members[]
Definition: ocpointer.cpp:75
static void * cons(Object *)
Definition: ocpointer.cpp:85
static void destruct(void *v)
Definition: ocpointer.cpp:106
static Member_ret_str_func s_memb[]
Definition: ocpointer.cpp:82
void hoc_free_list(Symlist **)
static void steer_val(void *v)
Definition: ocpointer.cpp:110
static const char ** pname(void *v)
Definition: ocpointer.cpp:70
void OcPointer_reg()
Definition: ocpointer.cpp:119
Symbol * hoc_parse_stmt(const char *, Symlist **)
Definition: code2.cpp:692
static double assign(void *v)
Definition: ocpointer.cpp:61
#define NULL
Definition: sptree.h:16
Definition: hocdec.h:227
Definition: model.h:57
short type
Definition: model.h:58
union Symbol::@18 u
HocStruct cTemplate * ctemplate
Definition: hocdec.h:152
Arrayinfo * arayinfo
Definition: hocdec.h:159
Definition: hocdec.h:84
Symlist * symtable
Definition: hocdec.h:197
void(* steer)(void *)
Definition: hocdec.h:209