tinyxml2 icon indicating copy to clipboard operation
tinyxml2 copied to clipboard

Errorcode enum name and strong type

Open jdlugosz opened this issue 14 years ago • 3 comments

Example: XMLDocument::Parse returns an int, not an enumeration type. The error codes are in fact declared using an enum, but it is unnamed so I can't use that type name myself, either. Besides declaring the various functions strongly to return the error code type (not some random integer value such as the number of nodes created), having the enum as a proper named type would let me map the error code to an exception in a standard way, using std::system_error (or boost's version as the case may be). As low-hanging fruit with some benefit to users of the code, there is no reason not to give the enum containing XML_NO_ERROR etc. a declared name. Even without strengthening the return types of the functions that return error values yet, it would still be a useful step.

jdlugosz avatar Nov 04 '12 19:11 jdlugosz

I can try that out. Opening an enhancement. Pull request is welcome.

leethomason avatar Nov 06 '12 21:11 leethomason

Worked out. Checked in.

leethomason avatar Nov 12 '12 23:11 leethomason

Thanks. Here is an example of how that enum type can then be used for C++11 style exceptions. It's not exactly reusable for everyone since you would have to configure whether you are using Boost types, std types, or some combination thereof. But it can be a good documentation example:

#include "tinyxml2.h"  // found on include path under Platform

// ===== Boilerplate for error_code enumeration =====

namespace boost {
namespace system {

// must be in same namespace as the master template definition
template<> struct is_error_code_enum< tinyxml2::XMLError >
{
  static const bool value = true;
};

}}


namespace tinyxml2 {

namespace internal {

class error_category : public boost::system::error_category
{
public:
  const char* name() const
  {
    return "tinyxml2 error";
  }

  std::string message(int value) const
  {
    // There is no function in tinyxml2 to give a string representation of the error code.
    // So if you need to know, match this decimal number against the enum position
    // in XMLError found in tinyxml2.h.
    boost::format fmt ("tinyXML2 error code %d");
    fmt % value;
    return fmt.str();
  }
};

}

inline
const boost::system::error_category& get_error_category()
 {
 static internal::error_category instance;
 return instance;
 }


// Must be in the same namespace as argument type
inline boost::system::error_code make_error_code(XMLError e)
{
  return boost::system::error_code(
      static_cast<int>(e), get_error_category());
}

}

// ===== end of error_code support =====

You can see the Boost documentation for what such a thing is good for. Note that even without nice readable strings for the error code, it is still very useful in that a program can catch an exception and easily test programmatically that it is some specific error that it might be handling specially, such as a XML_ERROR_EMPTY_DOCUMENT, as the number is still present (not just a log string).

jdlugosz avatar Dec 03 '12 04:12 jdlugosz