Skip to content

Commit

Permalink
Change IexBaseExc to no longer derive from std::string, but instead i…
Browse files Browse the repository at this point in the history
…nclude it

as a member variable. This resolves a problem with MSVC 2012 and dllexport-ing
template classes.
  • Loading branch information
Halfdan Ingvarsson authored and nickrasmussen committed Aug 8, 2018
1 parent dd86766 commit fa59776
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 80 deletions.
75 changes: 68 additions & 7 deletions IlmBase/Iex/IexBaseExc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,31 @@ stackTracer ()


BaseExc::BaseExc (const char* s) throw () :
std::string (s? s: ""),
_what (s? s: ""),
_stackTrace (currentStackTracer? currentStackTracer(): "")
{
// empty
}


BaseExc::BaseExc (const std::string &s) throw () :
std::string (s),
_what (s),
_stackTrace (currentStackTracer? currentStackTracer(): "")
{
// empty
}


BaseExc::BaseExc (std::stringstream &s) throw () :
std::string (s.str()),
_what (s.str()),
_stackTrace (currentStackTracer? currentStackTracer(): "")
{
// empty
}


BaseExc::BaseExc (const BaseExc &be) throw () :
std::string (be),
_what (be._what),
_stackTrace (be._stackTrace)
{
// empty
Expand All @@ -115,24 +115,85 @@ BaseExc::~BaseExc () throw ()
const char *
BaseExc::what () const throw ()
{
return c_str();
return _what.c_str();
}


BaseExc &
BaseExc::assign (std::stringstream &s)
{
std::string::assign (s.str());
_what.assign (s.str());
return *this;
}

BaseExc &
BaseExc::append (std::stringstream &s)
{
std::string::append (s.str());
_what.append (s.str());
return *this;
}

//-----------------
// Inline functions
//-----------------

const std::string &
BaseExc::name() const
{
return _what;
}

BaseExc &
BaseExc::operator = (std::stringstream &s)
{
return assign (s);
}


BaseExc &
BaseExc::operator += (std::stringstream &s)
{
return append (s);
}


BaseExc &
BaseExc::assign (const char *s)
{
_what.assign(s);
return *this;
}


BaseExc &
BaseExc::operator = (const char *s)
{
return assign(s);
}


BaseExc &
BaseExc::append (const char *s)
{
_what.append(s);
return *this;
}


BaseExc &
BaseExc::operator += (const char *s)
{
return append(s);
}


const std::string &
BaseExc::stackTrace () const
{
return _stackTrace;
}


IEX_INTERNAL_NAMESPACE_SOURCE_EXIT


Expand Down
98 changes: 26 additions & 72 deletions IlmBase/Iex/IexBaseExc.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,49 +72,50 @@ IEX_INTERNAL_NAMESPACE_HEADER_ENTER
// Our most basic exception class
//-------------------------------

class BaseExc: public std::string, public std::exception
class IEX_EXPORT BaseExc: public std::exception
{
public:

//----------------------------
// Constructors and destructor
//----------------------------

IEX_EXPORT BaseExc (const char *s = 0) throw(); // std::string (s)
IEX_EXPORT BaseExc (const std::string &s) throw(); // std::string (s)
IEX_EXPORT BaseExc (std::stringstream &s) throw(); // std::string (s.str())
BaseExc (const char *s = 0) throw(); // std::string (s)
BaseExc (const std::string &s) throw(); // std::string (s)
BaseExc (std::stringstream &s) throw(); // std::string (s.str())

IEX_EXPORT BaseExc (const BaseExc &be) throw();
IEX_EXPORT virtual ~BaseExc () throw ();
BaseExc (const BaseExc &be) throw();
virtual ~BaseExc () throw ();

//--------------------------------------------
// what() method -- e.what() returns e.c_str()
//--------------------------------------------

IEX_EXPORT virtual const char * what () const throw ();
virtual const char * what () const throw ();
const std::string & name() const;


//--------------------------------------------------
// Convenient methods to change the exception's text
//--------------------------------------------------

IEX_EXPORT BaseExc & assign (std::stringstream &s); // assign (s.str())
IEX_EXPORT BaseExc & operator = (std::stringstream &s);
BaseExc & assign (std::stringstream &s); // assign (s.str())
BaseExc & operator = (std::stringstream &s);

IEX_EXPORT BaseExc & append (std::stringstream &s); // append (s.str())
IEX_EXPORT BaseExc & operator += (std::stringstream &s);
BaseExc & append (std::stringstream &s); // append (s.str())
BaseExc & operator += (std::stringstream &s);


//--------------------------------------------------
// These methods from the base class get obscured by
// the definitions above.
//--------------------------------------------------

IEX_EXPORT BaseExc & assign (const char *s);
IEX_EXPORT BaseExc & operator = (const char *s);
BaseExc & assign (const char *s);
BaseExc & operator = (const char *s);

IEX_EXPORT BaseExc & append (const char *s);
IEX_EXPORT BaseExc & operator += (const char *s);
BaseExc & append (const char *s);
BaseExc & operator += (const char *s);


//--------------------------------------------------
Expand All @@ -124,10 +125,18 @@ class BaseExc: public std::string, public std::exception
// has been installed (see below, setStackTracer()).
//--------------------------------------------------

IEX_EXPORT const std::string & stackTrace () const;
const std::string & stackTrace () const;

private:

//--------------------------------------------------
// Conversion operators.
//--------------------------------------------------
operator const char *() const
{ return what(); }


private:
std::string _what;
std::string _stackTrace;
};

Expand Down Expand Up @@ -219,61 +228,6 @@ IEX_EXPORT void setStackTracer (StackTracer stackTracer);
IEX_EXPORT StackTracer stackTracer ();


//-----------------
// Inline functions
//-----------------

inline BaseExc &
BaseExc::operator = (std::stringstream &s)
{
return assign (s);
}


inline BaseExc &
BaseExc::operator += (std::stringstream &s)
{
return append (s);
}


inline BaseExc &
BaseExc::assign (const char *s)
{
std::string::assign(s);
return *this;
}


inline BaseExc &
BaseExc::operator = (const char *s)
{
return assign(s);
}


inline BaseExc &
BaseExc::append (const char *s)
{
std::string::append(s);
return *this;
}


inline BaseExc &
BaseExc::operator += (const char *s)
{
return append(s);
}


inline const std::string &
BaseExc::stackTrace () const
{
return _stackTrace;
}


IEX_INTERNAL_NAMESPACE_HEADER_EXIT

#endif // INCLUDED_IEXBASEEXC_H
2 changes: 1 addition & 1 deletion IlmBase/IexTest/testBaseExc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ test5()
}
catch (const IEX_INTERNAL_NAMESPACE::ArgExc &e)
{
assert (e == "ArgExc");
assert (std::string(e.what()) == "ArgExc");
}
}

Expand Down

0 comments on commit fa59776

Please sign in to comment.