00001 #include "stdafx.h"
00002
00003 #include "BON.h"
00004 #include "BONImpl.h"
00005
00006 #include "SetRep.h"
00007 #include "CodeGen.h"
00008 #include "Dumper.h"
00009
00010 #include "logger.h"
00011
00012 #include "algorithm"
00013
00014 #include "globals.h"
00015 extern Globals global_vars;
00016 extern int h_ind;
00017
00018 SetRep::SetRep( BON::FCO& ptr, BON::FCO& resp_ptr)
00019 : FCO( ptr, resp_ptr)
00020 , m_memberList()
00021 , m_finalMemberList()
00022 , m_setMethods()
00023 {
00024 }
00025
00026
00027 SetRep::~SetRep()
00028 {
00029 m_memberList.clear();
00030 m_finalMemberList.clear();
00031 m_setMethods.clear();
00032 }
00033
00034
00035 void SetRep::addMember( FCO * member)
00036 {
00037 SetMemberList_ConstIterator it =
00038 std::find( m_memberList.begin(), m_memberList.end(), member);
00039
00040 if (it == m_memberList.end())
00041 m_memberList.push_back( member);
00042 else
00043 global_vars.err << "Set member " << member->getName() << " is twice in set " << getName() << "\n";
00044 }
00045
00046
00047 void SetRep::extendMembership()
00048 {
00049
00050 SetMemberList_ConstIterator member_it = m_memberList.begin();
00051 for( ; member_it != m_memberList.end(); ++member_it)
00052 {
00053 FCO * member_ptr = *member_it;
00054 std::vector<FCO*> family;
00055 member_ptr->getIntDescendants( family);
00056 family.push_back( member_ptr);
00057 std::vector<FCO*>::iterator desc_it = family.begin();
00058 for( ; desc_it != family.end(); ++desc_it)
00059 {
00060 FCO * new_member = *desc_it;
00061 if ( !new_member->isAbstract() &&
00062 m_finalMemberList.end() == std::find( m_finalMemberList.begin(), m_finalMemberList.end(), new_member))
00063 m_finalMemberList.push_back( new_member);
00064 }
00065 }
00066 }
00067
00068
00069 void SetRep::createMethods()
00070 {
00071
00072 SetMemberList_Iterator it = m_finalMemberList.begin();
00073 for( ; it != m_finalMemberList.end(); ++it)
00074 {
00075 if ( Dumper::m_bGenRegular)
00076 m_setMethods.push_back( CodeGen::dumpSetGetter( this, *it, "", false, false));
00077
00078 if ( Dumper::m_bGenTemplates)
00079 m_setMethods.push_back( CodeGen::dumpSetGetterGeneric( this, *it, "", false, false));
00080 }
00081
00082
00083 std::vector<FCO*> common_anc = FCO::lcdIntersect( m_finalMemberList);
00084 if ( !common_anc.empty())
00085 {
00086 std::vector<FCO*>::iterator c_it = common_anc.begin();
00087 if ( c_it != common_anc.end())
00088 {
00089 if ( Dumper::m_bGenRegular)
00090 m_setMethods.push_back( CodeGen::dumpSetGetter( this, *c_it, "", true, true));
00091
00092 if ( Dumper::m_bGenTemplates)
00093 m_setMethods.push_back( CodeGen::dumpSetGetterGeneric( this, *c_it, "", true, true));
00094 }
00095 }
00096 else if ( common_anc.empty())
00097 {
00098 std::string common_kind = FCO::lcdKindIntersect( m_finalMemberList);
00099 if ( !common_kind.empty())
00100 {
00101 if ( Dumper::m_bGenRegular)
00102 m_setMethods.push_back( CodeGen::dumpSetGetter( this, 0, common_kind, true, false));
00103
00104 if ( Dumper::m_bGenTemplates)
00105 m_setMethods.push_back( CodeGen::dumpSetGetterGeneric( this, 0, common_kind, true, false));
00106
00107 }
00108
00109 else
00110 global_vars.err << "Note: " << getName() << "Impl::getAllMembers() omitted since it can return only what the SetImpl::getMembers() does."<< "\n";
00111 }
00112 }
00113
00114
00115 std::string SetRep::doDump()
00116 {
00117 std::string h_file, c_file;
00118
00119 dumpPre( h_file, c_file);
00120 dumpFCO( h_file, c_file);
00121
00122 if ( !m_setMethods.empty())
00123 h_file += CodeGen::indent(1) + "//\n" + CodeGen::indent(1) + "// set getters\n";
00124
00125 MethodLexicographicSort lex;
00126 std::sort( m_setMethods.begin(), m_setMethods.end(), lex);
00127
00128 std::vector<Method>::iterator i = m_setMethods.begin();
00129 for( ; i != m_setMethods.end(); ++i)
00130 {
00131 h_file += i->getHeader() + "\n";
00132 c_file += i->getSource() + "";
00133 }
00134
00135 h_file += hideAndExpose();
00136
00137 dumpPost( h_file, c_file);
00138
00139 sendOutH( h_file);
00140 sendOutS( c_file);
00141
00142 return "";
00143 }
00144
00145
00146 bool SetRep::checkSetElements()
00147 {
00148 bool res = true;
00149 const int number_of_lists = 1;
00150 const SetMemberList* lists[ number_of_lists] = {
00151 &m_memberList
00152 };
00153
00154 for( int i = 0; i < number_of_lists; ++i)
00155 {
00156 SetMemberList_ConstIterator member_it = lists[i]->begin();
00157
00158 for( ; member_it != lists[i]->end(); ++member_it)
00159 {
00160 FCO * member_ptr = *member_it;
00161
00162 if ( !member_ptr->checkIsPartOfFinal() && !member_ptr->isAbstract())
00163 {
00164 global_vars.err << std::string("CHECK: set member " + member_ptr->getName() + " [in set " + getName() + "] has no parent.\n");
00165 res = false;
00166 }
00167 }
00168 }
00169 return res;
00170 }
00171
00172
00173 std::string SetRep::setGetterTemplate( const FCO * fco)
00174 {
00175 #if(LONG_NAMES)
00176 if (fco)
00177 {
00178 bool same_nmsp = fco->getValidNmspc() == getValidNmspc();
00179 return "get" + ( same_nmsp ? fco->getValidName() : fco->getValidNmspc() + fco->getValidName())) + "Members";
00180 }
00181 else
00182 return "getAllMembers";
00183 #else
00184 if (fco)
00185 {
00186 bool same_nmsp = fco->getValidNmspc() == getValidNmspc();
00187 return "get" + ( same_nmsp ? fco->getValidName() : ( fco->getValidNmspc() + fco->getValidName())) + "s";
00188 }
00189 else
00190 return "getAllMembers";
00191 #endif
00192 }
00193
00194
00195 std::string SetRep::expose( const std::string& repl_container)
00196 {
00197 std::string h_file;
00198 h_file += FCO::expose( repl_container);
00199
00200 if (!m_setMethods.empty())
00201 h_file += CodeGen::indent(h_ind) + "//\n" + CodeGen::indent(h_ind) + "// exposed set getters\n";
00202 std::vector<Method>::iterator i = m_setMethods.begin();
00203 for( ; i != m_setMethods.end(); ++i)
00204 {
00205 h_file += i->getExposed( repl_container) + "\n";
00206 }
00207
00208 return h_file;
00209 }
00210
00211
00212 std::string SetRep::hide()
00213 {
00214 std::string h_file;
00215 h_file += FCO::hide();
00216
00217 if (!m_setMethods.empty())
00218 h_file += CodeGen::indent(h_ind) + "//\n" + CodeGen::indent(h_ind) + "// hidden set getters\n";
00219 std::vector<Method>::iterator i = m_setMethods.begin();
00220 for( ; i != m_setMethods.end(); ++i)
00221 {
00222 h_file += i->getHidden() + "\n";
00223 }
00224
00225 return h_file;
00226 }
00227
00228