This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

How to port from libg++ 2.7.2's libstdc++ to egcs'



I'm not really a C++ programmer, but rather a user of C++ code. I have a bunch 
of C++ code, i.e., developed with GNU's c++ and the libstdc++ from 2.7.2, and 
of course I cannot compile it using the egcs' c++ compiler (new standard etc.).

The question I have is, I guess, this; how does one ``port'' such old designed 
code into the new design?

As I said, I'm no C++ programmer, and I guess I shouldn't bother then, right?

I'm running egcs' c++ with the same options as GNU's c++-2.7.2, -c 
-fno-implicit-templates -Wall -ansi -Winline -pipe -fno-nonnull-objects 
-Woverloaded-virtual -pedantic -W -O2 -g.

Let me point you to an example. In a piece of code I'm using, there are these 
problems:

/home/chj/egcs/src/dv90-0.9beta/libdv90i/include/Dv90ObjCntPtr.h:19: parse 
error before `<'
In file included from /home/chj/egcs/src/dv90-0.9beta/libdv90i/include/Dv90Pola
rArea.h:7,
                 from Dv90PolarArea.cc:1:
/home/chj/egcs/src/dv90-0.9beta/libdv90i/include/Dv90Bearings.h:87: `struct 
Dv90ObjCntPtr' redeclared as different kind of symbol
/home/chj/egcs/src/dv90-0.9beta/libdv90i/include/Dv90ObjCntPtr.h:104: previous 
declaration of `template <class T> class Dv90ObjCntPtr<T>'
/home/chj/egcs/src/dv90-0.9beta/libdv90i/include/Dv90ObjCntPtr.h: In method 
`class F77Array2d<int> & Dv90ObjCntPtr<F77Array2d<int> >::operator *() const':
/home/chj/egcs/src/dv90-0.9beta/libdv90i/include/Dv90ObjCntPtr.h:266: member 
`the_p' is private
/home/chj/egcs/src/dv90-0.9beta/libdv90i/include/Dv90ObjCntPtr.h:266: warning: 
control reaches end of non-void function `Dv90ObjCntPtr<F77Array2d<int> >
::operator *() const'
make[1]: *** [Dv90PolarArea.o] Error 1

There's obviously a problem with the template definition in the file 
Dv90ObjCntPtr.h on lines 16 thru 19, especially 19. Perhaps, the problems in 
Dv90Bearings.h on line 87 and in Dv90ObjCntPtr.h on line 104 really follows 
from the previous problem, but I don't know.

Then, an other re-occurring problem shows up in Dv90ObjCntPtr.h lines 265 and 
266, the dereferencing operator has the pointer private. But, then again, 
perhaps the problem follows from the first problem mentioned.

Well, my issue is, here, not my specific problems, but the main question is 
rather what fools like I could do? Could we be held in the hand by some 
document or can't we do anything? (Get a *real* C++ programmer?)

TIA for any discussion, or even help on my specific problem.

/ChJ
// -*- C++ -*-
#ifndef _Dv90Bearings_h_
#define _Dv90Bearings_h_
// ------------------------------------------------------------------

#include "Dv90MathBearing.h"
#include "Dv90MapBearing.h"
#include "Dv90ArrayCntPtr.h"

// ------------------------------------------------------------------

// This class holds a vector of bearings. The constructor takes either a
// single argument theAngle, or the arguments startAngle, angleStep, and
// stopAngle.  In the construction process, the angleStep is adjusted so that
// the angle stopAngle - startAngle will become an integer multiple of
// AngleStep.  Two vectors with all the bearings
// (startAngle:angleStep:stopAngle) are also created.  One, acceccible via
// angleVec(), has elements of type Dv90MathAngle.  In the other one, reached
// by angleVectF77(), the elements are of the built-in type F77Real, and given
// in degrees. No automatic unit conversion will therfore be possible for the
// latter vector.  The vectors are pointed to by countable object pointers,
// which can be copied safe.

// ------------------------------------------------------------------

