ionStandardLibrary.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include "ionTypes.h"
14 #include "ionSmartPointer.h"
15 
16 #include <tinyformat.h>
17 
18 #include <algorithm>
19 #include <numeric>
20 #include <limits>
21 #include <iomanip>
22 #include <iostream>
23 #include <fstream>
24 #include <sstream>
25 #include <array>
26 
27 
28 using std::move;
29 using std::for_each;
30 using std::to_string;
31 
32 using std::fstream;
33 using std::ifstream;
34 using std::ofstream;
35 
36 using std::stringstream;
37 using std::istringstream;
38 using std::ostringstream;
39 
40 using std::cout;
41 using std::cerr;
42 using std::endl;
43 
45 template <typename T>
46 using NumericLimits = std::numeric_limits<T>;
47 
48 
50 template <typename T, typename U>
51 U * ConditionalMapAccess(map<T, U *> const & Map, T const Key)
52 {
53  auto Iterator = Map.find(Key);
54 
55  if (Iterator != Map.end())
56  return Iterator->second;
57 
58  return nullptr;
59 }
60 
62 template <typename T, typename U>
63 SharedPointer<U> ConditionalMapAccess(map<T, SharedPointer<U>> const & Map, T const Key)
64 {
65  auto Iterator = Map.find(Key);
66 
67  if (Iterator != Map.end())
68  return Iterator->second;
69 
70  return nullptr;
71 }
72 
74 template <typename T, typename U>
75 U * ConditionalMapAccess(unordered_map<T, U *> const & Map, T const Key)
76 {
77  auto Iterator = Map.find(Key);
78 
79  if (Iterator != Map.end())
80  return Iterator->second;
81 
82  return 0;
83 }
84 
86 template <typename T, typename U>
87 bool CheckMapAccess(map<T, U> const & Map, T const Key)
88 {
89  return Map.find(Key) != Map.end();
90 }
91 
93 template <typename T, typename U>
94 bool TryMapAccess(map<T, U> const & Map, T const Key, U & Value)
95 {
96  auto Iterator = Map.find(Key);
97 
98  if (Iterator != Map.end())
99  {
100  Value = Iterator->second;
101  return true;
102  }
103 
104  return false;
105 }
106 
108 template <typename T, typename U>
109 set<T> KeySet(map<T, U> const & Map)
110 {
111  set<T> Return;
112 
113  for (auto it : Map)
114  Return.insert(it.first);
115 
116  return Return;
117 }
118 
120 template <typename T>
121 void AddAtEnd(vector<T> & A, vector<T> const & B)
122 {
123  A.insert(A.end(), B.begin(), B.end());
124 }
125 
127 template <typename T>
128 void EraseRemove(vector<T> & A, T B)
129 {
130  A.erase(std::remove(std::begin(A), std::end(A), B), std::end(A));
131 }
132 
134 template <typename T, typename U>
135 void EraseRemove(vector<T> & A, U B)
136 {
137  A.erase(std::remove(std::begin(A), std::end(A), B), std::end(A));
138 }
139 
141 template <typename T>
142 void DeleteAndClear(vector<T> & A)
143 {
144  std::for_each(A.begin(), A.end(), [](T ptr) { delete ptr; });
145  A.clear();
146 }
147 
150 class File
151 {
152 
153 public:
154 
155  static bool Exists(string const & FileName)
156  {
157  ifstream ifile(FileName);
158  return ifile.good();
159  }
160 
161  static string ReadAsString(string const & FileName)
162  {
163  std::ifstream FileHandle(FileName);
164  std::string String;
165 
166  FileHandle.seekg(0, std::ios::end);
167  String.reserve((uint) FileHandle.tellg());
168  FileHandle.seekg(0, std::ios::beg);
169 
170  String.assign((std::istreambuf_iterator<char>(FileHandle)), std::istreambuf_iterator<char>());
171 
172  return String;
173  }
174 
175  static string StripExtension(string const & Path)
176  {
177  return Path.substr(0, Path.find_last_of("."));
178  }
179 
180  static string GetExtension(string const & Path)
181  {
182  size_t Index = Path.find_last_of(".");
183  if (Index == string::npos)
184  {
185  return "";
186  }
187  else
188  {
189  return Path.substr(Index + 1);
190  }
191  }
192 };
193 
196 class String
197 {
198 
199 public:
200 
201  static vector<string> SeparateLines(string const & str)
202  {
203  vector<string> Lines;
204  istringstream Stream(str);
205  string Line;
206 
207  while (getline(Stream, Line))
208  {
209  Lines.push_back(move(Line));
210  }
211 
212  return Lines;
213  }
214 
215  static vector<string> Explode(string const & str, char delimiter)
216  {
217  vector<string> Words;
218  istringstream Stream(str);
219  string Word;
220 
221  while (getline(Stream, Word, delimiter))
222  {
223  Words.push_back(move(Word));
224  }
225 
226  return Words;
227  }
228 
229  static bool BeginsWith(string const & s, string const & prefix, string & remainder)
230  {
231  if (s.size() < prefix.size())
232  {
233  return false;
234  }
235 
236  if (s.substr(0, prefix.size()) == prefix)
237  {
238  remainder = s.substr(prefix.size());
239  return true;
240  }
241 
242  return false;
243  }
244 
245  template <typename... Args>
246  static string Build(char const * const Format, Args const &... args)
247  {
248  return tfm::format(Format, args...);
249  }
250 
251 };
static string ReadAsString(string const &FileName)
Definition: ionStandardLibrary.h:161
static vector< string > Explode(string const &str, char delimiter)
Definition: ionStandardLibrary.h:215
static string Build(char const *const Format, Args const &...args)
Definition: ionStandardLibrary.h:246
std::shared_ptr< T > SharedPointer
Definition: ionSmartPointer.h:22
Helper methods for dealing with strings.
Definition: ionStandardLibrary.h:196
unsigned int uint
Definition: ionTypes.h:99
set< T > KeySet(map< T, U > const &Map)
Definition: ionStandardLibrary.h:109
Brings some standard library pointer classes into ion namespace.
void DeleteAndClear(vector< T > &A)
Deletes every pointer element of a vector and then clears the vector.
Definition: ionStandardLibrary.h:142
bool TryMapAccess(map< T, U > const &Map, T const Key, U &Value)
Definition: ionStandardLibrary.h:94
static string StripExtension(string const &Path)
Definition: ionStandardLibrary.h:175
static bool Exists(string const &FileName)
Definition: ionStandardLibrary.h:155
void AddAtEnd(vector< T > &A, vector< T > const &B)
Definition: ionStandardLibrary.h:121
static vector< string > SeparateLines(string const &str)
Definition: ionStandardLibrary.h:201
Defines typedefs for integer and floating point types.
static string GetExtension(string const &Path)
Definition: ionStandardLibrary.h:180
U * ConditionalMapAccess(map< T, U * > const &Map, T const Key)
Definition: ionStandardLibrary.h:51
std::numeric_limits< T > NumericLimits
Definition: ionStandardLibrary.h:46
void EraseRemove(vector< T > &A, T B)
Definition: ionStandardLibrary.h:128
bool CheckMapAccess(map< T, U > const &Map, T const Key)
Definition: ionStandardLibrary.h:87
Helper methods for dealing with files.
Definition: ionStandardLibrary.h:150
static bool BeginsWith(string const &s, string const &prefix, string &remainder)
Definition: ionStandardLibrary.h:229