1 | // -*- c++ -*- |
---|
2 | //***************************************************************************** |
---|
3 | /** @file countedref.cc |
---|
4 | * |
---|
5 | * @author Alexander Dreyer |
---|
6 | * @date 2012-08-15 |
---|
7 | * |
---|
8 | * This file defines reference countes interpreter objects and adds the |
---|
9 | * @c blackbox operations for high-level types 'reference' and 'shared'. |
---|
10 | * |
---|
11 | * @note This works was supported by the "Industrial Algebra" project. |
---|
12 | * |
---|
13 | * @par Copyright: |
---|
14 | * (c) 2012 by The Singular Team, see LICENSE file |
---|
15 | **/ |
---|
16 | //***************************************************************************** |
---|
17 | |
---|
18 | |
---|
19 | #include "mod2.h" |
---|
20 | #include "ipid.h" |
---|
21 | |
---|
22 | #include "countedref.h" |
---|
23 | |
---|
24 | #include "blackbox.h" |
---|
25 | #include "newstruct.h" |
---|
26 | #include "ipshell.h" |
---|
27 | |
---|
28 | |
---|
29 | /// Overloading ring destruction |
---|
30 | inline void CountedRefPtr_kill(ring r) { rKill(r); } |
---|
31 | |
---|
32 | |
---|
33 | /** @class CountedRefData |
---|
34 | * This class stores a reference counter as well as a Singular interpreter object. |
---|
35 | * It also take care of the context, e.g. the current ring, wrap object, etc. |
---|
36 | **/ |
---|
37 | class CountedRefData: |
---|
38 | public RefCounter { |
---|
39 | typedef CountedRefData self; |
---|
40 | public: |
---|
41 | typedef CountedRefWeakPtr<self*> back_ptr; |
---|
42 | private: |
---|
43 | typedef RefCounter base; |
---|
44 | |
---|
45 | /// Generate object linked to other reference (e.g. for subscripts) |
---|
46 | CountedRefData(leftv wrapid, back_ptr back): |
---|
47 | base(), m_data(wrapid), m_ring(back->m_ring), m_back(back) { |
---|
48 | } |
---|
49 | |
---|
50 | /// @name Disallow copying to avoid inconsistence |
---|
51 | //@{ |
---|
52 | self& operator=(const self&); |
---|
53 | CountedRefData(const self&); |
---|
54 | //@} |
---|
55 | |
---|
56 | public: |
---|
57 | typedef LeftvDeep::copy_tag copy_tag; |
---|
58 | |
---|
59 | /// Fix smart pointer type to referenced data |
---|
60 | typedef back_ptr::ptr_type ptr_type; |
---|
61 | |
---|
62 | /// Fix smart pointer type to ring |
---|
63 | typedef CountedRefPtr<ring, true> ring_ptr; |
---|
64 | |
---|
65 | /// Construct shared memory empty Singular object |
---|
66 | explicit CountedRefData(): |
---|
67 | base(), m_data(), m_ring(), m_back() { } |
---|
68 | |
---|
69 | /// Reference Singular object |
---|
70 | explicit CountedRefData(leftv data): |
---|
71 | base(), m_data(data), m_ring(parent(data)), m_back() { } |
---|
72 | |
---|
73 | /// Construct reference for Singular object |
---|
74 | CountedRefData(leftv data, copy_tag do_copy): |
---|
75 | base(), m_data(data, do_copy), m_ring(parent(data)), m_back() { } |
---|
76 | |
---|
77 | /// Destruct |
---|
78 | ~CountedRefData() { |
---|
79 | if (!m_back.unassigned()) { |
---|
80 | if (m_back == this) |
---|
81 | m_back.invalidate(); |
---|
82 | else |
---|
83 | m_data.clearid(root()); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | /// Generate object for indexing |
---|
88 | ptr_type wrapid() { return new self(m_data.idify(root()), weakref()); } |
---|
89 | |
---|
90 | /// Gerenate weak (but managed) reference to @c *this |
---|
91 | back_ptr weakref() { |
---|
92 | if (m_back.unassigned()) |
---|
93 | m_back = this; |
---|
94 | return m_back; |
---|
95 | } |
---|
96 | /// Replace with other Singular data |
---|
97 | self& operator=(leftv rhs) { |
---|
98 | m_data = rhs; |
---|
99 | m_ring = parent(rhs); |
---|
100 | return *this; |
---|
101 | } |
---|
102 | |
---|
103 | /// Write (shallow) copy to given handle |
---|
104 | BOOLEAN put(leftv res) { return broken() || m_data.put(res); } |
---|
105 | |
---|
106 | /// Extract (shallow) copy of stored data |
---|
107 | LeftvShallow operator*() const { return (broken()? LeftvShallow(): (const LeftvShallow&)m_data); } |
---|
108 | |
---|
109 | /// Determine active ring when ring dependency changes |
---|
110 | BOOLEAN rering() { |
---|
111 | if (m_ring ^ m_data.ringed()) m_ring = (m_ring? NULL: currRing); |
---|
112 | return (m_back && (m_back != this) && m_back->rering()); |
---|
113 | } |
---|
114 | |
---|
115 | /// Get the current context |
---|
116 | idhdl* root() { return (m_ring? &m_ring->idroot: &IDROOT); } |
---|
117 | |
---|
118 | /// Check whether identifier became invalid |
---|
119 | BOOLEAN broken() const { |
---|
120 | if (!m_back.unassigned() && !m_back) |
---|
121 | return complain("Back-reference broken"); |
---|
122 | |
---|
123 | if (m_ring) { |
---|
124 | if (m_ring != currRing) |
---|
125 | return complain("Referenced identifier not from current ring"); |
---|
126 | |
---|
127 | return m_data.isid() && m_data.brokenid(currRing->idroot) && |
---|
128 | complain("Referenced identifier not available in ring anymore"); |
---|
129 | } |
---|
130 | |
---|
131 | if (!m_data.isid()) return FALSE; |
---|
132 | return m_data.brokenid(IDROOT) && |
---|
133 | ((currPack == basePack) || m_data.brokenid(basePack->idroot)) && |
---|
134 | complain("Referenced identifier not available in current context"); |
---|
135 | } |
---|
136 | |
---|
137 | /// Reassign actual object |
---|
138 | BOOLEAN assign(leftv result, leftv arg) { |
---|
139 | |
---|
140 | if (!m_data.isid()) { |
---|
141 | (*this) = arg; |
---|
142 | return FALSE; |
---|
143 | } |
---|
144 | return put(result) || iiAssign(result, arg) || rering(); |
---|
145 | } |
---|
146 | /// Recover additional information (e.g. subexpression) from likewise object |
---|
147 | BOOLEAN retrieve(leftv res) { return m_data.retrieve(res); } |
---|
148 | |
---|
149 | /// Check whether data is all-zero |
---|
150 | BOOLEAN unassigned() const { return m_data.unassigned(); } |
---|
151 | |
---|
152 | private: |
---|
153 | /// Raise error message and return @c TRUE |
---|
154 | BOOLEAN complain(const char* text) const { |
---|
155 | Werror(text); |
---|
156 | return TRUE; |
---|
157 | } |
---|
158 | |
---|
159 | /// Store ring for ring-dependent objects |
---|
160 | static ring parent(leftv rhs) { |
---|
161 | return (rhs->RingDependend()? currRing: NULL); |
---|
162 | } |
---|
163 | |
---|
164 | protected: |
---|
165 | /// Singular object |
---|
166 | LeftvDeep m_data; |
---|
167 | |
---|
168 | /// Store namespace for ring-dependent objects |
---|
169 | ring_ptr m_ring; |
---|
170 | |
---|
171 | /// Reference to actual object for wrap structures |
---|
172 | back_ptr m_back; |
---|
173 | }; |
---|
174 | |
---|
175 | /// Supporting smart pointer @c CountedRefPtr |
---|
176 | inline void CountedRefPtr_kill(CountedRefData* data) { delete data; } |
---|
177 | |
---|
178 | |
---|
179 | /// blackbox support - initialization |
---|
180 | void* countedref_Init(blackbox*) |
---|
181 | { |
---|
182 | return NULL; |
---|
183 | } |
---|
184 | |
---|
185 | /// We use the as a marker of refernce types. |
---|
186 | BOOLEAN countedref_CheckAssign(blackbox *b, leftv L, leftv R) |
---|
187 | { |
---|
188 | return FALSE; |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | class CountedRef { |
---|
193 | typedef CountedRef self; |
---|
194 | |
---|
195 | public: |
---|
196 | /// name type for identifiers |
---|
197 | typedef int id_type; |
---|
198 | |
---|
199 | /// Name type for handling referenced data |
---|
200 | typedef CountedRefData data_type; |
---|
201 | |
---|
202 | /// Fix smart pointer type to referenced data |
---|
203 | typedef CountedRefPtr<CountedRefData*> data_ptr; |
---|
204 | |
---|
205 | /// Check whether argument is already a reference type |
---|
206 | static BOOLEAN is_ref(leftv arg) { |
---|
207 | int typ = arg->Typ(); |
---|
208 | return ((typ > MAX_TOK) && |
---|
209 | (getBlackboxStuff(typ)->blackbox_CheckAssign == countedref_CheckAssign)); |
---|
210 | } |
---|
211 | |
---|
212 | /// Reference given Singular data |
---|
213 | explicit CountedRef(leftv arg): m_data(new data_type(arg)) { } |
---|
214 | |
---|
215 | protected: |
---|
216 | /// Recover previously constructed reference |
---|
217 | CountedRef(data_ptr arg): m_data(arg) { assume(arg); } |
---|
218 | |
---|
219 | public: |
---|
220 | /// Construct copy |
---|
221 | CountedRef(const self& rhs): m_data(rhs.m_data) { } |
---|
222 | |
---|
223 | /// Replace reference |
---|
224 | self& operator=(const self& rhs) { |
---|
225 | m_data = rhs.m_data; |
---|
226 | return *this; |
---|
227 | } |
---|
228 | |
---|
229 | BOOLEAN assign(leftv result, leftv arg) { |
---|
230 | return m_data->assign(result,arg); |
---|
231 | } |
---|
232 | |
---|
233 | /// Extract (shallow) copy of stored data |
---|
234 | LeftvShallow operator*() { return m_data->operator*(); } |
---|
235 | |
---|
236 | /// Construct reference data object marked by given identifier number |
---|
237 | BOOLEAN outcast(leftv res, int typ) { |
---|
238 | res->rtyp = typ; |
---|
239 | return outcast(res); |
---|
240 | } |
---|
241 | |
---|
242 | /// Construct reference data object from *this |
---|
243 | BOOLEAN outcast(leftv res) { |
---|
244 | if (res->rtyp == IDHDL) |
---|
245 | IDDATA((idhdl)res->data) = (char *)outcast(); |
---|
246 | else |
---|
247 | res->data = (void *)outcast(); |
---|
248 | return FALSE; |
---|
249 | } |
---|
250 | |
---|
251 | /// Construct raw reference data |
---|
252 | data_type* outcast() { |
---|
253 | m_data.reclaim(); |
---|
254 | return m_data; |
---|
255 | } |
---|
256 | |
---|
257 | /// Kills a link to the referenced object |
---|
258 | void destruct() { m_data.release(); } |
---|
259 | |
---|
260 | /// Kills the link to the referenced object |
---|
261 | ~CountedRef() { } |
---|
262 | |
---|
263 | /// Replaces argument by a shallow copy of the references data |
---|
264 | BOOLEAN dereference(leftv arg) { |
---|
265 | m_data.reclaim(); |
---|
266 | BOOLEAN b= m_data->put(arg) || ((arg->next != NULL) && resolve(arg->next)); |
---|
267 | m_data.release(); |
---|
268 | return b; |
---|
269 | } |
---|
270 | |
---|
271 | /// Check whether object in valid in current context |
---|
272 | BOOLEAN broken() {return m_data->broken(); } |
---|
273 | |
---|
274 | /// Check whether (shared) data was initialized but not assigned yet. |
---|
275 | BOOLEAN unassigned() const { return m_data->unassigned(); } |
---|
276 | |
---|
277 | /// Get number of references pointing here, too |
---|
278 | BOOLEAN count(leftv res) { return construct(res, m_data.count() - 1); } |
---|
279 | |
---|
280 | // Get internal indentifier |
---|
281 | BOOLEAN enumerate(leftv res) { return construct(res, (long)(data_type*)m_data); } |
---|
282 | |
---|
283 | /// Check for likewise identifiers |
---|
284 | BOOLEAN likewise(leftv res, leftv arg) { |
---|
285 | return resolve(arg) || construct(res, operator*()->data == arg->data); |
---|
286 | } |
---|
287 | |
---|
288 | /// Check for identical reference objects |
---|
289 | BOOLEAN same(leftv res, leftv arg) { |
---|
290 | return construct(res, m_data == arg->Data()); |
---|
291 | } |
---|
292 | |
---|
293 | /// Get type of references data |
---|
294 | BOOLEAN type(leftv res) { |
---|
295 | return construct(res, Tok2Cmdname(operator*()->Typ())); |
---|
296 | }; |
---|
297 | |
---|
298 | /// Get (possibly) internal identifier name |
---|
299 | BOOLEAN name(leftv res) { return construct(res, operator*()->Name()); } |
---|
300 | |
---|
301 | /// Recover the actual object from raw Singular data |
---|
302 | static self cast(void* data) { |
---|
303 | assume(data != NULL); |
---|
304 | return self(static_cast<data_type*>(data)); |
---|
305 | } |
---|
306 | |
---|
307 | /// Recover the actual object from Singular interpreter object |
---|
308 | static self cast(leftv arg) { |
---|
309 | assume(arg != NULL); assume(is_ref(arg)); |
---|
310 | return self::cast(arg->Data()); |
---|
311 | } |
---|
312 | |
---|
313 | /// If necessary dereference. |
---|
314 | static BOOLEAN resolve(leftv arg) { |
---|
315 | assume(arg != NULL); |
---|
316 | while (is_ref(arg)) { if(CountedRef::cast(arg).dereference(arg)) return TRUE; }; |
---|
317 | return (arg->next != NULL) && resolve(arg->next); |
---|
318 | } |
---|
319 | |
---|
320 | /// Construct integer value |
---|
321 | static BOOLEAN construct(leftv res, long data) { |
---|
322 | res->data = (void*) data; |
---|
323 | res->rtyp = INT_CMD; |
---|
324 | return FALSE; |
---|
325 | } |
---|
326 | |
---|
327 | /// Construct string |
---|
328 | static BOOLEAN construct(leftv res, const char* data) { |
---|
329 | res->data = (void*)omStrDup(data); |
---|
330 | res->rtyp = STRING_CMD; |
---|
331 | return FALSE; |
---|
332 | } |
---|
333 | /// Construct void-style object |
---|
334 | static BOOLEAN construct(leftv res) { |
---|
335 | res->data = NULL; |
---|
336 | res->rtyp = NONE; |
---|
337 | return FALSE; |
---|
338 | } |
---|
339 | |
---|
340 | protected: |
---|
341 | /// Store pointer to actual data |
---|
342 | data_ptr m_data; |
---|
343 | }; |
---|
344 | |
---|
345 | /// blackbox support - convert to string representation |
---|
346 | void countedref_Print(blackbox *b, void* ptr) |
---|
347 | { |
---|
348 | if (ptr) (*CountedRef::cast(ptr))->Print(); |
---|
349 | else PrintS("<unassigned reference or shared memory>"); |
---|
350 | } |
---|
351 | |
---|
352 | /// blackbox support - convert to string representation |
---|
353 | char* countedref_String(blackbox *b, void* ptr) |
---|
354 | { |
---|
355 | if (ptr == NULL) return omStrDup(sNoName); |
---|
356 | return (*CountedRef::cast(ptr))->String(); |
---|
357 | } |
---|
358 | |
---|
359 | /// blackbox support - copy element |
---|
360 | void* countedref_Copy(blackbox*b, void* ptr) |
---|
361 | { |
---|
362 | if (ptr) return CountedRef::cast(ptr).outcast(); |
---|
363 | return NULL; |
---|
364 | } |
---|
365 | |
---|
366 | /// blackbox support - assign element |
---|
367 | BOOLEAN countedref_Assign(leftv result, leftv arg) |
---|
368 | { |
---|
369 | // Case: replace assignment behind reference |
---|
370 | if (result->Data() != NULL) { |
---|
371 | CountedRef ref = CountedRef::cast(result); |
---|
372 | return CountedRef::resolve(arg) || ref.assign(result, arg); |
---|
373 | } |
---|
374 | |
---|
375 | // Case: copy reference |
---|
376 | if (result->Typ() == arg->Typ()) |
---|
377 | return CountedRef::cast(arg).outcast(result); |
---|
378 | |
---|
379 | // Case: new reference |
---|
380 | if ((arg->rtyp == IDHDL) || CountedRef::is_ref(arg)) |
---|
381 | return CountedRef(arg).outcast(result); |
---|
382 | |
---|
383 | Werror("Can only take reference from identifier"); |
---|
384 | return TRUE; |
---|
385 | } |
---|
386 | |
---|
387 | BOOLEAN countedref_CheckInit(leftv res, leftv arg) |
---|
388 | { |
---|
389 | if (arg->Data() != NULL) return FALSE; |
---|
390 | res->rtyp = NONE; |
---|
391 | Werror("Noninitialized access"); |
---|
392 | return TRUE; |
---|
393 | } |
---|
394 | |
---|
395 | /// blackbox support - unary operations |
---|
396 | BOOLEAN countedref_Op1(int op, leftv res, leftv head) |
---|
397 | { |
---|
398 | if(op == TYPEOF_CMD) |
---|
399 | return blackboxDefaultOp1(op, res, head); |
---|
400 | |
---|
401 | if (countedref_CheckInit(res, head)) return TRUE; |
---|
402 | |
---|
403 | if ((op == DEF_CMD) || (op == head->Typ())) { |
---|
404 | res->rtyp = head->Typ(); |
---|
405 | return iiAssign(res, head); |
---|
406 | } |
---|
407 | |
---|
408 | CountedRef ref = CountedRef::cast(head); |
---|
409 | return ref.dereference(head) || |
---|
410 | iiExprArith1(res, head, op == LINK_CMD? head->Typ(): op); |
---|
411 | } |
---|
412 | |
---|
413 | |
---|
414 | |
---|
415 | /// blackbox support - binary operations (resolve seocnd argument) |
---|
416 | static BOOLEAN countedref_Op2_(int op, leftv res, leftv head, leftv arg) |
---|
417 | { |
---|
418 | if (CountedRef::is_ref(arg)) { |
---|
419 | CountedRef ref = CountedRef::cast(arg); |
---|
420 | return ref.dereference(arg) || iiExprArith2(res, head, op, arg); |
---|
421 | } |
---|
422 | return iiExprArith2(res, head, op, arg); |
---|
423 | } |
---|
424 | |
---|
425 | BOOLEAN countedref_Op2(int op, leftv res, leftv head, leftv arg) |
---|
426 | { |
---|
427 | if (countedref_CheckInit(res, head)) return TRUE; |
---|
428 | if (CountedRef::is_ref(head)) { |
---|
429 | CountedRef ref = CountedRef::cast(head); |
---|
430 | return ref.dereference(head) || countedref_Op2_(op, res, head, arg); |
---|
431 | } |
---|
432 | return countedref_Op2_(op, res, head, arg); |
---|
433 | } |
---|
434 | |
---|
435 | static BOOLEAN countedref_Op3__(int op, leftv res, leftv head, leftv arg1, leftv arg2) |
---|
436 | { |
---|
437 | |
---|
438 | if (CountedRef::is_ref(arg2)) { |
---|
439 | CountedRef ref = CountedRef::cast(arg2); |
---|
440 | return ref.dereference(arg2) || iiExprArith3(res, op, head, arg1, arg2); |
---|
441 | } |
---|
442 | return iiExprArith3(res, op, head, arg1, arg2); |
---|
443 | } |
---|
444 | |
---|
445 | static BOOLEAN countedref_Op3_(int op, leftv res, leftv head, leftv arg1, leftv arg2) |
---|
446 | { |
---|
447 | if (CountedRef::is_ref(arg1)) { |
---|
448 | CountedRef ref = CountedRef::cast(arg1); |
---|
449 | return ref.dereference(arg1) || countedref_Op3__(op, res, head, arg1, arg2); |
---|
450 | } |
---|
451 | return countedref_Op3__(op, res, head, arg1, arg2); |
---|
452 | } |
---|
453 | |
---|
454 | |
---|
455 | /// blackbox support - ternary operations |
---|
456 | BOOLEAN countedref_Op3(int op, leftv res, leftv head, leftv arg1, leftv arg2) |
---|
457 | { |
---|
458 | if (countedref_CheckInit(res, head)) return TRUE; |
---|
459 | if (CountedRef::is_ref(head)) { |
---|
460 | CountedRef ref = CountedRef::cast(head); |
---|
461 | return ref.dereference(head) || countedref_Op3_(op, res, head, arg1, arg2); |
---|
462 | } |
---|
463 | return countedref_Op3_(op, res, head, arg1, arg2); |
---|
464 | } |
---|
465 | |
---|
466 | |
---|
467 | /// blackbox support - destruction |
---|
468 | void countedref_destroy(blackbox *b, void* ptr) |
---|
469 | { |
---|
470 | if (ptr) CountedRef::cast(ptr).destruct(); |
---|
471 | } |
---|
472 | |
---|
473 | |
---|
474 | class CountedRefShared: |
---|
475 | public CountedRef { |
---|
476 | typedef CountedRefShared self; |
---|
477 | typedef CountedRef base; |
---|
478 | |
---|
479 | /// Reinterprete @c CountedRef as @c CountedRefShared |
---|
480 | CountedRefShared(const base& rhs): base(rhs) { } |
---|
481 | |
---|
482 | /// Generate from data pointer |
---|
483 | CountedRefShared(data_ptr rhs): base(rhs) { } |
---|
484 | |
---|
485 | public: |
---|
486 | /// Default constructor for initialized, but all-zero, shared data object |
---|
487 | CountedRefShared(): base(new data_type) { } |
---|
488 | |
---|
489 | /// Construct internal copy of Singular interpreter object |
---|
490 | explicit CountedRefShared(leftv arg): base(new data_type(arg, data_type::copy_tag())) { } |
---|
491 | |
---|
492 | /// Construct new reference to internal data |
---|
493 | CountedRefShared(const self& rhs): base(rhs) { } |
---|
494 | |
---|
495 | /// Desctruct |
---|
496 | ~CountedRefShared() { } |
---|
497 | |
---|
498 | /// Change reference to shared data |
---|
499 | self& operator=(const self& rhs) { |
---|
500 | return static_cast<self&>(base::operator=(rhs)); |
---|
501 | } |
---|
502 | |
---|
503 | /// Recovering outcasted @c CountedRefShared object from interpreter object |
---|
504 | static self cast(leftv arg) { return base::cast(arg); } |
---|
505 | |
---|
506 | /// Recovering outcasted @c CountedRefShared object from raw data |
---|
507 | static self cast(void* arg) { return base::cast(arg); } |
---|
508 | |
---|
509 | /// Temporarily wrap with identifier for '[' and '.' operation |
---|
510 | self wrapid() { return self(m_data->wrapid()); } |
---|
511 | |
---|
512 | /// Generate weak reference (may get invalid) |
---|
513 | data_type::back_ptr weakref() { return m_data->weakref(); } |
---|
514 | |
---|
515 | /// Recover more information (e.g. subexpression data) from computed result |
---|
516 | BOOLEAN retrieve(leftv res, int typ) { |
---|
517 | return (m_data->retrieve(res) && outcast(res, typ)); |
---|
518 | } |
---|
519 | }; |
---|
520 | |
---|
521 | /// Blackbox support - generate initialized, but all-zero - shared data |
---|
522 | void* countedref_InitShared(blackbox*) |
---|
523 | { |
---|
524 | return CountedRefShared().outcast(); |
---|
525 | } |
---|
526 | |
---|
527 | /// Blackbox support - unary operation for shared data |
---|
528 | BOOLEAN countedref_Op1Shared(int op, leftv res, leftv head) |
---|
529 | { |
---|
530 | if(op == TYPEOF_CMD) |
---|
531 | return blackboxDefaultOp1(op, res, head); |
---|
532 | |
---|
533 | if (countedref_CheckInit(res, head)) return TRUE; |
---|
534 | |
---|
535 | if ((op == DEF_CMD) || (op == head->Typ())) { |
---|
536 | res->rtyp = head->Typ(); |
---|
537 | return iiAssign(res, head); |
---|
538 | } |
---|
539 | |
---|
540 | CountedRefShared ref = CountedRefShared::cast(head); |
---|
541 | |
---|
542 | if ((op == LINK_CMD) ) { |
---|
543 | if (ref.dereference(head)) return TRUE; |
---|
544 | res->Copy(head); |
---|
545 | return (res->Typ() == NONE); |
---|
546 | } |
---|
547 | |
---|
548 | CountedRefShared wrap = ref.wrapid(); |
---|
549 | int typ = head->Typ(); |
---|
550 | return wrap.dereference(head) || iiExprArith1(res, head, op) || |
---|
551 | wrap.retrieve(res, typ); |
---|
552 | } |
---|
553 | |
---|
554 | |
---|
555 | /// blackbox support - binary operations |
---|
556 | BOOLEAN countedref_Op2Shared(int op, leftv res, leftv head, leftv arg) |
---|
557 | { |
---|
558 | if (countedref_CheckInit(res, head)) return TRUE; |
---|
559 | |
---|
560 | if (CountedRefShared::is_ref(head)) { |
---|
561 | CountedRefShared wrap = CountedRefShared::cast(head).wrapid(); |
---|
562 | int typ = head->Typ(); |
---|
563 | return wrap.dereference(head) || countedref_Op2_(op, res, head, arg) || |
---|
564 | wrap.retrieve(res, typ); |
---|
565 | } |
---|
566 | |
---|
567 | return countedref_Op2_(op, res, head, arg); |
---|
568 | } |
---|
569 | |
---|
570 | /// blackbox support - n-ary operations |
---|
571 | BOOLEAN countedref_OpM(int op, leftv res, leftv args) |
---|
572 | { |
---|
573 | if (args->Data() == NULL) return FALSE; |
---|
574 | |
---|
575 | if(op == SYSTEM_CMD) { |
---|
576 | if (args->next) { |
---|
577 | leftv next = args->next; |
---|
578 | args->next = NULL; |
---|
579 | |
---|
580 | char* name = (next->Typ() == STRING_CMD? |
---|
581 | (char*) next->Data(): (char*)next->Name()); |
---|
582 | next = next->next; |
---|
583 | |
---|
584 | if (strcmp(name, "help") == 0) { |
---|
585 | PrintS("system(<ref>, ...): extended functionality for reference/shared data <ref>\n"); |
---|
586 | PrintS(" system(<ref>, count) - number of references pointing to <ref>\n"); |
---|
587 | PrintS(" system(<ref>, enumerate) - unique number for identifying <ref>\n"); |
---|
588 | PrintS(" system(<ref>, undefined) - checks whether <ref> had been assigned\n"); |
---|
589 | PrintS(" system(<ref>, \"help\") - prints this information message\n"); |
---|
590 | PrintS(" system(<ref>, \"typeof\") - actual type referenced by <ref>\n"); |
---|
591 | PrintS(" system(<ref1>, same, <ref2>) - tests for identic reference objects\n"); |
---|
592 | return CountedRef::construct(res); |
---|
593 | } |
---|
594 | if (strncmp(name, "undef", 5) == 0) { |
---|
595 | return CountedRef::construct(res, args->Data()? |
---|
596 | (CountedRef::cast(args).unassigned()? 1: 2): 0); |
---|
597 | } |
---|
598 | |
---|
599 | CountedRef obj = CountedRef::cast(args); |
---|
600 | if (next) { |
---|
601 | if (strcmp(name, "same") == 0) return obj.same(res, next); |
---|
602 | // likewise may be hard to interprete, so we not not document it above |
---|
603 | if (strncmp(name, "like", 4) == 0) return obj.likewise(res, next); |
---|
604 | } |
---|
605 | if (strncmp(name, "count", 5) == 0) return obj.count(res); |
---|
606 | if (strncmp(name, "enum", 4) == 0) return obj.enumerate(res); |
---|
607 | if (strcmp(name, "name") == 0) return obj.name(res); // undecumented |
---|
608 | if (strncmp(name, "typ", 3) == 0) return obj.type(res); |
---|
609 | } |
---|
610 | return TRUE; |
---|
611 | } |
---|
612 | if (op == LIST_CMD){ |
---|
613 | res->rtyp = op; |
---|
614 | return jjLIST_PL(res, args); |
---|
615 | } |
---|
616 | CountedRef ref = CountedRef::cast(args); |
---|
617 | return ref.dereference(args) || iiExprArithM(res, args, op); |
---|
618 | } |
---|
619 | |
---|
620 | /// blackbox support - assign element |
---|
621 | BOOLEAN countedref_AssignShared(leftv result, leftv arg) |
---|
622 | { |
---|
623 | /// Case: replace assignment behind reference |
---|
624 | if ((result->Data() != NULL) && !CountedRefShared::cast(result).unassigned()) { |
---|
625 | CountedRef ref = CountedRef::cast(result); |
---|
626 | return CountedRef::resolve(arg) || ref.assign(result, arg); |
---|
627 | } |
---|
628 | |
---|
629 | /// Case: new reference to already shared data |
---|
630 | if (result->Typ() == arg->Typ()) { |
---|
631 | if (result->Data() != NULL) |
---|
632 | CountedRefShared::cast(result).destruct(); |
---|
633 | return CountedRefShared::cast(arg).outcast(result); |
---|
634 | } |
---|
635 | if(CountedRefShared::cast(result).unassigned()) { |
---|
636 | return CountedRefShared::cast(result).assign(result, arg); |
---|
637 | |
---|
638 | return FALSE; |
---|
639 | } |
---|
640 | |
---|
641 | /// Case: new shared data |
---|
642 | return CountedRefShared(arg).outcast(result); |
---|
643 | } |
---|
644 | |
---|
645 | /// blackbox support - destruction |
---|
646 | void countedref_destroyShared(blackbox *b, void* ptr) |
---|
647 | { |
---|
648 | if (ptr) CountedRefShared::cast(ptr).destruct(); |
---|
649 | } |
---|
650 | |
---|
651 | |
---|
652 | BOOLEAN countedref_serialize(blackbox *b, void *d, si_link f) |
---|
653 | { |
---|
654 | sleftv l; |
---|
655 | memset(&l,0,sizeof(l)); |
---|
656 | l.rtyp = STRING_CMD; |
---|
657 | l.data = (void*)omStrDup("shared"); // references are converted |
---|
658 | f->m->Write(f, &l); |
---|
659 | CountedRefShared::cast(d).dereference(&l); |
---|
660 | f->m->Write(f, &l); |
---|
661 | return FALSE; |
---|
662 | } |
---|
663 | |
---|
664 | BOOLEAN countedref_deserialize(blackbox **b, void **d, si_link f) |
---|
665 | { |
---|
666 | // rtyp must be set correctly (to the blackbox id) by routine calling |
---|
667 | leftv data=f->m->Read(f); |
---|
668 | CountedRefShared sh(data); |
---|
669 | *d = sh.outcast(); |
---|
670 | return FALSE; |
---|
671 | } |
---|
672 | |
---|
673 | void countedref_init() |
---|
674 | { |
---|
675 | blackbox *bbx = (blackbox*)omAlloc0(sizeof(blackbox)); |
---|
676 | bbx->blackbox_CheckAssign = countedref_CheckAssign; |
---|
677 | bbx->blackbox_destroy = countedref_destroy; |
---|
678 | bbx->blackbox_String = countedref_String; |
---|
679 | bbx->blackbox_Print = countedref_Print; |
---|
680 | bbx->blackbox_Init = countedref_Init; |
---|
681 | bbx->blackbox_Copy = countedref_Copy; |
---|
682 | bbx->blackbox_Assign = countedref_Assign; |
---|
683 | bbx->blackbox_Op1 = countedref_Op1; |
---|
684 | bbx->blackbox_Op2 = countedref_Op2; |
---|
685 | bbx->blackbox_Op3 = countedref_Op3; |
---|
686 | bbx->blackbox_OpM = countedref_OpM; |
---|
687 | bbx->blackbox_serialize = countedref_serialize; |
---|
688 | bbx->blackbox_deserialize = countedref_deserialize; |
---|
689 | bbx->data = omAlloc0(newstruct_desc_size()); |
---|
690 | setBlackboxStuff(bbx, "reference"); |
---|
691 | |
---|
692 | /// The @c shared type is "inherited" from @c reference. |
---|
693 | /// It just uses another constructor (to make its own copy of the). |
---|
694 | blackbox *bbxshared = |
---|
695 | (blackbox*)memcpy(omAlloc(sizeof(blackbox)), bbx, sizeof(blackbox)); |
---|
696 | bbxshared->blackbox_CheckAssign = countedref_CheckAssign; |
---|
697 | bbxshared->blackbox_Assign = countedref_AssignShared; |
---|
698 | bbxshared->blackbox_destroy = countedref_destroyShared; |
---|
699 | bbxshared->blackbox_Op1 = countedref_Op1Shared; |
---|
700 | bbxshared->blackbox_Op2 = countedref_Op2Shared; |
---|
701 | bbxshared->blackbox_Init = countedref_InitShared; |
---|
702 | bbxshared->data = omAlloc0(newstruct_desc_size()); |
---|
703 | setBlackboxStuff(bbxshared, "shared"); |
---|
704 | } |
---|
705 | |
---|
706 | #ifdef HAVE_DYNAMIC_COUNTEDREF |
---|
707 | extern "C" { void mod_init() { countedref_init(); } } |
---|
708 | #endif |
---|
709 | |
---|