From 5544156030b079033684ef48a2691bd372024ea7 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Fri, 26 Jul 2019 15:09:46 -0400 Subject: [PATCH] Added expandable template for C++ launcher. --- plates/cpp-launcher/CMakeLists.txt | 21 +++++ plates/cpp-launcher/apps/CMakeLists.txt | 16 ++++ plates/cpp-launcher/apps/launcher.cpp | 76 +++++++++++++++++++ plates/cpp-launcher/cmake-build.sh | 46 +++++++++++ plates/cpp-launcher/include/lib-launcher.hpp | 37 +++++++++ plates/cpp-launcher/src/CMakeLists.txt | 13 ++++ plates/cpp-launcher/src/lib-launcher.cpp | 80 ++++++++++++++++++++ 7 files changed, 289 insertions(+) create mode 100644 plates/cpp-launcher/CMakeLists.txt create mode 100644 plates/cpp-launcher/apps/CMakeLists.txt create mode 100644 plates/cpp-launcher/apps/launcher.cpp create mode 100755 plates/cpp-launcher/cmake-build.sh create mode 100644 plates/cpp-launcher/include/lib-launcher.hpp create mode 100644 plates/cpp-launcher/src/CMakeLists.txt create mode 100644 plates/cpp-launcher/src/lib-launcher.cpp diff --git a/plates/cpp-launcher/CMakeLists.txt b/plates/cpp-launcher/CMakeLists.txt new file mode 100644 index 0000000..8e81555 --- /dev/null +++ b/plates/cpp-launcher/CMakeLists.txt @@ -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) + diff --git a/plates/cpp-launcher/apps/CMakeLists.txt b/plates/cpp-launcher/apps/CMakeLists.txt new file mode 100644 index 0000000..ca4a62b --- /dev/null +++ b/plates/cpp-launcher/apps/CMakeLists.txt @@ -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) + diff --git a/plates/cpp-launcher/apps/launcher.cpp b/plates/cpp-launcher/apps/launcher.cpp new file mode 100644 index 0000000..9de049f --- /dev/null +++ b/plates/cpp-launcher/apps/launcher.cpp @@ -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 +#include + +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(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(pChoice); + std::cin.ignore(std::numeric_limits::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(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 for numeric_limits + */ + std::cin.ignore(std::numeric_limits::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; +} + diff --git a/plates/cpp-launcher/cmake-build.sh b/plates/cpp-launcher/cmake-build.sh new file mode 100755 index 0000000..471d829 --- /dev/null +++ b/plates/cpp-launcher/cmake-build.sh @@ -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 + diff --git a/plates/cpp-launcher/include/lib-launcher.hpp b/plates/cpp-launcher/include/lib-launcher.hpp new file mode 100644 index 0000000..d7abe77 --- /dev/null +++ b/plates/cpp-launcher/include/lib-launcher.hpp @@ -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 +#include + +// 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); + diff --git a/plates/cpp-launcher/src/CMakeLists.txt b/plates/cpp-launcher/src/CMakeLists.txt new file mode 100644 index 0000000..131d022 --- /dev/null +++ b/plates/cpp-launcher/src/CMakeLists.txt @@ -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}) + diff --git a/plates/cpp-launcher/src/lib-launcher.cpp b/plates/cpp-launcher/src/lib-launcher.cpp new file mode 100644 index 0000000..08a031d --- /dev/null +++ b/plates/cpp-launcher/src/lib-launcher.cpp @@ -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 + +/* 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; +} +