BashSpark
Loading...
Searching...
No Matches
shell_var.h
Go to the documentation of this file.
1
29#pragma once
30
31#include <string>
32
34
35namespace bs {
42 class shell_var {
43 public:
45 shell_var() = default;
46
47 public:
53 [[nodiscard]] std::string get_var(const std::string &sVar) const {
54 const auto pIter = this->m_mVariables.find(sVar);
55 return pIter != this->m_mVariables.end() ? pIter->second : "";
56 }
57
64 [[nodiscard]] std::string get_var_hop2(const std::string &sVar) const {
65 const auto pIter1 = this->m_mVariables.find(sVar);
66 if (pIter1 == this->m_mVariables.end()) return "";
67 const auto pIter2 = this->m_mVariables.find(pIter1->second);
68 return pIter2 != this->m_mVariables.end() ? pIter2->second : "";
69 }
70
76 void set_var(std::string sVar, std::string sValue) {
77 this->m_mVariables.insert_or_assign(std::move(sVar), std::move(sValue));
78 }
79
85 [[nodiscard]] bool has_var(const std::string &sVar) const noexcept {
86 return this->m_mVariables.contains(sVar);
87 }
88
93 [[nodiscard]] std::size_t get_var_size() const noexcept {
94 return this->m_mVariables.size();
95 }
96
101 [[nodiscard]] const std::unordered_map<std::string, std::string, shell_hash> &get_var() const noexcept {
102 return this->m_mVariables;
103 }
104
105 private:
107 std::unordered_map<std::string, std::string, shell_hash> m_mVariables;
108
109 };
110} // namespace bs
Represents a variable map for shell commands.
Definition shell_var.h:42
void set_var(std::string sVar, std::string sValue)
Sets the value of a variable.
Definition shell_var.h:76
std::string get_var_hop2(const std::string &sVar) const
Retrieves the value of a variable, whose value is another variable (1-hop resolution).
Definition shell_var.h:64
const std::unordered_map< std::string, std::string, shell_hash > & get_var() const noexcept
Retrieves all variables.
Definition shell_var.h:101
bool has_var(const std::string &sVar) const noexcept
Checks if a variable exists.
Definition shell_var.h:85
std::string get_var(const std::string &sVar) const
Retrieves the value of a variable.
Definition shell_var.h:53
shell_var()=default
Default constructor.
std::size_t get_var_size() const noexcept
Gets the number of variables.
Definition shell_var.h:93
BashSpark main namespace.
Definition command.h:39
Defines the bs::shell_hash functor implementing a 64-bit FNV-1a hash.