1 | // This file is a part of the Framsticks GDK. |
---|
2 | // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.framsticks.com/ for further information. |
---|
4 | |
---|
5 | #ifndef _STL_UTIL_H_ |
---|
6 | #define _STL_UTIL_H_ |
---|
7 | |
---|
8 | #include "nonstd_stl.h" |
---|
9 | |
---|
10 | template<typename T, std::size_t N> void push_back(vector<T>& v, T(&d)[N]) |
---|
11 | { |
---|
12 | for(unsigned int i=0;i<N;i++) |
---|
13 | v.push_back(d[i]); |
---|
14 | } |
---|
15 | |
---|
16 | template<typename T> void erase(vector<T>& v, const T& e) |
---|
17 | { |
---|
18 | typename vector<T>::iterator it=std::find(v.begin(),v.end(),e); |
---|
19 | if (it!=v.end()) |
---|
20 | v.erase(it); |
---|
21 | } |
---|
22 | |
---|
23 | template<typename T> void deleteVectorElements(vector<T*>& v) |
---|
24 | { |
---|
25 | for(typename vector<T*>::iterator it=v.begin();it!=v.end();it++) |
---|
26 | delete *it; |
---|
27 | v.clear(); |
---|
28 | } |
---|
29 | |
---|
30 | template<typename T> int findIndex(vector<T>& v, const T& e) |
---|
31 | { |
---|
32 | typename vector<T>::iterator it=find(v.begin(),v.end(),e); |
---|
33 | if (it!=v.end()) |
---|
34 | return &*it-&v.front(); |
---|
35 | return -1; |
---|
36 | } |
---|
37 | |
---|
38 | string ssprintf(const char* format, ...); |
---|
39 | |
---|
40 | string stripExt(const string& filename); // strip extension from filename |
---|
41 | |
---|
42 | bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file=true); |
---|
43 | bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file=true); |
---|
44 | bool writeCompleteFile(const char* filename, const std::string& text,bool warn_on_fail=true); |
---|
45 | bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail=true); |
---|
46 | |
---|
47 | template<class T> class DeletingVector // deletes the elements (pointers) in destructor |
---|
48 | { |
---|
49 | public: |
---|
50 | std::vector<T*> vector; |
---|
51 | ~DeletingVector() |
---|
52 | { |
---|
53 | for(int i=vector.size()-1;i>=0;i--) |
---|
54 | delete vector[i]; |
---|
55 | } |
---|
56 | T* operator[](int i) {return vector[i];} |
---|
57 | int size() {return vector.size();} |
---|
58 | void push_back(T* x) {vector.push_back(x);} |
---|
59 | }; |
---|
60 | |
---|
61 | #endif |
---|