[libxml++] Use scoped enums (enum class) instead of unscoped enums



commit 46d6e388191a18a50e883a3221844b37eade062a
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Wed Dec 30 19:41:10 2015 +0100

    Use scoped enums (enum class) instead of unscoped enums
    
    * examples/dom_build/main.cc:
    * examples/dom_xpath/main.cc: Update names of enum constants.
    * libxml++/document.[cc|h]:
    * libxml++/nodes/node.[cc|h]:
    * libxml++/parsers/parser.[cc|h]:
    * libxml++/parsers/textreader.[cc|h]: Replace enum by enum class.
    Modify the names of the enum constants. E.g. XML_INTERNAL_GENERAL_ENTITY ->
    XmlEntityType::INTERNAL_GENERAL.
    
    This patch breaks API and ABI. The API and ABI of libxml++ 3.x has not yet
    been frozen.

 examples/dom_build/main.cc     |    2 +-
 examples/dom_xpath/main.cc     |   10 ++++----
 libxml++/document.cc           |    5 ++-
 libxml++/document.h            |   20 +++++++++------
 libxml++/nodes/node.cc         |    4 +-
 libxml++/nodes/node.h          |   12 ++++----
 libxml++/parsers/parser.cc     |   16 ++++++------
 libxml++/parsers/parser.h      |   10 ++++----
 libxml++/parsers/textreader.cc |    8 +++---
 libxml++/parsers/textreader.h  |   51 +++++++++++++++++++++++++---------------
 10 files changed, 78 insertions(+), 60 deletions(-)
