Added expandable template for C++ launcher.
This commit is contained in:
		
							parent
							
								
									7b2fe537b7
								
							
						
					
					
						commit
						5544156030
					
				
							
								
								
									
										21
									
								
								plates/cpp-launcher/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								plates/cpp-launcher/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,21 @@ | ||||
| 
 | ||||
| ############################################################################### | ||||
| ## Author: Shaun Reed                                                        ## | ||||
| ## Legal: All Content (c) 2019 Shaun Reed, all rights reserved               ## | ||||
| ##                                                                           ## | ||||
| ## Contact: shaunrd0@gmail.com  | URL: www.shaunreed.com | GitHub: shaunrd0  ## | ||||
| ############################################################################## | ||||
| 
 | ||||
| # Define the version of CMake | ||||
| cmake_minimum_required(VERSION 2.8) | ||||
| 
 | ||||
| # Define the your project name | ||||
| project(cpp-launcher) | ||||
| 
 | ||||
| # Include any directories the compiler may need | ||||
| include_directories(./include) | ||||
| 
 | ||||
| # Point CMake to look for more CMakeLists within the following directories | ||||
| add_subdirectory(src) | ||||
| add_subdirectory(apps) | ||||
| 
 | ||||
							
								
								
									
										16
									
								
								plates/cpp-launcher/apps/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								plates/cpp-launcher/apps/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,16 @@ | ||||
| ############################################################################### | ||||
| ## Author: Shaun Reed                                                        ## | ||||
| ## Legal: All Content (c) 2019 Shaun Reed, all rights reserved               ## | ||||
| ##                                                                           ## | ||||
| ## Contact: shaunrd0@gmail.com  | URL: www.shaunreed.com | GitHub: shaunrd0  ## | ||||
| ############################################################################## | ||||
| 
 | ||||
| # Create a reference / variable to refer to our source code | ||||
| set(LAUNCHER_SRC launcher.cpp) | ||||
| 
 | ||||
| # Add our executable, naming it and linking it to our source code | ||||
| add_executable(launcher ${LAUNCHER_SRC}) | ||||
| 
 | ||||
| # Link to our custom library, defined in c-cmake/src/ | ||||
| target_link_libraries(launcher lib-launcher) | ||||
| 
 | ||||
							
								
								
									
										76
									
								
								plates/cpp-launcher/apps/launcher.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								plates/cpp-launcher/apps/launcher.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,76 @@ | ||||
| /*#############################################################################
 | ||||
| ## Author: Shaun Reed                                                        ## | ||||
| ## Legal: All Content (c) 2019 Shaun Reed, all rights reserved               ## | ||||
| ##                                                                           ## | ||||
| ## Contact: shaunrd0@gmail.com  | URL: www.shaunreed.com | GitHub: shaunrd0  ## | ||||
| ############################################################################## | ||||
| ## launcher.cpp | ||||
| */ | ||||
| 
 | ||||
| #include <iostream> | ||||
| #include <lib-launcher.hpp> | ||||
| 
 | ||||
| int main () { | ||||
|   // Because the launcher is this executable.. (main() will become our exe)
 | ||||
|   // Initialize the user choice to launcher problem at runtime
 | ||||
|   int pChoice = LAUNCH; | ||||
| 
 | ||||
|   // Cast the integer pChoice into an assignment for our Problem enum
 | ||||
|   // No failsafes needed here since we know pChoice = LAUNCH
 | ||||
|   Problem pSelect = static_cast<Problem>(pChoice); | ||||
| 
 | ||||
|   do | ||||
|   { | ||||
|     printf("\nWelcome to the cpp launcher!\n" | ||||
|            "Input the problem number to run the example.\n"); | ||||
| 
 | ||||
|     //ProblemList(); List and explain problem selection
 | ||||
|     //ProblemSelect(); Select problem, handle errors, return result to &pSelect
 | ||||
|      | ||||
|     std::cin >> pChoice; | ||||
|      | ||||
|     if(pChoice == LAUNCH) | ||||
|     { // Ensure that pSelect = LAUNCH and restart
 | ||||
|     pSelect = static_cast<Problem>(pChoice); | ||||
|     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); | ||||
|     RunProblem(pSelect); | ||||
|     continue; | ||||
|     } | ||||
|     else if ( pChoice > QTY ) | ||||
|     { // If we have entered a value too large, restart
 | ||||
|     std::printf("\nThe value you entered is too large." | ||||
|     "\nPlease enter a value below %d\n", QTY); | ||||
|     pSelect = Problem::Launch; // Set our launcher to restart and continue
 | ||||
|     continue; | ||||
|     } | ||||
| 
 | ||||
|     if (!std::cin )  | ||||
|     { // Check for cin error state
 | ||||
|     std::cin.clear(); | ||||
|     std::cin.ignore(); | ||||
|     pChoice = ERROR; | ||||
|     } | ||||
|     // One last input check for other error values
 | ||||
|     if (pChoice < EXIT) pChoice = ERROR; | ||||
| 
 | ||||
|     // Cast the integer pChoice into an assignment for our Problem enum    
 | ||||
|     pSelect = static_cast<Problem>(pChoice); | ||||
| 
 | ||||
|     /*
 | ||||
|     * We should expect a clear input buffer at this point | ||||
|     * Clear cin up to next endline to prepare for the next input | ||||
|     * Depends on include <limits> for numeric_limits<streamsize> | ||||
|     */ | ||||
|     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); | ||||
| 
 | ||||
