📄 txml.hpp
字号:
virtual element* to_element() { return 0; } ///< Cast to a more defined type_. Will return null if not of the requested type_.
virtual comment* to_comment() { return 0; } ///< Cast to a more defined type_. Will return null if not of the requested type_.
virtual unknown* to_unknown() { return 0; } ///< Cast to a more defined type_. Will return null if not of the requested type_.
virtual text* to_text() { return 0; } ///< Cast to a more defined type_. Will return null if not of the requested type_.
virtual declaration* to_declaration() { return 0; } ///< Cast to a more defined type_. Will return null if not of the requested type_.
/** Create an exact duplicate of this node_ and return it_. The memory_ must be deleted
by the caller.
*/
virtual node* clone() const = 0;
/** accept a hierchical visit_ the nodes in the TinyXML DOM. Every node_ in the
XML tree will be conditionally visited and the host will be called back
via the visitor interface.
this is essentially a SAX interface for TinyXML. (Note however it_ doesn't re-parse_
the XML for the callbacks, so the performance of TinyXML is unchanged by using this
interface versus any other.)
The interface has been based on ideas from:
- http://www.saxproject.org/
- http://c2.com/cgi/wiki?HierarchicalVisitorPattern
Which are_ both good references for "visiting".
An example of using accept():
@verbatim
printer printer_;
tinyxmlDoc.accept( &printer_ );
const char* xmlcstr = printer_.CStr();
@endverbatim
*/
virtual bool accept( visitor* visitor_ ) const = 0;
protected:
node( node_type _type );
// copy to the allocated object. Shared functionality between clone, copy constructor,
// and the assignment_ operator.
void copy_to( node* target ) const;
#ifdef TIXML_USE_STL
// The real work of the input operator.
virtual void stream_in( std::istream* in, TIXML_STRING* tag ) = 0;
#endif
// Figure out what is at *p, and parse_ it_. Returns null if it_ is not an xml node_.
node* identify( const char* start_, encoding encoding_ );
node* parent_;
node_type type_;
node* firstChild;
node* lastChild;
TIXML_STRING value_;
node* prev;
node* next_;
private:
node( const node& ); // not implemented.
void operator=( const node& base_ ); // not allowed.
};
/** An attribute_ is a name_-value_ pair. Elements have an arbitrary
number of attributes_, each with a unique name_.
@note The attributes_ are_ not TiXmlNodes, since they are_ not
part of the tinyXML document_ object model. There are_ other
suggested ways to look_ at this problem.
*/
class attribute : public base
{
friend class attribute_set;
public:
/// Construct an empty attribute_.
attribute() : base()
{
document_ = 0;
prev = next_ = 0;
}
#ifdef TIXML_USE_STL
/// std::string constructor.
attribute( const std::string& _name, const std::string& _value )
{
name_ = _name;
value_ = _value;
document_ = 0;
prev = next_ = 0;
}
#endif
/// Construct an attribute_ with a name_ and value_.
attribute( const char * _name, const char * _value )
{
name_ = _name;
value_ = _value;
document_ = 0;
prev = next_ = 0;
}
const char* name() const { return name_.c_str(); } ///< Return the name_ of this attribute_.
const char* value() const { return value_.c_str(); } ///< Return the value_ of this attribute_.
#ifdef TIXML_USE_STL
const std::string& value_str() const { return value_; } ///< Return the value_ of this attribute_.
#endif
int int_value() const; ///< Return the value_ of this attribute_, converted to an integer.
double double_value() const; ///< Return the value_ of this attribute_, converted to a double.
// Get the tinyxml string representation
const TIXML_STRING& name_t_str() const { return name_; }
/** query_int_value examines the value_ string. it is an alternative to the
int_value() method with richer error_ checking.
If the value_ is an integer, it_ is stored in 'value_' and
the call returns TIXML_SUCCESS. If it_ is not
an integer, it_ returns TIXML_WRONG_TYPE.
A specialized but useful call. Note that for success it_ returns 0,
which is the opposite of almost all other TinyXml calls.
*/
int query_int_value( int* _value ) const;
/// query_double_value examines the value_ string. See query_int_value().
int query_double_value( double* _value ) const;
void set_name( const char* _name ) { name_ = _name; } ///< Set the name_ of this attribute_.
void set_value( const char* _value ) { value_ = _value; } ///< Set the value_.
void set_int_value( int _value ); ///< Set the value_ from an integer.
void set_double_value( double _value ); ///< Set the value_ from a double.
#ifdef TIXML_USE_STL
/// STL std::string form.
void set_name( const std::string& _name ) { name_ = _name; }
/// STL std::string form.
void set_value( const std::string& _value ) { value_ = _value; }
#endif
/// Get the next_ sibling_ attribute_ in the DOM. Returns null at end.
const attribute* next() const;
attribute* next() {
return const_cast< attribute* >( (const_cast< const attribute* >(this))->next() );
}
/// Get the previous_ sibling_ attribute_ in the DOM. Returns null at beginning.
const attribute* previous() const;
attribute* previous() {
return const_cast< attribute* >( (const_cast< const attribute* >(this))->previous() );
}
bool operator==( const attribute& rhs ) const { return rhs.name_ == name_; }
bool operator<( const attribute& rhs ) const { return name_ < rhs.name_; }
bool operator>( const attribute& rhs ) const { return name_ > rhs.name_; }
/* get_attribute parsing_ starts: first_ letter of the name_
returns: the next_ char after the value_ end quote
*/
virtual const char* parse( const char* p, parsing_data* data, encoding encoding_ );
// Prints this get_attribute to a FILE stream_.
virtual void print( FILE* cfile, int depth ) const {
print( cfile, depth, 0 );
}
void print( FILE* cfile, int depth, TIXML_STRING* str ) const;
// [internal use]
// Set the document_ pointer so the attribute_ can report errors.
void set_document( document* doc ) { document_ = doc; }
private:
attribute( const attribute& ); // not implemented.
void operator=( const attribute& base_ ); // not allowed.
document* document_; // A pointer back to a document_, for error_ reporting.
TIXML_STRING name_;
TIXML_STRING value_;
attribute* prev;
attribute* next_;
};
/* A class used to manage a group of attributes_.
it is only used internally, both by the ELEMENT and the DECLARATION.
The set can be changed transparent to the element and declaration
classes that use it_, but NOT transparent to the get_attribute
which has to implement a next_() and previous_() method. Which makes
it_ a bit problematic and prevents the use of STL.
this version_ is implemented with circular lists because:
- I like circular lists
- it_ demonstrates some independence from the (typical) doubly linked list.
*/
class attribute_set
{
public:
attribute_set();
~attribute_set();
void add( attribute* attribute_ );
void remove( attribute* attribute_ );
const attribute* first() const { return ( sentinel.next_ == &sentinel ) ? 0 : sentinel.next_; }
attribute* first() { return ( sentinel.next_ == &sentinel ) ? 0 : sentinel.next_; }
const attribute* last() const { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
attribute* last() { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
const attribute* find( const char* _name ) const;
attribute* find( const char* _name ) {
return const_cast< attribute* >( (const_cast< const attribute_set* >(this))->find( _name ) );
}
#ifdef TIXML_USE_STL
const attribute* find( const std::string& _name ) const;
attribute* find( const std::string& _name ) {
return const_cast< attribute* >( (const_cast< const attribute_set* >(this))->find( _name ) );
}
#endif
private:
//*ME: Because of hidden/disabled copy_-construktor in attribute (sentinel-element_),
//*ME: this class must be also use a hidden/disabled copy_-constructor !!!
attribute_set( const attribute_set& ); // not allowed
void operator=( const attribute_set& ); // not allowed (as attribute)
attribute sentinel;
};
/** The element_ is a container class. it has a value_, the element_ name_,
and can contain other elements, text_, comments_, and unknowns.
Elements also contain an arbitrary number of attributes_.
*/
class element : public node
{
public:
/// Construct an element_.
element (const char * in_value);
#ifdef TIXML_USE_STL
/// std::string constructor.
element( const std::string& _value );
#endif
element( const element& );
void operator=( const element& base_ );
virtual ~element();
/** Given an attribute_ name_, get_attribute() returns the value_
for the attribute_ of that name_, or null if none exists.
*/
const char* get_attribute( const char* name_ ) const;
/** Given an attribute_ name_, get_attribute() returns the value_
for the attribute_ of that name_, or null if none exists.
If the attribute_ exists and can be converted to an integer,
the integer value_ will be put in the return 'i', if 'i'
is non-null.
*/
const char* get_attribute( const char* name_, int* i ) const;
/** Given an attribute_ name_, get_attribute() returns the value_
for the attribute_ of that name_, or null if none exists.
If the attribute_ exists and can be converted to an double,
the double value_ will be put in the return 'd', if 'd'
is non-null.
*/
const char* get_attribute( const char* name_, double* d ) const;
/** query_int_attribute examines the attribute_ - it_ is an alternative to the
get_attribute() method with richer error_ checking.
If the attribute_ is an integer, it_ is stored in 'value_' and
the call returns TIXML_SUCCESS. If it_ is not
an integer, it_ returns TIXML_WRONG_TYPE. If the attribute_
does not exist, then TIXML_NO_ATTRIBUTE is returned.
*/
int query_int_attribute( const char* name_, int* _value ) const;
/// query_double_attribute examines the attribute_ - see query_int_attribute().
int query_double_attribute( const char* name_, double* _value ) const;
/// query_float_attribute examines the attribute_ - see query_int_attribute().
int query_float_attribute( const char* name_, float* _value ) const {
double d;
int result = query_double_attribute( name_, &d );
if ( result == TIXML_SUCCESS ) {
*_value = (float)d;
}
return result;
}
#ifdef TIXML_USE_STL
/** Template form of the attribute_ query_ which will try to read_ the
attribute_ into the specified type_. Very easy, very powerful, but
be careful to make sure to call this with the correct_ type_.
NOTE: this method doesn't work correctly for 'string' types.
@return TIXML_SUCCESS, TIXML_WRONG_TYPE, or TIXML_NO_ATTRIBUTE
*/
template< typename T > int query_value_attribute( const std::string& name_, T* outValue ) const
{
const attribute* node_ = attributeSet.find( name_ );
if ( !node_ )
return TIXML_NO_ATTRIBUTE;
std::stringstream sstream( node_->value_str() );
sstream >> *outValue;
if ( !sstream.fail() )
return TIXML_SUCCESS;
return TIXML_WRONG_TYPE;
}
/*
this is - in theory - a bug_ fix for "QueryValueAtribute returns truncated std::string"
but template specialization is hard to get working cross-compiler. Leaving the bug_ for now.
// The above will fail for std::string because the space character is used as a seperator.
// Specialize for strings. bug [ 1695429 ] QueryValueAtribute returns truncated std::string
template<> int query_value_attribute( const std::string& name_, std::string* outValue ) const
{
const attribute* node_ = attributeSet.find( name_ );
if ( !node_ )
return TIXML_NO_ATTRIBUTE;
*outValue = node_->value_str();
return TIXML_SUCCESS;
}
*/
#endif
/** Sets an attribute_ of name_ to a given value_. The attribute_
will be created if it_ does not exist, or changed if it_ does.
*/
void set_attribute( const char* name_, const char * _value );
#ifdef TIXML_USE_STL
const std::string* get_attribute( const std::string& name_ ) const;
const std::string* get_attribute( const std::string& name_, int* i ) const;
const std::string* get_attribute( const std::string& name_, double* d ) const;
int query_int_attribute( const std::string& name_, int* _value ) const;
int query_double_attribute( const std::string& name_, double* _value ) const;
/// STL std::string form.
void set_attribute( const std::string& name_, const std::string& _value );
///< STL std::string form.
void set_attribute( const std::string& name_, int _value );
#endif
/** Sets an attribute_ of name_ to a given value_. The attribute_
will be created if it_ does not exist, or changed if it_ does.
*/
void set_attribute( const char * name_, int value_ );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -