From e0b7ed05b23f828aa1e4415f2074b4c152b0a9be Mon Sep 17 00:00:00 2001 From: Felix Natter Date: Fri, 8 Sep 2000 20:30:24 +0000 Subject: [PATCH] porting-howto.html: New version. 2000-09-08 Felix Natter * docs/17_intro/porting-howto.html: New version. From-SVN: r36275 --- libstdc++-v3/ChangeLog | 4 + libstdc++-v3/docs/17_intro/porting-howto.html | 244 ++++++++++++++++++++++++-- 2 files changed, 232 insertions(+), 16 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index ad0d3d243cb..f50c647a5be 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,7 @@ +2000-09-08 Felix Natter + + * docs/17_intro/porting-howto.html: New version. + 2000-09-07 Benjamin Kosnik * config/cpu/i386/bits/atomicity.h (__exchange_and_add): Change unused diff --git a/libstdc++-v3/docs/17_intro/porting-howto.html b/libstdc++-v3/docs/17_intro/porting-howto.html index 4e6422e67c1..54290b328c2 100644 --- a/libstdc++-v3/docs/17_intro/porting-howto.html +++ b/libstdc++-v3/docs/17_intro/porting-howto.html @@ -34,11 +34,17 @@ + second upload to libstdc++-page. + + Revision 0.9Wed Sep 6 02:59:32 2000fnatter + + + 5 new sections. +

- Abstract + Abstract

