home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / Nuntius / UArticleCache.cp < prev    next >
Encoding:
Text File  |  1994-02-20  |  3.9 KB  |  174 lines  |  [TEXT/MPS ]

  1. // Copyright © 1992 Peter Speck, speck@dat.ruc.dk. All rights reserved.
  2. // UArticleCache.cp
  3.  
  4. #include "UArticleCache.h"
  5. #include "UArticle.h"
  6. #include "UFatalError.h"
  7.  
  8. #pragma segment MyArticle
  9.  
  10. #define qDebugACBrief qDebug & 0
  11. #define qDebugAC qDebugACBrief & 0
  12.  
  13. PArticleCache *gArticleCache = nil;
  14.  
  15. void InitUArticleCache()
  16. {
  17. #if qDebug
  18.     if (gArticleCache)
  19.         ProgramBreak("gArticleCache is not nil in InitUArticleCache");
  20. #endif
  21.     PArticleCache *ac = new PArticleCache();
  22.     ac->IArticleCache();
  23.     gArticleCache = ac;
  24. }
  25.  
  26. void CloseDownUArticleCache()
  27. {
  28.     FreeIfPtrObject(gArticleCache); gArticleCache = nil;
  29. }
  30.  
  31. //-----------------------
  32.  
  33. PArticleCache::PArticleCache()
  34. {
  35. //    inherited::Initialize();
  36.     fArticleList = nil;
  37.     fRefCountList = nil;
  38. }
  39.  
  40. void PArticleCache::IArticleCache()
  41. {
  42. //    inherited::IObject();
  43.     FailInfo fi;
  44.     if (fi.Try())
  45.     {
  46.         fArticleList = NewList();
  47.         TLongintList *lList = new TLongintList();
  48.         lList->ILongintList();
  49.         fRefCountList = lList;
  50.         fi.Success();
  51.     }
  52.     else // fail
  53.     {
  54. //        Free();
  55.         FreeIfPtrObject(this);
  56.         fi.ReSignal();
  57.     }
  58. }
  59.  
  60. PArticleCache::~PArticleCache()
  61. {
  62.     if (fArticleList)
  63.     {
  64. #if qDebug
  65.         if (fArticleList->GetSize())
  66.             fprintf(stderr, "PArticleCache, had %ld TArticles in cache when closed down\n", fArticleList->GetSize());
  67. #endif
  68.         fArticleList->FreeAll();
  69.         fArticleList->Free();
  70.     }
  71.     FreeIfObject(fRefCountList);
  72. // inherited::Free();
  73. }
  74.  
  75. TArticle *PArticleCache::GetArticle(const CStr255 &dotName, long articleID)
  76. {
  77.     ArrayIndex index = fArticleList->GetSize();
  78.     while (index >= 1)
  79.     {
  80.         TArticle *article = (TArticle*) fArticleList->At(index);
  81.         if (article->IsArticle(dotName, articleID))
  82.         {
  83. #if qDebugACBrief
  84.             fprintf(stderr, "TAC::GetArticle: group = %s, article = %ld found with index = %ld\n", (char*)dotName, articleID, index);
  85. #endif
  86.             fRefCountList->AtPut(index, fRefCountList->At(index) + 1);
  87.             return article;
  88.         }
  89.         index--;
  90.     }
  91.  
  92.     // not found, create new
  93. #if qDebugAC
  94.     fprintf(stderr, "TAC::GetArticle: group = %s, article = %ld not found creating new\n", (char*)dotName, articleID);
  95. #endif
  96.     TArticle *article = new TArticle();
  97.     article->IArticle(dotName, articleID);
  98.     fArticleList->InsertLast(article);
  99.     fRefCountList->InsertLast(1);
  100.     return article;
  101. }
  102.  
  103. void PArticleCache::ReturnArticle(TArticle *article)
  104. {
  105.     if (!article)
  106.         return;
  107. #if qDebug
  108.     if (!VerboseIsObject(article))
  109.         ProgramBreak("article is not object");
  110. #endif
  111.     ArrayIndex index = fArticleList->GetSize();
  112.     while (index >= 1)
  113.     {
  114.         if (fArticleList->At(index) == article)
  115.         {
  116.             long refs = fRefCountList->At(index) - 1;
  117. #if qDebugACBrief
  118.             if (qDebugAC || refs != 0)
  119.             {
  120.                 CStr255 ddnn;
  121.                 long articleID;
  122.                 article->GetArticleID(ddnn, articleID);
  123.                 fprintf(stderr, "TAC: retuned article, group = %s, article = %ld, new refs = %ld\n", (char*)ddnn, articleID, refs);
  124.             }
  125. #endif
  126.             if (refs)
  127.                 fRefCountList->AtPut(index, refs);
  128.             else
  129.             {
  130.                 fRefCountList->DeleteElementsAt(index, 1);
  131.                 fArticleList->DeleteElementsAt(index, 1);
  132.                 article->Free();
  133.             }
  134.             return;
  135.         }
  136.         index--;
  137.     }
  138. #if qDebug
  139.     CStr255 ddnn;
  140.     long articleID;
  141.     article->GetArticleID(ddnn, articleID);
  142.     fprintf(stderr, "TAC: got (unknow) retuned article, group = %s, article = %ld\n", (char*)ddnn, articleID);
  143.     ProgramBreak("Did not find returned article");
  144. #endif
  145. }
  146.  
  147. void PArticleCache::DiscardObject(TObject *obj)
  148. {
  149. #if qDebug
  150.     ProgramBreak("PArticleCache::DiscardObject() called!");
  151. #endif
  152.     if (!obj)
  153.         return;
  154.     PanicExitToShell("PArticleCache::DiscardObject() called");
  155. }
  156.  
  157. void PArticleCache::DebugDump()
  158. {
  159. #if qDebug
  160.     fprintf(stderr, "Debug dump of PArticleCache:\n");
  161.     ArrayIndex index = 1;
  162.     while (index <= fArticleList->GetSize())
  163.     {
  164.         TArticle *article = (TArticle*) fArticleList->At(index);
  165.         long refs = fRefCountList->At(index);
  166.         CStr255 dotName;
  167.         long articleID;
  168.         article->GetArticleID(dotName, articleID);
  169.         fprintf(stderr, "  group =%20s, article = %6ld, refs = %ld\n", (char*)dotName, articleID, refs);
  170.     }
  171.     fprintf(stderr, "End of dump\n");
  172. #endif
  173. }
  174.