|     // Run the problem, print exit, or print error.
 | ||||
|     RunProblem(pSelect);  | ||||
| 
 | ||||
|   // Run loop until invalid or exit input recieved
 | ||||
|   } while (pChoice > EXIT || pChoice == LAUNCH);  | ||||
|   // Exit the launcher if the selection is in range
 | ||||
|   // Exit if pSelect is set to ERROR state value
 | ||||
| 
 | ||||
|   return 0; | ||||
| } | ||||
| 
 | ||||
							
								
								
									
										46
									
								
								plates/cpp-launcher/cmake-build.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										46
									
								
								plates/cpp-launcher/cmake-build.sh
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,46 @@ | ||||
| #!/bin/bash | ||||
| ## Author: Shaun Reed | Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ## | ||||
| ## A custom bash script for building cmake projects.                         ## | ||||
| ## Intended to be ran in root directory of the project alongside CMakeLists  ## | ||||
| ############################################################################### | ||||
| 
 | ||||
| # Infinite while loop - break on conditions | ||||
| while true | ||||
| do | ||||
| 
 | ||||
|   printf "\nEnter 1 to build, 2 to cleanup previous build, 0 to exit.\n" | ||||
|   read bChoice | ||||
| 
 | ||||
|   # Build loop | ||||
|   # If input read is == 1 | ||||
|   if [ $bChoice -eq 1 ] | ||||
|   then | ||||
|     mkdir build | ||||
|     (cd build && cmake .. && cmake --build .) | ||||
|   fi | ||||
| 
 | ||||
|   # Clean-up loop | ||||
|   # If input read is == 2 | ||||
|   if [ $bChoice -eq 2 ] | ||||
|   then | ||||
|     rm -Rv build/* | ||||
|   fi | ||||
| 
 | ||||
|   # Exit loops, all other input -  | ||||
| 
 | ||||
|   # If input read is >= 3, exit | ||||
|   if [ $bChoice -ge 3 ] | ||||
|   then | ||||
|     break  | ||||
|   fi | ||||
|    | ||||
|   # If input read is <= 0, exit | ||||
|   if [ $bChoice -le 0 ] | ||||
|   then | ||||
|     break | ||||
|   fi | ||||
| 
 | ||||
|   # Bash will print an error if symbol or character input | ||||
| 
 | ||||
| done | ||||
| 
 | ||||
							
								
								
									
										37
									
								
								plates/cpp-launcher/include/lib-launcher.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								plates/cpp-launcher/include/lib-launcher.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,37 @@ | ||||
| /*#############################################################################
 | ||||
| ## Author: Shaun Reed                                                        ## | ||||
| ## Legal: All Content (c) 2019 Shaun Reed, all rights reserved               ## | ||||
| ##                                                                           ## | ||||
| ## Contact: shaunrd0@gmail.com  | URL: www.shaunreed.com | GitHub: shaunrd0  ## | ||||
| ############################################################################## | ||||
| ## lib-launcher.hpp | ||||
| */ | ||||
| 
 | ||||
| #include <iostream> | ||||
| #include <limits> | ||||
| 
 | ||||
| // Define our constants
 | ||||
| // These are used for ranges within our control loops
 | ||||
| const int QTY = 5; | ||||
| const int EXIT = 0; | ||||
| const int ERROR = -1; | ||||
| const int LAUNCH = 99; | ||||
| 
 | ||||
| /* An enumeration for use with RunProblem() when selecting which problem to run
 | ||||
|  * This is meant to be expanded as needed. | ||||
|  * Be sure to add a corresponding case within RunProblem() | ||||
|  * | ||||
|  * @Launcher (LAUNCH 99) the value used for the launcher as a problem number | ||||
|  * @Exit (EXIT 0) is the normal exit index | ||||
|  * @Error (ERROR -1) is considered an error | ||||
|  */ | ||||
| enum class Problem  | ||||
| { Launch = 99, Error = -1, Exit, One, Two, Three, Four, Five }; | ||||
| 
 | ||||
