00001 #include "stdafx.h"
00002
00003 #include "BON.h"
00004 #include "BONImpl.h"
00005
00006 #include "AttributeRep.h"
00007 #include "FCO.h"
00008 #include "CodeGen.h"
00009
00010 #include "cctype"
00011 #include "algorithm"
00012
00013 #include "globals.h"
00014 #include "Dumper.h"
00015 extern Globals global_vars;
00016 extern int ind;
00017
00018
00019 AttributeRep::AttributeRep( BON::FCO& ptr)
00020 : Any( ptr)
00021 {
00022 }
00023
00024
00025 AttributeRep::~AttributeRep()
00026 {
00027 }
00028
00029
00030 bool AttributeRep::isGlobal()
00031 {
00032 return m_ptr->getAttribute("GlobalScope")->getBooleanValue();
00033 }
00034
00035
00036 bool AttributeRep::isViewable()
00037 {
00038 return m_ptr->getAttribute("IsViewable")->getBooleanValue();
00039 }
00040
00041
00042 std::string AttributeRep::getPrompt()
00043 {
00044 return Dumper::xmlFilter( m_ptr->getAttribute("Prompt")->getStringValue());
00045 }
00046
00047 std::string AttributeRep::getNameToUse()
00048 {
00049 std::string p = getName();
00050
00051 Any::convertToValidName(p);
00052 return p;
00053 }
00054
00055
00056 void AttributeRep::addOwner( FCO * owner)
00057 {
00058 std::vector<FCO*>::iterator it =
00059 std::find( m_ownerList.begin(), m_ownerList.end(), owner);
00060
00061 if ( it == m_ownerList.end())
00062 m_ownerList.push_back( owner);
00063 else
00064 global_vars.err << getName() << " attribute owned by " << owner->getName() << " twice\n";
00065 }
00066
00067
00068 int AttributeRep::hasAnyOwner() const
00069 {
00070 return m_ownerList.size();
00071 }
00072
00073
00074 EnumAttributeRep::EnumAttributeRep( BON::FCO& ptr)
00075 : AttributeRep( ptr), m_noOfItems(0), m_noOfDefault(0)
00076 {
00077 m_items.clear();
00078 m_itemsVal.clear();
00079 }
00080
00081
00082 bool EnumAttributeRep::getMenuItems()
00083 {
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 std::string items = Dumper::xmlFilter( m_ptr->getAttribute("MenuItems")->getStringValue());
00097
00098 items += '\n';
00099 std::string::size_type len = items.length();
00100
00101 std::string whitespace = "\r\n\t ";
00102 std::string::size_type start_i = 0;
00103 while (start_i < len && whitespace.find(items[start_i]) != std::string::npos )
00104 ++start_i;
00105
00106 std::string::size_type i = start_i, comma_i = 0;
00107 while( i < len )
00108 {
00109 if ( items[i] == '\n' && i - start_i > 0)
00110 {
00111 if ( comma_i != 0)
00112 {
00113 std::string::size_type st,end;
00114
00115 st = start_i;
00116 while( st < comma_i && whitespace.find( items[st]) != std::string::npos) ++st;
00117 end = comma_i-1;
00118 while( end > start_i && whitespace.find( items[end]) != std::string::npos) --end;
00119
00120 if ( end>=st)
00121 m_items.push_back(items.substr( st, end - st + 1));
00122 else
00123 {
00124 m_items.push_back("Error");
00125 global_vars.err << MSG_ERROR << "Error: Enumitem #" << 1 + m_noOfItems << " of " << getPtr() << " has wrong format. (If comma is used a second value is required.)\n";
00126 }
00127
00128
00129 st = comma_i+1;
00130 while( st < i && whitespace.find( items[st]) != std::string::npos) ++st;
00131 end = i;
00132 while( end > comma_i+1 && whitespace.find( items[end]) != std::string::npos) --end;
00133
00134 if ( end>=st && comma_i+1 < i)
00135 m_itemsVal.push_back(items.substr( st, end - st + 1));
00136 else
00137 {
00138 m_itemsVal.push_back("Error");
00139 global_vars.err << MSG_ERROR << "Error: Enumitem #" << 1 + m_noOfItems << " of " << getPtr() << " has wrong format. (If comma is used a second value is required.)\n";
00140 }
00141
00142 comma_i = 0;
00143 }
00144 else
00145 {
00146 std::string::size_type st,end;
00147
00148 st = start_i;
00149 while( st < i && whitespace.find( items[st]) != std::string::npos) ++st;
00150 end = i;
00151 while( end > start_i && whitespace.find( items[end]) != std::string::npos) --end;
00152
00153 if ( end>=st)
00154 {
00155 m_items.push_back(items.substr( st, end - st + 1));
00156 m_itemsVal.push_back(items.substr( st, end - st + 1));
00157 }
00158 }
00159 ++m_noOfItems;
00160 start_i = i+1;
00161 while (start_i < len && whitespace.find(items[start_i]) != std::string::npos) ++start_i;
00162 i = start_i-1;
00163 }
00164 else if ( items[i] == ',')
00165 {
00166 comma_i = i;
00167 }
00168 ++i;
00169 }
00170
00171 if( m_noOfItems == 0)
00172 global_vars.err << MSG_ERROR << "Error: No enumeration items found. Please check: " << getPtr() << ".\n";
00173
00174 bool filled_def_item = m_ptr->getAttribute( "DefaultItem")->getStatus() >= BON::AS_Here;
00175 std::string def_item = Dumper::xmlFilter( m_ptr->getAttribute("DefaultItem")->getStringValue());
00176
00177 bool found ( false);
00178 i = 0;
00179 while (i < m_noOfItems && !found)
00180 {
00181 if ( m_items[i] == def_item ) found = true;
00182 else ++i;
00183 }
00184
00185 if (found)
00186 m_noOfDefault = i;
00187 else if( filled_def_item)
00188 global_vars.err << MSG_ERROR << "Error: Default enumitem not found among enumerated items at " << getPtr() << ".\n";
00189
00190 return true;
00191 }
00192
00193
00194
00195 std::string EnumAttributeRep::enumTypeName( EnumAttributeRep * a)
00196 {
00197 std::string type = a->getNameToUse();
00198 type += "_Type";
00199 return type;
00200 }
00201
00202
00203 std::string EnumAttributeRep::getMethodName()
00204 {
00205 return "get" + getNameToUse();
00206 }
00207
00208
00209 std::string EnumAttributeRep::getSetMethodName()
00210 {
00211 return "set" + getNameToUse();
00212 }
00213
00214
00215 Method EnumAttributeRep::createMethodForAttr( FCO * container)
00216 {
00217 Method m2;
00218 if (m_noOfItems == 0) getMenuItems();
00219 if (m_noOfItems == 0) return m2;
00220
00221 std::string mmm = "";
00222 mmm = CodeGen::indent(1) + "typedef enum\n" + CodeGen::indent(1) + "{";
00223 for(int i = 0; i < m_noOfItems; ++i)
00224 {
00225 std::string item_i = m_itemsVal[i] + '_' + EnumAttributeRep::enumTypeName( this);
00226 if ( !Any::checkIfValidName( item_i))
00227 {
00228 global_vars.err << "Invalid enumeration value \"" << item_i << "\" defined in " << EnumAttributeRep::enumTypeName( this) << ". Non alphanumeric characters replaced with underscore.\n";
00229 Any::convertToValidName( item_i);
00230 }
00231
00232 if ( i != 0) mmm += ",";
00233 mmm += "\n" + CodeGen::indent(2) + item_i;
00234 }
00235 mmm += "\n" + CodeGen::indent(1) + "} " + EnumAttributeRep::enumTypeName( this) + ";\n";
00236
00237 container->addClassGlobal( mmm);
00238
00239 std::string src;
00240
00241 src = CodeGen::indent(0) + "{\n";
00242 src += CodeGen::indent(1) + "std::string val = FCOImpl::getAttribute(\"" + getName() + "\")->getStringValue();\n";
00243 for( int i = 0; i < m_noOfItems; ++i)
00244 {
00245 std::string item_i = m_itemsVal[i] + '_' + EnumAttributeRep::enumTypeName( this);
00246 if ( !Any::checkIfValidName( item_i))
00247 Any::convertToValidName( item_i);
00248
00249 src += "\n" + CodeGen::indent(1);
00250 if ( i != 0) src += "else ";
00251 src += "if ( val == \"" + m_itemsVal[i] + "\") return " + item_i + ";";
00252 }
00253 src += "\n" + CodeGen::indent(1) + "else throw(\"None of the possible items\");\n}\n\n\n";
00254
00255 m2.m_returnValue = container->getLValidNameImpl() + "::" + EnumAttributeRep::enumTypeName( this);
00256 m2.m_signature = getMethodName() + "()";
00257 m2.m_container = container;
00258 m2.m_implementation = src;
00259 m2.m_comment = "";
00260
00261 return m2;
00262 }
00263
00264
00265 Method EnumAttributeRep::createSetMethodForAttr( FCO * container)
00266 {
00267
00268
00269 Method m2;
00270 if (m_noOfItems == 0) return m2;
00271
00272 std::string src;
00273
00274 src = CodeGen::indent(0) + "{\n";
00275 src += CodeGen::indent(1) + "std::string str_val = \"\";\n\n";
00276
00277 for( int i = 0; i < m_noOfItems; ++i)
00278 {
00279 std::string item_i = m_itemsVal[i] + '_' + EnumAttributeRep::enumTypeName( this);
00280 if ( !Any::checkIfValidName( item_i))
00281 Any::convertToValidName( item_i);
00282
00283 src += CodeGen::indent(1);
00284 if ( i != 0) src += "else ";
00285 src += "if ( val == " + item_i + ") str_val = \"" + m_itemsVal[i] + "\";\n";
00286 }
00287 src += CodeGen::indent(1) + "else throw(\"None of the possible items\");\n";
00288 src += "\n";
00289 src += CodeGen::indent(1) + "FCOImpl::getAttribute(\"" + getName() + "\")->setStringValue( str_val);\n";
00290 src += "}\n\n\n";
00291
00292 m2.m_returnValue = "void";
00293 m2.m_signature = getSetMethodName() + "( " + container->getValidNameImpl() + "::" + EnumAttributeRep::enumTypeName( this) + " val)";
00294 m2.m_container = container;
00295 m2.m_implementation = src;
00296 m2.m_comment = "";
00297
00298 return m2;
00299 }
00300
00301
00302 std::string EnumAttributeRep::doDumpErroneousAttrHdr()
00303 {
00304 std::string hdr;
00305 hdr = CodeGen::indent(1) + "/*virtual*/ inline std::string " + getMethodName() + "() ";
00306 hdr += "{ ";
00307 hdr += "throw std::string(\"Interface inherited kind. \" + getName() + \" doesn't have " + getNameToUse() + " attribute\");";
00308 hdr += " }\n";
00309
00310
00311 hdr += "\n";
00312
00313
00314
00315 hdr += CodeGen::indent(1) + "/*virtual*/ inline void " + getSetMethodName() + "(" + EnumAttributeRep::enumTypeName( this) + " val)";
00316 hdr += "{ ";
00317 hdr += "throw std::string(\"Interface inherited kind. \" + getName() + \" doesn't have " + getNameToUse() + " attribute\");";
00318 hdr += " }\n";
00319
00320 return hdr;
00321 }
00322
00323
00324 std::string EnumAttributeRep::doDumpErroneousAttrSrc( FCO*)
00325 {
00326 return "";
00327 }
00328
00329
00330 std::string BoolAttributeRep::getMethodName()
00331 {
00332 return "is" + getNameToUse();
00333 }
00334
00335
00336 std::string BoolAttributeRep::getSetMethodName()
00337 {
00338 return "set" + getNameToUse();
00339 }
00340
00341
00342 Method BoolAttributeRep::createMethodForAttr( FCO * container)
00343 {
00344 Method m;
00345 std::string src;
00346 src = CodeGen::indent(0) + "{\n";
00347 src += CodeGen::indent(1) + "return FCOImpl::getAttribute(\"" + getName() + "\")->getBooleanValue();\n";
00348 src += CodeGen::indent(0) + "}\n\n\n";
00349
00350 m.m_returnValue = "bool";
00351 m.m_signature = getMethodName() + "() ";
00352 m.m_implementation = src;
00353 m.m_container = container;
00354 m.m_comment = "";
00355 return m;
00356 }
00357
00358
00359 Method BoolAttributeRep::createSetMethodForAttr( FCO * container)
00360 {
00361 Method m;
00362 std::string src;
00363 src = CodeGen::indent(0) + "{\n";
00364 src += CodeGen::indent(1) + "FCOImpl::getAttribute(\"" + getName() + "\")->setBooleanValue( val);\n";
00365 src += CodeGen::indent(0) + "}\n\n\n";
00366
00367 m.m_returnValue = "void";
00368 m.m_signature = getSetMethodName() + "( bool val)";
00369 m.m_implementation = src;
00370 m.m_container = container;
00371 m.m_comment = "";
00372 return m;
00373 }
00374
00375
00376 std::string BoolAttributeRep::doDumpErroneousAttrHdr()
00377 {
00378 std::string hdr;
00379 hdr = CodeGen::indent(1) + "/*virtual*/ inline bool " + getMethodName() + "() ";
00380 hdr += "{";
00381 hdr += "throw std::string(\"Interface inherited kind. \" + getName() + \" doesn't have " + getNameToUse() + " attribute\");";
00382 hdr += "}\n";
00383
00384
00385 hdr += "\n";
00386 hdr += CodeGen::indent(1) + "/*virtual*/ inline void " + getSetMethodName() + "( bool) ";
00387 hdr += "{";
00388 hdr += "throw std::string(\"Interface inherited kind. \" + getName() + \" doesn't have " + getNameToUse() + " attribute\");";
00389 hdr += "}\n";
00390
00391
00392 return hdr;
00393 }
00394
00395
00396 std::string BoolAttributeRep::doDumpErroneousAttrSrc( FCO*)
00397 {
00398 return "";
00399 }
00400
00401
00402 std::string FieldAttributeRep::getMethodName()
00403 {
00404 return "get" + getNameToUse();
00405 }
00406
00407
00408 std::string FieldAttributeRep::getSetMethodName()
00409 {
00410 return "set" + getNameToUse();
00411 }
00412
00413
00414 std::string FieldAttributeRep::getTypeStr()
00415 {
00416 std::string val_type = m_ptr->getAttribute("DataType")->getStringValue();
00417 if ( val_type == "integer")
00418 return "long";
00419 else if ( val_type == "double")
00420 return "double";
00421 else if ( val_type == "string")
00422 return "std::string";
00423
00424 return "Error";
00425 }
00426
00427
00428 std::string FieldAttributeRep::getSetTypeStr()
00429 {
00430 std::string val_type = m_ptr->getAttribute("DataType")->getStringValue();
00431 if ( val_type == "integer")
00432 return "const long";
00433 else if ( val_type == "double")
00434 return "const double";
00435 else if ( val_type == "string")
00436 return "const std::string&";
00437
00438 return "Error";
00439 }
00440
00441
00442 std::string FieldAttributeRep::getMethodStr()
00443 {
00444 std::string val_type = m_ptr->getAttribute("DataType")->getStringValue();
00445 if ( val_type == "integer")
00446 return "Integer";
00447 else if ( val_type == "double")
00448 return "Real";
00449 else if ( val_type == "string")
00450 return "String";
00451
00452 return "Error";
00453 }
00454
00455
00456 Method FieldAttributeRep::createMethodForAttr( FCO * container)
00457 {
00458 Method m;
00459 std::string src;
00460 src = CodeGen::indent(0) + "{\n";
00461 src += CodeGen::indent(1) + "return FCOImpl::getAttribute(\"" + getName() + "\")->get" + getMethodStr() + "Value();\n";
00462 src += CodeGen::indent(0) + "}\n\n\n";
00463
00464 m.m_returnValue = getTypeStr();
00465 m.m_signature = getMethodName() + "() ";
00466 m.m_implementation = src;
00467 m.m_container = container;
00468 m.m_comment = "";
00469
00470 return m;
00471 }
00472
00473
00474 Method FieldAttributeRep::createSetMethodForAttr( FCO * container)
00475 {
00476 Method m;
00477 std::string src;
00478 src = CodeGen::indent(0) + "{\n";
00479 src += CodeGen::indent(1) + "FCOImpl::getAttribute(\"" + getName() + "\")->set" + getMethodStr() + "Value( val);\n";
00480 src += CodeGen::indent(0) + "}\n\n\n";
00481
00482 m.m_returnValue = "void";
00483 m.m_signature = getSetMethodName() + "( " + getSetTypeStr() + " val)";
00484 m.m_implementation = src;
00485 m.m_container = container;
00486 m.m_comment = "";
00487
00488 return m;
00489 }
00490
00491
00492 std::string FieldAttributeRep::doDumpErroneousAttrHdr()
00493 {
00494 std::string hdr;
00495 hdr = CodeGen::indent(1) + "/*virtual*/ inline " + getTypeStr() + " " + getMethodName() + "() ";
00496 hdr += "{";
00497 hdr += "throw std::string(\"Interface inherited kind. \" + getName() + \" doesn't have " + getNameToUse() + " attribute\");";
00498 hdr += "}\n";
00499
00500
00501 hdr += "\n";
00502 hdr += CodeGen::indent(1) + "/*virtual*/ inline void " + getSetMethodName() + "( " + getSetTypeStr() + ") ";
00503 hdr += "{";
00504 hdr += "throw std::string(\"Interface inherited kind. \" + getName() + \" doesn't have " + getNameToUse() + " attribute\");";
00505 hdr += "}\n";
00506
00507 return hdr;
00508 }
00509
00510
00511 std::string FieldAttributeRep::doDumpErroneousAttrSrc( FCO*)
00512 {
00513 return "";
00514 }