00001
00002
00003 #ifndef ROOT_stressIterators
00004 #define ROOT_stressIterators
00005
00006 #ifdef WIN32
00007 #pragma warning(disable: 4290)
00008 #endif
00009
00010 static Int_t gCount = 0;
00011
00012
00013
00014 template<class T>
00015 struct SEnumFunctor {
00016 bool operator()(TObject *aObj) const throw(std::exception) {
00017 if (!aObj)
00018 throw std::invalid_argument("SEnumFunctor: aObj is a NULL pointer");
00019
00020 if ((aObj->IsA() == TObjString::Class())) {
00021 TObjString *str(dynamic_cast<TObjString*>(aObj));
00022 if (!str)
00023 throw std::runtime_error("SEnumFunctor: Container's element is not a TObjString object.");
00024
00025 ++gCount;
00026 std::cout << str->String().Data() << std::endl;
00027 }
00028
00029 return true;
00030 }
00031 };
00032
00033
00034 template<>
00035 struct SEnumFunctor<TMap> {
00036 bool operator()(TObject *aObj) const throw(std::exception) {
00037 if (!aObj)
00038 throw std::invalid_argument("SEnumFunctor: aObj is a NULL pointer");
00039
00040 if ((aObj->IsA() == TPair::Class())) {
00041 TPair *pair(dynamic_cast<TPair*>(aObj));
00042 if (!pair)
00043 throw std::runtime_error("SEnumFunctor: Container's element is not a TPair object.");
00044
00045 TObjString *key(dynamic_cast<TObjString*>(pair->Key()));
00046 TObjString *value(dynamic_cast<TObjString*>(pair->Value()));
00047 if (!key || !value)
00048 throw std::runtime_error("SEnumFunctor: Can't retriev key/value of a pair");
00049
00050 ++gCount;
00051 std::cout << key->String().Data() << " : " << value->String().Data() << std::endl;
00052 }
00053
00054 return true;
00055 }
00056 };
00057
00058
00059 template<class T>
00060 struct SFind : std::binary_function<TObject*, TString, bool> {
00061 bool operator()(TObject *_Obj, const TString &_ToFind) const {
00062 TObjString *str(dynamic_cast<TObjString*>(_Obj));
00063 if (!str)
00064 throw std::runtime_error("SFind: Container's element is not a TObString object.");
00065 return !str->String().CompareTo(_ToFind);
00066 }
00067 };
00068
00069
00070 template<>
00071 struct SFind<TMap> : std::binary_function<TObject*, TString, bool> {
00072 bool operator()(TObject *_Obj, const TString &_ToFind) const {
00073 TPair *pair(dynamic_cast<TPair*>(_Obj));
00074 if (!pair)
00075 throw std::runtime_error("SFind: Container's element is not a TPair object.");
00076
00077 TObjString *str(dynamic_cast<TObjString*>(pair->Value()));
00078 return !str->String().CompareTo(_ToFind);
00079 }
00080 };
00081
00082
00083
00084
00085 template<class T>
00086 void TestContainer_for_each(const T &container, Int_t aSize) throw(std::exception)
00087 {
00088 gCount = 0;
00089
00090 TIterCategory<T> iter(&container);
00091 std::for_each(iter.Begin(), TIterCategory<T>::End(), SEnumFunctor<T>());
00092 if (aSize != gCount)
00093 throw std::runtime_error("Test case <TestList_for_each> has failed.");
00094 std::cout << "->> Ok." << std::endl;
00095 }
00096
00097
00098
00099
00100 template<class T>
00101 void TestContainer_for_each2(const T &container) throw(std::exception)
00102 {
00103 gCount = 0;
00104
00105 TIterCategory<T> iter(&container);
00106 TIterCategory<T> iter_end(&container);
00107
00108 iter_end();
00109 iter_end();
00110 iter_end();
00111 iter_end();
00112 std::for_each(iter.Begin(), iter_end, SEnumFunctor<T>());
00113 if (3 != gCount)
00114 throw std::runtime_error("Test case <TestList_for_each2> has failed.");
00115 std::cout << "->> Ok." << std::endl;
00116 }
00117
00118
00119
00120 template<class T>
00121 void TestContainer_find_if(const T &container, const TString &aToFind) throw(std::exception)
00122 {
00123 typedef TIterCategory<T> iterator_t;
00124
00125 iterator_t iter(&container);
00126 iterator_t found(
00127 std::find_if(iter.Begin(), iterator_t::End(), bind2nd(SFind<T>(), aToFind))
00128 );
00129 if (!(*found))
00130 throw std::runtime_error("Test case <TestContainer_find_if> has failed. Can't find object.");
00131
00132
00133 if (((*found)->IsA() == TPair::Class())) {
00134 TPair *pair(dynamic_cast<TPair*>(*found));
00135 if (!pair)
00136 throw std::runtime_error("TestContainer_find_if: Container's element is not a TPair object.");
00137 TObjString *key(dynamic_cast<TObjString*>(pair->Key()));
00138 TObjString *val(dynamic_cast<TObjString*>(pair->Value()));
00139 std::cout << "I found: [" << key->String().Data() << " : " << val->String().Data() << "]" << std::endl;
00140 std::cout << "->> Ok." << std::endl;
00141 return;
00142 }
00143
00144 TObjString *str(dynamic_cast<TObjString*>(*found));
00145 if (!str)
00146 throw std::runtime_error("Test case <TestContainer_find_if> has failed. String object is NULL");
00147
00148 std::cout << "I found: " << str->String().Data() << std::endl;
00149 std::cout << "->> Ok." << std::endl;
00150 }
00151
00152
00153
00154 template<class T>
00155 void TestContainer_count_if(const T &container, const TString &aToFind, Int_t Count) throw(std::exception)
00156 {
00157 typedef TIterCategory<T> iterator_t;
00158
00159 iterator_t iter(&container);
00160 typename iterator_t::difference_type cnt(
00161 std::count_if(iter.Begin(), iterator_t::End(), bind2nd(SFind<T>(), aToFind))
00162 );
00163
00164 if (Count != cnt)
00165 throw std::runtime_error("Test case <TestContainer_count_if> has failed.");
00166
00167 std::cout << "->> Ok." << std::endl;
00168 }
00169
00170 #endif