| /* This function allows for selection of the next problem to run.
 | ||||
|  * | ||||
|  * @param pSelect - The index to use within our enumeration. | ||||
|  *                  Allows for easy integer to problem selection. | ||||
|  */ | ||||
| void RunProblem(Problem pSelect); | ||||
| 
 | ||||
							
								
								
									
										13
									
								
								plates/cpp-launcher/src/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								plates/cpp-launcher/src/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,13 @@ | ||||
| ############################################################################### | ||||
| ## Author: Shaun Reed                                                        ## | ||||
| ## Legal: All Content (c) 2019 Shaun Reed, all rights reserved               ## | ||||
| ##                                                                           ## | ||||
| ## Contact: shaunrd0@gmail.com  | URL: www.shaunreed.com | GitHub: shaunrd0  ## | ||||
| ############################################################################## | ||||
| 
 | ||||
| # Create any links we might need | ||||
| set(LIB_LAUNCHER_SRC lib-launcher.cpp) | ||||
| 
 | ||||
| # Define our library within CMake and link to the source code | ||||
| add_library(lib-launcher ${LIB_LAUNCHER_SRC}) | ||||
| 
 | ||||
							
								
								
									
										80
									
								
								plates/cpp-launcher/src/lib-launcher.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								plates/cpp-launcher/src/lib-launcher.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,80 @@ | ||||
| /*#############################################################################
 | ||||
| ## Author: Shaun Reed                                                        ## | ||||
| ## Legal: All Content (c) 2019 Shaun Reed, all rights reserved               ## | ||||
| ##                                                                           ## | ||||
| ## Contact: shaunrd0@gmail.com  | URL: www.shaunreed.com | GitHub: shaunrd0  ## | ||||
| ############################################################################## | ||||
| ## lib-launcher.cpp | ||||
| */ | ||||
| 
 | ||||
| #include <lib-launcher.hpp> | ||||
| 
 | ||||
| /* This function allows for selection of the next problem to run.
 | ||||
|  * | ||||
|  * @param pSelect - The index to use within our enumeration. | ||||
|  *                  Allows for easy integer to problem selection. | ||||
|  */ | ||||
| void RunProblem(Problem pSelect) {  | ||||
| 
 | ||||
|   switch (pSelect) { | ||||
|      | ||||
|     case Problem::One:   | ||||
|       std::printf("\nYou are on problem 1!\n" | ||||
|       "Press enter to continue."); | ||||
|       std::cin.ignore(); | ||||
|       break; | ||||
|      | ||||
|     case Problem::Two: | ||||
|       std::printf("\nYou are on problem 2!\n" | ||||
|       "Press enter to continue."); | ||||
|       std::cin.ignore(); | ||||
|       break; | ||||
|      | ||||
|     case Problem::Three: | ||||
|       std::printf("\nYou are on problem 3!\n" | ||||
|       "Press enter to continue."); | ||||
|       std::cin.ignore(); | ||||
|       break; | ||||
|      | ||||
|     case Problem::Four: | ||||
|       std::printf("\nYou are on problem 4!\n" | ||||
|       "Press enter to continue."); | ||||
|       std::cin.ignore(); | ||||
|       break; | ||||
|      | ||||
|     case Problem::Five: | ||||
|       std::printf("\nYou are on problem 5!\n" | ||||
|       "Press enter to continue."); | ||||
|       std::cin.get(); | ||||
|       break; | ||||
| 
 | ||||
|     case Problem::Exit: | ||||
|       std::printf("\nYou are on problem 0! This is a safe exit.\n" | ||||
|       "Press enter to continue."); | ||||
|       std::cin.ignore(); | ||||
|       break; | ||||
| 
 | ||||
|     case Problem::Error: | ||||
|       std::printf("\nYou are on problem -1! This is considered and error.\n" | ||||
|       "Press enter to exit."); | ||||
|       std::cin.ignore(); | ||||
|       break; | ||||
| 
 | ||||
|     case Problem::Launch: | ||||
|       // Do nothing, break and let main() restart the launcher
 | ||||
|       std::printf("\nRestarting the launcher...\n" | ||||
|       "Press enter to continue."); | ||||
|       std::cin.ignore(); | ||||
|       break; | ||||
| 
 | ||||
|     default: | ||||
|       std::printf("\nYou have entered an invalid value." | ||||
|       "\nPress Enter to try again."); | ||||
|       //ProblemList();
 | ||||
|       break; | ||||
| 
 | ||||
|   } | ||||
| 
 | ||||
|   return; | ||||
| } | ||||
| 
 | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user