---
diff --git a/examples/dom_build/main.cc b/examples/dom_build/main.cc
index 568d944..13e081c 100644
--- a/examples/dom_build/main.cc
+++ b/examples/dom_build/main.cc
@@ -36,7 +36,7 @@ main(int /* argc */, char** /* argv */)
   {
     xmlpp::Document document;
     document.set_internal_subset("example_xml_doc", "", "example_xml_doc.dtd");
-    document.set_entity_declaration("example1", xmlpp::XML_INTERNAL_GENERAL_ENTITY,
+    document.set_entity_declaration("example1", xmlpp::XmlEntityType::INTERNAL_GENERAL,
       "", "example_xml_doc.dtd", "Entity content");
     document.add_processing_instruction("application1", "This is an example document");
     document.add_comment("First comment");
diff --git a/examples/dom_xpath/main.cc b/examples/dom_xpath/main.cc
index c35e409..52c0bd8 100644
--- a/examples/dom_xpath/main.cc
+++ b/examples/dom_xpath/main.cc
@@ -29,12 +29,12 @@ Glib::ustring result_type_to_ustring(xmlpp::XPathResultType result_type)
 {
   switch (result_type)
   {
-    case xmlpp::XPATH_RESULT_NODESET: return "nodeset";
-    case xmlpp::XPATH_RESULT_BOOLEAN: return "boolean";
-    case xmlpp::XPATH_RESULT_NUMBER:  return "number";
-    case xmlpp::XPATH_RESULT_STRING:  return "string";
+    case xmlpp::XPathResultType::NODESET: return "nodeset";
+    case xmlpp::XPathResultType::BOOLEAN: return "boolean";
+    case xmlpp::XPathResultType::NUMBER:  return "number";
+    case xmlpp::XPathResultType::STRING:  return "string";
 
-    case xmlpp::XPATH_RESULT_UNDEFINED:
+    case xmlpp::XPathResultType::UNDEFINED:
     default:
       return "undefined";
   }
diff --git a/libxml++/document.cc b/libxml++/document.cc
index d441b1d..5de36c0 100644
--- a/libxml++/document.cc
+++ b/libxml++/document.cc
@@ -415,10 +415,11 @@ void Document::set_entity_declaration(const Glib::ustring& name, XmlEntityType t
                               const Glib::ustring& publicId, const Glib::ustring& systemId,
                               const Glib::ustring& content)
 {
-  auto entity = xmlAddDocEntity( impl_, (const xmlChar*) name.c_str(), type,
+  auto entity = xmlAddDocEntity(impl_, (const xmlChar*)name.c_str(),
+    static_cast<int>(type),
     publicId.empty() ? nullptr : (const xmlChar*)publicId.c_str(),
     systemId.empty() ? nullptr : (const xmlChar*)systemId.c_str(),
-    (const xmlChar*) content.c_str() );
+    (const xmlChar*)content.c_str());
   if (!entity)
     throw internal_error("Could not add entity declaration " + name);
 }
diff --git a/libxml++/document.h b/libxml++/document.h
index 23b9d8d..99f3fa5 100644
--- a/libxml++/document.h
+++ b/libxml++/document.h
@@ -39,14 +39,18 @@ extern "C" {
 namespace xmlpp
 {
 
-typedef enum {
-    XML_INTERNAL_GENERAL_ENTITY = 1,
-    XML_EXTERNAL_GENERAL_PARSED_ENTITY = 2,
-    XML_EXTERNAL_GENERAL_UNPARSED_ENTITY = 3,
-    XML_INTERNAL_PARAMETER_ENTITY = 4,
-    XML_EXTERNAL_PARAMETER_ENTITY = 5,
-    XML_INTERNAL_PREDEFINED_ENTITY = 6
-} XmlEntityType;
+// xmlpp::XmlEntityType is similar to xmlEntityType in libxml2.
+/** The valid entity types.
+ */
+enum class XmlEntityType
+{
+  INTERNAL_GENERAL = 1,
+  EXTERNAL_GENERAL_PARSED = 2,
+  EXTERNAL_GENERAL_UNPARSED = 3,
+  INTERNAL_PARAMETER = 4,
+  EXTERNAL_PARAMETER = 5,
+  INTERNAL_PREDEFINED = 6
+};
 
 //TODO: Make Document inherit from Node, when we can break ABI one day?
 //
diff --git a/libxml++/nodes/node.cc b/libxml++/nodes/node.cc
index 46d202e..86a7846 100644
--- a/libxml++/nodes/node.cc
+++ b/libxml++/nodes/node.cc
@@ -155,7 +155,7 @@ xmlXPathObject* eval_common(const Glib::ustring& xpath,
   if (!xpath_value)
   {
     if (result_type)
-      *result_type = xmlpp::XPATH_RESULT_UNDEFINED;
+      *result_type = xmlpp::XPathResultType::UNDEFINED;
 
     throw xmlpp::exception("Invalid XPath: " + xpath);
   }
@@ -168,7 +168,7 @@ xmlXPathObject* eval_common(const Glib::ustring& xpath,
         xpath_value->type == XPATH_STRING)
       *result_type = static_cast<xmlpp::XPathResultType>(xpath_value->type);
     else
-      *result_type = xmlpp::XPATH_RESULT_UNDEFINED;
+      *result_type = xmlpp::XPathResultType::UNDEFINED;
   }
 
   return xpath_value;
diff --git a/libxml++/nodes/node.h b/libxml++/nodes/node.h
index 8e56348..53ea51e 100644
--- a/libxml++/nodes/node.h
+++ b/libxml++/nodes/node.h
@@ -33,13 +33,13 @@ class Element;
  *   - number
  *   - string
  */
-enum XPathResultType
+enum class XPathResultType
 {
-    XPATH_RESULT_UNDEFINED = 0,
-    XPATH_RESULT_NODESET = 1,
-    XPATH_RESULT_BOOLEAN = 2,
-    XPATH_RESULT_NUMBER = 3,
-    XPATH_RESULT_STRING = 4
+  UNDEFINED = 0,
+  NODESET = 1,
+  BOOLEAN = 2,
+  NUMBER = 3,
+  STRING = 4
 };
 
 /** Represents XML Nodes.
diff --git a/libxml++/parsers/parser.cc b/libxml++/parsers/parser.cc
index d4ecdc6..e1b9883 100644
--- a/libxml++/parsers/parser.cc
+++ b/libxml++/parsers/parser.cc
@@ -237,7 +237,7 @@ void Parser::callback_parser_error(void* ctx, const char* msg, ...)
 {
   va_list var_args;
   va_start(var_args, msg);
-  callback_error_or_warning(MsgParserError, ctx, msg, var_args);
+  callback_error_or_warning(MsgType::ParserError, ctx, msg, var_args);
   va_end(var_args);
 }
 
@@ -246,7 +246,7 @@ void Parser::callback_parser_warning(void* ctx, const char* msg, ...)
 {
   va_list var_args;
   va_start(var_args, msg);
-  callback_error_or_warning(MsgParserWarning, ctx, msg, var_args);
+  callback_error_or_warning(MsgType::ParserWarning, ctx, msg, var_args);
   va_end(var_args);
 }
 
@@ -255,7 +255,7 @@ void Parser::callback_validity_error(void* ctx, const char* msg, ...)
 {
   va_list var_args;
   va_start(var_args, msg);
-  callback_error_or_warning(MsgValidityError, ctx, msg, var_args);
+  callback_error_or_warning(MsgType::ValidityError, ctx, msg, var_args);
   va_end(var_args);
 }
 
@@ -264,7 +264,7 @@ void Parser::callback_validity_warning(void* ctx, const char* msg, ...)
 {
   va_list var_args;
   va_start(var_args, msg);
-  callback_error_or_warning(MsgValidityWarning, ctx, msg, var_args);
+  callback_error_or_warning(MsgType::ValidityWarning, ctx, msg, var_args);
   va_end(var_args);
 }
 
@@ -296,16 +296,16 @@ void Parser::callback_error_or_warning(MsgType msg_type, void* ctx,
       {
         switch (msg_type)
         {
-          case MsgParserError:
+          case MsgType::ParserError:
             parser->on_parser_error(ubuff);
             break;
-          case MsgParserWarning:
+          case MsgType::ParserWarning:
             parser->on_parser_warning(ubuff);
             break;
-          case MsgValidityError:
+          case MsgType::ValidityError:
             parser->on_validity_error(ubuff);
             break;
-          case MsgValidityWarning:
+          case MsgType::ValidityWarning:
             parser->on_validity_warning(ubuff);
             break;
         }
diff --git a/libxml++/parsers/parser.h b/libxml++/parsers/parser.h
index a101d60..b0d842d 100644
--- a/libxml++/parsers/parser.h
+++ b/libxml++/parsers/parser.h
@@ -185,12 +185,12 @@ protected:
   static void callback_validity_error(void* ctx, const char* msg, ...);
   static void callback_validity_warning(void* ctx, const char* msg, ...);
 
-  enum MsgType
+  enum class MsgType
   {
-    MsgParserError,
-    MsgParserWarning,
-    MsgValidityError,
-    MsgValidityWarning
+    ParserError,
+    ParserWarning,
+    ValidityError,
+    ValidityWarning
   };
 
   static void callback_error_or_warning(MsgType msg_type, void* ctx,
diff --git a/libxml++/parsers/textreader.cc b/libxml++/parsers/textreader.cc
index 01ff921..66958a9 100644
--- a/libxml++/parsers/textreader.cc
+++ b/libxml++/parsers/textreader.cc
@@ -157,12 +157,12 @@ Glib::ustring TextReader::get_namespace_uri() const
       xmlTextReaderNamespaceUri(impl_), true);
 }
 
-TextReader::xmlNodeType TextReader::get_node_type() const
+TextReader::NodeType TextReader::get_node_type() const
 {
   int result = xmlTextReaderNodeType(impl_);
   if(result == -1)
     check_for_exceptions();
-  return (xmlNodeType)result;
+  return static_cast<NodeType>(result);
 }
 
 Glib::ustring TextReader::get_prefix() const
@@ -189,12 +189,12 @@ Glib::ustring TextReader::get_xml_lang() const
       xmlTextReaderXmlLang(impl_));
 }
 
-TextReader::xmlReadState TextReader::get_read_state() const
+TextReader::ReadState TextReader::get_read_state() const
 {
   int result = xmlTextReaderReadState(impl_);
   if(result == -1)
     check_for_exceptions();
-  return (xmlReadState)result;
+  return static_cast<ReadState>(result);
 }
 
 void TextReader::close()
diff --git a/libxml++/parsers/textreader.h b/libxml++/parsers/textreader.h
index 15a67f0..2cd34e6 100644
--- a/libxml++/parsers/textreader.h
+++ b/libxml++/parsers/textreader.h
@@ -29,38 +29,47 @@ namespace xmlpp
 class TextReader: public NonCopyable
 {
   public:
-    enum xmlNodeType {
+    // xmlpp::TextReader::NodeType is similar to xmlReaderTypes in libxml2.
+    /** Node type of the current node.
+     * See DotGNU's <a 
href="http://www.gnu.org/software/dotgnu/pnetlib-doc/System/Xml/XmlNodeType.html";>XmlNodeType</a> enum.
+     */
+    enum class NodeType
+    {
+      InternalError = -1,
+      None = 0,
+      Element = 1,
       Attribute = 2,
+      Text = 3,
       CDATA = 4,
+      EntityReference = 5,
+      Entity = 6,
+      ProcessingInstruction = 7,
       Comment = 8,
       Document = 9,
-      DocumentFragment = 11,
       DocumentType = 10,
-      Element = 1,
-      EndElement = 15,
-      EndEntity = 16,
-      Entity = 6,
-      EntityReference = 5,
-      None = 0,
+      DocumentFragment = 11,
       Notation = 12,
-      ProcessingInstruction = 7,
-      SignificantWhitespace = 14,
-      Text = 3,
       Whitespace = 13,
+      SignificantWhitespace = 14,
+      EndElement = 15,
+      EndEntity = 16,
       XmlDeclaration = 17
     };
 
-    enum xmlReadState
+    // xmlpp::TextReader::ReadState is similar to xmlTextReaderMode in libxml2.
+    enum class ReadState
     {
-      Closed = 4,
-      EndOfFile = 3,
-      Error = 2,
+      InternalError = -1,
       Initial = 0,
       Interactive = 1,
+      Error = 2,
+      EndOfFile = 3,
+      Closed = 4,
       Reading = 5
     };
 
-    enum ParserProperties
+    // xmlpp::TextReader::ParserProperties is similar to xmlParserProperties in libxml2.
+    enum class ParserProperties
     {
       LoadDtd = 1,
       DefaultAttrs = 2,
@@ -174,9 +183,13 @@ class TextReader: public NonCopyable
     Glib::ustring get_namespace_uri() const;
 
     /** Get the node type of the current node.
-     * @returns The xmlpp::xmlNodeType of the current node, or -1 in case of error.
+     * @returns The xmlpp::TextReader::NodeType of the current node.
+     *          In case of error, either returns xmlpp::TextReader::NodeType::InternalError
+     *          or throws an exception.
+     * @throws xmlpp::parse_error
+     * @throws xmlpp::validity_error
      */
-    xmlNodeType get_node_type() const;
+    NodeType get_node_type() const;
 
     /** Get the namespace prefix associated with the current node.
      * @returns The namespace prefix, or an empty string if not available.
@@ -191,7 +204,7 @@ class TextReader: public NonCopyable
     Glib::ustring get_value() const;
     Glib::ustring get_xml_lang() const;
 
-    xmlReadState get_read_state() const;
+    ReadState get_read_state() const;
 
     void close();
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]