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

Generated on Mon Nov 23 04:45:25 2009 for NOvA Offline by  doxygen 1.3.9.1