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

Last change on this file since 155 was 140, checked in by sz, 11 years ago

introducing the Foraminifera encoding (format 'F')

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