cgma
SettingHandler Class Reference

#include <SettingHandler.hpp>

List of all members.

Public Member Functions

 ~SettingHandler ()
void add_setting (const char *name, void(*setFn)(int), int(*getFn)())
void add_setting (const char *name, void(*setFn)(double), double(*getFn)())
void add_setting (const char *name, void(*setFn)(CubitBoolean), CubitBoolean(*getFn)())
void add_setting (const char *name, void(*setFn)(CubitString), CubitString(*getFn)())
void get_settings_list (std::vector< std::pair< CubitString, SettingHolder * > > &list)
void print_settings ()
void save_settings (const char *filename)
void save_settings ()
void restore_settings (const char *filename)
SettingHolderget_setting_holder (CubitString name)
SettingType get_setting_type (SettingHolder *setting)
int get_setting_int (SettingHolder *setting)
double get_setting_double (SettingHolder *setting)
CubitBoolean get_setting_bool (SettingHolder *setting)
CubitString get_setting_string (SettingHolder *setting)
int get_setting_debug_value (SettingHolder *setting)
void set_setting_int (SettingHolder *setting, int int_value)
void set_setting_double (SettingHolder *setting, double double_value)
void set_setting_bool (SettingHolder *setting, CubitBoolean bool_value)
void set_setting_string (SettingHolder *setting, CubitString string_value)
void set_setting_debug_value (SettingHolder *setting, int debug_value)

Static Public Member Functions

static SettingHandlerinstance ()
static void delete_instance ()

Private Member Functions

 SettingHandler ()

Private Attributes

void(* debug_set_function )(const int index, const int value)
int(* debug_get_function )(const int index)
CubitBoolean debug_flags_added
std::map< CubitString,
SettingHolder * > 
mSettingsList

Static Private Attributes

static SettingHandlerinstance_ = NULL

Friends

class SettingHolder

Detailed Description

Definition at line 18 of file SettingHandler.hpp.


Constructor & Destructor Documentation

Definition at line 48 of file SettingHandler.cpp.

{
    std::map<CubitString, SettingHolder*>::iterator iter = mSettingsList.begin();
  std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();

  for( ; iter != end; ++iter)
    delete (*iter).second;
  mSettingsList.clear();
}

Definition at line 34 of file SettingHandler.cpp.


Member Function Documentation

void SettingHandler::add_setting ( const char *  name,
void(*)(int)  setFn,
int(*)()  getFn 
)

Definition at line 58 of file SettingHandler.cpp.

{
  CubitString cs = name;
  //Check to see if a setting with the same name already exists
  if (mSettingsList.find(cs) != mSettingsList.end()) {
    std::cerr << "The " << name << " setting has the same name as another setting.\n" 
        << "This is a bug.  Please report!\n";
    exit(-1);
  }
  SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
  mSettingsList[cs] = setting;
}
void SettingHandler::add_setting ( const char *  name,
void(*)(double)  setFn,
double(*)()  getFn 
)

Definition at line 73 of file SettingHandler.cpp.

