BuildCache
string_list.hpp
1 //--------------------------------------------------------------------------------------------------
2 // Copyright (c) 2018 Marcus Geelnard
3 //
4 // This software is provided 'as-is', without any express or implied warranty. In no event will the
5 // authors be held liable for any damages arising from the use of this software.
6 //
7 // Permission is granted to anyone to use this software for any purpose, including commercial
8 // applications, and to alter it and redistribute it freely, subject to the following restrictions:
9 //
10 // 1. The origin of this software must not be misrepresented; you must not claim that you wrote
11 // the original software. If you use this software in a product, an acknowledgment in the
12 // product documentation would be appreciated but is not required.
13 //
14 // 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
15 // being the original software.
16 //
17 // 3. This notice may not be removed or altered from any source distribution.
18 //--------------------------------------------------------------------------------------------------
19 
20 #ifndef BUILDCACHE_STRING_LIST_HPP_
21 #define BUILDCACHE_STRING_LIST_HPP_
22 
23 #include <initializer_list>
24 #include <string>
25 #include <vector>
26 
27 namespace bcache {
31 public:
32  typedef typename std::vector<std::string>::iterator iterator;
33  typedef typename std::vector<std::string>::const_iterator const_iterator;
34 
37  }
38 
41  string_list_t(std::initializer_list<std::string> list) : m_strings(list) {
42  }
43 
46  string_list_t(const std::vector<std::string>& vec) : m_strings(vec) {
47  }
48 
53  string_list_t(const int argc, const char** argv) {
54  for (int i = 0; i < argc; ++i) {
55  m_strings.emplace_back(std::string(argv[i]));
56  }
57  }
58 
64  string_list_t(const std::string& str, const std::string& delimiter);
65 
70  static string_list_t split_args(const std::string& cmd);
71 
78  std::string join(const std::string& separator, const bool escape = false) const;
79 
81  void clear() {
82  m_strings.clear();
83  }
84 
86  void pop_back() {
87  m_strings.pop_back();
88  }
89 
90  std::string& operator[](const size_t idx) {
91  return m_strings[idx];
92  }
93 
94  const std::string& operator[](const size_t idx) const {
95  return m_strings[idx];
96  }
97 
98  string_list_t& operator+=(const std::string& str) {
99  m_strings.emplace_back(str);
100  return *this;
101  }
102 
103  string_list_t& operator+=(const string_list_t& list) {
104  for (const auto& str : list) {
105  m_strings.emplace_back(str);
106  }
107  return *this;
108  }
109 
110  string_list_t operator+(const std::string& str) const {
111  string_list_t result(*this);
112  result += str;
113  return result;
114  }
115 
116  string_list_t operator+(const string_list_t& list) const {
117  string_list_t result(*this);
118  result += list;
119  return result;
120  }
121 
122  size_t size() const {
123  return m_strings.size();
124  }
125 
126  iterator begin() {
127  return m_strings.begin();
128  }
129 
130  const_iterator begin() const {
131  return m_strings.begin();
132  }
133 
134  const_iterator cbegin() const {
135  return m_strings.cbegin();
136  }
137 
138  iterator end() {
139  return m_strings.end();
140  }
141 
142  const_iterator end() const {
143  return m_strings.end();
144  }
145 
146  const_iterator cend() const {
147  return m_strings.cend();
148  }
149 
150 private:
151  static std::string escape_arg(const std::string& arg);
152 
153  static std::string unescape_arg(const std::string& arg);
154 
155  std::vector<std::string> m_strings;
156 };
157 } // namespace bcache
158 
159 #endif // BUILDCACHE_STRING_LIST_HPP_
bcache::string_list_t::string_list_t
string_list_t()
Construct an empty list.
Definition: string_list.hpp:36
bcache::string_list_t::pop_back
void pop_back()
Remove the last element.
Definition: string_list.hpp:86
bcache::string_list_t::string_list_t
string_list_t(const int argc, const char **argv)
Construct a list from command line arguments.
Definition: string_list.hpp:53
bcache::string_list_t::split_args
static string_list_t split_args(const std::string &cmd)
Construct a list of arguments from a string with a shell-like format.
Definition: string_list.cpp:49
bcache
The top level namespace.
Definition: compressor.cpp:34
bcache::string_list_t
A convenient vector of strings container with support functions for program argument handling.
Definition: string_list.hpp:30
bcache::string_list_t::join
std::string join(const std::string &separator, const bool escape=false) const
Join all elements into a single string.
Definition: string_list.cpp:101
bcache::string_list_t::string_list_t
string_list_t(const std::vector< std::string > &vec)
Construct a list from a vector of strings.
Definition: string_list.hpp:46
bcache::string_list_t::string_list_t
string_list_t(std::initializer_list< std::string > list)
Construct a list from an initializer list.
Definition: string_list.hpp:41
bcache::string_list_t::clear
void clear()
Remove all the elements.
Definition: string_list.hpp:81