From 4476ff379eecc73659e824f2e2cd372e6f3cd527 Mon Sep 17 00:00:00 2001
From: Matthew McGill <mhollismcgill@gmail.com>
Date: Thu, 1 Nov 2018 16:02:14 +0000
Subject: [PATCH] Minor refactoring

Former-commit-id: 23c210c67f573f625702a92dd0e743cf519f037e
---
 libIRDB/include/core/IRDB_Objects.hpp | 18 ++++++++++--------
 libIRDB/include/core/transform_step.h |  8 ++++----
 libIRDB/test/fill_in_cfg.cpp          | 11 ++++++-----
 libIRDB/test/fill_in_cfg.hpp          |  6 +++---
 tools/thanos/thanos.cpp               | 19 +++++++++----------
 5 files changed, 32 insertions(+), 30 deletions(-)

diff --git a/libIRDB/include/core/IRDB_Objects.hpp b/libIRDB/include/core/IRDB_Objects.hpp
index f95a2947b..5232f51a4 100644
--- a/libIRDB/include/core/IRDB_Objects.hpp
+++ b/libIRDB/include/core/IRDB_Objects.hpp
@@ -46,17 +46,19 @@ class IRDBObjects_t
 		
 	private:
                 pqxxDB_t *pqxx_interface = new pqxxDB_t();
-		// maps for speed of finding needed files, file IRs and/or variants
-		// that have already been read from the DB
+		// type aliases of maps. maps allow speed of finding needed files, file IRs 
+		// and/or variants that have already been read from the DB 
+		using IdToVariantMap_t = std::map<db_id_t, const std::shared_ptr<VariantID_t>>; 
+
+		using FileIRInfo_t = std::pair<File_t *const, std::shared_ptr<FileIR_t>>;
+		using IdToFileIRInfoMap_t = std::map<db_id_t, FileIRInfo_t>; 
                 
-                // maps variant id to variant
-		std::map<db_id_t, std::shared_ptr<VariantID_t>> variant_map;
-                // maps file id to (file, file ir)
-		std::map<db_id_t, std::pair<File_t*, std::shared_ptr<FileIR_t>>> file_IR_map;
+		IdToVariantMap_t variant_map;
+        	IdToFileIRInfoMap_t file_IR_map;
                 
                 // minor helpers (used to check assertions)
-                bool FilesAlreadyPresent(std::set<File_t*> the_files);
-                bool FilesBeingShared(std::shared_ptr<VariantID_t> the_variant);
+                bool FilesAlreadyPresent(const std::set<File_t*>& the_files) const;
+                bool FilesBeingShared(const std::shared_ptr<VariantID_t>& the_variant) const;
 };
 
 #endif
diff --git a/libIRDB/include/core/transform_step.h b/libIRDB/include/core/transform_step.h
index 40f09fbd7..5b864c69e 100644
--- a/libIRDB/include/core/transform_step.h
+++ b/libIRDB/include/core/transform_step.h
@@ -9,15 +9,15 @@ namespace Transform_SDK
 		public:
 			// Step names must be unique, allows arguments to
 			// be directed to their matching transform steps.
-			virtual std::string GetStepName(void) = 0;
+			virtual std::string getStepName(void) const = 0;
 
 			// Allows all steps to parse args before any step takes time to execute
-			virtual int ParseArgs(int argc, char* argv[])
+			virtual int parseArgs(int argc, const char* const argv[])
 			{
 				return 0; // success
 			}
 				
-			virtual int ExecuteStep(libIRDB::IRDBObjects_t *irdb_objects)
+			virtual int executeStep(libIRDB::IRDBObjects_t *const irdb_objects)
 			{
 				return 0; // success
 			}
@@ -30,6 +30,6 @@ namespace Transform_SDK
 }
 
 extern "C"
-Transform_SDK::TransformStep_t* GetTransformStep(void);    
+std::unique_ptr<Transform_SDK::TransformStep_t> GetTransformStep(void);    
 
 #endif
diff --git a/libIRDB/test/fill_in_cfg.cpp b/libIRDB/test/fill_in_cfg.cpp
index e78c40b29..9ffd312fa 100644
--- a/libIRDB/test/fill_in_cfg.cpp
+++ b/libIRDB/test/fill_in_cfg.cpp
@@ -580,10 +580,10 @@ void PopulateCFG::fill_in_landing_pads(FileIR_t *firp)
 	
 }
 
-int PopulateCFG::ParseArgs
+int PopulateCFG::parseArgs
         (
         int argc, 
-        char* argv[]
+        const char* const argv[]
         )
 {   
     if(argc<2)
@@ -611,7 +611,7 @@ int PopulateCFG::ParseArgs
     return 0;
 }
 