{
  CubitString cs = name;
  //Check to see if a setting with the same name already exists
  if (mSettingsList.find(cs) != mSettingsList.end()) {
    std::cerr << "The " << name << " setting has the same name as another setting.\n" 
        << "This is a bug.  Please report!\n";
    exit(-1);
    
  }
  SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
  mSettingsList[cs] = setting;
}
void SettingHandler::add_setting ( const char *  name,
void(*)(CubitBoolean setFn,
CubitBoolean(*)()  getFn 
)

Definition at line 89 of file SettingHandler.cpp.

{
   CubitString cs = name;
  //Check to see if a setting with the same name already exists
  if (mSettingsList.find(cs) != mSettingsList.end()) {
    std::cerr << "The " << name << " setting has the same name as another setting.\n" 
        << "This is a bug.  Please report!\n";
    exit(-1);
    
  }
  SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
  mSettingsList[cs] = setting;
}
void SettingHandler::add_setting ( const char *  name,
void(*)(CubitString setFn,
CubitString(*)()  getFn 
)

Definition at line 105 of file SettingHandler.cpp.

{
   CubitString cs = name;
  //Check to see if a setting with the same name already exists
  if (mSettingsList.find(cs) != mSettingsList.end()) {
    std::cerr << "The " << name << " setting has the same name as another setting.\n" 
        << "This is a bug.  Please report!\n";
    exit(-1);
    
  }
  SettingHolder* setting = new SettingHolder(cs, setFn, getFn);
  mSettingsList[cs] = setting;
}

Definition at line 39 of file SettingHandler.cpp.

{
  if (instance_)
  {
    delete instance_;
    instance_ = 0;
  }
}

Definition at line 84 of file SettingHandler.hpp.

{ return (setting->get_bool_function)(); }
double SettingHandler::get_setting_double ( SettingHolder setting) [inline]

Definition at line 81 of file SettingHandler.hpp.

{ return (setting->get_double_function)(); }

Definition at line 347 of file SettingHandler.cpp.

{ 
  std::map<CubitString, SettingHolder*>::iterator it = mSettingsList.find(name);
  return it  ==  mSettingsList.end()  ? NULL : it->second;
}
int SettingHandler::get_setting_int ( SettingHolder setting) [inline]

Definition at line 78 of file SettingHandler.hpp.

{ return (setting->get_int_function)(); }

Definition at line 87 of file SettingHandler.hpp.

{ return (setting->get_string_function)(); }

Definition at line 75 of file SettingHandler.hpp.

{ return setting->setting_type; }
void SettingHandler::get_settings_list ( std::vector< std::pair< CubitString, SettingHolder * > > &  list)

Definition at line 353 of file SettingHandler.cpp.

{
  // return a list of the settings
  std::map<CubitString, SettingHolder*>::iterator iter = mSettingsList.begin();
  while(iter != mSettingsList.end())
  {
    std::pair<CubitString, SettingHolder*>  tmp_pair;
    CubitString key = (*iter).first;
    tmp_pair.first = key;
    tmp_pair.second = (*iter).second;
    list.push_back( (tmp_pair) );
    iter++;
  }
}

Definition at line 19 of file SettingHandler.cpp.

{   
  if (!instance_)
    {   
      instance_ = new SettingHandler;
      if (!instance_)
    {
      std::cerr << " *** Unable to instantiate setting_handler object ***" << std::endl;
      exit(1);
    }
    }
  return instance_;
}

Definition at line 171 of file SettingHandler.cpp.

{
  int rows, cols;
  if (!AppUtil::instance()->get_terminal_size(rows, cols))
    cols = 71;
  --cols;

  // Get starting and ending iterators from the map
  std::map<CubitString, SettingHolder*>::iterator iter = mSettingsList.begin();
  const std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();
  
  // Step through the map and build a file to hold the settings
  for ( ; iter != end; ++iter ) 
  {
    SettingHolder* setting = iter->second;
    int vallen = cols - setting->name.length() - 1;
    if (vallen < 1) vallen = 1;
    switch( setting->setting_type )
    {
      case 0:
        PRINT_INFO("%s %*d\n", setting->name.c_str(), vallen,
            (setting->get_int_function)());
        break;
      case 1:
        PRINT_INFO("%s %*f\n", setting->name.c_str(), vallen,
            (setting->get_double_function)());
        break;
      case 2:
        PRINT_INFO("%s %*s\n", setting->name.c_str(), vallen, 
            (setting->get_bool_function)() ? "true" : "false");
        break;
      case 3:
        PRINT_INFO("%s %*s\n", setting->name.c_str(), vallen, 
            (setting->get_string_function)().c_str());
        break;
      default:
        PRINT_INFO("%s %*s\n", setting->name.c_str(), vallen,
            "ERROR : UNKNOWN SETTING TYPE");
    }
  }
}
void SettingHandler::restore_settings ( const char *  filename)

Definition at line 268 of file SettingHandler.cpp.

{

  //Open the file for reading
  CubitFile file(filename, "r");

  if (file) {
    std::cerr << "File " << filename << " could not be opened.  Settings not restored." << std::endl;
    return;
   }

  //Read the first 2 lines of the file, we know they are not settings
  //so just get rid of them
  char junk[100];
  fgets(junk, 100, file.file());
  fgets(junk, 100, file.file());

  char name[51]; //Allocate 50 bytes for the characters and 1 for a null termination
  CubitString cubit_name; //A CubitString that will be used for checking if the setting
                          //is in the map.
  char value[120]; //This will be used when getting the value of a setting
  SettingHolder* setting;
  int int_value = -1;
  double double_value = 0.0;
  CubitBoolean bool_value = CUBIT_FALSE;
  CubitString string_value = "";

  while (!feof(file.file())) {

  //Get the setting name.  This will be in the first 50 characters of a line
  fgets(name, 51, file.file());

  cubit_name = name;
  cubit_name.trim();

  //Find the setting in the map
  std::map<CubitString, SettingHolder*>::iterator temp;

  temp = mSettingsList.find(cubit_name);
  
    //Read in the rest of the line no matter what
  fgets(value, 120, file.file());
    
  if (temp == mSettingsList.end()) {
    std::cerr << "Setting " << cubit_name.c_str() << " was not found." << std::endl;  
  }
  else {
    setting = (*temp).second;
    //Integer Settings
    if (setting->setting_type == 0) {
      int_value = atoi(value);
      (setting->set_int_function)(int_value);
    }
    
    //Double Settings
    if (setting->setting_type == 1) {
      double_value = atof(value);
      (setting->set_double_function)(double_value);
    }    

    //Boolean Settings
    if (setting->setting_type == 2) {
//      bool_value = (CubitBoolean)(atoi(value));
      bool_value = (atoi(value)) ? true : false;
      (setting->set_bool_function)(bool_value);
    }    
    
    //CubitString Settings
    if (setting->setting_type == 3) {
      string_value = value;
      (setting->set_string_function)(string_value);
    }     

  }

  } //End while

}
void SettingHandler::save_settings ( const char *  filename)

Definition at line 120 of file SettingHandler.cpp.

{
  CubitFile file(filename, "w");
  
  if (!file) {
    std::cerr << "File " << filename << " could not be opened.  Settings not saved." << std::endl;
    return;
   }

  //Put a header at the top of the file
  fprintf(file.file(), "#Setting Name                                 Setting Value\n");
  fprintf(file.file(), "#------------------------------------------------------------\n");

  // Get starting and ending iterators from the map
  std::map<CubitString, SettingHolder*>::iterator start = mSettingsList.begin();
  std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();
  
  // Step through the map and build a file to hold the settings
  while(start != end) {
    SettingHolder* setting = (*start).second;

    //Integer settings
    if (setting->setting_type == 0) {
      fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_int_function)());
    }
    
    //Double settings
    else if (setting->setting_type == 1) {
      fprintf(file.file(), "%-50.50s%f \n", setting->name.c_str(), (setting->get_double_function)());
    }

    //Boolean settings
    else if (setting->setting_type == 2) {
      fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_bool_function)());
    }
    
    //String settings
    else if (setting->setting_type == 3) {
      fprintf(file.file(), "%-50.50s%s \n", setting->name.c_str(), (setting->get_string_function)().c_str());
    }

    else
    {
      assert(false);
      PRINT_ERROR("Error with SettingHolder type!  Please report.");
    }

    ++start;
  }
}

