Click here to Skip to main content
15,861,125 members
Articles / Programming Languages / Visual C++ 10.0

Linq-To-XML Style of Node Creation for C++

Rate me:
Please Sign up or sign in to vote.
4.78/5 (14 votes)
12 Apr 2016Ms-PL11 min read 43.3K   500   29  
Linq-To-XML Node Creation for Native C++
#pragma once

#include "BaseConverter.h"
#ifdef ELMAX_USE_BOOST_REGEX
	#include <boost/regex.hpp>
#else
	#include <regex>
#endif

namespace Elmax
{
//! Class to convert values to and fro string with regex checks in accessors methods.
class RegexConverter : public Elmax::BaseConverter
{
public:
	//! Default constructor
	RegexConverter(void);
	//! Virtual destructor
	virtual ~RegexConverter(void);
	//! Convert the boolean into string
	//!
	//! @param dest is the string to be set
	//! @param val is the source boolean value to convert from
	//! @returns true if successful
	virtual bool SetBool(std::wstring& dest, bool val);
	//! Convert the character into string
	//!
	//! @param dest is the string to be set
	//! @param val is the source character value to convert from
	//! @returns true if successful
	virtual bool SetChar(std::wstring& dest, char val);
	//! Convert the short integer into string
	//!
	//! @param dest is the string to be set
	//! @param val is the source short integer value to convert from
	//! @returns true if successful
	virtual bool SetShort(std::wstring& dest, short val);
	//! Convert the 32bit integer into string
	//!
	//! @param dest is the string to be set
	//! @param val is the source 32bit integer value to convert from
	//! @returns true if successful
	virtual bool SetInt32(std::wstring& dest, int val);
	//! Convert the 64bit integer into string
	//!
	//! @param dest is the string to be set
	//! @param val is the source 64bit integer value to convert from
	//! @returns true if successful
	virtual bool SetInt64(std::wstring& dest, __int64 val);
	//! Convert the unsigned character into string
	//!
	//! @param dest is the string to be set
	//! @param val is the source unsigned character value to convert from
	//! @returns true if successful
	virtual bool SetUChar(std::wstring& dest, unsigned char val);
	//! Convert the unsigned short into string
	//!
	//! @param dest is the string to be set
	//! @param val is the source unsigned short value to convert from
	//! @returns true if successful
	virtual bool SetUShort(std::wstring& dest, unsigned short val);
	//! Convert the unsigned 32bit integer into string
	//!
	//! @param dest is the string to be set
	//! @param val is the source unsigned 32bit integer value to convert from
	//! @returns true if successful
	virtual bool SetUInt32(std::wstring& dest, unsigned int val);
	//! Convert the unsigned 64bit integer into string
	//!
	//! @param dest is the string to be set
	//! @param val is the source unsigned 64bit integer value to convert from
	//! @returns true if successful
	virtual bool SetUInt64(std::wstring& dest, unsigned __int64 val);
	//! Convert the float into string
	//!
	//! @param dest is the string to be set
	//! @param val is the source float value to convert from
	//! @returns true if successful
	virtual bool SetFloat(std::wstring& dest, float val);
	//! Convert the double into string
	//!
	//! @param dest is the string to be set
	//! @param val is the source double value to convert from
	//! @returns true if successful
	virtual bool SetDouble(std::wstring& dest, double val);
	//! Set the wide string into string
	//!
	//! @param dest is the string to be set
	//! @param val is the source wide string value to set from
	//! @returns true if successful
	virtual bool SetString(std::wstring& dest, const std::wstring& val);
	//! Set the ascii string into string
	//!
	//! @param dest is the string to be set
	//! @param val is the source ascii string value to set from
	//! @returns true if successful
	virtual bool SetString(std::wstring& dest, const std::string& val);
	//! Convert the string into boolean ("true" or "yes" or "1" or "ok" get true value 
	//! while "false" or "no" or "0" or "cancel" get false value)
	//!
	//! @param src is the source string to convert from
	//! @param defaultVal is the default boolean value to use if src is invalid or empty
	//! @param val is the destination boolean value to be set
	//! @returns true if successful
	virtual bool GetBool(const std::wstring& src, bool defaultVal, bool& val);
	//! Convert the string into character
	//!
	//! @param src is the source string to convert from
	//! @param defaultVal is the default character value to use if src is invalid or empty
	//! @param val is the destination character value to be set
	//! @returns true if successful
	virtual bool GetChar(const std::wstring& src, char defaultVal, char& val);
	//! Convert the string into short integer
	//!
	//! @param src is the source string to convert from
	//! @param defaultVal is the default short integer value to use if src is invalid or empty
	//! @param val is the destination short integer value to be set
	//! @returns true if successful
	virtual bool GetShort(const std::wstring& src, short defaultVal, short& val);
	//! Convert the string into 32bit integer
	//!
	//! @param src is the source string to convert from
	//! @param defaultVal is the default 32bit integer value to use if src is invalid or empty
	//! @param val is the destination 32bit integer value to be set
	//! @returns true if successful
	virtual bool GetInt32(const std::wstring& src, int defaultVal, int& val);
	//! Convert the string into 64bit integer
	//!
	//! @param src is the source string to convert from
	//! @param defaultVal is the default 64bit integer value to use if src is invalid or empty
	//! @param val is the destination 64bit integer value to be set
	//! @returns true if successful
	virtual bool GetInt64(const std::wstring& src, __int64 defaultVal, __int64& val);
	//! Convert the string into unsigned character
	//!
	//! @param src is the source string to convert from
	//! @param defaultVal is the default unsigned character value to use if src is invalid or empty
	//! @param val is the destination unsigned character value to be set
	//! @returns true if successful
	virtual bool GetUChar(const std::wstring& src, unsigned char defaultVal, unsigned char& val);
	//! Convert the string into unsigned short
	//!
	//! @param src is the source string to convert from
	//! @param defaultVal is the default unsigned short value to use if src is invalid or empty
	//! @param val is the destination unsigned short value to be set
	//! @returns true if successful
	virtual bool GetUShort(const std::wstring& src, unsigned short defaultVal, unsigned short& val);
	//! Convert the string into unsigned 32bit integer
	//!
	//! @param src is the source string to convert from
	//! @param defaultVal is the default unsigned 32bit integer value to use if src is invalid or empty
	//! @param val is the destination unsigned 32bit integer value to be set
	//! @returns true if successful
	virtual bool GetUInt32(const std::wstring& src, unsigned int defaultVal, unsigned int& val);
	//! Convert the string into unsigned 64bit integer
	//!
	//! @param src is the source string to convert from
	//! @param defaultVal is the default unsigned 64bit integer value to use if src is invalid or empty
	//! @param val is the destination unsigned 64bit integer value to be set
	//! @returns true if successful
	virtual bool GetUInt64(const std::wstring& src, unsigned __int64 defaultVal, unsigned __int64& val);
	//! Convert the string into float
	//!
	//! @param src is the source string to convert from
	//! @param defaultVal is the default float value to use if src is invalid or empty
	//! @param val is the destination float value to be set
	//! @returns true if successful
	virtual bool GetFloat(const std::wstring& src, float defaultVal, float& val);
	//! Convert the string into double
	//!
	//! @param src is the source string to convert from
	//! @param defaultVal is the default double value to use if src is invalid or empty
	//! @param val is the destination double value to be set
	//! @returns true if successful
	virtual bool GetDouble(const std::wstring& src, double defaultVal, double& val);
	//! Set the string into wide string
	//!
	//! @param src is the source string to convert from
	//! @param defaultVal is the default wide string value to use if src is invalid or empty
	//! @param val is the destination wide string value to be set
	//! @returns true if successful
	virtual bool GetString(const std::wstring& src, const std::wstring& defaultVal, std::wstring& val);
	//! Convert the string into ascii string
	//!
	//! @param src is the source string to convert from
	//! @param defaultVal is the default ascii string value to use if src is invalid or empty
	//! @param val is the destination ascii string value to be set
	//! @returns true if successful
	virtual bool GetString(const std::wstring& src, const std::string& defaultVal, std::string& val);
private:
#ifdef ELMAX_USE_BOOST_REGEX
	//! regex object for float
	boost::wregex m_rxFloat;
	//! regex object for integer
	boost::wregex m_rxInt;
	//! regex object for unsigned integer
	boost::wregex m_rxUInt;
#else
	//! regex object for float
	std::tr1::wregex m_rxFloat;
	//! regex object for integer
	std::tr1::wregex m_rxInt;
	//! regex object for unsigned integer
	std::tr1::wregex m_rxUInt;
#endif // ELMAX_USE_BOOST_REGEX

};

} // ns Elmax

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions