Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ConfigEditor.cxx

Go to the documentation of this file.
00001 
00002 // $Id: ConfigEditor.cxx,v 1.1 2007/02/09 05:38:27 fmwk Exp $
00003 //
00005 //
00006 // messier@indiana.edu
00008 #include "Config/ConfigEditor.h"
00009 #include "Config/Config.h"
00010 #include "Config/Table.h"
00011 #include "Config/Observer.h"
00012 #include "Config/Exception.h"
00013 #include <unistd.h>
00014 
00015 //......................................................................
00016 
00017 cfg::ConfigEditor::ConfigEditor(cfg::Config* config) :
00018   fNewConfig ( false  ),
00019   fConfig    ( config ), 
00020   fConfigCopy( 0 )  
00021 { }
00022 
00023 //......................................................................
00024 
00025 cfg::ConfigEditor::~ConfigEditor() 
00026 {
00027   //======================================================================
00028   // Free memory used during edit
00029   //======================================================================
00030   if (fConfigCopy) { delete fConfigCopy; fConfigCopy = 0; }
00031 }
00032 
00033 //......................................................................
00034 
00035 void cfg::ConfigEditor::Edit() 
00036 {
00037   //======================================================================
00038   // Actually do the edit
00039   //======================================================================
00040   
00041   // Test if the edit of the configuration is OK
00042   if (fConfig->EditOK()==false) return;
00043   
00044   // Make a copy of the configuration prior to any changes
00045   fConfigCopy = new cfg::Config(fConfig->GetName(),"dummy","dummy");
00046   cfg::Config::Copy(*fConfigCopy,
00047                     *fConfig,
00048                     fConfig->GetVersion(),
00049                     fConfig->GetSource());
00050   
00051   // Make the edit
00052   int apply = this->EditConfig(*fConfig);
00053   
00054   // Apply any changes
00055   if (apply==0) this->Cancel();
00056   else          this->Apply();
00057 }
00058 
00059 //......................................................................
00060 
00061 int cfg::ConfigEditor::EditConfig(cfg::Config& /*cfg*/) 
00062 {
00063   //======================================================================
00064   // User edit routine. Modify the configuration cfg.
00065   //======================================================================
00066   return 0; // Do not apply edits
00067   // return 1; // Apply edits
00068 }
00069 
00070 //......................................................................
00071 
00072 void cfg::ConfigEditor::Apply() 
00073 {
00074   //======================================================================
00075   // Apply the changes made to the configuration.
00076   //======================================================================
00077   cfg::Table& t = cfg::Table::Instance();
00078   
00079   // Insert the configuration into the table of configurations 
00080   if (fNewConfig) t.AdoptConfig(fConfig);
00081   
00082   // Alert observers of this configuration to the change it its
00083   // state
00084   cfg::Table::ObsList olist;
00085   t.GetObservers(fConfig->GetName(), fConfig->GetVersion(), olist);
00086   
00087   cfg::Table::ObsList::iterator    itr(olist.begin());
00088   cfg::Table::ObsList::iterator itrEnd(olist.end());
00089   for (; itr!=itrEnd; ++itr) try {(*itr)->Update(*fConfig); }
00090   catch (cfg::Exception e) {
00091     std::cout << " *-> " << fConfig->GetName() << "." 
00092               << fConfig->GetVersion() << " ";
00093     e.Print();
00094     exit(-1);
00095   }
00096 }
00097 
00098 //......................................................................
00099 
00100 void cfg::ConfigEditor::Reset() 
00101 {
00102   //======================================================================
00103   // Set the state of the configuration back to what it was prior to edit
00104   //======================================================================
00105   cfg::Config::Copy(*fConfig,
00106                     *fConfigCopy,
00107                     fConfigCopy->GetVersion(),
00108                     fConfigCopy->GetSource());
00109 }
00110 
00111 //......................................................................
00112 
00113 void cfg::ConfigEditor::Cancel() 
00114 {
00115   //======================================================================
00116   // Cancel the edit operation
00117   //======================================================================
00118   cfg::Config::Copy(*fConfig,
00119                     *fConfigCopy,
00120                     fConfigCopy->GetVersion(),
00121                     fConfigCopy->GetSource());
00122   if (fNewConfig) { delete fConfig; fConfig = 0; }
00123 }
00124 

Generated on Sat Oct 11 02:35:27 2008 for NOvA Offline by  doxygen 1.3.9.1