forked from gridlab-d/gridlab-d
-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.cpp
172 lines (147 loc) · 3.81 KB
/
template.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "gridlabd.h"
#ifndef _class_template_h_
#define _class_template_h_
class classtemplate{
public:
// required
classtemplate(MODULE *module);
int create();
TIMESTAMP sync(TIMESTAMP t0, TIMESTAMP t1);
static CLASS *oclass;
static classtemplate *defaults
// optional
int init(OBJECT *parent);
int isa(char *classname);
TIMESTAMP presync(TIMESTAMP t0, TIMESTAMP t1);
TIMESTAMP postsync(TIMESTAMPT t0, TIMESTAMP t1);
int commit();
int notify(NOTIFYMODULE type, PROPERTY *prop); // type = {NM_PREUPDATE, NM_POSTUPDATE, NM_RESET}
};
#endif
CLASS *classtemplate::oclass = NULL;
classtemplate *classtemplate::defaults = NULL;
classtemplate::classtemplate(MODULE *module){
if (oclass==NULL)
{
// register the class definition
oclass = gl_register_class(mod,"house",sizeof(house_e),PC_PRETOPDOWN|PC_BOTTOMUP|PC_POSTTOPDOWN);
if (oclass==NULL)
GL_THROW("unable to register object class implemented by %s",__FILE__);
// publish the class properties
if (gl_publish_variable(oclass,
// insert properties here
NULL) < 1)
GL_THROW("unable to publish properties in %s",__FILE__);
}
}
int classtemplate::create(OBJECT *parent){
return 1;
}
EXPORT int create_classtemplate(OBJECT **obj, OBJECT *parent){
*obj = gl_create_object(classtemplate::oclass);
if(*obj != 0){
classtemplate *my = OBJECTDATA(*obj, classtemplate);
gl_set_parent(*obj, parent);
my->create();
return 1;
}
return 0;
}
TIMESTAMP classtemplate::presync(TIMESTAMP t0, TIMESTAMP t1){
return TS_NEVER;
}
TIMESTAMP classtemplate::sync(TIMESTAMP t0, TIMESTAMP t1){
return TS_NEVER;
}
TIMESTAMP classtemplate::postsync(TIMESTAMP t0, TIMESTAMP t1){
return TS_NEVER;
}
EXPORT TIMESTAMP classtemplate_sync(OBJECT *obj, TIMESTAMP t0, PASSCONFIG pass){
classtemplate *my = OBJECTDATA(obj, classtemplate);
TIMESTAMP t1 = TS_NEVER;
if (obj->clock <= ROUNDOFF)
obj->clock = t0; //set the object clock if it has not been set yet
try {
switch (pass)
{
case PC_PRETOPDOWN:
t1 = my->presync(obj->clock, t0);
break;
case PC_BOTTOMUP:
t1 = my->sync(obj->clock, t0);
obj->clock = t0;
break;
case PC_POSTTOPDOWN:
t1 = my->postsync(obj->clock, t0);
obj->clock = t0;
break;
default:
gl_error("classtemplate::sync- invalid pass configuration");
t1 = TS_INVALID; // serious error in exec.c
}
}
catch (char *msg)
{
gl_error("classtemplate::sync exception caught: %s", msg);
t1 = TS_INVALID;
}
catch (...)
{
gl_error("classtemplate::sync exception caught: no info");
t1 = TS_INVALID;
}
return t1;
}
int classtemplate::init(OBJECT *parent){
return 1;
}
EXPORT int classtemplate_init(OBJECT *obj){
try{
classtemplate *my = OBJECTDATA(obj, classtemplate);
return my->init(obj->parent);
}
catch (char *msg){
gl_error("classtemplate:%d (%s) %s", obj->id, obj->name ? obj->name : "anonymous", msg);
return 0;
}
}
int classtemplate::commit(TIMESTAMP t){
return 1;
}
EXPORT int classtemplate_commit(OBJECT *obj){
classtemplate *my = OBJECTDATA(obj, classtemplate);
try {
return my->commit(obj->clock);
} catch(const char *msg){
GL_THROW("%s (classtemplate:%d): %s", obj->name, obj->id, msg);
return 0;
}
}
int classtemplate::isa(char *classname){
return strcmp(oclass->name, classname);
}
EXPORT int isa_classtemplate(OBJECT *obj, char *classname)
{
if(obj != 0 && classname != 0){
return OBJECTDATA(obj,classtemplate)->isa(classname);
} else {
return 0;
}
}
int classtemplate::prenotify(PROPERTY *prop){
return 1;
}
int classtemplate::postnotify(PROPERTY *prop){
return 1;
}
EXPORT int notify_classtemplate(OBJECT *obj, NOTIFYMODULE nm, PROPERTY *prop){
classtemplate *my = OBJECTDATA(obj, classtemplate);
switch(nm){
case NM_PREUPDATE:
return my->prenotify(prop);
case NM_POSTUPDATE:
return my->postnotify(prop);
}
return 0;
}
// eof