reflection_tut.cpp

Go to the documentation of this file.
00001 
00033 #include <tut/tut.h>
00034 
00035 #include "linden_common.h"
00036 #include "lltut.h"
00037 #include "reflective.h"
00038 #include "metaclasst.h"
00039 #include "metapropertyt.h"
00040 //#include "referencemetaproperty.h"
00041 #include "stdtypes.h"
00042 //#include "reflectivemetapropertyt.h"
00043 
00044 namespace tut
00045 {
00046   class TestAggregatedData : public LLReflective
00047   {
00048   public:
00049         TestAggregatedData() {;}
00050         virtual const LLMetaClass& getMetaClass() const;
00051   
00052   private:
00053   };
00054   
00055   class TestReflectionData : public LLReflective
00056   {
00057   public:
00058         TestReflectionData() : mInt(42), mString("foo"), mNullPtr(NULL), mPtr(new TestAggregatedData()), mRef(*(new TestAggregatedData)) {;}
00059         virtual ~TestReflectionData() {delete mPtr;}
00060         virtual const LLMetaClass& getMetaClass() const;
00061         
00062         static U32 getPropertyCount() {return 5;}
00063         
00064   private:
00065   
00066         friend class LLMetaClassT<TestReflectionData>;
00067     S32 mInt;
00068         std::string mString;
00069         TestAggregatedData* mNullPtr;
00070         TestAggregatedData* mPtr;
00071         TestAggregatedData mObj;
00072         TestAggregatedData& mRef;
00073   };
00074 }
00075 
00076 template <>
00077 void LLMetaClassT<tut::TestReflectionData>::reflectProperties(LLMetaClass& meta_class)
00078 {
00079         reflectProperty(meta_class, "mInt", &tut::TestReflectionData::mInt);
00080         reflectProperty(meta_class, "mString", &tut::TestReflectionData::mString);
00081         reflectPtrProperty(meta_class, "mNullPtr", &tut::TestReflectionData::mNullPtr);
00082         reflectPtrProperty(meta_class, "mPtr", &tut::TestReflectionData::mPtr);
00083         reflectProperty(meta_class, "mObj", &tut::TestReflectionData::mObj);
00084         //reflectProperty(meta_class, "mRef", &tut::TestReflectionData::mRef); // AARGH!
00085 }
00086 
00087 namespace tut
00088 {
00089         // virtual
00090         const LLMetaClass& TestReflectionData::getMetaClass() const
00091         {
00092            return LLMetaClassT<TestReflectionData>::instance();
00093     }
00094         
00095         const LLMetaClass& TestAggregatedData::getMetaClass() const
00096         {
00097            return LLMetaClassT<TestAggregatedData>::instance();
00098     }
00099 }
00100 
00101 namespace tut
00102 {
00103   typedef tut::test_group<TestReflectionData> TestReflectionGroup;
00104   typedef TestReflectionGroup::object TestReflectionObject;
00105   TestReflectionGroup gTestReflectionGroup("reflection");
00106 
00107   template<> template<>
00108   void TestReflectionObject::test<1>()
00109   {
00110         // Check properties can be found.
00111     const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
00112         const LLMetaProperty* null = NULL;
00113         ensure_not_equals(meta_class.findProperty("mInt"), null);
00114         ensure_not_equals(meta_class.findProperty("mString"), null);
00115   }
00116   
00117   template<> template<>
00118   void TestReflectionObject::test<2>()
00119   {
00120         // Check non-existent property cannot be found.
00121     const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
00122         const LLMetaProperty* null = NULL;
00123         ensure_equals(meta_class.findProperty("foo"), null);
00124   }
00125   
00126   template<> template<>
00127   void TestReflectionObject::test<3>()
00128   {
00129         // Check integer property has correct value.    
00130     const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
00131         ensure_equals(meta_class.findProperty("mInt")->getLLSD(this).asInteger(), 42);
00132   }
00133   
00134   template<> template<>
00135   void TestReflectionObject::test<4>()
00136   {
00137         // Check string property has correct value.     
00138     const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
00139         ensure_equals(meta_class.findProperty("mString")->getLLSD(this).asString(), std::string("foo"));
00140   }
00141   
00142   template<> template<>
00143   void TestReflectionObject::test<5>()
00144   {
00145         // Check NULL reference property has correct value.
00146         const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
00147         const LLReflective* null = NULL;
00148         ensure_equals(meta_class.findProperty("mNullPtr")->get(this), null);
00149   }
00150   
00151   template<> template<>
00152   void TestReflectionObject::test<6>()
00153   {
00154         // Check reference property has correct value.
00155         const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
00156         const LLReflective* null = NULL;
00157         const LLReflective* ref = meta_class.findProperty("mPtr")->get(this);
00158         ensure_not_equals(ref, null);
00159   }
00160   
00161   template<> template<>
00162   void TestReflectionObject::test<7>()
00163   {
00164         // Check reflective property has correct value.
00165         const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
00166         const LLReflective* null = NULL;
00167         const LLReflective* ref = meta_class.findProperty("mObj")->get(this);
00168         ensure_not_equals(ref, null);
00169   }
00170 
00171   template<> template<>
00172   void TestReflectionObject::test<8>()
00173   {
00174         // Check property count.
00175     const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
00176         ensure_equals(meta_class.getPropertyCount(), TestReflectionData::getPropertyCount());
00177   }
00178   
00179   template<> template<>
00180   void TestReflectionObject::test<9>()
00181   {
00182         // Check property iteration.
00183     const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
00184         U32 count = 0;
00185         LLMetaClass::PropertyIterator iter;
00186         for(iter = meta_class.beginProperties(); iter != meta_class.endProperties(); ++iter)
00187         {
00188                 ++count;
00189         }
00190         ensure_equals(count, TestReflectionData::getPropertyCount());
00191   }
00192   
00193   template<> template<>
00194   void TestReflectionObject::test<10>()
00195   {
00196         // Check meta classes of different types do not compare equal.
00197         const LLMetaClass* reflection_data_meta_class = &(LLMetaClassT<TestReflectionData>::instance());
00198         const LLMetaClass* aggregated_data_meta_class = &(LLMetaClassT<TestAggregatedData>::instance());
00199         ensure_not_equals(reflection_data_meta_class, aggregated_data_meta_class);
00200   }
00201   
00202   template<> template<>
00203   void TestReflectionObject::test<11>()
00204   {
00205         // Check class cast checks.
00206         const LLMetaClass& meta_class = LLMetaClassT<TestReflectionData>::instance();
00207         TestAggregatedData* aggregated_data = new TestAggregatedData();
00208         LLMetaClass::PropertyIterator iter;
00209         U32 exception_count = 0;
00210         for(iter = meta_class.beginProperties(); iter != meta_class.endProperties(); ++iter)
00211         {
00212                 try
00213                 {
00214                         const LLMetaProperty* property = (*iter).second;
00215                         const LLReflective* reflective = property->get(aggregated_data); // Wrong reflective type, should throw exception.
00216 
00217                         // useless op to get rid of compiler warning.
00218                         reflective = NULL;
00219                 }
00220                 catch(...)
00221                 {
00222                         ++exception_count;
00223                 }
00224         }
00225         ensure_equals(exception_count, getPropertyCount());
00226         
00227   }
00228 }

Generated on Thu Jul 1 06:09:59 2010 for Second Life Viewer by  doxygen 1.4.7