Changeset 1592b9 in git


Ignore:
Timestamp:
Sep 19, 2012, 4:49:01 PM (11 years ago)
Author:
Hans Schoenemann <hannes@…>
Branches:
(u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', 'a800fe4b3e9d37a38c5a10cc0ae9dfa0c15a4ee6')
Children:
abe5c8b5ebdd2be025cd137e4a089c3780efb8a3
Parents:
6b81068b7df492eba0593f871f54cb7afc957691
Message:
chg: format
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Singular/countedref.cc

    r6b8106 r1592b9  
    66 * @date 2012-08-15
    77 *
    8  * This file defines reference countes interpreter objects and adds the 
     8 * This file defines reference countes interpreter objects and adds the
    99 * @c blackbox operations for high-level types 'reference' and 'shared'.
    1010 *
    1111 * @note This works was supported by the "Industrial Algebra" project.
    12  * 
     12 *
    1313 * @par Copyright:
    1414 *   (c) 2012 by The Singular Team, see LICENSE file
     
    8181        m_back.invalidate();
    8282      else
    83         m_data.clearid(root()); 
     83        m_data.clearid(root());
    8484    }
    8585  }
     
    9090  /// Gerenate  weak (but managed) reference to @c *this
    9191  back_ptr weakref() {
    92     if (m_back.unassigned()) 
     92    if (m_back.unassigned())
    9393      m_back = this;
    9494    return m_back;
     
    118118  /// Check whether identifier became invalid
    119119  BOOLEAN broken() const {
    120     if (!m_back.unassigned() && !m_back) 
    121       return complain("Back-reference broken");   
     120    if (!m_back.unassigned() && !m_back)
     121      return complain("Back-reference broken");
    122122
    123123    if (m_ring) {
    124       if (m_ring != currRing) 
    125         return complain("Referenced identifier not from current ring");   
     124      if (m_ring != currRing)
     125        return complain("Referenced identifier not from current ring");
    126126
    127127      return m_data.isid()  && m_data.brokenid(currRing->idroot) &&
    128         complain("Referenced identifier not available in ring anymore"); 
     128        complain("Referenced identifier not available in ring anymore");
    129129    }
    130    
     130
    131131    if (!m_data.isid()) return FALSE;
    132132    return  m_data.brokenid(IDROOT) &&
     
    136136
    137137  /// Reassign actual object
    138   BOOLEAN assign(leftv result, leftv arg) { 
     138  BOOLEAN assign(leftv result, leftv arg) {
    139139
    140140    if (!m_data.isid()) {
     
    147147  BOOLEAN retrieve(leftv res) { return m_data.retrieve(res); }
    148148
    149   /// Check whether data is all-zero 
     149  /// Check whether data is all-zero
    150150  BOOLEAN unassigned() const { return m_data.unassigned(); }
    151151
    152152private:
    153153  /// Raise error message and return @c TRUE
    154   BOOLEAN complain(const char* text) const  {
    155     Werror(text);
     154  BOOLEAN complain(const char* text) const
     155  {
     156    WerrorS(text);
    156157    return TRUE;
    157158  }
    158159
    159160  /// Store ring for ring-dependent objects
    160   static ring parent(leftv rhs) {
    161     return (rhs->RingDependend()? currRing: NULL);
     161  static ring parent(leftv rhs)
     162  {
     163    return (rhs->RingDependend()? currRing: NULL);
    162164  }
    163165
     
    169171  ring_ptr m_ring;
    170172
    171   /// Reference to actual object for wrap structures 
     173  /// Reference to actual object for wrap structures
    172174  back_ptr m_back;
    173175};
     
    214216  }
    215217
    216   /// Reference given Singular data 
     218  /// Reference given Singular data
    217219  explicit CountedRef(leftv arg):  m_data(new data_type(arg)) { }
    218220
     
    254256
    255257  /// Construct raw reference data
    256   data_type* outcast() { 
     258  data_type* outcast() {
    257259    m_data.reclaim();
    258260    return m_data;
     
    287289  /// Check for likewise identifiers
    288290  BOOLEAN likewise(leftv res, leftv arg) {
    289     return resolve(arg) || construct(res, operator*()->data == arg->data); 
     291    return resolve(arg) || construct(res, operator*()->data == arg->data);
    290292  }
    291293
    292294  /// Check for identical reference objects
    293   BOOLEAN same(leftv res, leftv arg) { 
     295  BOOLEAN same(leftv res, leftv arg) {
    294296    return construct(res, m_data == arg->Data());
    295297  }
    296298
    297299  /// Get type of references data
    298   BOOLEAN type(leftv res) { 
     300  BOOLEAN type(leftv res) {
    299301    return construct(res, Tok2Cmdname(operator*()->Typ()));
    300302  };
     
    363365/// blackbox support - copy element
    364366void* countedref_Copy(blackbox*b, void* ptr)
    365 { 
     367{
    366368  if (ptr) return CountedRef::cast(ptr).outcast();
    367369  return NULL;
     
    374376  if (result->Data() != NULL) {
    375377    CountedRef ref = CountedRef::cast(result);
    376     return CountedRef::resolve(arg) || ref.assign(result, arg); 
    377   }
    378  
     378    return CountedRef::resolve(arg) || ref.assign(result, arg);
     379  }
     380
    379381  // Case: copy reference
    380382  if (result->Typ() == arg->Typ())
     
    385387    return CountedRef(arg).outcast(result);
    386388
    387   Werror("Can only take reference from identifier");
     389  WerrorS("Can only take reference from identifier");
    388390  return TRUE;
    389391}
     
    393395  if (arg->Data() != NULL) return FALSE;
    394396  res->rtyp = NONE;
    395   Werror("Noninitialized access");
     397  WerrorS("Noninitialized access");
    396398  return TRUE;
    397399}
    398                                                                  
     400
    399401/// blackbox support - unary operations
    400402BOOLEAN countedref_Op1(int op, leftv res, leftv head)
     
    404406
    405407  if (countedref_CheckInit(res, head)) return TRUE;
    406  
    407   if ((op == DEF_CMD) || (op == head->Typ())) {
     408
     409  if ((op == DEF_CMD) || (op == head->Typ()))
     410  {
    408411    res->rtyp = head->Typ();
    409412    return iiAssign(res, head);
     
    420423static BOOLEAN countedref_Op2_(int op, leftv res, leftv head, leftv arg)
    421424{
    422   if (CountedRef::is_ref(arg)) {
     425  if (CountedRef::is_ref(arg))
     426  {
    423427    CountedRef ref = CountedRef::cast(arg);
    424428    return ref.dereference(arg) || iiExprArith2(res, head, op, arg);
     
    430434{
    431435  if (countedref_CheckInit(res, head)) return TRUE;
    432   if (CountedRef::is_ref(head)) {
     436  if (CountedRef::is_ref(head))
     437  {
    433438    CountedRef ref = CountedRef::cast(head);
    434439    return ref.dereference(head) || countedref_Op2_(op, res, head, arg);
     
    440445{
    441446
    442   if (CountedRef::is_ref(arg2)) {
     447  if (CountedRef::is_ref(arg2))
     448  {
    443449    CountedRef ref = CountedRef::cast(arg2);
    444450    return ref.dereference(arg2) || iiExprArith3(res, op, head, arg1, arg2);
     
    449455static BOOLEAN countedref_Op3_(int op, leftv res, leftv head, leftv arg1, leftv arg2)
    450456{
    451   if (CountedRef::is_ref(arg1)) {
     457  if (CountedRef::is_ref(arg1))
     458  {
    452459    CountedRef ref = CountedRef::cast(arg1);
    453460    return ref.dereference(arg1) || countedref_Op3__(op, res, head, arg1, arg2);
     
    461468{
    462469  if (countedref_CheckInit(res, head)) return TRUE;
    463   if (CountedRef::is_ref(head)) {
     470  if (CountedRef::is_ref(head))
     471  {
    464472    CountedRef ref = CountedRef::cast(head);
    465473    return ref.dereference(head) || countedref_Op3_(op, res, head, arg1, arg2);
     
    477485
    478486class CountedRefShared:
    479   public CountedRef {
     487  public CountedRef
     488  {
    480489  typedef CountedRefShared self;
    481490  typedef CountedRef base;
     
    494503  explicit CountedRefShared(leftv arg):  base(new data_type(arg, data_type::copy_tag())) { }
    495504
    496   /// Construct new reference to internal data 
     505  /// Construct new reference to internal data
    497506  CountedRefShared(const self& rhs): base(rhs) { }
    498507
    499   /// Desctruct 
     508  /// Desctruct
    500509  ~CountedRefShared() { }
    501510
    502   /// Change reference to shared data 
     511  /// Change reference to shared data
    503512  self& operator=(const self& rhs) {
    504513    return static_cast<self&>(base::operator=(rhs));
     
    518527
    519528  /// Recover more information (e.g. subexpression data) from computed result
    520   BOOLEAN retrieve(leftv res, int typ) {
     529  BOOLEAN retrieve(leftv res, int typ)
     530  {
    521531    return (m_data->retrieve(res) && outcast(res, typ));
    522532  }
    523533};
    524534
    525 /// Blackbox support - generate initialized, but all-zero - shared data 
     535/// Blackbox support - generate initialized, but all-zero - shared data
    526536void* countedref_InitShared(blackbox*)
    527537{
     
    537547  if (countedref_CheckInit(res, head)) return TRUE;
    538548
    539   if ((op == DEF_CMD) || (op == head->Typ())) {
     549  if ((op == DEF_CMD) || (op == head->Typ()))
     550  {
    540551    res->rtyp = head->Typ();
    541552    return iiAssign(res, head);
     
    544555  CountedRefShared ref = CountedRefShared::cast(head);
    545556
    546   if ((op == LINK_CMD) ) {
     557  if ((op == LINK_CMD) )
     558  {
    547559    if (ref.dereference(head)) return TRUE;
    548560    res->Copy(head);
     
    562574  if (countedref_CheckInit(res, head))  return TRUE;
    563575
    564   if (CountedRefShared::is_ref(head)) {
     576  if (CountedRefShared::is_ref(head))
     577  {
    565578    CountedRefShared wrap = CountedRefShared::cast(head).wrapid();
    566579    int typ = head->Typ();
     
    577590  if (args->Data() == NULL) return FALSE;
    578591
    579   if(op == SYSTEM_CMD) {
    580     if (args->next) {
     592  if(op == SYSTEM_CMD)
     593  {
     594    if (args->next)
     595    {
    581596      leftv next = args->next;
    582597      args->next = NULL;
    583598
    584       char* name = (next->Typ() == STRING_CMD? 
     599      char* name = (next->Typ() == STRING_CMD?
    585600                    (char*) next->Data(): (char*)next->Name());
    586601      next = next->next;
    587602
    588       if (strcmp(name, "help") == 0) {
     603      if (strcmp(name, "help") == 0)
     604      {
    589605        PrintS("system(<ref>, ...): extended functionality for reference/shared data <ref>\n");
    590606        PrintS("  system(<ref>, count)         - number of references pointing to <ref>\n");
     
    596612        return CountedRef::construct(res);
    597613      }
    598       if (strncmp(name, "undef", 5) == 0) {
    599         return CountedRef::construct(res, args->Data()?
     614      if (strncmp(name, "undef", 5) == 0)
     615      {
     616        return CountedRef::construct(res, args->Data()?
    600617                          (CountedRef::cast(args).unassigned()? 1: 2): 0);
    601618      }
    602619
    603620      CountedRef obj = CountedRef::cast(args);
    604       if (next) {
     621      if (next)
     622      {
    605623        if (strcmp(name, "same") == 0) return obj.same(res, next);
    606624        // likewise may be hard to interprete, so we not not document it above
    607         if (strncmp(name, "like", 4) == 0) return obj.likewise(res, next); 
     625        if (strncmp(name, "like", 4) == 0) return obj.likewise(res, next);
    608626      }
    609627      if (strncmp(name, "count", 5) == 0) return obj.count(res);
     
    614632    return TRUE;
    615633  }
    616   if (op == LIST_CMD){
     634  if (op == LIST_CMD)
     635  {
    617636    res->rtyp = op;
    618637    return jjLIST_PL(res, args);
     
    628647  if ((result->Data() != NULL)  && !CountedRefShared::cast(result).unassigned()) {
    629648    CountedRef ref = CountedRef::cast(result);
    630     return CountedRef::resolve(arg) || ref.assign(result, arg); 
    631   }
    632  
     649    return CountedRef::resolve(arg) || ref.assign(result, arg);
     650  }
     651
    633652  /// Case: new reference to already shared data
    634   if (result->Typ() == arg->Typ()) {
    635     if (result->Data() != NULL)
     653  if (result->Typ() == arg->Typ())
     654  {
     655    if (result->Data() != NULL)
    636656      CountedRefShared::cast(result).destruct();
    637657    return CountedRefShared::cast(arg).outcast(result);
    638   } 
    639   if(CountedRefShared::cast(result).unassigned()) {
    640    return CountedRefShared::cast(result).assign(result, arg);
    641 
    642     return FALSE;
    643   }
    644    
     658  }
     659  if(CountedRefShared::cast(result).unassigned())
     660  {
     661    return CountedRefShared::cast(result).assign(result, arg);
     662  }
     663
    645664  /// Case: new shared data
    646665  return CountedRefShared(arg).outcast(result);
Note: See TracChangeset for help on using the changeset viewer.