Definition at line 213 of file SettingHandler.cpp.

{
  
  const char* default_filename = "cubit.settings";

  CubitFile file(default_filename, "w");
  
  if (!file) {
    std::cerr << "File " << default_filename << " could not be opened.  Settings not saved." << std::endl;
    return;
   }

  //Put a header at the top of the file
  fprintf(file.file(), "#Setting Name                                 Setting Value\n");
  fprintf(file.file(), "#------------------------------------------------------------\n");

  // Get starting and ending iterators from the map
  std::map<CubitString, SettingHolder*>::iterator start = mSettingsList.begin();
  std::map<CubitString, SettingHolder*>::iterator end = mSettingsList.end();

  // Step through the map and build a file to hold the settings
  while(start != end) {
    SettingHolder* setting = (*start).second;

    //Integer settings
    if (setting->setting_type == 0) {
      fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_int_function)());
    }
    
    //Double settings
    else if (setting->setting_type == 1) {
      fprintf(file.file(), "%-50.50s%f \n", setting->name.c_str(), (setting->get_double_function)());
    }

    //Boolean settings
    else if (setting->setting_type == 2) {
      fprintf(file.file(), "%-50.50s%d \n", setting->name.c_str(), (setting->get_bool_function)());
    }
    
    //String settings
    else if (setting->setting_type == 3) {
      fprintf(file.file(), "%-50.50s%s \n", setting->name.c_str(), (setting->get_string_function)().c_str());
    }

    else
    {
      assert(false);
      PRINT_ERROR("Error with SettingHolder type!  Please report.");
    }

    ++start;
  }
}
void SettingHandler::set_setting_bool ( SettingHolder setting,
CubitBoolean  bool_value 
) [inline]

Definition at line 99 of file SettingHandler.hpp.

{ (setting->set_bool_function)(bool_value); }
void SettingHandler::set_setting_debug_value ( SettingHolder setting,
int  debug_value 
) [inline]
void SettingHandler::set_setting_double ( SettingHolder setting,
double  double_value 
) [inline]

Definition at line 96 of file SettingHandler.hpp.

{ (setting->set_double_function)(double_value); }
void SettingHandler::set_setting_int ( SettingHolder setting,
int  int_value 
) [inline]

Definition at line 93 of file SettingHandler.hpp.

{ (setting->set_int_function)(int_value); }
void SettingHandler::set_setting_string ( SettingHolder setting,
CubitString  string_value 
) [inline]

Definition at line 102 of file SettingHandler.hpp.

{ (setting->set_string_function)(string_value); }

Friends And Related Function Documentation

friend class SettingHolder [friend]

Definition at line 20 of file SettingHandler.hpp.


Member Data Documentation

int(* SettingHandler::debug_get_function)(const int index) [private]

Definition at line 57 of file SettingHandler.hpp.

void(* SettingHandler::debug_set_function)(const int index, const int value) [private]

Definition at line 56 of file SettingHandler.hpp.

SettingHandler * SettingHandler::instance_ = NULL [static, private]

Definition at line 62 of file SettingHandler.hpp.

Definition at line 70 of file SettingHandler.hpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines