[dasher: 11/27] Transfer ownership of Alphabet and LanguageModel to AlphabetManager



commit a344a694bcfaf23fbe7a54a9fb8794c0667f9684
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Fri Aug 6 09:10:43 2010 +0100

    Transfer ownership of Alphabet and LanguageModel to AlphabetManager
    
    GC DasherInterfaceBase::m_Alphabet, GetAlphabet(), obtain via NCMgr when needed
    NodeCreationManager creates alph & lm, but passes (ownership) to AlphMgr
      => Added AlphabetManager::GetAlphabet()
    Setting LP_REAL_ORIENTATION handled by NCManager, as LP_ORIENTATION changes

 Src/DasherCore/AlphabetManager.cpp     |    5 +++
 Src/DasherCore/AlphabetManager.h       |    1 +
 Src/DasherCore/DasherInterfaceBase.cpp |   21 ++----------
 Src/DasherCore/DasherInterfaceBase.h   |    7 ----
 Src/DasherCore/NodeCreationManager.cpp |   58 ++++++++++++++++----------------
 Src/DasherCore/NodeCreationManager.h   |    8 ++---
 6 files changed, 41 insertions(+), 59 deletions(-)
---
diff --git a/Src/DasherCore/AlphabetManager.cpp b/Src/DasherCore/AlphabetManager.cpp
index 4317dd2..9db092b 100644
--- a/Src/DasherCore/AlphabetManager.cpp
+++ b/Src/DasherCore/AlphabetManager.cpp
@@ -50,11 +50,16 @@ CAlphabetManager::CAlphabetManager(CDasherInterfaceBase *pInterface, CNodeCreati
   m_pInterface = pInterface;
 
   m_iLearnContext = m_pLanguageModel->CreateEmptyContext();
+}
 
+const CAlphabet *CAlphabetManager::GetAlphabet() const {
+  return m_pAlphabet;
 }
 
 CAlphabetManager::~CAlphabetManager() {
   m_pLanguageModel->ReleaseContext(m_iLearnContext);
+  delete m_pAlphabet;
+  delete m_pLanguageModel;
 }
 
 CAlphabetManager::CAlphNode::CAlphNode(CDasherNode *pParent, int iOffset, unsigned int iLbnd, unsigned int iHbnd, int iColour, const string &strDisplayText, CAlphabetManager *pMgr)
