From 7215102725c1903be1d6b69ddbb576891d67b96c Mon Sep 17 00:00:00 2001 From: Vratislav Chudoba Date: Wed, 20 Mar 2024 13:41:25 +0300 Subject: [PATCH] Makefile cleaned. Warnings in ConfigDictionary class resolved. --- .gitignore | 24 ++++++++++++++- Utilities/ConfigDictionary.cpp | 37 +++++++++++++++++------- Utilities/ConfigDictionary.h | 53 +++++++++++++++++++++++----------- makefile | 29 ++----------------- 4 files changed, 88 insertions(+), 55 deletions(-) diff --git a/.gitignore b/.gitignore index ab6278b..652e7e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,25 @@ -*~ +# Ignore compiled files +*.o +*.out +*.d +*.so + +#Ignore files automatically build by ROOT +*.pcm +*Dict.cpp + + +# Ignore log files +*.log + +# Ignore build directories +build/ + +# Ignore data and calibration files *.root *.cal + +#Ignore VS code stuff +.vscode/ + +# config.ini \ No newline at end of file diff --git a/Utilities/ConfigDictionary.cpp b/Utilities/ConfigDictionary.cpp index 7e3790b..c9ab7e4 100644 --- a/Utilities/ConfigDictionary.cpp +++ b/Utilities/ConfigDictionary.cpp @@ -161,46 +161,61 @@ void ConfigDictionary::FromString(std::string params){ } //_____________________________________________________________________________ -std::string ConfigDictionary::GetString(std::string key)throw(std::string){ +std::string ConfigDictionary::GetString(std::string key){ //Extracts string from given key //(if it exist, otherwise raises exception) //This works also for long strings with white spaces in it! if (configMap.find(key) == configMap.end()){ Error("ConfigDictionary::GetString", "Couldn't find the key: %s!",key.c_str()); - throw(key); + // throw(key); + throw std::runtime_error("Couldn't find the key: " + key); } return configMap[key]; } //_____________________________________________________________________________ -int ConfigDictionary::GetInt(std::string key)throw(std::string){ - //Extracts integer from given key - //(if it exist, otherwise raises exception) - return Get(key); +int ConfigDictionary::GetInt(std::string key) { + // Extracts integer from given key + // (if it exists, otherwise raises exception) + try { + return Get(key); + } catch (const std::string& message) { + // Catch the exception thrown by Get and re-throw it as std::runtime_error + throw std::runtime_error(message); + } } //_____________________________________________________________________________ -double ConfigDictionary::GetDouble(std::string key)throw(std::string){ +double ConfigDictionary::GetDouble(std::string key){ //Extracts double precision floating number from given key //(if it exist, otherwise raises exception) - return Get(key); + try + { + return Get(key); + } catch (const std::string& message) { + // Catch the exception thrown by Get and re-throw it as std::runtime_error + throw std::runtime_error(message); + } } //_____________________________________________________________________________ -bool ConfigDictionary::GetBool(std::string key)throw(std::string){ +bool ConfigDictionary::GetBool(std::string key){ //Extracts boolean from given key //(if it exist, otherwise raises exception) //Differs from template to make it more readable. + + // Check if the key exists in the config map if (configMap.find(key) == configMap.end()){ Error("ConfigDictionary::GetBool", "Couldn't find the key: %s!",key.c_str()); - throw(key); + throw std::runtime_error("Couldn't find the key: " + key); } //Convert string to bool: if (configMap[key].compare("true") == 0) return true; - else return false; + else + return false; } //_____________________________________________________________________________ diff --git a/Utilities/ConfigDictionary.h b/Utilities/ConfigDictionary.h index a6fd7d4..b34a264 100644 --- a/Utilities/ConfigDictionary.h +++ b/Utilities/ConfigDictionary.h @@ -33,10 +33,10 @@ public: //These throw errors if couldn't find key: //Most of methods below are just using templates to //make them usable in Cint. - std::string GetString(std::string)throw(std::string); - int GetInt(std::string)throw(std::string); - double GetDouble(std::string)throw(std::string); - bool GetBool(std::string)throw(std::string); + std::string GetString(std::string); + int GetInt(std::string); + double GetDouble(std::string); + bool GetBool(std::string); //These will always set 'something' into map: void SetString(std::string,std::string); @@ -50,7 +50,7 @@ public: //_________________________________________________________________________ template - T Get(std::string key) throw (std::string) { + T Get(std::string key) { //This method will get object of any class //that was - presumably - stored before in string format in CD. //Object needs to have proper >> operator. @@ -62,11 +62,20 @@ public: Error( "ConfigDictionary::Get", "Couldn't find the key: %s!", key.c_str()); - throw(key); + throw std::runtime_error("Couldn't find the key: " + key); } + // Use std::istringstream to convert the string to the desired type std::istringstream inStream(configMap[key]); T item; + // Extract the value from the stringstream inStream >> item; + // Check if extraction failed + if (inStream.fail()) { + // Throw std::runtime_error with an informative message + throw std::runtime_error("Failed to extract value for key: " + key); + } + + // Return the extracted item return item; } @@ -83,29 +92,39 @@ public: //_________________________________________________________________________ template - int GetArray(std::string key,std::vector & output)throw(std::string){ + int GetArray(std::string key,std::vector & output){ //Preconditions: T must have >> operator //Effect: searches for key in map and pushes values stored in it into output vector //Returns: number of elements pushed into vector - int retNumber = 0; + + int retNumber = 0; // Counter for the number of elements pushed into the vector + + // Check if the key exists in the config map if (configMap.find(key) == configMap.end()){ Error( "ConfigDictionary::GetInt", "Couldn't find the key: %s!",key.c_str()); - throw(key); + throw std::runtime_error("Couldn't find the key: " + key); } + + // Create a string stream from the value associated with the key std::istringstream arrayString(configMap[key]); + // Temporary string to hold each item from the string stream std::string itemString; + //Dangerous - maybe? - exceptions for ss not handled... - while (arrayString){ - std::getline(arrayString,itemString,ARRAY_DELIM); - std::stringstream itemStream(itemString); - if (itemString.length() > 0){ - T item; - itemStream>>item; - output.push_back(item); - retNumber++; + // Loop to extract each item from the string stream + while (std::getline(arrayString,itemString,ARRAY_DELIM)){ + std::stringstream itemStream(itemString); // Create a string stream from the item string + if (!itemString.empty()) { // Check if the item string is not empty + T item; // Temporary variable to hold the converted item + itemStream >> item; // Extract the item from the string stream + output.push_back(item); // Push the item into the output vector + retNumber++; // Increment the counter } + } + + // Return the number of elements pushed into the vector return retNumber; } diff --git a/makefile b/makefile index 5d254ef..1b720e3 100755 --- a/makefile +++ b/makefile @@ -51,23 +51,22 @@ clean_htmldoc: clean: -$(RM) $(UTILITIESOBJS) $(UTILITIESCPP_DEPS) - -$(RM) $(UTILITIES)/UtilitiesDict.* libUtilities.so + -$(RM) $(UTILITIES)/UtilitiesDict.* libUtilities.so $(UTILITIES)/*.pcm -@echo ' ' -$(RM) $(ACULDATAOBJS) $(ACULDATACPP_DEPS) -$(RM) $(ACULDATA)/AculDataDict.* *.pcm $(ACULDATA)/*.pcm -$(RM) libAculData.so -@echo ' ' -$(RM) $(ACULCALIBOBJS) $(ACULCALIBCPP_DEPS) - -$(RM) $(ACULCALIB)/AculCalibDict.* libAculCalib.so + -$(RM) $(ACULCALIB)/AculCalibDict.* libAculCalib.so $(ACULCALIB)/*.pcm -@echo ' ' -$(RM) $(TELOSSOBJS) $(TELOSSCPP_DEPS) - -$(RM) $(TELOSS)/TELossDict.* *.pcm $(TELOSS)/*.pcm + -$(RM) $(TELOSS)/TELossDict.* *.pcm $(TELOSS)/*.pcm $(TELOSS)/*.pcm -$(RM) libTELoss.so -@echo ' ' -$(RM) htmldoc -@echo ' ' -# <<<<<<< HEAD # Those *Dictionary* files below need special treating: $(UTILITIES)/UtilitiesDict.cpp: -@echo 'Pre-building UtilitiesDict.cpp and UtilitiesDict.h files' @@ -98,28 +97,6 @@ $(TELOSS)/TELossDict.cpp: -ln -s $(TELOSS)/TELossDict_rdict.pcm . -@echo ' ' -# ======= -# Those *Cint* files below need special treating: -# $(UTILITIES)/UtilitiesDict.cpp: -# -@echo 'Pre-building UtilitiesDict.cpp and UtilitiesDict.h files' -# -rootcint -f $(UTILITIES)/UtilitiesDict.cpp -c -p $(UTILITIES_HEADERS) -# -@echo ' ' - -# $(ACULDATA)/AculDataCint.cpp: -# -@echo 'Pre-building AculDataCint.cpp and AculDataCint.h files' -# -rootcint -f $(ACULDATA)/AculDataCint.cpp -c -p $(ACULDATA_HEADERS) -# -@echo ' ' - -# $(ACULCALIB)/AculCalibDict.cpp: -# -@echo 'Pre-building AculCalibDict.cpp and AculCalibDict.h files' -# -rootcint -f $(ACULCALIB)/AculCalibDict.cpp -c -p $(ACULCALIB_HEADERS) -# -@echo ' ' - -# $(TELOSS)/TELossCint.cpp: -# -@echo 'Pre-building TELossCint.cpp and TELossCint.h files' -# -rootcint -f $(TELOSS)/TELossCint.cpp -c -p $(TELOSS)/TELoss.h $(TELOSS)/linkdef.h -# >>>>>>> newArch - #*.so files libUtilities.so: $(UTILITIESOBJS) @echo 'Building target: $@' -- 2.18.1