class Dv90Bearings 
{
public:
  // ----------------------------------------------------------------
  // Constructors.

  Dv90Bearings();
  Dv90Bearings(const Dv90MathBearing&  theAngle);
  Dv90Bearings(const Dv90MathBearing&  startAngle,
	       const Dv90MathAngle&    angleStep,
	       const Dv90MathBearing&  stopAngle);

  // ----------------------------------------------------------------
  // Functions for accessing data.

  const Dv90MathBearing&           startAngle()    const;
  const Dv90MathBearing&           stopAngle()     const;
  const Dv90MathAngle&             angleStep()     const;
  int                              numOfAngles()   const;
  Dv90ConstMathBearingArrayCntPtr  angleVect()     const;
  F77ConstRealArrayCntPtr          angleVectF77()  const;

  // ----------------------------------------------------------------
  // Equivalence operators.

  int operator==(const Dv90Bearings& rhs) const;
  int operator!=(const Dv90Bearings& rhs) const;

  // ----------------------------------------------------------------
  // Other public functions.

  int              inSector(const Dv90MathBearing& )       const;
  Dv90MathBearing  nearestAngle(const Dv90MathBearing& )   const;
  Dv90MathBearing  nearestAngleGt(const Dv90MathBearing& ) const;
  Dv90Bearings     sectorCut(const Dv90Bearings& other)    const;

private:
  // ----------------------------------------------------------------
  // Constructors. (To prevent direct conversion from doubles.)

  Dv90Bearings(double, double, double);

  // ----------------------------------------------------------------
  // Utility functions.

  void          checkInput();
  void          adjustStep();
  void          initAngleVect();
  void          initAngleVectF77();
  int           sameDirAs(const Dv90Bearings& other)  const;
  Dv90MathAngle angleDiff(const Dv90MathBearing& lhs, 
			  const Dv90MathBearing& rhs) const;
  
  // ----------------------------------------------------------------
  // Data hold by the class.

  Dv90MathBearing             startAngle_;
  Dv90MathBearing             stopAngle_;
  Dv90MathAngle               angleStep_;
  Dv90MathAngle               diffAngle_;
  int                         numOfAngles_;
  Dv90MathBearingArrayCntPtr  angleVect_;
  F77RealArrayCntPtr          angleVectF77_;

// ----- End of class Dv90Bearings -----
};

// ==================================================================
// Global functions.
// ==================================================================

// ----------------------------------------------------------------
// I/O-operators.

extern ostream& operator << (ostream& os, const Dv90Bearings& a);


// ==================================================================
#endif  // _Dv90Bearings_h_

// -*- C++ -*-
#ifndef _Dv90ObjCntPtr_h_
#define _Dv90ObjCntPtr_h_

#include "Dv90RefCount.h"

// -----------------------------------------------------------------
// Declaration of class Dv90ConstObjCntPtr<T>
// -----------------------------------------------------------------
// This is a counting pointer class holding a reference to a constant object.
// Declaring an object of this class constant protects the reference from
// beeing altered. A non-constant reference to an object of this class
// can not be used for changing the heap object pointed at.
// -----------------------------------------------------------------

template<class T>
class Dv90ConstObjCntPtr
{
friend class Dv90ObjCntPtr<T>;

public:
  // -----------------------------------------------------------------
  // Constructors.

  Dv90ConstObjCntPtr();                    // Construct as null pointer.
  Dv90ConstObjCntPtr(T* just_newed);       // Init. with ptr. at heap.
  Dv90ConstObjCntPtr(const Dv90ConstObjCntPtr<T>& other); // Copy constr.

  // -----------------------------------------------------------------
  // Destructor.

  ~Dv90ConstObjCntPtr();              // Decrease count, destroy if 0.

  // -----------------------------------------------------------------
  // Assignment operators.

  Dv90ConstObjCntPtr<T>& operator = (const Dv90ConstObjCntPtr<T>& rhs);
						       //Adjust counts.
  Dv90ConstObjCntPtr<T>& operator = (T*);	       // Decrease lhs count.

