File indexing completed on 2025-04-19 09:09:52
0001 #ifdef COMPILE__Getter_Function
0002 #ifndef OBJECT_TYPE
0003 #error object type undefined
0004 #error specify an object type using #define OBJECT_TYPE Object_Type
0005 #endif
0006 #ifndef PARAMETER_TYPE
0007 #error parameter type undefined
0008 #error specify a parameter type using #define PARAMETER_TYPE Parameter_Type
0009 #endif
0010 #ifndef EXACTMATCH
0011 #define EXACTMATCH true
0012 #endif
0013 #ifndef SORT_CRITERION
0014 #define SORT_CRITERION std::less<std::string>
0015 #endif
0016
0017 #include "ATOOLS/Org/Getter_Function.H"
0018 #include "ATOOLS/Org/STL_Tools.H"
0019 #include "ATOOLS/Org/Shell_Tools.H"
0020 #include "ATOOLS/Org/Exception.H"
0021 #include "ATOOLS/Org/Message.H"
0022 #include "ATOOLS/Org/MyStrStream.H"
0023 #include <typeinfo>
0024 #include <cstdlib>
0025
0026 using namespace ATOOLS;
0027
0028 template<class ObjectType,class ParameterType,class SortCriterion>
0029 typename Getter_Function<ObjectType,ParameterType,SortCriterion>::
0030 String_Getter_Map *Getter_Function<ObjectType,ParameterType,SortCriterion>::
0031 s_getters=NULL;
0032
0033 template<class ObjectType,class ParameterType,class SortCriterion>
0034 bool Getter_Function<ObjectType,ParameterType,SortCriterion>::
0035 s_exactmatch=EXACTMATCH;
0036
0037 template<class ObjectType,class ParameterType,class SortCriterion>
0038 Getter_Function<ObjectType,ParameterType,SortCriterion>::
0039 Getter_Function(const std::string &name):
0040 m_display(true)
0041 {
0042 static bool initialized=false;
0043 if (!initialized || s_getters==NULL) {
0044 s_getters = new String_Getter_Map();
0045 initialized=true;
0046 }
0047 #ifdef DEBUG__Getter_Function
0048 std::cout<<"Getter_Function::Getter_Function(..): "
0049 <<"Added getter '"<<this<<"'("<<Demangle(typeid(*this).name())
0050 <<") -> \""<<name<<"\"."<<std::endl;
0051 #endif
0052 typename String_Getter_Map::iterator git(s_getters->find(name));
0053 if (git!=s_getters->end()) {
0054 std::cout<<std::string(80,'#')<<std::endl;
0055 std::cout<<"Getter_Function<"<<Demangle(typeid(ObjectType*).name())
0056 <<","<<Demangle(typeid(ParameterType*).name())<<"> {\n"
0057 <<" Doubled identifier \""<<name<<"\"!\n Now replacing '"
0058 <<Demangle(typeid(*git->second).name())<<"'.\n "
0059 <<"This operation may lead to wrong results "
0060 <<"or a program crash.\n}"<<std::endl;
0061 std::cout<<std::string(80,'#')<<std::endl;
0062 s_getters->erase(git);
0063 }
0064 s_getters->insert(std::pair<const std::string,
0065 Getter_Function *const>(name,this));
0066 }
0067
0068 template<class ObjectType,class ParameterType,class SortCriterion>
0069 Getter_Function<ObjectType,ParameterType,SortCriterion>::~Getter_Function()
0070 {
0071 if (s_getters==NULL) return;
0072 for (typename String_Getter_Map::iterator git=s_getters->begin();
0073 git!=s_getters->end();++git) {
0074 if (git->second==this) {
0075 s_getters->erase(git);
0076 break;
0077 }
0078 }
0079 if (s_getters->empty()) {
0080 delete s_getters;
0081 s_getters=NULL;
0082 }
0083 }
0084
0085 template<class ObjectType,class ParameterType,class SortCriterion>
0086 void Getter_Function<ObjectType,ParameterType,SortCriterion>::
0087 PrintInfo(std::ostream &str,const size_t width) const
0088 {
0089 str<<Demangle(typeid(*this).name());
0090 }
0091
0092 template<class ObjectType,class ParameterType,class SortCriterion>
0093 ObjectType * Getter_Function<ObjectType,ParameterType,SortCriterion>::
0094 operator()(const Parameter_Type ¶meters) const
0095 {
0096 std::cout<<"Getter_Function::operator(): "
0097 <<"Virtual function called."<<std::endl;
0098 return NULL;
0099 }
0100
0101 template<class ObjectType,class ParameterType,class SortCriterion>
0102 void Getter_Function<ObjectType,ParameterType,SortCriterion>::
0103 PrintGetterInfo(std::ostream &str,const size_t width, const std::string &indent,
0104 const std::string &separator, const std::string &lineend,
0105 const std::string &replacefrom, const std::string &replaceto)
0106 {
0107 if (s_getters==NULL) return;
0108 const IOS_BASE::fmtflags def=str.flags();
0109 str.setf(IOS_BASE::left,IOS_BASE::adjustfield);
0110 for (typename String_Getter_Map::const_iterator git=s_getters->begin();
0111 git!=s_getters->end();++git)
0112 {
0113 if (!git->second->m_display) continue;
0114 std::string escapedname=StringReplace(git->first,replacefrom,replaceto);
0115 str<<indent<<std::setw(width)<<escapedname<<separator;
0116 git->second->PrintInfo(str,width);
0117 str<<lineend;
0118 }
0119 str.setf(def);
0120 }
0121
0122 template<class ObjectType,class ParameterType,class SortCriterion>
0123 ObjectType *Getter_Function<ObjectType,ParameterType,SortCriterion>::
0124 GetObject(const Parameter_Type ¶meters) const
0125 {
0126 return (*this)(parameters);
0127 }
0128
0129 template<class ObjectType,class ParameterType,class SortCriterion>
0130 ObjectType *Getter_Function<ObjectType,ParameterType,SortCriterion>::
0131 GetObject(const std::string &name,const Parameter_Type ¶meters)
0132 {
0133 if (s_getters==NULL) return NULL;
0134 if (!s_exactmatch) {
0135 for (typename String_Getter_Map::reverse_iterator git=s_getters->rbegin();
0136 git!=s_getters->rend();++git) {
0137 if ((name.length()==0 && git->first.length()==0) ||
0138 (git->first.length()>0 && name.find(git->first)==0))
0139 return (*git->second)(parameters);
0140 }
0141 return NULL;
0142 }
0143 typename String_Getter_Map::iterator git=s_getters->find(name);
0144 if (git!=s_getters->end()) return (*git->second)(parameters);
0145 return NULL;
0146 }
0147
0148 template<class ObjectType,class ParameterType,class SortCriterion>
0149 std::vector<const Getter_Function<ObjectType,ParameterType,SortCriterion> *>
0150 Getter_Function<ObjectType,ParameterType,SortCriterion>::
0151 GetGetters(const std::string &name)
0152 {
0153 Getter_List list;
0154 if (s_getters==NULL) return list;
0155 for (typename String_Getter_Map::reverse_iterator git=s_getters->rbegin();
0156 git!=s_getters->rend();++git) {
0157 if (name.length()==0 ||
0158 (git->first.length()>0 && git->first.find(name)!=std::string::npos))
0159 list.push_back(git->second);
0160 }
0161 return list;
0162 }
0163
0164 template class ATOOLS::Getter_Function<OBJECT_TYPE,PARAMETER_TYPE,SORT_CRITERION>;
0165
0166 #endif