-int PopulateCFG::ExecuteStep(IRDBObjects_t *irdb_objects)
+int PopulateCFG::executeStep(IRDBObjects_t *const irdb_objects)
 {
     try 
 	{
@@ -664,7 +664,8 @@ int PopulateCFG::ExecuteStep(IRDBObjects_t *irdb_objects)
 
 
 extern "C"
-Transform_SDK::TransformStep_t* GetTransformStep(void)
+unique_ptr<Transform_SDK::TransformStep_t> GetTransformStep(void)
 {
-	return new PopulateCFG();
+	unique_ptr<Transform_SDK::TransformStep_t> the_step(new PopulateCFG());
+	return the_step;
 }
diff --git a/libIRDB/test/fill_in_cfg.hpp b/libIRDB/test/fill_in_cfg.hpp
index 53007c207..255ca46dc 100644
--- a/libIRDB/test/fill_in_cfg.hpp
+++ b/libIRDB/test/fill_in_cfg.hpp
@@ -32,12 +32,12 @@ class PopulateCFG : public libIRDB::Transform_SDK::TransformStep_t
 		// are not managed by this class).
 	}
 	
-	std::string GetStepName(void) override
+	std::string getStepName(void) const override
 	{
 		return std::string("fill_in_cfg");
 	}
-        int ParseArgs(int argc, char* argv[]) override;
-	int ExecuteStep(libIRDB::IRDBObjects_t*) override;
+        int parseArgs(int argc, const char* const argv[]) override;
+	int executeStep(libIRDB::IRDBObjects_t *const) override;
     
     private: // methods
         
diff --git a/tools/thanos/thanos.cpp b/tools/thanos/thanos.cpp
index e6a0da08b..48d7adef9 100644
--- a/tools/thanos/thanos.cpp
+++ b/tools/thanos/thanos.cpp
@@ -21,7 +21,7 @@ using namespace Transform_SDK;
 enum class Mode { DEBUG, VERBOSE, DEFAULT };
 
 int execute_step(int argc, char* argv[], bool step_optional, Mode exec_mode, 
-                 IRDBObjects_t* shared_objects, TransformStep_t* the_step);
+                 IRDBObjects_t* shared_objects, unique_ptr<TransformStep_t>& the_step);
 
 
 // The toolchain driver script ps_analyze.sh communicates
@@ -158,9 +158,9 @@ int main(int argc, char *argv[])
                             }
                             else
                             {
-				TransformStep_t* (*func)(void);
-				func = (TransformStep_t* (*)(void)) sym;
-				TransformStep_t* the_step = (*func)();
+				unique_ptr<TransformStep_t> (*func)(void);
+				func = (unique_ptr<TransformStep_t> (*)(void)) sym;
+				unique_ptr<TransformStep_t> the_step = (*func)();
 				assert(the_step != NULL);
 
                                 bool step_optional = true;
@@ -180,9 +180,9 @@ int main(int argc, char *argv[])
                                     dup2(log_output_fd, STDOUT_FILENO);
                                     dup2(log_output_fd, STDERR_FILENO); 
 				}
-
+				
 				int step_retval = execute_step(argc, argv, step_optional, exec_mode, shared_objects, the_step);
-                                
+
 				// cleanup from logging
 				if(exec_mode == Mode::DEFAULT || exec_mode == Mode::VERBOSE)
                                 {
@@ -194,7 +194,6 @@ int main(int argc, char *argv[])
                                 close(saved_stderr);
 
 				// cleanup plugin
-				delete the_step;
 				free(argv);
                                 dlclose(dlhdl);
 				
@@ -281,9 +280,9 @@ int main(int argc, char *argv[])
 
 
 int execute_step(int argc, char* argv[], bool step_optional, Mode exec_mode, 
-                 IRDBObjects_t* shared_objects, TransformStep_t* the_step)
+                 IRDBObjects_t* shared_objects, unique_ptr<TransformStep_t>& the_step)
 {
-    int parse_retval = the_step->ParseArgs(argc, argv);
+    int parse_retval = the_step->parseArgs(argc, argv);
     if(parse_retval != 0)
     {
         return parse_retval;
@@ -305,7 +304,7 @@ int execute_step(int argc, char* argv[], bool step_optional, Mode exec_mode,
         }
     }
 
-    int step_error = the_step->ExecuteStep(shared_objects);
+    int step_error = the_step->executeStep(shared_objects);
 
     if(step_error)
     {
-- 
GitLab