Some notes on porting applications from libstdc++-2.90 (or earlier @@ -97,7 +103,19 @@ Libc-macros (i.e. isspace from <cctype>) -

7. About... +
7. + State of streams + +
+
8. vector::at is missing (i.e. gcc 2.95.2) +
+
9. Using std::char_traits<char>::eof() +
+
10. Using string::clear()/string::erase() +
+
11. Using stringstream's +
+
12. About...
@@ -119,14 +137,14 @@
  • - +

    wrap your code in namespace std { ... } => This is not an option because only symbols from the standard c++-library are defined in namespace std::.

  • - +

    put a kind of using-declaration in your source (either using namespace std; or i.e. using @@ -135,7 +153,7 @@

  • - +

    use a fully qualified name for each libstdc++-symbol (i.e. std::string, std::cout) => can always be used @@ -240,7 +258,7 @@

    - Table 1. Namespace std:: in Open-Source programs + Table 1. Namespace std:: in Open-Source programs

    @@ -269,7 +287,7 @@

    - Table 2. Notations for categories + Table 2. Notations for categories

    @@ -295,8 +313,7 @@

    As you can see, this currently lacks an example of a project which uses libstdc++-symbols in headers in a back-portable way (except - for Gtk--: see the section on the Gtk-- hack - ). + for Gtk--: see the ).

    @@ -328,10 +345,10 @@

    When using libstdc++-v3, you can use -

    +

    - +
     		  #include <fstream>
     		
    @@ -412,7 +429,7 @@
    • - +

      you cannot do ostream::operator<<(iterator) to print the address of the iterator => use @@ -420,14 +437,14 @@

    • - +

      you cannot clear an iterator's reference (iterator = 0) => use iterator = iterator_type(); ?

    • - +

      if (iterator) won't work any more => use if (iterator != iterator_type()) @@ -446,7 +463,7 @@

      Glibc 2.0.x and 2.1.x define the <ctype.h> -functionality as macros (isspace, isalpha etc.). Libstdc++-v3 "shadows" these macros - as described in the section on C-headers. + as described in the .

      Older implementations of libstdc++ (g++-2 for egcs 1.x and g++-3 @@ -499,9 +516,204 @@ "

    +
    +

    + 7. + State of streams + +

    +

    + At least some older implementations don't have + std::ios_base, so you should use + std::ios::badbit, std::ios::failbit + and std::ios::eofbit and + std::ios::goodbit. +

    +
    +
    +

    + 8. vector::at is missing (i.e. gcc 2.95.2) +

    +

    + For my use, I added it to + prefix/include/g++-3/stl_vector.h: +

    +  reference operator[](size_type __n) { return *(begin() + __n); }
    +  reference at(size_type __n) {
    +    if (begin() + __n >= end())
    +      throw out_of_range("vector::at");
    +    return *(begin() + __n);
    +  }
    +  const_reference operator[](size_type __n) const { return *(begin() + __n); }
    +  const_reference at(size_type __n) const {
    +    if (begin() + __n >= end())
    +      throw out_of_range("vector::at");
    +    return *(begin() + __n);
    +  }
    +	  
    +

    +
    +
    +

    + 9. Using std::char_traits<char>::eof() +

    +

    +

    +		#ifdef HAVE_CHAR_TRAITS
    +		#define CPP_EOF std::char_traits<char>::eof()
    +		#else
    +		#define CPP_EOF EOF
    +		#endif
    +	  
    +

    +
    +
    +

    + 10. Using string::clear()/string::erase() +

    +

    + There are two functions for deleting the contents of a string: + clear and erase (the latter + returns the string). +

    +		void 
    +		clear() { _M_mutate(0, this->size(), 0); }
    +	  
    +
    +      basic_string& 
    +      erase(size_type __pos = 0, size_type __n = npos)
    +      { 
    +		return this->replace(_M_check(__pos), _M_fold(__pos, __n),
    +			     _M_data(), _M_data()); 
    +      }
    +	  
    + The implementation of erase seems to be more + complicated (from libstdc++-v3), but clear is not + implemented in gcc 2.95.2's libstdc++, so you should use + erase (which is probably faster than + operator=(charT*)). +

    +
    +
    +

    + 11. Using stringstream's +

    +

    + Libstdc++-v3 includes the new + i/ostringstream-classes, (<sstream>), but with older + implementations you still have to use i/ostrstream + (<strstream>): +

    +		#ifdef HAVE_SSTREAM
    +		#include <sstream>
    +		#else
    +		#include <strstream>
    +		#endif
    +	  
    +
    +
      +
    • + +

      strstream is considered to be + deprecated +

      +
    • +
    • + +

      strstream is limited to + char +

      +
    • +
    • + +

      with ostringstream you don't + have to take care of terminating the string or freeing its + memory +

      +
    • +
    • + +

      istringstream can be re-filled + (clear(); str(input);) +

      +
    • +
    +
    +

    +

    + You can then use output-stringstreams like this: +

    +		#ifdef HAVE_SSTREAM
    +		std::ostringstream oss;
    +		#else
    +		std::ostrstream oss;
    +		#endif
    +		oss << "Name=" << m_name << ", number=" << m_number << std::endl;
    +		...
    +		#ifndef HAVE_SSTREAM
    +		oss << std::ends; // terminate the char*-string
    +		#endif
    +		// str() returns char* for ostrstream and a string for ostringstream
    +		// this also causes ostrstream to think that the buffer's memory
    +		// is yours
    +		m_label.set_text(oss.str());
    +		#ifndef HAVE_SSTREAM
    +		// let the ostrstream take care of freeing the memory
    +		oss.freeze(false);
    +		#endif
    +	  
    +

    +

    + Input-stringstreams can be used similarly: +

    +		std::string input;
    +		...
    +		#ifdef HAVE_SSTREAM
    +		std::istringstream iss(input);
    +		#else
    +		std::istrstream iss(input.c_str());
    +		#endif
    +		int i;
    +		iss >> i; 
    +	  
    + One (the only?) restriction is that an istrstream cannot be re-filled: +
    +		std::istringstream iss(numerator);
    +		iss >> m_num;
    +		// this is not possible with istrstream
    +		iss.clear();
    +		iss.str(denominator);
    +		iss >> m_den;
    +	  
    + If you don't care about speed, you can put these conversions in + a template-function: +
    +		template <class X>
    +		void fromString(const string& input, X& any)
    +		{
    +		#ifdef HAVE_SSTREAM
    +		std::istringstream iss(input);
    +		#else
    +		std::istrstream iss(input.c_str());
    +		#endif
    +		X temp;
    +		iss >> temp;
    +		if (iss.fail())
    +		   throw runtime_error(..)
    +		any = temp;
    +		}
    +	  
    +

    +

    + I have read the Josuttis book on Standard C++, so some information + comes from there. Additionally, there is information in + "info iostream", which covers the old implementation that gcc 2.95.2 + uses. +

    +

    - 7. About... + 12. About...

    Please send any experience, additions, corrections or questions to -- 2.11.4.GIT