source: cpp/common/stl-util.h @ 197

Last change on this file since 197 was 197, checked in by Maciej Komosinski, 10 years ago

GDK used by developers since 1999, distributed on the web since 2002

  • Property svn:eol-style set to native
File size: 1.8 KB
Line 
1// This file is a part of the Framsticks GDK.
2// Copyright (C) 1999-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#include <stdarg.h>
10
11template<typename T, std::size_t N> void push_back(vector<T>& v, T(&d)[N])
12{
13for(unsigned int i=0;i<N;i++)
14        v.push_back(d[i]);
15}
16
17template<typename T> void erase(vector<T>& v, const T& e)
18{
19typename vector<T>::iterator it=std::find(v.begin(),v.end(),e);
20if (it!=v.end())
21        v.erase(it);
22}
23
24template<typename T> void deleteVectorElements(vector<T*>& v)
25{
26for(typename vector<T*>::iterator it=v.begin();it!=v.end();it++)
27        delete *it;
28v.clear();
29}
30
31template<typename T> int findIndex(vector<T>& v, const T& e)
32{
33typename vector<T>::iterator it=find(v.begin(),v.end(),e);
34if (it!=v.end())
35        return &*it-&v.front();
36return -1;
37}
38
39string ssprintf(const char* format, ...);
40string ssprintf_va(const char* format, va_list ap);
41
42string stripExt(const string& filename); // strip extension from filename
43string getFileExt(const string& filename); // get extension (starting with ".") from filename
44
45bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file=true);
46bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file=true);
47bool writeCompleteFile(const char* filename, const std::string& text,bool warn_on_fail=true);
48bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail=true);
49
50template<class T> class DeletingVector  // deletes the elements (pointers) in destructor
51{
52  public:
53std::vector<T*> vector;
54~DeletingVector()
55        {
56        for(int i=vector.size()-1;i>=0;i--)
57                delete vector[i];
58        }
59T* operator[](int i) {return vector[i];}
60int size() {return vector.size();}
61void push_back(T* x) {vector.push_back(x);}
62};
63
64#endif
Note: See TracBrowser for help on using the repository browser.