variables.h

back


/*
 BSD 3-Clause License
 
 Copyright (c) 2025, k4m1 <me@k4m1.net>
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 
 1. Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
 
 3. Neither the name of the copyright holder nor the names of its
    contributors may be used to endorse or promote products derived from
    this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef __VARIABLES__
#define __VARIABLES__

#include <iostream>
#include <stdexcept>
#include <unordered_map>
#include <string>

class variables {
    public:
        /* Get a variable with given name from our variable list.
         *
         * @param std::string name -- Name of the variable
         * @return std::string variable content if one exists, "" otherwise.
         */
        std::string get_variable(std::string name) {
            try {
                auto search = this->variable_map.find(name);
                if (search != this->variable_map.end()) {
                    return search->second;
                }
                return "";
            } catch (const std::out_of_range& e) {
                std::cerr << "Error: " << e.what() << std::endl;
                return "";
            }
        }

        /* Create a new variable with given name, or update existing
         * one.
         *
         * @param std::string name -- Name of variable we're working with
         * @param std::string value -- Value to set the variable content to be
         */
        void set_variable(std::string name, std::string value) {
            this->variable_map.insert_or_assign(name, value);
        }
        
        /* Remove variable with given name
         *
         * @param std::string name -- Name of variable we're removing
         */
        void delete_variable(std::string name) {
            this->variable_map.erase(name);
        }

    private:
        std::unordered_map<std::string, std::string> variable_map {{}};
};

#endif