libidx
/home/rex/ebltrunk/core/libidx/include/stl.h
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Pierre Sermanet   *
00003  *   pierre.sermanet@gmail.com   *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source 
00014  *       Initiative (http://www.opensource.org) must display the 
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef STL_H_
00034 #define STL_H_
00035 
00036 #include "defines.h"
00037 
00038 #ifdef __NOSTL__
00039 #include <string.h>
00040 #include <stdio.h>
00041 #else // using STL
00042 #include <string>
00043 #include <iostream>
00044 #include <sstream>
00045 #include <vector>
00046 #include <list>
00047 #include <algorithm>
00048 #endif
00049 
00050 namespace ebl {
00051 
00053   template <typename T> class idx;
00054   class eblexception;
00055   
00056 }
00057 
00058 namespace std {
00059 
00061   // string
00062 
00063 #ifdef __NOSTL__
00064 
00066   class EXPORT string {
00067   public:
00068     string();
00069     string(char *v);
00070     string(const char *v);
00071     virtual ~string();
00072 
00074     const char *c_str() const;
00075     
00077     size_t size() const;
00078     
00080     size_t length() const;
00081 
00083     void clear();
00084     
00086     inline bool resize(size_t newsize);
00087     
00088     string& operator+=(char *v);
00089     string& operator+=(const char *v);
00090     string& operator+=(const string &v);
00091     
00092     string& operator=(const char *v);
00093     string& operator=(char *v);
00094     
00095     string& operator<<(char *v);
00096     string& operator<<(const char *v);
00097     string& operator<<(const string &v);
00098     string& operator<<(void *v);
00099     string& operator<<(int v);
00100     string& operator<<(uint v);
00101     string& operator<<(char v);
00102     string& operator<<(unsigned char v);
00103     string& operator<<(float v);
00104     string& operator<<(double v);
00105     string& operator<<(long v);
00106 /* #ifndef __ANDROID__ */
00107 /*     string& operator<<(size_t v); */
00108 /* #endif */
00109 
00110   protected:
00111     char *s;
00112   };
00113 
00114 #else // extending STL
00115   
00117   template<class T>
00118     EXPORT string& operator<<(string& e, const T v);
00119 
00121   template<>
00122     EXPORT string& operator<<(string& e, void *v);
00123   template<>
00124     EXPORT string& operator<<(string& e, const int v);
00125   template<>
00126     EXPORT string& operator<<(string& e, const uint v);
00127   template<>
00128     EXPORT string& operator<<(string& e, const char v);
00129   template<>
00130     EXPORT string& operator<<(string& e, const unsigned char v);
00131   template<>
00132     EXPORT string& operator<<(string& e, const float v);
00133   template<>
00134     EXPORT string& operator<<(string& e, const double v);
00135   template<>
00136     EXPORT string& operator<<(string& e, const long v); 
00137   template<>
00138     EXPORT string& operator<<(string& e, const ebl::intg v); 
00139 #ifndef __ANDROID__
00140   template<>
00141     EXPORT string& operator<<(string& e, const size_t &v);
00142 #endif
00143     
00144 #endif
00145 
00146   // extending string
00147 
00149   EXPORT string strip_num(const string &e);
00150     
00152   EXPORT string strip_last_num(const string &e);
00153 
00155   EXPORT string return_last_num(const string &e);
00156     
00159   EXPORT string string_replace(const string &s,
00160                                const char *toreplace, 
00161                                const char *replacement);
00162 
00164   // ostream
00165     
00166 #ifdef __NOSTL__
00167   
00168   class EXPORT ostream;
00169 
00170   // global variables declaration
00171   extern IDXEXPORT ostream cout;
00172   extern IDXEXPORT ostream cerr;
00173   enum t_endl { };
00174   extern IDXEXPORT t_endl endl;
00175   
00177   class EXPORT ostream {
00178   public:
00179     ostream(FILE *fp = stdout);
00180     virtual ~ostream();
00181 
00183     template<class T> ostream& operator<<(const T &v);
00184     template<class T> ostream& operator<<(T &v);
00185     ostream& operator<<(const ebl::eblexception &v);
00186     ostream& operator<<(ebl::eblexception &v);
00187     ostream& operator<<(string &v);
00188     ostream& operator<<(const string &v);
00189     ostream& operator<<(void *v);
00190     ostream& operator<<(char *v);
00191     ostream& operator<<(const char *v);
00192     ostream& operator<<(t_endl v);
00193     ostream& operator<<(short v);
00194     ostream& operator<<(char v);
00195     ostream& operator<<(unsigned char v);
00196     ostream& operator<<(float v);
00197     ostream& operator<<(int v);
00198     ostream& operator<<(uint v);
00199     ostream& operator<<(double v);
00200     ostream& operator<<(long v);
00201 #ifndef __ANDROID__
00202     ostream& operator<<(size_t v);
00203 #endif
00204     
00205   private:
00206     FILE *fp;
00207 #ifdef __ANDROID__
00208     string buf;
00209 #endif
00210   };
00211   
00212 #endif /* __NOSTL__ */
00213 
00215   // vector
00216 
00217 #ifdef __NOSTL__
00218 
00220   template <typename T>
00221     class EXPORT vector : public ebl::idx<T> {
00222   public:
00223     vector();
00224     vector(size_t n, const T value = 0);
00225     virtual ~vector();
00226 
00228     //    void push_back(T &e);
00229     
00231     void push_back(T e);    
00233     size_t size() const;    
00235     size_t length() const;
00237     bool empty() const;
00239     void clear();
00241     T& operator[](size_t i);
00243     const T& operator[](size_t i) const;
00244   };
00245 
00246 #endif
00247 
00249   template<class T>
00250     EXPORT string& operator<<(string &o, const vector<T> &v);  
00252   template<class T>
00253     EXPORT ostream& operator<<(ostream &o, const vector<T> &v);  
00255   template<class T>
00256     EXPORT string& operator<<(string &o, const list<T> &v);  
00258   template<class T>
00259     EXPORT ostream& operator<<(ostream &o, const list<T> &v);
00260   
00262   // min/max
00263 
00264 #ifdef __NOSTL__
00265 
00266   template <typename T>
00267     inline T min(const T &e1, const T &e2);
00268   
00269   template <typename T>
00270     inline T max(const T &e1, const T &e2);
00271 
00272 #endif
00273 
00275 
00278   class EXPORT file {
00279   public:
00282     file(const char *filename, const char *mode);
00284     virtual ~file();
00286     FILE *get_fp();
00288     void incr_ref();
00290     void decr_ref();
00292     bool no_references();
00294     uint references();
00295     
00296   protected:
00297     FILE *fp;
00298     uint refcounter; 
00299   };
00300 
00301 } // end namespace std
00302 
00303 namespace ebl {
00304 
00306   // error reporting
00307 
00309   class EXPORT eblexception : public std::string {
00310   public:
00311     eblexception();
00312     eblexception(const std::string &s);
00313     virtual ~eblexception();
00314   };
00315 
00316 } // end namespace ebl
00317 
00318 #include "stl.hpp"
00319 
00320 #endif /* STL_H_ */