diff --git a/Src/DasherCore/AlphabetManager.h b/Src/DasherCore/AlphabetManager.h
index 598646e..6680b1c 100644
--- a/Src/DasherCore/AlphabetManager.h
+++ b/Src/DasherCore/AlphabetManager.h
@@ -125,6 +125,7 @@ namespace Dasher {
     /// will enter. (Also used to build context for preceding characters.)
     virtual CAlphNode *GetRoot(CDasherNode *pParent, unsigned int iLower, unsigned int iUpper, bool bEnteredLast, int iOffset);
 
+    const CAlphabet *GetAlphabet() const;
   protected:
     ///
     /// Factory method for CAlphNode construction, so subclasses can override.
diff --git a/Src/DasherCore/DasherInterfaceBase.cpp b/Src/DasherCore/DasherInterfaceBase.cpp
index faf81b0..f446a05 100644
--- a/Src/DasherCore/DasherInterfaceBase.cpp
+++ b/Src/DasherCore/DasherInterfaceBase.cpp
@@ -88,7 +88,6 @@ static char THIS_FILE[] = __FILE__;
 CDasherInterfaceBase::CDasherInterfaceBase() {
 
   // Ensure that pointers to 'owned' objects are set to NULL.
-  m_Alphabet = NULL;
   m_pDasherModel = NULL;
   m_DasherScreen = NULL;
   m_pDasherView = NULL;
@@ -180,13 +179,6 @@ void CDasherInterfaceBase::Realize() {
   CParameterNotificationEvent oEvent(LP_NODE_BUDGET);
   InterfaceEventHandler(&oEvent);
 
-  // Set up real orientation to match selection
-  if(GetLongParameter(LP_ORIENTATION) == Dasher::Opts::AlphabetDefault)
-    SetLongParameter(LP_REAL_ORIENTATION, m_Alphabet->GetOrientation());
-  else
-    SetLongParameter(LP_REAL_ORIENTATION, GetLongParameter(LP_ORIENTATION));
-
-
   // FIXME - need to rationalise this sort of thing.
   // InvalidateContext(true);
   ScheduleRedraw();
@@ -216,7 +208,6 @@ CDasherInterfaceBase::~CDasherInterfaceBase() {
   GameMode::CDasherGameMode::DestroyTeacher();
 
   delete m_pDasherModel;        // The order of some of these deletions matters
-  delete m_Alphabet;
   delete m_pDasherView;
   delete m_ColourIO;
   delete m_AlphIO;
@@ -300,12 +291,7 @@ void CDasherInterfaceBase::InterfaceEventHandler(Dasher::CEvent *pEvent) {
     case BP_DRAW_MOUSE_LINE:
       ScheduleRedraw();
       break;
-    case LP_ORIENTATION:
-      if(GetLongParameter(LP_ORIENTATION) == Dasher::Opts::AlphabetDefault)
-	// TODO: See comment in DasherModel.cpp about prefered values
-	SetLongParameter(LP_REAL_ORIENTATION, m_Alphabet->GetOrientation());
-      else
-	SetLongParameter(LP_REAL_ORIENTATION, GetLongParameter(LP_ORIENTATION));
+    case LP_REAL_ORIENTATION:
       ScheduleRedraw();
       break;
     case SP_ALPHABET_ID:
@@ -362,7 +348,8 @@ void CDasherInterfaceBase::InterfaceEventHandler(Dasher::CEvent *pEvent) {
       if(GetBoolParameter(BP_LM_ADAPTIVE))
 	 strTrainfileBuffer += pEditEvent->m_sText;
       if (GetBoolParameter(BP_SPEAK_WORDS) && SupportsSpeech()) {
-        if (pEditEvent->m_sText == m_Alphabet->GetText(m_Alphabet->GetSpaceSymbol())) {
+        const CAlphabet *pAlphabet = m_pNCManager->GetAlphabet();
+        if (pEditEvent->m_sText == pAlphabet->GetText(pAlphabet->GetSpaceSymbol())) {
           Speak(m_strCurrentWord, false);
           m_strCurrentWord="";
         } else
@@ -401,8 +388,6 @@ void CDasherInterfaceBase::CreateNCManager() {
   //now create the new manager...
   m_pNCManager = new CNodeCreationManager(this, m_pEventHandler, m_pSettingsStore, m_AlphIO);
 
-  m_Alphabet = m_pNCManager->GetAlphabet();
-
   //and start a new tree of nodes from it (retaining old offset -
   // this will be a sensible default of 0 if no nodes previously existed).
   // This deletes the old tree of nodes... 
diff --git a/Src/DasherCore/DasherInterfaceBase.h b/Src/DasherCore/DasherInterfaceBase.h
index 868015c..0ed8f40 100644
--- a/Src/DasherCore/DasherInterfaceBase.h
+++ b/Src/DasherCore/DasherInterfaceBase.h
@@ -104,12 +104,6 @@ public:
 
   void DeleteAlphabet(const std::string & AlphID);
 
-  /// Get a pointer to the current alphabet object
-
-  CAlphabet *GetAlphabet() {
-    return m_Alphabet;
-  }
-
   /// Gets a pointer to the object doing user logging
 
   CUserLogBase* GetUserLogPtr();
@@ -586,7 +580,6 @@ protected:
   /// @name Child components
   /// Various objects which are 'owned' by the core. 
   /// @{
-  CAlphabet *m_Alphabet;
   CDasherModel *m_pDasherModel;
   CDasherScreen *m_DasherScreen;
   CDasherView *m_pDasherView;
diff --git a/Src/DasherCore/NodeCreationManager.cpp b/Src/DasherCore/NodeCreationManager.cpp
index e57f4d5..3e6a220 100644
--- a/Src/DasherCore/NodeCreationManager.cpp
+++ b/Src/DasherCore/NodeCreationManager.cpp
@@ -21,19 +21,17 @@ CNodeCreationManager::CNodeCreationManager(Dasher::CDasherInterfaceBase *pInterf
   m_pInterface(pInterface), m_pControlManager(NULL) {
 
   const Dasher::CAlphIO::AlphInfo &oAlphInfo(pAlphIO->GetInfo(pSettingsStore->GetStringParameter(SP_ALPHABET_ID)));
-  m_pAlphabet = new CAlphabet(oAlphInfo);
+  CAlphabet *pAlphabet = new CAlphabet(oAlphInfo);
   
-  pSettingsStore->SetStringParameter(SP_TRAIN_FILE, m_pAlphabet->GetTrainingFile());
-  pSettingsStore->SetStringParameter(SP_GAME_TEXT_FILE, m_pAlphabet->GetGameModeFile());
+  pSettingsStore->SetStringParameter(SP_TRAIN_FILE, pAlphabet->GetTrainingFile());
+  pSettingsStore->SetStringParameter(SP_GAME_TEXT_FILE, pAlphabet->GetGameModeFile());
   
-  pSettingsStore->SetStringParameter(SP_DEFAULT_COLOUR_ID, m_pAlphabet->GetPalette());
+  pSettingsStore->SetStringParameter(SP_DEFAULT_COLOUR_ID, pAlphabet->GetPalette());
   
-  if(pSettingsStore->GetLongParameter(LP_ORIENTATION) == Dasher::Opts::AlphabetDefault)
-    pSettingsStore->SetLongParameter(LP_REAL_ORIENTATION, m_pAlphabet->GetOrientation());
   // --
   
   // Create an appropriate language model;
-  
+  CLanguageModel *pLanguageModel;
   //WZ: Mandarin Dasher Change
   //If statement checks for the specific Super PinYin alphabet, and sets language model to PPMPY
   if((oAlphInfo.m_iConversionID==2)&&(pSettingsStore->GetStringParameter(SP_ALPHABET_ID)=="Chinese Super Pin Yin, grouped by Dictionary")){
@@ -43,8 +41,8 @@ CNodeCreationManager::CNodeCreationManager(Dasher::CDasherInterfaceBase *pInterf
     CAlphabet *pCHAlphabet = new CAlphabet(oCHAlphInfo);
     
     //std::cout<<"CHALphabet size "<< pCHAlphabet->GetNumberTextSymbols(); [7603]
-    m_pLanguageModel = new CPPMPYLanguageModel(pEventHandler, pSettingsStore, pCHAlphabet, m_pAlphabet);
-    m_pTrainer = new CMandarinTrainer(m_pLanguageModel, m_pAlphabet, pCHAlphabet);
+    pLanguageModel = new CPPMPYLanguageModel(pEventHandler, pSettingsStore, pCHAlphabet, pAlphabet);
+    m_pTrainer = new CMandarinTrainer(pLanguageModel, pAlphabet, pCHAlphabet);
     std::cout<<"Setting PPMPY model"<<std::endl;
   }
   else{
@@ -53,36 +51,33 @@ CNodeCreationManager::CNodeCreationManager(Dasher::CDasherInterfaceBase *pInterf
     // FIXME - return to using enum here
     switch (pSettingsStore->GetLongParameter(LP_LANGUAGE_MODEL_ID)) {
       case 0:
-        m_pLanguageModel = new CPPMLanguageModel(pEventHandler, pSettingsStore, m_pAlphabet);
+        pLanguageModel = new CPPMLanguageModel(pEventHandler, pSettingsStore, pAlphabet);
         break;
       case 2:
-        m_pLanguageModel = new CWordLanguageModel(pEventHandler, pSettingsStore, m_pAlphabet);
+        pLanguageModel = new CWordLanguageModel(pEventHandler, pSettingsStore, pAlphabet);
         break;
       case 3:
-        m_pLanguageModel = new CMixtureLanguageModel(pEventHandler, pSettingsStore, m_pAlphabet);
+        pLanguageModel = new CMixtureLanguageModel(pEventHandler, pSettingsStore, pAlphabet);
         break;  
       case 4:
-        m_pLanguageModel = new CCTWLanguageModel(pEventHandler, pSettingsStore, m_pAlphabet);
+        pLanguageModel = new CCTWLanguageModel(pEventHandler, pSettingsStore, pAlphabet);
         break;
         
       default:
         // If there is a bogus value for the language model ID, we'll default
         // to our trusty old PPM language model.
-        m_pLanguageModel = new CPPMLanguageModel(pEventHandler, pSettingsStore, m_pAlphabet);    
+        pLanguageModel = new CPPMLanguageModel(pEventHandler, pSettingsStore, pAlphabet);    
         break;
     }
-    m_pTrainer = new CTrainer(m_pLanguageModel, m_pAlphabet);
+    m_pTrainer = new CTrainer(pLanguageModel, pAlphabet);
   }
     
-  // TODO: Tell the alphabet manager about the alphabet here, so we
-  // don't end up having to duck out to the NCM all the time
-
     switch(oAlphInfo.m_iConversionID) {
       default:
         //TODO: Error reporting here
         //fall through to
       case 0: // No conversion required
-        m_pAlphabetManager = new CAlphabetManager(pInterface, this, m_pAlphabet, m_pLanguageModel);
+        m_pAlphabetManager = new CAlphabetManager(pInterface, this, pAlphabet, pLanguageModel);
         break;
 #ifdef JAPANESE
       case 1: // Japanese
@@ -90,25 +85,25 @@ CNodeCreationManager::CNodeCreationManager(Dasher::CDasherInterfaceBase *pInterf
 #ifdef WIN32
         new CIMEConversionHelper;
 #else
-        new CCannaConversionHelper(this, m_pAlphabet, GetLongParameter(LP_CONVERSION_TYPE), GetLongParameter(LP_CONVERSION_ORDER));
+        new CCannaConversionHelper(this, pAlphabet, GetLongParameter(LP_CONVERSION_TYPE), GetLongParameter(LP_CONVERSION_ORDER));
 #endif
         //TODO ownership/deletion
-        m_pAlphabetManager = new CConvertingAlphMgr(pInterface, this, pConversionManager, m_pAlphabet, m_pLanguageModel);
+        m_pAlphabetManager = new CConvertingAlphMgr(pInterface, this, pConversionManager, pAlphabet, pLanguageModel);
         break;
 #endif
       case 2:   //(ACL) Modify AlphabetManager for Mandarin Dasher
-        m_pAlphabetManager = new CMandarinAlphMgr(pInterface, this, m_pAlphabet, m_pLanguageModel);
+        m_pAlphabetManager = new CMandarinAlphMgr(pInterface, this, pAlphabet, pLanguageModel);
     }
 
-  if (!m_pAlphabet->GetTrainingFile().empty()) {
+  if (!pAlphabet->GetTrainingFile().empty()) {
     //1. Look for system training text...
     CLockEvent oEvent("Training on System Text", true, 0);
     pEventHandler->InsertEvent(&oEvent);
-    m_pTrainer->LoadFile(GetStringParameter(SP_SYSTEM_LOC) + m_pAlphabet->GetTrainingFile());
+    m_pTrainer->LoadFile(GetStringParameter(SP_SYSTEM_LOC) + pAlphabet->GetTrainingFile());
     //Now add in any user-provided individual training text...
     oEvent.m_strMessage = "Training on User Text"; oEvent.m_bLock=true; oEvent.m_iPercent = 0;
     pEventHandler->InsertEvent(&oEvent);
-    m_pTrainer->LoadFile(GetStringParameter(SP_USER_LOC) + m_pAlphabet->GetTrainingFile());
+    m_pTrainer->LoadFile(GetStringParameter(SP_USER_LOC) + pAlphabet->GetTrainingFile());
     oEvent.m_bLock = false;
     pEventHandler->InsertEvent(&oEvent);
   }
@@ -120,9 +115,9 @@ CNodeCreationManager::CNodeCreationManager(Dasher::CDasherInterfaceBase *pInterf
 #ifdef DEBUG_LM_READWRITE
   {
     //test...
-    m_pLanguageModel->WriteToFile("test.model");
-    CPPMLanguageModel *pLan = (CPPMLanguageModel *)m_pLanguageModel;
-    CPPMLanguageModel *pLM2 = new CPPMLanguageModel(pEventHandler, pSettingsStore, m_pAlphabet);
+    pLanguageModel->WriteToFile("test.model");
+    CPPMLanguageModel *pLan = (CPPMLanguageModel *)pLanguageModel;
+    CPPMLanguageModel *pLM2 = new CPPMLanguageModel(pEventHandler, pSettingsStore, pAlphabet);
     pLM2->ReadFromFile("test.model");
     if (!pLan->eq(pLM2)) {
       std::cout << "Not equal!" << std::endl;
@@ -132,8 +127,8 @@ CNodeCreationManager::CNodeCreationManager(Dasher::CDasherInterfaceBase *pInterf
   }
 #endif
 
+  HandleEvent(&CParameterNotificationEvent(LP_ORIENTATION));
   HandleEvent(&CParameterNotificationEvent(BP_CONTROL_MODE));
-
 }
 
 CNodeCreationManager::~CNodeCreationManager() {
@@ -152,6 +147,11 @@ void CNodeCreationManager::HandleEvent(CEvent *pEvent) {
           ? new CControlManager(m_pEventHandler, m_pSettingsStore, this, m_pInterface)
           : NULL;        
         break;
+      case LP_ORIENTATION: {
+        const long iOverride(GetLongParameter(LP_ORIENTATION));
+        SetLongParameter(LP_REAL_ORIENTATION,
+                         iOverride == Dasher::Opts::AlphabetDefault ? GetAlphabet()->GetOrientation() : iOverride);
+      }
     }
   }
 }
diff --git a/Src/DasherCore/NodeCreationManager.h b/Src/DasherCore/NodeCreationManager.h
index 0f160c3..6cca995 100644
--- a/Src/DasherCore/NodeCreationManager.h
+++ b/Src/DasherCore/NodeCreationManager.h
@@ -41,18 +41,16 @@ class CNodeCreationManager : public Dasher::CDasherComponent {
   Dasher::CControlManager *GetControlManager() {return m_pControlManager;}
   
   ///
-  /// Get a reference to the alphabet
+  /// Get a reference to the current alphabet
   ///
 
-  Dasher::CAlphabet *GetAlphabet() {
-    return m_pAlphabet;
+  const Dasher::CAlphabet *GetAlphabet() const {
+    return m_pAlphabetManager->GetAlphabet();
   }
 
   void ImportTrainingText(const std::string &strPath);
 
  private:
-  Dasher::CLanguageModel *m_pLanguageModel;     // pointer to the language model
-  Dasher::CAlphabet *m_pAlphabet;        // pointer to the alphabet
   Dasher::CTrainer *m_pTrainer;
   
   Dasher::CDasherInterfaceBase *m_pInterface;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]