Skip to content

Commit 46d6e38

Browse files
author
Kjell Ahlstedt
committed
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.
1 parent 74475f3 commit 46d6e38

File tree

10 files changed

+78
-60
lines changed

10 files changed

+78
-60
lines changed

examples/dom_build/main.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ main(int /* argc */, char** /* argv */)
3636
{
3737
xmlpp::Document document;
3838
document.set_internal_subset("example_xml_doc", "", "example_xml_doc.dtd");
39-
document.set_entity_declaration("example1", xmlpp::XML_INTERNAL_GENERAL_ENTITY,
39+
document.set_entity_declaration("example1", xmlpp::XmlEntityType::INTERNAL_GENERAL,
4040
"", "example_xml_doc.dtd", "Entity content");
4141
document.add_processing_instruction("application1", "This is an example document");
4242
document.add_comment("First comment");

examples/dom_xpath/main.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ Glib::ustring result_type_to_ustring(xmlpp::XPathResultType result_type)
2929
{
3030
switch (result_type)
3131
{
32-
case xmlpp::XPATH_RESULT_NODESET: return "nodeset";
33-
case xmlpp::XPATH_RESULT_BOOLEAN: return "boolean";
34-
case xmlpp::XPATH_RESULT_NUMBER: return "number";
35-
case xmlpp::XPATH_RESULT_STRING: return "string";
32+
case xmlpp::XPathResultType::NODESET: return "nodeset";
33+
case xmlpp::XPathResultType::BOOLEAN: return "boolean";
34+
case xmlpp::XPathResultType::NUMBER: return "number";
35+
case xmlpp::XPathResultType::STRING: return "string";
3636

37-
case xmlpp::XPATH_RESULT_UNDEFINED:
37+
case xmlpp::XPathResultType::UNDEFINED:
3838
default:
3939
return "undefined";
4040
}

libxml++/document.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,11 @@ void Document::set_entity_declaration(const Glib::ustring& name, XmlEntityType t
415415
const Glib::ustring& publicId, const Glib::ustring& systemId,
416416
const Glib::ustring& content)
417417
{
418-
auto entity = xmlAddDocEntity( impl_, (const xmlChar*) name.c_str(), type,
418+
auto entity = xmlAddDocEntity(impl_, (const xmlChar*)name.c_str(),
419+
static_cast<int>(type),
419420
publicId.empty() ? nullptr : (const xmlChar*)publicId.c_str(),
420421
systemId.empty() ? nullptr : (const xmlChar*)systemId.c_str(),
421-
(const xmlChar*) content.c_str() );
422+
(const xmlChar*)content.c_str());
422423
if (!entity)
423424
throw internal_error("Could not add entity declaration " + name);
424425
}

libxml++/document.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,18 @@ extern "C" {
3939
namespace xmlpp
4040
{
4141

42-
typedef enum {
43-
XML_INTERNAL_GENERAL_ENTITY = 1,
44-
XML_EXTERNAL_GENERAL_PARSED_ENTITY = 2,
45-
XML_EXTERNAL_GENERAL_UNPARSED_ENTITY = 3,
46-
XML_INTERNAL_PARAMETER_ENTITY = 4,
47-
XML_EXTERNAL_PARAMETER_ENTITY = 5,
48-
XML_INTERNAL_PREDEFINED_ENTITY = 6
49-
} XmlEntityType;
42+
// xmlpp::XmlEntityType is similar to xmlEntityType in libxml2.
43+
/** The valid entity types.
44+
*/
45+
enum class XmlEntityType
46+
{
47+
INTERNAL_GENERAL = 1,
48+
EXTERNAL_GENERAL_PARSED = 2,
49+
EXTERNAL_GENERAL_UNPARSED = 3,
50+
INTERNAL_PARAMETER = 4,
51+
EXTERNAL_PARAMETER = 5,
52+
INTERNAL_PREDEFINED = 6
53+
};
5054

5155
//TODO: Make Document inherit from Node, when we can break ABI one day?
5256
//

libxml++/nodes/node.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ xmlXPathObject* eval_common(const Glib::ustring& xpath,
155155
if (!xpath_value)
156156
{
157157
if (result_type)
158-
*result_type = xmlpp::XPATH_RESULT_UNDEFINED;
158+
*result_type = xmlpp::XPathResultType::UNDEFINED;
159159

160160
throw xmlpp::exception("Invalid XPath: " + xpath);
161161
}
@@ -168,7 +168,7 @@ xmlXPathObject* eval_common(const Glib::ustring& xpath,
168168
xpath_value->type == XPATH_STRING)
169169
*result_type = static_cast<xmlpp::XPathResultType>(xpath_value->type);
170170
else
171-
*result_type = xmlpp::XPATH_RESULT_UNDEFINED;
171+
*result_type = xmlpp::XPathResultType::UNDEFINED;
172172
}
173173

174174
return xpath_value;

libxml++/nodes/node.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ class Element;
3333
* - number
3434
* - string
3535
*/
36-
enum XPathResultType
36+
enum class XPathResultType
3737
{
38-
XPATH_RESULT_UNDEFINED = 0,
39-
XPATH_RESULT_NODESET = 1,
40-
XPATH_RESULT_BOOLEAN = 2,
41-
XPATH_RESULT_NUMBER = 3,
42-
XPATH_RESULT_STRING = 4
38+
UNDEFINED = 0,
39+
NODESET = 1,
40+
BOOLEAN = 2,
41+
NUMBER = 3,
42+
STRING = 4
4343
};
4444

4545
/** Represents XML Nodes.

libxml++/parsers/parser.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void Parser::callback_parser_error(void* ctx, const char* msg, ...)
237237
{
238238
va_list var_args;
239239
va_start(var_args, msg);
240-
callback_error_or_warning(MsgParserError, ctx, msg, var_args);
240+
callback_error_or_warning(MsgType::ParserError, ctx, msg, var_args);
241241
va_end(var_args);
242242
}
243243

@@ -246,7 +246,7 @@ void Parser::callback_parser_warning(void* ctx, const char* msg, ...)
246246
{
247247
va_list var_args;
248248
va_start(var_args, msg);
249-
callback_error_or_warning(MsgParserWarning, ctx, msg, var_args);
249+
callback_error_or_warning(MsgType::ParserWarning, ctx, msg, var_args);
250250
va_end(var_args);
251251
}
252252

@@ -255,7 +255,7 @@ void Parser::callback_validity_error(void* ctx, const char* msg, ...)
255255
{
256256
va_list var_args;
257257
va_start(var_args, msg);
258-
callback_error_or_warning(MsgValidityError, ctx, msg, var_args);
258+
callback_error_or_warning(MsgType::ValidityError, ctx, msg, var_args);
259259
va_end(var_args);
260260
}
261261

@@ -264,7 +264,7 @@ void Parser::callback_validity_warning(void* ctx, const char* msg, ...)
264264
{
265265
va_list var_args;
266266
va_start(var_args, msg);
267-
callback_error_or_warning(MsgValidityWarning, ctx, msg, var_args);
267+
callback_error_or_warning(MsgType::ValidityWarning, ctx, msg, var_args);
268268
va_end(var_args);
269269
}
270270

@@ -296,16 +296,16 @@ void Parser::callback_error_or_warning(MsgType msg_type, void* ctx,
296296
{
297297
switch (msg_type)
298298
{
299-
case MsgParserError:
299+
case MsgType::ParserError:
300300
parser->on_parser_error(ubuff);
301301
break;
302-
case MsgParserWarning:
302+
case MsgType::ParserWarning:
303303
parser->on_parser_warning(ubuff);
304304
break;
305-
case MsgValidityError:
305+
case MsgType::ValidityError:
306306
parser->on_validity_error(ubuff);
307307
break;
308-
case MsgValidityWarning:
308+
case MsgType::ValidityWarning:
309309
parser->on_validity_warning(ubuff);
310310
break;
311311
}

libxml++/parsers/parser.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,12 @@ class Parser : public NonCopyable
185185
static void callback_validity_error(void* ctx, const char* msg, ...);
186186
static void callback_validity_warning(void* ctx, const char* msg, ...);
187187

188-
enum MsgType
188+
enum class MsgType
189189
{
190-
MsgParserError,
191-
MsgParserWarning,
192-
MsgValidityError,
193-
MsgValidityWarning
190+
ParserError,
191+
ParserWarning,
192+
ValidityError,
193+
ValidityWarning
194194
};
195195

196196
static void callback_error_or_warning(MsgType msg_type, void* ctx,

libxml++/parsers/textreader.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ Glib::ustring TextReader::get_namespace_uri() const
157157
xmlTextReaderNamespaceUri(impl_), true);
158158
}
159159

160-
TextReader::xmlNodeType TextReader::get_node_type() const
160+
TextReader::NodeType TextReader::get_node_type() const
161161
{
162162
int result = xmlTextReaderNodeType(impl_);
163163
if(result == -1)
164164
check_for_exceptions();
165-
return (xmlNodeType)result;
165+
return static_cast<NodeType>(result);
166166
}
167167

168168
Glib::ustring TextReader::get_prefix() const
@@ -189,12 +189,12 @@ Glib::ustring TextReader::get_xml_lang() const
189189
xmlTextReaderXmlLang(impl_));
190190
}
191191

192-
TextReader::xmlReadState TextReader::get_read_state() const
192+
TextReader::ReadState TextReader::get_read_state() const
193193
{
194194
int result = xmlTextReaderReadState(impl_);
195195
if(result == -1)
196196
check_for_exceptions();
197-
return (xmlReadState)result;
197+
return static_cast<ReadState>(result);
198198
}
199199

200200
void TextReader::close()

libxml++/parsers/textreader.h

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,47 @@ namespace xmlpp
2929
class TextReader: public NonCopyable
3030
{
3131
public:
32-
enum xmlNodeType {
32+
// xmlpp::TextReader::NodeType is similar to xmlReaderTypes in libxml2.
33+
/** Node type of the current node.
34+
* See DotGNU's <a href="http://www.gnu.org/software/dotgnu/pnetlib-doc/System/Xml/XmlNodeType.html">XmlNodeType</a> enum.
35+
*/
36+
enum class NodeType
37+
{
38+
InternalError = -1,
39+
None = 0,
40+
Element = 1,
3341
Attribute = 2,
42+
Text = 3,
3443
CDATA = 4,
44+
EntityReference = 5,
45+
Entity = 6,
46+
ProcessingInstruction = 7,
3547
Comment = 8,
3648
Document = 9,
37-
DocumentFragment = 11,
3849
DocumentType = 10,
39-
Element = 1,
40-
EndElement = 15,
41-
EndEntity = 16,
42-
Entity = 6,
43-
EntityReference = 5,
44-
None = 0,
50+
DocumentFragment = 11,
4551
Notation = 12,
46-
ProcessingInstruction = 7,
47-
SignificantWhitespace = 14,
48-
Text = 3,
4952
Whitespace = 13,
53+
SignificantWhitespace = 14,
54+
EndElement = 15,
55+
EndEntity = 16,
5056
XmlDeclaration = 17
5157
};
5258

53-
enum xmlReadState
59+
// xmlpp::TextReader::ReadState is similar to xmlTextReaderMode in libxml2.
60+
enum class ReadState
5461
{
55-
Closed = 4,
56-
EndOfFile = 3,
57-
Error = 2,
62+
InternalError = -1,
5863
Initial = 0,
5964
Interactive = 1,
65+
Error = 2,
66+
EndOfFile = 3,
67+
Closed = 4,
6068
Reading = 5
6169
};
6270

63-
enum ParserProperties
71+
// xmlpp::TextReader::ParserProperties is similar to xmlParserProperties in libxml2.
72+
enum class ParserProperties
6473
{
6574
LoadDtd = 1,
6675
DefaultAttrs = 2,
@@ -174,9 +183,13 @@ class TextReader: public NonCopyable
174183
Glib::ustring get_namespace_uri() const;
175184

176185
/** Get the node type of the current node.
177-
* @returns The xmlpp::xmlNodeType of the current node, or -1 in case of error.
186+
* @returns The xmlpp::TextReader::NodeType of the current node.
187+
* In case of error, either returns xmlpp::TextReader::NodeType::InternalError
188+
* or throws an exception.
189+
* @throws xmlpp::parse_error
190+
* @throws xmlpp::validity_error
178191
*/
179-
xmlNodeType get_node_type() const;
192+
NodeType get_node_type() const;
180193

181194
/** Get the namespace prefix associated with the current node.
182195
* @returns The namespace prefix, or an empty string if not available.
@@ -191,7 +204,7 @@ class TextReader: public NonCopyable
191204
Glib::ustring get_value() const;
192205
Glib::ustring get_xml_lang() const;
193206

194-
xmlReadState get_read_state() const;
207+
ReadState get_read_state() const;
195208

196209
void close();
197210

0 commit comments

Comments
 (0)