mirror of
				https://github.com/Icinga/icinga2.git
				synced 2025-10-31 11:14:10 +01:00 
			
		
		
		
	
							parent
							
								
									67b82a81c9
								
							
						
					
					
						commit
						2140cef041
					
				| @ -24,10 +24,12 @@ using namespace icinga; | |||||||
| 
 | 
 | ||||||
| Dictionary::Ptr ReflectionObject::Serialize(int attributeTypes) const | Dictionary::Ptr ReflectionObject::Serialize(int attributeTypes) const | ||||||
| { | { | ||||||
|  | 	const ReflectionType *type = GetReflectionType(); | ||||||
|  | 
 | ||||||
| 	Dictionary::Ptr update = boost::make_shared<Dictionary>(); | 	Dictionary::Ptr update = boost::make_shared<Dictionary>(); | ||||||
| 
 | 
 | ||||||
| 	for (int i = 0; i < GetFieldCount(); i++) { | 	for (int i = 0; i < type->GetFieldCount(); i++) { | ||||||
| 		ReflectionField field = GetFieldInfo(i); | 		ReflectionField field = type->GetFieldInfo(i); | ||||||
| 
 | 
 | ||||||
| 		if ((field.Attributes & attributeTypes) == 0) | 		if ((field.Attributes & attributeTypes) == 0) | ||||||
| 			continue; | 			continue; | ||||||
| @ -40,8 +42,10 @@ Dictionary::Ptr ReflectionObject::Serialize(int attributeTypes) const | |||||||
| 
 | 
 | ||||||
| void ReflectionObject::Deserialize(const Dictionary::Ptr& update, int attributeTypes) | void ReflectionObject::Deserialize(const Dictionary::Ptr& update, int attributeTypes) | ||||||
| { | { | ||||||
| 	for (int i = 0; i < GetFieldCount(); i++) { | 	const ReflectionType *type = GetReflectionType(); | ||||||
| 		ReflectionField field = GetFieldInfo(i); | 
 | ||||||
|  | 	for (int i = 0; i < type->GetFieldCount(); i++) { | ||||||
|  | 		ReflectionField field = type->GetFieldInfo(i); | ||||||
| 
 | 
 | ||||||
| 		if ((field.Attributes & attributeTypes) == 0) | 		if ((field.Attributes & attributeTypes) == 0) | ||||||
| 			continue; | 			continue; | ||||||
|  | |||||||
| @ -38,10 +38,9 @@ struct ReflectionField | |||||||
| 	int ID; | 	int ID; | ||||||
| 	String Name; | 	String Name; | ||||||
| 	int Attributes; | 	int Attributes; | ||||||
| 	Value DefaultValue; |  | ||||||
| 
 | 
 | ||||||
| 	ReflectionField(int id, const String& name, int attributes, const Value& default_value = Empty) | 	ReflectionField(int id, const String& name, int attributes) | ||||||
| 		: ID(id), Name(name), Attributes(attributes), DefaultValue(default_value) | 		: ID(id), Name(name), Attributes(attributes) | ||||||
| 	{ } | 	{ } | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| @ -51,14 +50,20 @@ enum InvokationType | |||||||
| 	ITSet | 	ITSet | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | class I2_BASE_API ReflectionType | ||||||
|  | { | ||||||
|  | public: | ||||||
|  | 	virtual int GetFieldId(const String& name) const = 0; | ||||||
|  | 	virtual ReflectionField GetFieldInfo(int id) const = 0; | ||||||
|  | 	virtual int GetFieldCount(void) const = 0; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| class I2_BASE_API ReflectionObject : public Object | class I2_BASE_API ReflectionObject : public Object | ||||||
| { | { | ||||||
| public: | public: | ||||||
| 	DECLARE_PTR_TYPEDEFS(ReflectionObject); | 	DECLARE_PTR_TYPEDEFS(ReflectionObject); | ||||||
| 
 | 
 | ||||||
| 	virtual int GetFieldId(const String& name) const = 0; | 	virtual const ReflectionType *GetReflectionType(void) const = 0; | ||||||
| 	virtual ReflectionField GetFieldInfo(int id) const = 0; |  | ||||||
| 	virtual int GetFieldCount(void) const = 0; |  | ||||||
| 	virtual void SetField(int id, const Value& value) = 0; | 	virtual void SetField(int id, const Value& value) = 0; | ||||||
| 	virtual Value GetField(int id) const = 0; | 	virtual Value GetField(int id) const = 0; | ||||||
| 
 | 
 | ||||||
| @ -66,6 +71,11 @@ public: | |||||||
| 	void Deserialize(const Dictionary::Ptr& update, int attributeTypes); | 	void Deserialize(const Dictionary::Ptr& update, int attributeTypes); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | template<typename T> | ||||||
|  | class ReflectionTypeImpl | ||||||
|  | { | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| template<typename T> | template<typename T> | ||||||
| class ReflectionObjectImpl | class ReflectionObjectImpl | ||||||
| { | { | ||||||
|  | |||||||
| @ -48,9 +48,8 @@ public: | |||||||
| 
 | 
 | ||||||
| 		return instance; | 		return instance; | ||||||
| 	} | 	} | ||||||
| private: |  | ||||||
| 	friend T *T::GetInstance(void); |  | ||||||
| 
 | 
 | ||||||
|  | private: | ||||||
| 	static T *m_Instance; | 	static T *m_Instance; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -81,36 +81,30 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp) | |||||||
| { | { | ||||||
| 	std::vector<Field>::const_iterator it; | 	std::vector<Field>::const_iterator it; | ||||||
| 
 | 
 | ||||||
|  | 	/* forward declaration */ | ||||||
| 	if (klass.Name.find_first_of(':') == std::string::npos) | 	if (klass.Name.find_first_of(':') == std::string::npos) | ||||||
| 		std::cout << "class " << klass.Name << ";" << std::endl << std::endl; | 		std::cout << "class " << klass.Name << ";" << std::endl << std::endl; | ||||||
| 
 | 
 | ||||||
|  | 	/* ReflectionTypeImpl */ | ||||||
| 	std::cout << "template<>" << std::endl | 	std::cout << "template<>" << std::endl | ||||||
| 		  << "class ReflectionObjectImpl<" << klass.Name << ">" | 		<< "class ReflectionTypeImpl<" << klass.Name << ">" | ||||||
| 		  << " : public " << (klass.Parent.empty() ? "ReflectionObject" : klass.Parent) << std::endl | 		<< " : public ReflectionType, public Singleton<ReflectionTypeImpl<" << klass.Name << "> >" << std::endl | ||||||
| 		<< "{" << std::endl | 		<< "{" << std::endl | ||||||
| 		  << "public:" << std::endl | 		<< "public:" << std::endl; | ||||||
| 		  << "\t" << "DECLARE_PTR_TYPEDEFS(ReflectionObjectImpl<" << klass.Name << ">);" << std::endl; |  | ||||||
| 
 |  | ||||||
| 	if (!klass.Fields.empty()) { |  | ||||||
| 		/* constructor */ |  | ||||||
| 		std::cout << "public:" << std::endl |  | ||||||
| 			  << "\t" << "ReflectionObjectImpl<" << klass.Name << ">(void)" << std::endl |  | ||||||
| 			  << "\t" << "{" << std::endl; |  | ||||||
| 
 |  | ||||||
| 		for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) { |  | ||||||
| 			std::cout << "\t\t" << "Set" << it->GetFriendlyName() << "(" << "GetDefault" << it->GetFriendlyName() << "());" << std::endl; |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		std::cout << "\t" << "}" << std::endl << std::endl; |  | ||||||
| 
 | 
 | ||||||
| 	/* GetFieldId */ | 	/* GetFieldId */ | ||||||
| 		std::cout << "public:" << std::endl | 	std::cout << "\t" << "virtual int GetFieldId(const String& name) const" << std::endl | ||||||
| 				  << "\t" << "virtual int GetFieldId(const String& name) const" << std::endl | 		<< "\t" << "{" << std::endl | ||||||
|  | 		<< "\t\t" << "return StaticGetFieldId(name);" << std::endl | ||||||
|  | 		<< "\t" << "}" << std::endl << std::endl; | ||||||
|  | 
 | ||||||
|  | 	/* StaticGetFieldId */ | ||||||
|  | 	std::cout << "\t" << "static int StaticGetFieldId(const String& name)" << std::endl | ||||||
| 		<< "\t" << "{" << std::endl | 		<< "\t" << "{" << std::endl | ||||||
| 		<< "\t\t" << "int offset = "; | 		<< "\t\t" << "int offset = "; | ||||||
| 
 | 
 | ||||||
| 	if (!klass.Parent.empty()) | 	if (!klass.Parent.empty()) | ||||||
| 			 std::cout << klass.Parent << "::GetFieldCount()"; | 		std::cout << "ReflectionTypeImpl<" << klass.Parent << ">::StaticGetFieldCount()"; | ||||||
| 	else | 	else | ||||||
| 		std::cout << "0"; | 		std::cout << "0"; | ||||||
| 
 | 
 | ||||||
| @ -127,7 +121,7 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp) | |||||||
| 		<< "\t\t" << "return "; | 		<< "\t\t" << "return "; | ||||||
| 
 | 
 | ||||||
| 	if (!klass.Parent.empty()) | 	if (!klass.Parent.empty()) | ||||||
| 			 std::cout << klass.Parent << "::GetFieldId(name)"; | 		std::cout << "ReflectionTypeImpl<" << klass.Parent << ">::StaticGetFieldId(name)"; | ||||||
| 	else | 	else | ||||||
| 		std::cout << "-1"; | 		std::cout << "-1"; | ||||||
| 
 | 
 | ||||||
| @ -135,13 +129,18 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp) | |||||||
| 		<< "\t" << "}" << std::endl << std::endl; | 		<< "\t" << "}" << std::endl << std::endl; | ||||||
| 
 | 
 | ||||||
| 	/* GetFieldInfo */ | 	/* GetFieldInfo */ | ||||||
| 		std::cout << "public:" << std::endl | 	std::cout << "\t" << "virtual ReflectionField GetFieldInfo(int id) const" << std::endl | ||||||
| 				  << "\t" << "virtual ReflectionField GetFieldInfo(int id) const" << std::endl | 		<< "\t" << "{" << std::endl | ||||||
|  | 		<< "\t\t" << "return StaticGetFieldInfo(id);" << std::endl | ||||||
|  | 		<< "\t" << "}" << std::endl << std::endl; | ||||||
|  | 
 | ||||||
|  | 	/* StaticGetFieldInfo */ | ||||||
|  | 	std::cout << "\t" << "static ReflectionField StaticGetFieldInfo(int id)" << std::endl | ||||||
| 		<< "\t" << "{" << std::endl; | 		<< "\t" << "{" << std::endl; | ||||||
| 
 | 
 | ||||||
| 	if (!klass.Parent.empty()) | 	if (!klass.Parent.empty()) | ||||||
| 			std::cout << "\t\t" << "int real_id = id - " << klass.Parent << "::GetFieldCount();" << std::endl | 		std::cout << "\t\t" << "int real_id = id - " << "ReflectionTypeImpl<" << klass.Parent << ">::StaticGetFieldCount();" << std::endl | ||||||
| 					  << "\t\t" << "if (real_id < 0) { return " << klass.Parent << "::GetFieldInfo(id); }" << std::endl; | 		<< "\t\t" << "if (real_id < 0) { return " << "ReflectionTypeImpl<" << klass.Parent << ">::StaticGetFieldInfo(id); }" << std::endl; | ||||||
| 
 | 
 | ||||||
| 	std::cout << "\t\t" << "switch ("; | 	std::cout << "\t\t" << "switch ("; | ||||||
| 
 | 
 | ||||||
| @ -155,7 +154,7 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp) | |||||||
| 	num = 0; | 	num = 0; | ||||||
| 	for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) { | 	for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) { | ||||||
| 		std::cout << "\t\t\t" << "case " << num << ":" << std::endl | 		std::cout << "\t\t\t" << "case " << num << ":" << std::endl | ||||||
| 					  << "\t\t\t\t" << "return ReflectionField(" << num << ", \"" << it->Name << "\", " << it->Attributes << ", " << "GetDefault" << it->GetFriendlyName() << "());" << std::endl; | 			<< "\t\t\t\t" << "return ReflectionField(" << num << ", \"" << it->Name << "\", " << it->Attributes << ");" << std::endl; | ||||||
| 		num++; | 		num++; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| @ -166,24 +165,57 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp) | |||||||
| 	std::cout << "\t" << "}" << std::endl << std::endl; | 	std::cout << "\t" << "}" << std::endl << std::endl; | ||||||
| 
 | 
 | ||||||
| 	/* GetFieldCount */ | 	/* GetFieldCount */ | ||||||
| 		std::cout << "public:" << std::endl | 	std::cout << "\t" << "virtual int GetFieldCount(void) const" << std::endl | ||||||
| 				  << "\t" << "virtual int GetFieldCount(void) const" << std::endl | 		<< "\t" << "{" << std::endl | ||||||
|  | 		<< "\t\t" << "return StaticGetFieldCount();" << std::endl | ||||||
|  | 		<< "\t" << "}" << std::endl << std::endl; | ||||||
|  | 
 | ||||||
|  | 	/* StaticGetFieldCount */ | ||||||
|  | 	std::cout << "\t" << "static int StaticGetFieldCount(void)" << std::endl | ||||||
| 		<< "\t" << "{" << std::endl | 		<< "\t" << "{" << std::endl | ||||||
| 		<< "\t\t" << "return " << klass.Fields.size(); | 		<< "\t\t" << "return " << klass.Fields.size(); | ||||||
| 
 | 
 | ||||||
| 	if (!klass.Parent.empty()) | 	if (!klass.Parent.empty()) | ||||||
| 			std::cout << " + " << klass.Parent + "::GetFieldCount()"; | 		std::cout << " + " << "ReflectionTypeImpl<" << klass.Parent << ">::StaticGetFieldCount()"; | ||||||
| 
 | 
 | ||||||
| 	std::cout << ";" << std::endl | 	std::cout << ";" << std::endl | ||||||
| 		<< "\t" << "}" << std::endl << std::endl; | 		<< "\t" << "}" << std::endl << std::endl; | ||||||
| 
 | 
 | ||||||
|  | 	std::cout << "};" << std::endl << std::endl; | ||||||
|  | 
 | ||||||
|  | 	/* ReflectionObjectImpl */ | ||||||
|  | 	std::cout << "template<>" << std::endl | ||||||
|  | 		  << "class ReflectionObjectImpl<" << klass.Name << ">" | ||||||
|  | 		  << " : public " << (klass.Parent.empty() ? "ReflectionObject" : klass.Parent) << std::endl | ||||||
|  | 		  << "{" << std::endl | ||||||
|  | 		  << "public:" << std::endl | ||||||
|  | 		  << "\t" << "DECLARE_PTR_TYPEDEFS(ReflectionObjectImpl<" << klass.Name << ">);" << std::endl << std::endl; | ||||||
|  | 
 | ||||||
|  | 	/* GetType */ | ||||||
|  | 	std::cout << "\t" << "virtual const ReflectionType *GetReflectionType(void) const" << std::endl | ||||||
|  | 			  << "\t" << "{" << std::endl | ||||||
|  | 			  << "\t\t" << "return ReflectionTypeImpl<" << klass.Name << ">::GetInstance();" << std::endl | ||||||
|  | 			  << "\t" << "}" << std::endl << std::endl; | ||||||
|  | 
 | ||||||
|  | 	if (!klass.Fields.empty()) { | ||||||
|  | 		/* constructor */ | ||||||
|  | 		std::cout << "public:" << std::endl | ||||||
|  | 			  << "\t" << "ReflectionObjectImpl<" << klass.Name << ">(void)" << std::endl | ||||||
|  | 			  << "\t" << "{" << std::endl; | ||||||
|  | 
 | ||||||
|  | 		for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) { | ||||||
|  | 			std::cout << "\t\t" << "Set" << it->GetFriendlyName() << "(" << "GetDefault" << it->GetFriendlyName() << "());" << std::endl; | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		std::cout << "\t" << "}" << std::endl << std::endl; | ||||||
|  | 
 | ||||||
| 		/* SetField */ | 		/* SetField */ | ||||||
| 		std::cout << "protected:" << std::endl | 		std::cout << "protected:" << std::endl | ||||||
| 				  << "\t" << "virtual void SetField(int id, const Value& value)" << std::endl | 				  << "\t" << "virtual void SetField(int id, const Value& value)" << std::endl | ||||||
| 				  << "\t" << "{" << std::endl; | 				  << "\t" << "{" << std::endl; | ||||||
| 
 | 
 | ||||||
| 		if (!klass.Parent.empty()) | 		if (!klass.Parent.empty()) | ||||||
| 			std::cout << "\t\t" << "int real_id = id - " << klass.Parent << "::GetFieldCount();" << std::endl | 			std::cout << "\t\t" << "int real_id = id - ReflectionTypeImpl<" << klass.Parent << ">::StaticGetFieldCount(); " << std::endl | ||||||
| 			          << "\t\t" << "if (real_id < 0) { " << klass.Parent << "::SetField(id, value); return; }" << std::endl; | 			          << "\t\t" << "if (real_id < 0) { " << klass.Parent << "::SetField(id, value); return; }" << std::endl; | ||||||
| 
 | 
 | ||||||
| 		std::cout << "\t\t" << "switch ("; | 		std::cout << "\t\t" << "switch ("; | ||||||
| @ -225,7 +257,7 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp) | |||||||
| 				  << "\t" << "{" << std::endl; | 				  << "\t" << "{" << std::endl; | ||||||
| 
 | 
 | ||||||
| 		if (!klass.Parent.empty()) | 		if (!klass.Parent.empty()) | ||||||
| 			std::cout << "\t\t" << "int real_id = id - " << klass.Parent << "::GetFieldCount();" << std::endl | 			std::cout << "\t\t" << "int real_id = id - ReflectionTypeImpl<" << klass.Parent << ">::StaticGetFieldCount(); " << std::endl | ||||||
| 					  << "\t\t" << "if (real_id < 0) { return " << klass.Parent << "::GetField(id); }" << std::endl; | 					  << "\t\t" << "if (real_id < 0) { return " << klass.Parent << "::GetField(id); }" << std::endl; | ||||||
| 
 | 
 | ||||||
| 		std::cout << "\t\t" << "switch ("; | 		std::cout << "\t\t" << "switch ("; | ||||||
| @ -337,6 +369,7 @@ void ClassCompiler::CompileStream(const std::string& path, std::istream *stream) | |||||||
| 	stream->exceptions(std::istream::badbit); | 	stream->exceptions(std::istream::badbit); | ||||||
| 
 | 
 | ||||||
| 	std::cout << "#include \"base/reflectionobject.h\"" << std::endl | 	std::cout << "#include \"base/reflectionobject.h\"" << std::endl | ||||||
|  | 			  << "#include \"base/singleton.h\"" << std::endl | ||||||
| 			  << "#include \"base/debug.h\"" << std::endl | 			  << "#include \"base/debug.h\"" << std::endl | ||||||
| 			  << "#include \"base/value.h\"" << std::endl | 			  << "#include \"base/value.h\"" << std::endl | ||||||
| 			  << "#include \"base/array.h\"" << std::endl | 			  << "#include \"base/array.h\"" << std::endl | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user