⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configitem.h

📁 一个Linux下读写文件的代码
💻 H
字号:
/*  The following is the Simplified BSD License *  * Copyright (c) 2008, Warren Kurt vonRoeschlaub * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions  * are met: *  *  * Redistributions of source code must retain the above copyright  *       notice, this list of conditions and the following disclaimer. *  * Redistributions in binary form must reproduce the above copyright  *       notice, this list of conditions and the following disclaimer  *       in the documentation and/or other materials provided with the  *       distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#ifndef CONFIGITEM_H_#define CONFIGITEM_H_#include "configfile.h"#include "configitem_base.h"#include <sstream>namespace configlib{/* * Template for defining a configuration item. * The tamplate extends the configitem_base object with all * object specific functionality. */	template <typename _Tp>class configitem : protected configitem_base{public:	configitem(configfile& file,			   const std::string& section,			   const std::string& name,			   const std::string& param,			   const _Tp& def)	: configitem_base(file, section, name, param)	, def_value(def)	, cur_value(def)	{	}		virtual ~configitem() {}		virtual bool parse(const std::string& value)	{		item_parse(cur_value, value);		return true;			}		virtual bool parse(const std::string& section,					   const std::string& name)	{		if (configitem_base::parse(section, name))		{			item_toggle(cur_value, def_value);			return true;		}		else			return false;	}		virtual bool parse(const std::string& section,					   const std::string& name,			   		   const std::string& value)	{		if (configitem_base::parse(section, name, value))		{			item_parse(cur_value, value);			return true;		}		else			return false;	}		_Tp & get() { return cur_value; }	void set(const _Tp & value)	{		cur_value = value;		set_value(item_get(cur_value));	}	const _Tp& def_get() { return def_value; }	// If an error shows up on any of the following lines, then the	// caller tried to call an operator function on an object that	// doesn't support that operator.  Don't change the functions	// below, just add an object specific version of the template	// function called. 		_Tp & operator=(const _Tp & rhs) { set(rhs); return get(); }	_Tp & operator++() { item_toggle(cur_value, def_get()); set(cur_value); return get(); }	_Tp operator++(int) { return operator++(); }	bool operator==(const _Tp & other) const { return other == cur_value; }	bool operator!=(const _Tp & other) const { return !(*this == other); }	operator const _Tp() { return get(); }	private:	_Tp def_value;	_Tp cur_value;};/** * Template functions to override for special cases. **//* * item_parse - template function for parsing a string into the template class. */template <class _Tp>void item_parse(_Tp& item, const std::string& value){	std::stringstream stream_value(value, std::stringstream::in);	stream_value >> item;	}/* * item_parse - strings must be overridden or just the first word will be parsed */void item_parse(std::string& item, const std::string& value){	item = value;}/* * item_get - template function for turning the template class into a string. */template <class _Tp>std::string item_get(const _Tp& item){	std::stringstream stream_value(std::stringstream::out);	stream_value << item;	return stream_value.str();}/* * item_toggle - template function for incrementing the template class, called * 				for parameterless configuration settings */template <class _Tp>void item_toggle(_Tp& item, const _Tp& /*default_value*/){	++item;}/* * item_toggle - strings don't increment. */void item_toggle(std::string& item, const std::string& default_value){	if (default_value.length() > 0)		item.clear();	else		item = "Set";}/* * item_toggle - bool variables should flip-flop */void item_toggle(bool item, const bool /*default_value*/){	item = !item;}}#endif /*CONFIGITEM_H_*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -