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/observe.h>
13 #include <string.h>
14 #include "classreg.h"
15 #include "oc_ansi.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 OcPointer::OcPointer(const char* st, double* d)
26  : Observer() {
27  sti_ = NULL;
28  s_ = new char[strlen(st) + 1];
29  strcpy(s_, st);
30  p_ = d;
31  valid_ = true;
33 }
34 
36  if (sti_) {
37  delete sti_;
38  }
39  delete[] s_;
41 }
42 
44  valid_ = false;
45 }
46 
47 void OcPointer::assign(double x) {
48  assert(valid_);
49  *p_ = x;
50  if (sti_) {
51  sti_->play_one(x);
52  }
53 }
54 
55 static double assign(void* v) {
56  OcPointer* ocp = (OcPointer*) v;
57  if (!ocp->valid_) {
58  hoc_execerror("Pointer points to freed address:", ocp->s_);
59  }
60  ocp->assign(*getarg(1));
61  return *ocp->p_;
62 }
63 
64 static const char** pname(void* v) {
65  OcPointer* ocp = (OcPointer*) v;
66  return (const char**) &ocp->s_;
67 }
68 
69 static Member_func members[] = {{"val", 0}, // will be changed below
70  {"assign", assign}, // will call assign_stmt if it exists
71  {nullptr, nullptr}};
72 
73 static Member_ret_str_func s_memb[] = {{"s", pname}, {nullptr, nullptr}};
74 
75 
76 static void* cons(Object*) {
77  double* p;
78  const char* s;
79  if (hoc_is_pdouble_arg(1)) {
80  p = hoc_pgetarg(1);
81  s = "unknown";
82  } else {
83  s = gargstr(1);
84  ParseTopLevel ptl;
85  p = hoc_val_pointer(s);
86  }
87  if (!p) {
88  hoc_execerror("Pointer constructor failed", 0);
89  }
90  OcPointer* ocp = new OcPointer(s, p);
91  if (ifarg(2)) {
92  ocp->sti_ = new StmtInfo(gargstr(2));
93  }
94  return (void*) ocp;
95 }
96 
97 static void destruct(void* v) {
98  delete (OcPointer*) v;
99 }
100 
101 static void steer_val(void* v) {
102  OcPointer* ocp = (OcPointer*) v;
103  hoc_spop();
104  if (!ocp->valid_) {
105  hoc_execerror("Pointer points to freed address:", ocp->s_);
106  }
107  hoc_pushpx(ocp->p_);
108 }
109 
111  class2oc("Pointer", cons, destruct, members, nullptr, s_memb);
112  // now make the val variable an actual double
113  Symbol* sv = hoc_lookup("Pointer");
114  Symbol* sx = hoc_table_lookup("val", sv->u.ctemplate->symtable);
115  sx->type = VAR;
116  sx->arayinfo = NULL;
117  sv->u.ctemplate->steer = steer_val;
118 }
119 
120 StmtInfo::StmtInfo(const char* s)
121  : stmt_(s) {
122  parse();
123 }
124 
127 }
128 
129 
131  char buf[256], *d;
132  const char* s;
133  symlist_ = NULL;
134  ParseTopLevel ptl;
135  bool see_arg = false;
136  for (s = stmt_.c_str(), d = buf; *s; ++s, ++d) {
137  if (*s == '$' && s[1] == '1') {
138  strcpy(d, "hoc_ac_");
139  s++;
140  d += 6;
141  see_arg = true;
142  } else {
143  *d = *s;
144  }
145  }
146  if (!see_arg) {
147  strcpy(d, "=hoc_ac_");
148  d += 8;
149  }
150  *d = '\0';
152 }
153 
154 void StmtInfo::play_one(double val) {
155  ParseTopLevel ptl;
156  hoc_ac_ = val;
158 }
bool valid_
Definition: ocpointer.h:16
void assign(double)
Definition: ocpointer.cpp:47
OcPointer(const char *, double *)
Definition: ocpointer.cpp:25
virtual ~OcPointer()
Definition: ocpointer.cpp:35
virtual void update(Observable *)
Definition: ocpointer.cpp:43
double * p_
Definition: ocpointer.h:13
StmtInfo * sti_
Definition: ocpointer.h:15
char * s_
Definition: ocpointer.h:14
StmtInfo(const char *)
Definition: ocpointer.cpp:120
std::string stmt_
Definition: ocpointer.h:25
void parse()
Definition: ocpointer.cpp:130
void play_one(double)
Definition: ocpointer.cpp:154
Symbol * symstmt_
Definition: ocpointer.h:27
Symlist * symlist_
Definition: ocpointer.h:26
virtual ~StmtInfo()
Definition: ocpointer.cpp:125
void class2oc(const char *, ctor_f *cons, dtor_f *destruct, Member_func *, Member_ret_obj_func *, Member_ret_str_func *)
Definition: hoc_oop.cpp:1631
Symbol * hoc_table_lookup(const char *, Symlist *)
Definition: symbol.cpp:48
char * gargstr(int narg)
Definition: code2.cpp:227
#define v
Definition: md1redef.h:11
char buf[512]
Definition: init.cpp:13
void hoc_run_stmt(Symbol *sym)
Definition: code2.cpp:672
void hoc_pushpx(double *d)
Definition: code.cpp:834
double * hoc_val_pointer(const char *s)
Definition: code2.cpp:728
void hoc_free_list(Symlist **)
Definition: symbol.cpp:254
double hoc_ac_
Definition: hoc_init.cpp:222
Symbol * hoc_spop()
Definition: code.cpp:928
double * hoc_pgetarg(int narg)
Definition: oc_ansi.h:253
Symbol * hoc_lookup(const char *)
Definition: symbol.cpp:59
int hoc_is_pdouble_arg(int narg)
Definition: code.cpp:868
Symbol * hoc_parse_stmt(const char *str, Symlist **psymlist)
Definition: code2.cpp:679
#define assert(ex)
Definition: hocassrt.h:24
#define getarg
Definition: hocdec.h:17
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
void hoc_execerror(const char *s1, const char *s2)
Definition: nrnoc_aux.cpp:39
size_t p
s
Definition: multisend.cpp:521
int ifarg(int)
Definition: code.cpp:1607
HOC interpreter function declarations (included by hocdec.h)
static Member_func members[]
Definition: ocpointer.cpp:69
static void * cons(Object *)
Definition: ocpointer.cpp:76
static void destruct(void *v)
Definition: ocpointer.cpp:97
static Member_ret_str_func s_memb[]
Definition: ocpointer.cpp:73
static void steer_val(void *v)
Definition: ocpointer.cpp:101
static const char ** pname(void *v)
Definition: ocpointer.cpp:64
void OcPointer_reg()
Definition: ocpointer.cpp:110
static double assign(void *v)
Definition: ocpointer.cpp:55
#define NULL
Definition: spdefs.h:105
Definition: hocdec.h:173
Definition: model.h:47
union Symbol::@28 u
short type
Definition: model.h:48
cTemplate * ctemplate
Definition: hocdec.h:126
Arrayinfo * arayinfo
Definition: hocdec.h:130
Symlist * symtable
Definition: hocdec.h:148
void(* steer)(void *)
Definition: hocdec.h:160