vcfrttiimpl.h

来自「这是VCF框架的代码」· C头文件 代码 · 共 2,696 行 · 第 1/5 页

H
2,696
字号
#ifndef _VCF_VCFRTTIIMPL_H__#define _VCF_VCFRTTIIMPL_H__//VCFRTTIImpl.h/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*/#if _MSC_VER > 1000#   pragma once#endif/**This header contains all the concrete template classes used for support theVCF's RTTI API's*/namespace VCF   {#ifdef VCF_RTTI/**\class EnumSetProperty VCFRTTIImpl.h "vcf/FoundationKit/VCFRTTIImpl.h"This is a special class for handing a enum mask value that is the combinationof one or more other values.*/class FOUNDATIONKIT_API EnumSetProperty : public Property {public:	typedef unsigned long (Object::*GetFunction)(void);	typedef void (Object::*SetFunction)(const unsigned long& );	EnumSetProperty( const String& typeName, GetFunction propGetFunction, int count, unsigned long* setArray, String* names ){		init();		typeName_ = typeName;		getFunction_ = propGetFunction;		setType( pdEnumMask );		isReadOnly_ = true;		for (int i=0;i<count;i++ ) {			nameVals_[names[i]] = setArray[i];		}	};	EnumSetProperty( const String& typeName, GetFunction propGetFunction, SetFunction propSetFunction,					int count, unsigned long* setArray, String* names ){		init();		typeName_ = typeName;		getFunction_ = propGetFunction;		setFunction_ = propSetFunction;		setType( pdEnumMask );		for (int i=0;i<count;i++ ) {			nameVals_[names[i]] = setArray[i];		}	};	EnumSetProperty( const EnumSetProperty& prop ):		Property( prop ){		init();		typeName_ = prop.typeName_;		getFunction_ = prop.getFunction_;		setFunction_ = prop.setFunction_;		nameVals_ = prop.nameVals_;	};	virtual ~EnumSetProperty(){};	virtual String getTypeClassName() {		return typeName_;	}	void addAsString( const String& val ) {		std::map<String,unsigned long>::iterator found = nameVals_.find( val );		if ( found != nameVals_.end() ) {			add( found->second );		}	}	void removeAsString( const String& val ) {		std::map<String,unsigned long>::iterator found = nameVals_.find( val );		if ( found != nameVals_.end() ) {			remove( found->second );		}	}	void add( const unsigned long& val ) {		unsigned long tmp = value_;		tmp |= val;		value_ = tmp;	}	void remove( const unsigned long& val ) {		unsigned long tmp = value_;		tmp &= ~val;		value_ = tmp;	}	bool contains( const unsigned long& val )  {		return (((unsigned long)value_) & val) ? true : false;	}	bool hasNames()  {		return !nameVals_.empty();	}		virtual Property* clone(){		return new EnumSetProperty(*this);	};	void init(){		getFunction_ = NULL;		setFunction_ = NULL;	};	virtual VariantData* get( Object* source ){		if ( (NULL != getFunction_) && (NULL != source) ){						value_ = (source->*getFunction_)();			value_.type = getType();			return &value_;		}		else {			return NULL;		}	};	virtual void set( Object* source, VariantData* value ){		if ( (NULL != setFunction_) && (NULL != source) ){			if ( true == isBound() ){				VariantData* originalValue = get( source );				PropertyChangeEvent changeEvent( source, originalValue, value );				try {					PropertyChanged.fireEvent( &changeEvent );					(source->*setFunction_)( *value );				}				catch ( PropertyChangeException ){					//do not call the set method					//re-throw the exception ?				}			}			else {				(source->*setFunction_)( *value );			}		}	};	virtual void set( Object* source, const String& value ){		String tmp = value;		size_t pos = tmp.find( "," );		while ( pos != VCF::String::npos ) {			addAsString( tmp.substr( 0, pos ) );			tmp.erase( 0, pos+1 );			pos = tmp.find( "," );		}		if ( !tmp.empty() ) {			addAsString( tmp );		}		VariantData newVal = value_;		set( source, &newVal );	};	virtual String toString() {		String result;		VariantData* data = Property::get();		if ( NULL != data ) {			unsigned long maskVal = *data;			std::map<String,unsigned long>::iterator it = nameVals_.begin();			while ( it != nameVals_.end() ) {				if ( it->second & maskVal ) {					if ( !result.empty() ) {						result += ",";					}					result += it->first;				}				it ++;			}		}		return result;	}	bool getNameValuesAsSet( std::vector<String>& names, std::vector<unsigned long>& values ) {		std::map<String,unsigned long>::iterator it = nameVals_.begin();		while ( it != nameVals_.end() ) {			names.push_back( it->first );						values.push_back( it->second );			it ++;		}		return (!names.empty()) && (!values.empty());	}protected:	String typeName_;	GetFunction getFunction_;	SetFunction setFunction_;	std::map<String,unsigned long> nameVals_;};/***This is specific template class used to wrap the*C++ enum type (ENUM_TYPE) to the Enum class.*/template <class ENUM_TYPE>class TypedEnum : public Enum {public:	TypedEnum( const ENUM_TYPE& lower, const ENUM_TYPE& upper ){		enum_ = lower;		lower_ = (int)lower;		upper_ = (int)upper;		enumNames_.clear();		namesAvailable_ = false;	};	TypedEnum( const ENUM_TYPE& lower, const ENUM_TYPE& upper, const unsigned long& enumNameCount, String* enumNames ){		enum_ = lower;		lower_ = (int)lower;		upper_ = (int)upper;		enumNames_.clear();		if ( enumNameCount > 0 ){			for ( unsigned long i=0;i<enumNameCount;i++ ){				enumNames_.push_back( enumNames[i] );			}		}		namesAvailable_ = enumNames_.size() > 0 ? true : false;	};	virtual ~TypedEnum() {};	virtual int next(){		int i = (int)enum_;		i++;		if ( i > upper_ ){			i = lower_;		}		enum_ = (ENUM_TYPE)i;		return i;	};	virtual int begin(){		enum_ = (ENUM_TYPE)lower_;		return (int)enum_;	};	virtual int end(){		enum_ = (ENUM_TYPE)upper_;		return (int)enum_;	};	virtual int get(){		return (int)enum_;	};	void set( const ENUM_TYPE& val ){		enum_ = val;	};	virtual void set( const int& intVal ){		enum_ = (ENUM_TYPE)intVal;	}	ENUM_TYPE getValAsEnum(){		return enum_;	};	operator ENUM_TYPE (){		return getValAsEnum();	};	TypedEnum<ENUM_TYPE>& operator=( const ENUM_TYPE& val ){		set( val );		return *this;	};	virtual void setFromString( const String& textVal ){		if ( true == namesAvailable_ ){			std::vector<String>::iterator it = enumNames_.begin();			int i = 0;			bool found = false;			while ( it != enumNames_.end() ){				if ( (*it) == textVal ){					found = true;					break;				}				i++;				it++;			}			if ( true == found ){				set( i );			}			else {				Enum::setFromString( textVal );			}		}		else {			Enum::setFromString( textVal );		}	};	virtual String toString(){		String result = "";		if ( true == namesAvailable_ ){			int index = get();			if ( (index >= lower_) && (index <= upper_) ) {								result = enumNames_[index];			}		}		else {			result += get();		}		return result;	};private:	ENUM_TYPE enum_;	int lower_;	int upper_;	std::vector<String> enumNames_;	bool namesAvailable_;};/**\class TypedEventProperty VCFRTTIImpl.h "vcf/FoundationKit/VCFRTTIImpl.h"Concrete template class for supporting event RTTI.*/template <typename SourceType, typename EventType>class TypedEventProperty : public EventProperty {public:	TypedEventProperty( const String& eventClassName, const String& handlerClassName,			const String& delegateName, DelegateMethod delegateMethod ):			EventProperty(eventClassName,handlerClassName,delegateName,delegateMethod)	{}	typedef void (Object::*HandlerMethod)(EventType*);	virtual EventHandler* createEventHandler( Object* source, EventHandlerMethod method, const String& name ) {		return new EventHandlerInstance<Object,EventType>( source, (HandlerMethod)method, name );	}	virtual EventProperty* clone() {		return new TypedEventProperty<SourceType,EventType>(*this);	}	virtual bool isAbstract() {		return false;	}};/**\class TypedProperty VCFRTTIImpl.h "vcf/FoundationKit/VCFRTTIImpl.h"*Typed properties are designed to only work with Object derived classes.*Many thanks go to Mario Motta (author VDK, mmotta@guest.net) for inspiring this from his*VDKReadWriteValueProp class.**@version 1.0*@author Jim Crafton*/template <class PROPERTY>class TypedProperty : public Property {public:	typedef PROPERTY (Object::*GetFunction)(void);	typedef void (Object::*SetFunction)(const PROPERTY& );	TypedProperty( GetFunction propGetFunction, const PropertyDescriptorType& propertyType ){		init();		getFunction_ = propGetFunction;		setType( propertyType );		isReadOnly_ = true;	};	TypedProperty( GetFunction propGetFunction, SetFunction propSetFunction,		               const PropertyDescriptorType& propertyType ){		init();		getFunction_ = propGetFunction;		setFunction_ = propSetFunction;		setType( propertyType );	};	TypedProperty( const TypedProperty<PROPERTY>& prop ):		Property( prop ){		init();		getFunction_ = prop.getFunction_;		setFunction_ = prop.setFunction_;	};	virtual ~TypedProperty(){};	virtual String getTypeClassName() {		String result;		VariantData* val = Property::get();		if ( NULL != val ){			switch ( val->type ){				case pdInt: {					result = CLASS_INTEGER;				}				break;				case pdUInt: {					result = CLASS_UINT;				}				break;				case pdLong: {					result = CLASS_LONG;				}				break;				case pdShort: {					result = CLASS_SHORT;				}				break;				case pdUShort: {					result = CLASS_USHORT;				}				break;				case pdULong: {					result = CLASS_ULONG;				}				break;				case pdFloat: {					result = CLASS_FLOAT;				}				break;				case pdChar: {					result = CLASS_CHAR;				}				break;				case pdDouble: {					result = CLASS_DOUBLE;				}				break;				case pdBool: {					result = CLASS_BOOL;				}				break;				case pdString: {					result = CLASS_STRING;				}				break;				case pdEnum: {					result = CLASS_ENUM;				}				break;				default : {					result = StringUtils::getClassNameFromTypeInfo( typeid(PROPERTY) );				}				break;			}		}		return result;	}	virtual Property* clone(){		return new TypedProperty<PROPERTY>(*this);	};	void init(){		getFunction_ = NULL;		setFunction_ = NULL;	};	virtual VariantData* get( Object* source ){		if ( (NULL != getFunction_) && (NULL != source) ){						value_ = (source->*getFunction_)();			value_.type = getType();			return &value_;		}		else {			return NULL;		}	};	virtual void set( Object* source, VariantData* value ){		if ( (NULL != setFunction_) && (NULL != source) ){			if ( true == isBound() ){				VariantData* originalValue = get( source );				PropertyChangeEvent changeEvent( source, originalValue, value );				try {					PropertyChanged.fireEvent( &changeEvent );					(source->*setFunction_)( *value );				}

⌨️ 快捷键说明

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