  // -----------------------------------------------------------------
  // Dereferencing operator.

  const T& operator*() const;			       // Access object.

  // -----------------------------------------------------------------
  // Member selector operator.

  const T*  operator -> () const;

  // -----------------------------------------------------------------
  // Utility functions.

  int unique()      const;			  // Is count one?
  int isNull()      const;			  // Is null pointer?
  int operator ! () const;                        // Not operator.
  operator void* () const;                        // Conversion operator.

  // With the compiler g++ version 2.6.3
  // this conversion operator had to be eliminated because of the compilers
  // implicite conversion of void pointers (void*) to class pointers (T*).
  // C++ should not, unlike C, allow such conversion.
  // I we try to initialize a Dv90ObjCntPtr<T> object with an object of type
  // Dv90ConstObjCntPtr<T>, having this operator enabled, the compiler
  // will initialize the object with the Dv90ObjCnt(T* just_newed)
  // constructor, using the other object, converted to a void pointer, as
  // the just_newed pointer. Of course, this proceeding will be disastrous.
  //
  // With g++ 2.7.2 it seems to work ok.
  // -----------------------------------------------------------------
  // Relation operators.
  
friend int operator == (const Dv90ConstObjCntPtr<T>& lhs, 
			const Dv90ConstObjCntPtr<T>& rhs);

friend int operator != (const Dv90ConstObjCntPtr<T>& lhs, 
			const Dv90ConstObjCntPtr<T>& rhs);

private:
  // -----------------------------------------------------------------
  // Member data.

  Dv90RefCount refCount;  // Number of pointers to heap object.
  T*           the_p;     // The real bult-in pointer.

  // ----------------------------------------------------------------
  // I/O-operators.

friend ostream& operator << (ostream& os, const Dv90ConstObjCntPtr<T>& obj);
};
  // ----- End of class Dv90ConstObjCntPtr -----

// =================================================================

// -----------------------------------------------------------------
// Declaration of class Dv90ConstCntPtr<T>
// -----------------------------------------------------------------
// This class extends the Dv90ConstObjCntPtr<T> class by letting the
// overriding dereferencing operator and member selector operator returning
// non-constant references.
// -----------------------------------------------------------------

template<class T>
class Dv90ObjCntPtr : public Dv90ConstObjCntPtr<T>
{
public:
  // -----------------------------------------------------------------
  // Constructors.

  Dv90ObjCntPtr();                   // Construct as null pointer.
  Dv90ObjCntPtr(T* just_newed);      // Construct with pointer at heap object.
  Dv90ObjCntPtr(const Dv90ObjCntPtr<T>& other);   // Copy constructor.

  // -----------------------------------------------------------------
  // Assignment operators.

  Dv90ObjCntPtr<T>& operator = (const Dv90ObjCntPtr<T>& rhs);
						       // Adjust counts.
  Dv90ObjCntPtr<T>& operator = (T*);		       // Decrease lhs count.

  // -----------------------------------------------------------------
  // Dereferencing operator.

  T& operator*() const;				       // Access object.

  // -----------------------------------------------------------------
  // Member selector operator.

  T*  operator -> () const;

  // ----------------------------------------------------------------
  // I/O-operators.

friend ostream& operator << (ostream& os, const Dv90ObjCntPtr<T>& obj);
 
  // ----- End of class Dv90ObjCntPtr -----
};
// =================================================================


// =================================================================
// Inline definitions for Dv90ConstObjCntPtr<T>
// =================================================================

// -----------------------------------------------------------------
// Constructors.

template<class T> inline
Dv90ConstObjCntPtr<T>::Dv90ConstObjCntPtr() : the_p(0) {}

template<class T> inline
Dv90ConstObjCntPtr<T>::Dv90ConstObjCntPtr(T* just_newed) : the_p(just_newed) {}

template<class T> inline
Dv90ConstObjCntPtr<T>::Dv90ConstObjCntPtr(const Dv90ConstObjCntPtr<T>& other)
{
  the_p    = other.the_p;
  refCount = other.refCount;
}

// -----------------------------------------------------------------
// Destructor.

template<class T> inline
Dv90ConstObjCntPtr<T>::~Dv90ConstObjCntPtr()
{
  if (refCount.unique()) delete the_p;
}

// -----------------------------------------------------------------
// Dereferencing operator.

template<class T> inline
const T& Dv90ConstObjCntPtr<T>::operator *() const {return *the_p;}

// -----------------------------------------------------------------
// Member selecting operator.

template<class T> inline
const T* Dv90ConstObjCntPtr<T>::operator->() const {return the_p;}

// -----------------------------------------------------------------
// Utility functions.

template<class T> inline
int Dv90ConstObjCntPtr<T>::unique()      const {return refCount.unique();}

template<class T> inline
int Dv90ConstObjCntPtr<T>::isNull()      const {return the_p == 0;}

template<class T> inline
int Dv90ConstObjCntPtr<T>::operator ! () const {return isNull();}

template<class T> inline
Dv90ConstObjCntPtr<T>::operator void*() const{return (void*)(isNull()?0:this);}

// -----------------------------------------------------------------
// Relation operators.
  
template<class T> inline
int operator == (const Dv90ConstObjCntPtr<T>& lhs,
		 const Dv90ConstObjCntPtr<T>& rhs)
{
  return lhs.the_p == rhs.the_p;
}

template<class T> inline
int operator != (const Dv90ConstObjCntPtr<T>& lhs, 
		 const Dv90ConstObjCntPtr<T>& rhs)
{
  return lhs.the_p != rhs.the_p;
}
// ----------------------------------------------------------------
// I/O-operators.

template<class T> inline
ostream& operator << (ostream& os, const Dv90ConstObjCntPtr<T>& obj)
{
  if (!os.opfx()) return os;
  os << "the_p: "    << obj.the_p    <<  "; " << "refCount: " << obj.refCount;
  os.osfx();
  return os;
}

// -----------------------------------------------------------------

// =================================================================
// Inline definitions for Dv90ObjCntPtr<T>
// =================================================================

// -----------------------------------------------------------------
// Constructors.

template<class T> inline
Dv90ObjCntPtr<T>::Dv90ObjCntPtr() {}

template<class T> inline
Dv90ObjCntPtr<T>::Dv90ObjCntPtr(T* just_newed)
: Dv90ConstObjCntPtr<T>(just_newed) {}

template<class T> inline
Dv90ObjCntPtr<T>::Dv90ObjCntPtr(const Dv90ObjCntPtr<T>& other)
: Dv90ConstObjCntPtr<T>(other) {}

// -----------------------------------------------------------------
// Assignment operators.

template<class T> inline
Dv90ObjCntPtr<T>& Dv90ObjCntPtr<T>::operator = (const Dv90ObjCntPtr<T>& rhs)
{ 
  Dv90ConstObjCntPtr<T>::operator=(rhs); 
  return *this; 
}

template<class T> inline
Dv90ObjCntPtr<T>& Dv90ObjCntPtr<T>::operator = (T* rhs)
{
  Dv90ConstObjCntPtr<T>::operator=(rhs); 
  return *this; 
}

// -----------------------------------------------------------------
// Dereferencing operator.

template<class T> inline
T& Dv90ObjCntPtr<T>::operator *() const {return *the_p;}

// -----------------------------------------------------------------
// Member selecting operator.

template<class T> inline
T* Dv90ObjCntPtr<T>::operator->() const {return the_p;}

// ----------------------------------------------------------------
// I/O-operators.

template<class T> inline
ostream& operator << (ostream& os, const Dv90ObjCntPtr<T>& obj)
{
  if (!os.opfx()) return os;
  os << (Dv90ConstObjCntPtr<T>) obj;
  os.osfx();
  return os;
}
// ==================================================================
#endif  // _Dv90ObjCntPtr_h_




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]