Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Context.cpp

Go to the documentation of this file.
00001 // /******************************************************************************\
00002 // *
00003 // * File:          Context.cpp
00004 // * Creation date: June 22, 2001 11:59
00005 // * Author:        ClassBuilder
00006 // *                XXXX
00007 // * Purpose:       Method implementations of class 'SqContext'
00008 // *
00009 // * Modifications: @INSERT_MODIFICATIONS(* )
00010 // *
00011 // *
00012 // \******************************************************************************/
00013 //@START_USER1
00018 /* --------------------------------------------------------------
00019 Copyright (C) 2001 LifeLine Networks BV <soap2corba@lifeline.nl>
00020 
00021 This program is free software; you can redistribute it and/or
00022 modify it under the terms of the GNU General Public License
00023 as published by the Free Software Foundation; either
00024 version 2 of the License, or (at your option) any later
00025 version.
00026 
00027 This program is distributed in the hope that it will be useful,
00028 but WITHOUT ANY WARRANTY; without even the implied warranty of
00029 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00030 GNU General Public License for more details.
00031 
00032 You should have received a copy of the GNU General Public License
00033 along with this program; if not, write to the Free Software
00034 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00035 -------------------------------------------------------------- */
00036 //@END_USER1
00037 
00038 
00039 // Master include file
00040 #include "ObjCtx.h"
00041 
00042 
00043 //@START_USER2
00044 //@END_USER2
00045 
00046 
00047 // Static members
00048 
00049 
00053 SqContext::SqContext(SqContextManager* pContextManager, unsigned long key,
00054                      unsigned short timeout) //@INIT_753
00055     : m_key(key)
00056     , m_timeout(timeout)
00057     , m_UserData()
00058     , m_lastAct(time (NULL))
00059     , m_ObjSeq(0)
00060     , m_contextKey()
00061 {//@CODE_753
00062     ConstructorInclude(pContextManager);
00063 
00064     // Put in your own code
00065 }//@CODE_753
00066 
00067 
00071 SqContext::~SqContext()
00072 {//@CODE_625
00073     DestructorInclude();
00074 
00075     // Put in your own code
00076 }//@CODE_625
00077 
00078 
00082 unsigned long SqContext::getNextObjSeq()
00083 {//@CODE_777
00084     return ++m_ObjSeq;
00085 }//@CODE_777
00086 
00087 
00094 unsigned short SqContext::timeoutRemaining(const time_t* pNow)
00095 {//@CODE_766
00096     // avoid calling expensive function time() too often
00097     time_t now = (pNow != NULL) ? (*pNow) : time (NULL);
00098     unsigned short passed = (unsigned short) ((unsigned long) now - (unsigned long) m_lastAct);
00099     if (passed >= m_timeout) {
00100         return 0;
00101     }
00102     return m_timeout - passed;
00103 }//@CODE_766
00104 
00105 
00109 void SqContext::SetKey(unsigned long key)
00110 {//@CODE_715
00111     if (m_key != key)
00112     {
00113         SqContextManager* refContextManager = _refContextManager;
00114         refContextManager->RemoveContext(this);
00115 
00116         m_key = key;
00117 
00118         refContextManager->AddContext(this);
00119     }
00120 }//@CODE_715
00121 
00122 
00123 //{{AFX DO NOT EDIT CODE BELOW THIS LINE !!!
00124 
00128 void SqContext::ConstructorInclude(SqContextManager* pContextManager)
00129 {
00130     // INIT_UNIQUEVALUETREE_OWNED_ACTIVE(SqContext, Context, SqObjectReference, ObjectReference)
00131    
00132     _firstObjectReference = (SqObjectReference*)0;
00133     _countObjectReference = 0;
00134     // INIT_UNIQUEVALUETREE_OWNED_PASSIVE(SqContextManager, ContextManager, SqContext, Context)
00135     _refContextManager = (SqContextManager*)0;
00136     _parentContextManager = (SqContext*)0;
00137     _leftContextManager = (SqContext*)0;
00138     _rightContextManager = (SqContext*)0;
00139     assert(pContextManager);
00140     pContextManager->AddContext(this);
00141 }
00142 
00143 
00147 void SqContext::DestructorInclude()
00148 {
00149     // EXIT_UNIQUEVALUETREE_OWNED_ACTIVE(SqContext, Context, SqObjectReference, ObjectReference)
00150     { for (SqObjectReference* item = GetFirstObjectReference(); item; item = GetFirstObjectReference())
00151           delete item; }
00152     // EXIT_UNIQUEVALUETREE_OWNED_PASSIVE(SqContextManager, ContextManager, SqContext, Context)
00153     
00154     if (_refContextManager)
00155         _refContextManager->RemoveContext(this);
00156 }
00157 
00158 
00159 SqObjectReference* SqContext::FindObjectReference(unsigned long ObjSeq)
00160 {
00161     // BODY_UNIQUEVALUETREE_FIND(GetObjSeq(), ObjSeq, SqContext, Context, SqObjectReference, ObjectReference)
00162     
00163     SqObjectReference* result = 0;
00164     if (_firstObjectReference)
00165     {
00166         SqObjectReference* item = _firstObjectReference;
00167         unsigned long bit = 0x1;
00168         while (1)
00169         {
00170             if (item->GetObjSeq() == ObjSeq)
00171             {
00172                 result = item;
00173                 break;
00174             }
00175 
00176             if ((item->GetObjSeq() & bit) == (ObjSeq & bit))
00177             {
00178                 if (item->_leftContext)
00179                 {
00180                     item = item->_leftContext;
00181                 }
00182                 else
00183                 {
00184                     break;
00185                 }
00186             }
00187             else
00188             {
00189                 if (item->_rightContext)
00190                 {
00191                     item = item->_rightContext;
00192                 }
00193                 else
00194                 {
00195                     break;
00196                 }
00197             }
00198 
00199             bit <<= 1;
00200         }
00201     } 
00202     return result;
00203 }
00204 
00205 
00206 // Methods for the relation(s) of the class
00207 // METHODS_UNIQUEVALUETREE_OWNED_ACTIVE(GetObjSeq(), SqContext, Context, SqObjectReference, ObjectReference)
00208 void SqContext::AddObjectReference(SqObjectReference* item)
00209 {
00210     
00211     assert(this);
00212 
00213     assert(item);
00214     assert(item->_refContext == (SqContext*)0);
00215 
00216     _countObjectReference++;
00217 
00218     item->_refContext = this;
00219 
00220     if (_firstObjectReference)
00221     {
00222         SqObjectReference* current = _firstObjectReference;
00223         unsigned long bit = 0x1;
00224         while (1)
00225         {
00226             assert(current->GetObjSeq() != item->GetObjSeq());
00227 
00228             if ((current->GetObjSeq() & bit) == (item->GetObjSeq() & bit))
00229             {
00230                 if (current->_leftContext)
00231                 {
00232                     current = current->_leftContext;
00233                 }
00234                 else
00235                 {
00236                     current->_leftContext = item;
00237                     item->_parentContext = current;
00238                     break;
00239                 }
00240             }
00241             else
00242             {
00243                 if (current->_rightContext)
00244                 {
00245                     current = current->_rightContext;
00246                 }
00247                 else
00248                 {
00249                     current->_rightContext = item;
00250                     item->_parentContext = current;
00251                     break;
00252                 }
00253             }
00254 
00255             bit <<= 1;
00256         }
00257     }
00258     else
00259     {
00260         _firstObjectReference = item;
00261     } 
00262 }
00263 
00264 void SqContext::RemoveObjectReference(SqObjectReference* item)
00265 {
00266     
00267     assert(this);
00268 
00269     assert(item);
00270     assert(item->_refContext == this);
00271 
00272     SqContext::ObjectReferenceIterator::Check(item);
00273 
00274     _countObjectReference--;
00275 
00276     SqObjectReference* replacement = 0;
00277     SqObjectReference* move = 0;
00278     if (item->_leftContext)
00279     {
00280         replacement = item->_leftContext;
00281         replacement->_parentContext = item->_parentContext;
00282         move = item->_rightContext;
00283     }
00284     else if (item->_rightContext)
00285     {
00286         replacement = item->_rightContext;
00287         replacement->_parentContext = item->_parentContext;
00288     }
00289 
00290     SqObjectReference* parent = item->_parentContext;
00291     if (parent)
00292     {
00293         if (parent->_leftContext == item)
00294         {
00295             parent->_leftContext = replacement;
00296         }
00297         else
00298         {
00299             parent->_rightContext = replacement;
00300         }
00301     }
00302     else
00303     {
00304         _firstObjectReference = replacement;
00305     }
00306 
00307     if (replacement)
00308     {
00309         while (1)
00310         {
00311             SqObjectReference* tmp = replacement->_rightContext;
00312             replacement->_rightContext = move;
00313             if (move)
00314             {
00315                 move->_parentContext = replacement;
00316             }
00317             
00318             if (!replacement->_leftContext)
00319             {
00320                 if (tmp)
00321                 {
00322                     replacement->_leftContext = tmp;
00323                     tmp = 0;
00324                 }
00325                 else
00326                 {
00327                     break;
00328                 }
00329             }
00330             move = tmp;
00331             replacement = replacement->_leftContext;
00332         }
00333     }
00334 
00335     item->_refContext = (SqContext*)0;
00336     item->_parentContext = (SqObjectReference*)0;
00337     item->_leftContext = (SqObjectReference*)0;
00338     item->_rightContext = (SqObjectReference*)0; 
00339 }
00340 
00341 void SqContext::DeleteAllObjectReference()
00342 {
00343     
00344     assert(this);
00345 
00346     for (SqObjectReference* item = GetFirstObjectReference(); item; item = GetFirstObjectReference())
00347           delete item; 
00348 }
00349 
00350 void SqContext::ReplaceObjectReference(SqObjectReference* item, SqObjectReference* newItem)
00351 {
00352     
00353     assert(this);
00354 
00355     assert(item);
00356     assert(item->_refContext == this);
00357 
00358     assert(newItem);
00359     assert(newItem->_refContext == (SqContext*)0);
00360 
00361     if (item->GetObjSeq() == newItem->GetObjSeq())
00362     {
00363         SqContext::ObjectReferenceIterator::Check(item, newItem);
00364         if (_firstObjectReference == item)
00365         {
00366             _firstObjectReference = newItem;
00367         }
00368         if (item->_parentContext)
00369         {
00370             if (item->_parentContext->_leftContext == item)
00371             {
00372                 item->_parentContext->_leftContext = newItem;
00373             }
00374             else if (item->_parentContext->_rightContext == item)
00375             {
00376                 item->_parentContext->_rightContext = newItem;
00377             }
00378         }
00379         newItem->_refContext = this;
00380         newItem->_parentContext = item->_parentContext;
00381         newItem->_leftContext = item->_leftContext;
00382         newItem->_rightContext = item->_rightContext;
00383         item->_refContext = (SqContext*)0;
00384         item->_parentContext = (SqObjectReference*)0;
00385         item->_leftContext = (SqObjectReference*)0;
00386         item->_rightContext = (SqObjectReference*)0;
00387     }
00388     else
00389     {
00390         SqContext::ObjectReferenceIterator::Check(item);
00391         RemoveObjectReference(item);
00392         AddObjectReference(newItem);
00393     } 
00394 }
00395 
00396 SqObjectReference* SqContext::GetFirstObjectReference() const
00397 {
00398     
00399     assert(this);
00400     return _firstObjectReference; 
00401 }
00402 
00403 SqObjectReference* SqContext::GetLastObjectReference() const
00404 {
00405     
00406     assert(this);
00407 
00408     SqObjectReference* result = _firstObjectReference;
00409     while (result)
00410     {
00411         while (result->_rightContext)
00412         {
00413             result = result->_rightContext;
00414         }
00415 
00416         if (result->_leftContext)
00417         {
00418             result = result->_leftContext;
00419         }
00420         else
00421         {
00422             break;
00423         }
00424     }
00425 
00426     return result; 
00427 }
00428 
00429 SqObjectReference* SqContext::GetNextObjectReference(SqObjectReference* pos) const
00430 {
00431     
00432     assert(this);
00433 
00434     SqObjectReference* result = 0;
00435     if (pos == (SqObjectReference*)0)
00436         result = _firstObjectReference;
00437     else
00438     {
00439         assert(pos->_refContext == this);
00440 
00441         if (pos->_leftContext)
00442         {
00443             result = pos->_leftContext;
00444         }
00445         else
00446         {
00447             if (pos->_rightContext)
00448             {
00449                 result = pos->_rightContext;
00450             }
00451             else
00452             {
00453                 SqObjectReference* parent = pos->_parentContext;
00454                 while (parent && (parent->_rightContext == 0 || parent->_rightContext == pos))
00455                 {
00456                     pos = parent;
00457                     parent = parent->_parentContext;
00458                 }
00459 
00460                 if (parent)
00461                 {
00462                     result = parent->_rightContext;
00463                 }
00464             }
00465         }
00466     }
00467 
00468     return result; 
00469 }
00470 
00471 SqObjectReference* SqContext::GetPrevObjectReference(SqObjectReference* pos) const
00472 {
00473     
00474     assert(this);
00475 
00476     SqObjectReference* result = 0;
00477     if (pos == (SqObjectReference*)0)
00478         result = GetLastObjectReference();
00479     else
00480     {
00481         assert(pos->_refContext == this);
00482 
00483         if (pos->_parentContext)
00484         {
00485             if (pos->_parentContext->_leftContext == pos || pos->_parentContext->_leftContext == 0)
00486             {
00487                 result = pos->_parentContext;
00488             }
00489             else /* Right branche and valid left branche */
00490             {
00491                 result = pos->_parentContext->_leftContext;
00492                 while (1)
00493                 {
00494                     while (result->_rightContext)
00495                     {
00496                         result = result->_rightContext;
00497                     }
00498 
00499                     if (result->_leftContext)
00500                     {
00501                         result = result->_leftContext;
00502                     }
00503                     else
00504                     {
00505                         break;
00506                     }
00507                 }
00508             }
00509         }
00510     }
00511 
00512     return result; 
00513 }
00514 
00515 int SqContext::GetObjectReferenceCount() const
00516 {
00517     
00518     assert(this);
00519     return _countObjectReference; 
00520 }
00521 // METHODS_ITERATOR_MULTI_ACTIVE(SqContext, Context, SqObjectReference, ObjectReference)
00522 SqContext::ObjectReferenceIterator* SqContext::ObjectReferenceIterator::_first = 0;
00523 SqContext::ObjectReferenceIterator* SqContext::ObjectReferenceIterator::_last = 0;
00524 
00525 SqContext::ObjectReferenceIterator::ObjectReferenceIterator(
00526     const SqContext* iterContext,
00527     int (SqObjectReference::*method)() const,
00528     SqObjectReference* refObjectReference)
00529 {
00530     assert(iterContext);
00531 
00532     _iterContext = iterContext;
00533     _refObjectReference = _prevObjectReference = _nextObjectReference = refObjectReference;
00534     _prev = (ObjectReferenceIterator*)0;
00535     _next = (ObjectReferenceIterator*)0;
00536     _method = method;
00537     if (_last)
00538     {
00539         _last->_next = this;
00540         _prev = _last;
00541         _last = this;
00542     }
00543     else
00544         _first = _last = this;
00545 }
00546 
00547 SqContext::ObjectReferenceIterator::ObjectReferenceIterator(
00548     const SqContext& iterContext,
00549     int (SqObjectReference::*method)() const,
00550     SqObjectReference* refObjectReference)
00551 {
00552     assert(&iterContext);
00553 
00554     _iterContext = &iterContext;
00555     _refObjectReference = _prevObjectReference = _nextObjectReference = refObjectReference;
00556     _prev = (ObjectReferenceIterator*)0;
00557     _next = (ObjectReferenceIterator*)0;
00558     _method = method;
00559     if (_last)
00560     {
00561         _last->_next = this;
00562         _prev = _last;
00563         _last = this;
00564     }
00565     else
00566         _first = _last = this;
00567 }
00568 
00569 SqContext::ObjectReferenceIterator::ObjectReferenceIterator(
00570     const ObjectReferenceIterator& iterator,
00571     int (SqObjectReference::*method)() const)
00572 {
00573     _iterContext = iterator._iterContext;
00574     _refObjectReference = iterator._refObjectReference;
00575     _prevObjectReference = iterator._prevObjectReference;
00576     _nextObjectReference = iterator._nextObjectReference;
00577     _prev = (ObjectReferenceIterator*)0;
00578     _next = (ObjectReferenceIterator*)0;
00579     _method = method;
00580     if (_last)
00581     {
00582         _last->_next = this;
00583         _prev = _last;
00584         _last = this;
00585     }
00586     else
00587         _first = _last = this;
00588 }
00589 
00590 SqContext::ObjectReferenceIterator::~ObjectReferenceIterator()
00591 {
00592     if (_next)
00593         _next->_prev = _prev;
00594     else
00595         _last = _prev;
00596 
00597     if (_prev)
00598         _prev->_next = _next;
00599     else
00600         _first = _next;
00601 }
00602 
00603 void SqContext::ObjectReferenceIterator::Check(SqObjectReference* itemObjectReference)
00604 {
00605     for (ObjectReferenceIterator* item = _first; item; item = item->_next)
00606     {
00607         if (item->_prevObjectReference == itemObjectReference)
00608         {
00609             item->_prevObjectReference = item->_iterContext->GetNextObjectReference(item->_prevObjectReference);
00610             item->_refObjectReference = 0;
00611         }
00612         if (item->_nextObjectReference == itemObjectReference)
00613         {
00614             item->_nextObjectReference = item->_iterContext->GetPrevObjectReference(item->_nextObjectReference);
00615             item->_refObjectReference = 0;
00616         }
00617     }
00618 }
00619 
00620 void SqContext::ObjectReferenceIterator::Check(SqObjectReference* itemObjectReference, SqObjectReference* newItemObjectReference)
00621 {
00622     for (ObjectReferenceIterator* item = _first; item; item = item->_next)
00623     {
00624         if (item->_refObjectReference == itemObjectReference)
00625         {
00626             item->_refObjectReference = item->_prevObjectReference = item->_nextObjectReference = newItemObjectReference;
00627         }
00628         if (item->_prevObjectReference == itemObjectReference)
00629         {
00630             item->_prevObjectReference = newItemObjectReference;
00631             item->_refObjectReference = 0;
00632         }
00633         if (item->_nextObjectReference == itemObjectReference)
00634         {
00635             item->_nextObjectReference = newItemObjectReference;
00636             item->_refObjectReference = 0;
00637         }
00638     }
00639 }
00640 // METHODS_UNIQUEVALUETREE_OWNED_PASSIVE(SqContextManager, ContextManager, SqContext, Context)
00641 
00642 //}}AFX DO NOT EDIT CODE ABOVE THIS LINE !!!
00643 
00644 //@START_USER3
00645 

This documentation is part of the "SOAP to CORBA bridge" project
Copyright © 2000 by Lifeline Networks bv.
All rights are reserved.