EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
XMLParserEx.h
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Bloody.Rabbit
24 */
25 
26 #ifndef __UTILS__XML_PARSER_EX_H__INCL__
27 #define __UTILS__XML_PARSER_EX_H__INCL__
28 
29 #include "utils/str2conv.h"
30 #include "utils/XMLParser.h"
31 
38 : public XMLParser
39 {
40 public:
41  template< typename T >
43 
44  template< typename T >
45  class ValueParser;
46 
54  template< typename T >
55  void AddMemberParser( const char* name, T& instance, bool ( T::* method )( const TiXmlElement* ) )
56  {
57  AddParser( name, new MemberElementParser< T >( instance, method ) );
58  }
59 
66  template< typename T >
67  void AddValueParser( const char* name, T& value )
68  {
69  AddParser( name, new ValueParser< T >( value ) );
70  }
71 
72 protected:
79  template< typename T >
80  void AddMemberParser( const char* name, bool ( T::* method )( const TiXmlElement* ) )
81  {
82  AddMemberParser< T >( name, static_cast< T& >( *this ), method );
83  }
84 };
85 
91 template< typename T >
93 : public ElementParser
94 {
95 public:
97  typedef T Class;
99  typedef bool ( Class::* Method )( const TiXmlElement* );
100 
107  MemberElementParser( Class& instance, const Method& method )
108  : mInstance( instance ),
109  mMethod( method )
110  {
111  }
112 
121  bool Parse( const TiXmlElement* field )
122  {
123  return ( mInstance.*mMethod )( field );
124  }
125 
126 protected:
128  Class& mInstance;
131 };
132 
138 template< typename T >
140 : public ElementParser
141 {
142 public:
144  typedef T Value;
145 
151  ValueParser( Value& value )
152  : mValue( value )
153  {
154  }
155 
164  bool Parse( const TiXmlElement* field )
165  {
166  // obtain the text element
167  const TiXmlNode* contents = field->FirstChild();
168  if( NULL == contents || TiXmlNode::TINYXML_TEXT != contents->Type() )
169  {
170  sLog.Error( "XMLParser", "Expected a text element in element '%s' at line %d.", field->Value(), field->Row() );
171  return false;
172  }
173 
174  // parse the text
175  mValue = str2< Value >( contents->Value() );
176 
177  // return
178  return true;
179  }
180 
181 protected:
183  Value& mValue;
184 };
185 
186 #endif /* !__UTILS__XML_PARSER_EX_H__INCL__ */
T Value
Type of parsed value.
Definition: XMLParserEx.h:144
bool Parse(const TiXmlElement *field)
Invokes parser method.
Definition: XMLParserEx.h:121
bool Parse(const TiXmlElement *field)
Parse the value and store it.
Definition: XMLParserEx.h:164
Value & mValue
Reference to storage variable.
Definition: XMLParserEx.h:183
An implementation of ElementParser for member method parsers.
Definition: XMLParserEx.h:42
void AddMemberParser(const char *name, bool(T::*method)(const TiXmlElement *))
Adds a member parser, assuming that instance is this.
Definition: XMLParserEx.h:80
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
A somewhat extended version of XMLParser.
Definition: XMLParserEx.h:37
This virtual interface must be implemented by all parsers.
Definition: XMLParser.h:42
Utility for parsing XML files.
Definition: XMLParser.h:34
const Method mMethod
The parser method.
Definition: XMLParserEx.h:130
ValueParser(Value &value)
Primary constructor.
Definition: XMLParserEx.h:151
Class & mInstance
The instance that the method should be invoked upon.
Definition: XMLParserEx.h:128
void AddParser(const char *name, ElementParser *parser)
Adds a parser.
Definition: XMLParser.cpp:101
bool(Class::* Method)(const TiXmlElement *)
Type of method.
Definition: XMLParserEx.h:99
void AddValueParser(const char *name, T &value)
Adds a value parser.
Definition: XMLParserEx.h:67
MemberElementParser(Class &instance, const Method &method)
Primary constructor.
Definition: XMLParserEx.h:107
Parses and stores a value.
Definition: XMLParserEx.h:45
void AddMemberParser(const char *name, T &instance, bool(T::*method)(const TiXmlElement *))
Adds a member parser.
Definition: XMLParserEx.h:55