Code Monkey home page Code Monkey logo

Comments (3)

leethomason avatar leethomason commented on July 17, 2024

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

from tinyxml2.

leethomason avatar leethomason commented on July 17, 2024

Worked out. Checked in.

from tinyxml2.

jdlugosz avatar jdlugosz commented on July 17, 2024

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).

from tinyxml2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.