libFAUDES

Sections

Index

cfl_types.cpp

Go to the documentation of this file.
00001 /** @file cfl_types.cpp Runtime interface, faudes types */
00002 
00003 /* FAU Discrete Event Systems Library (libfaudes)
00004 
00005 Copyright (C) 2009 Ruediger Berndt
00006 Copyright (C) 2010 Thomas Moor
00007 
00008 This library is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU Lesser General Public
00010 License as published by the Free Software Foundation; either
00011 version 2.1 of the License, or (at your option) any later version.
00012 
00013 This library is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 Lesser General Public License for more details.
00017 
00018 You should have received a copy of the GNU Lesser General Public
00019 License along with this library; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00021 
00022 
00023 #include "cfl_types.h"
00024 
00025 namespace faudes{
00026 
00027 /*
00028 ********************************************************************
00029 ********************************************************************
00030 ********************************************************************
00031 
00032 implemantation of faudes Type
00033 
00034 ********************************************************************
00035 ********************************************************************
00036 ********************************************************************
00037 */
00038 
00039 // static members: ref to the only one instance
00040 const std::string Type::mName("TypeStdName");
00041 
00042 // constructor
00043 Type::Type(void)  {}
00044 
00045 // copy constructor
00046 Type::Type(const Type& rType)  {(void) rType;}
00047 
00048 // destructor
00049 Type::~Type(void) {}
00050 
00051 // pointer constructor
00052 Type* Type::New(void) const { 
00053   return new Type(); 
00054 }
00055 
00056 // pointer copy constructor
00057 Type* Type::Copy(void) const { 
00058   return new Type(*this); 
00059 }
00060 
00061 // cast
00062 const Type* Type::Cast(const Type* pOther) const {
00063   return pOther;
00064 }
00065 
00066 // Clear()
00067 void Type::Clear(void) {
00068   FD_DC("Type::Clear(): not re-implemented in " << typeid(*this).name());
00069 }
00070 
00071 // assign
00072 Type& Type::Assign(const Type& rSource) {
00073   FD_DC("Type(" << this << ")::Assign(" << &rSource << ")");
00074   Clear();
00075   return *this;
00076 }
00077 
00078 // equality
00079 bool Type::Equal(const Type& rOther) const { 
00080   return true;
00081 }   
00082 
00083 // equality
00084 bool Type::operator==(const Type& rOther) const { 
00085   return true;
00086 }
00087 
00088 // equality
00089 bool Type::operator!=(const Type& rOther) const { 
00090   return false;
00091 }
00092 
00093 // assign
00094 Type& Type::operator=(const Type& rSource) {
00095   FD_DC("Type(" << this << ")::AssignementOperator(" << &rSource << ")");
00096   Clear();
00097   return *this;
00098 }
00099 
00100 // assign
00101 Type& Type::DoAssign(const Type& rSource) {
00102   FD_DC("Type(" << this << ")::DoAssign(" << &rSource << ")");
00103   return *this;
00104 }
00105 
00106 // equality
00107 bool Type::DoEqual(const Type& rOther) const {
00108   FD_DC("Type(" << this << ")::DoEqual(" << &rOther << ")");
00109   return true;
00110 }
00111 
00112 // Name(rName)
00113 void Type::Name(const std::string& rName) {
00114 }
00115 
00116 // Name()
00117 const std::string& Type::Name(void) const {
00118   return mName;
00119 }
00120 
00121 // Write(context)
00122 void Type::Write(const Type* pContext) const {
00123   TokenWriter tw(TokenWriter::Stdout);
00124   DoWrite(tw,"",pContext);
00125 }
00126 
00127 // Write(rFileName, label, context, openmode)
00128 void Type::Write(const std::string& rFileName, const std::string& rLabel, 
00129      const Type* pContext, std::ios::openmode openmode) const {
00130   try {
00131     TokenWriter tw(rFileName, openmode);
00132     DoWrite(tw, rLabel, pContext);
00133   }
00134   catch (std::ios::failure&) {
00135     std::stringstream errstr;
00136     errstr << "Exception opening/writing file \"" << rFileName << "\"";
00137     throw Exception("Type::Write", errstr.str(), 2);
00138   }
00139 }
00140 
00141 // Write(rFileName, openmode)
00142 void Type::Write(const std::string& rFileName, std::ios::openmode openmode) const {
00143   Write(rFileName,"",0,openmode);
00144 }
00145 
00146 // Write(tw,label,context)
00147 void Type::Write(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
00148   DoWrite(rTw,rLabel,pContext);
00149 }
00150 
00151 // ToString(label, context)
00152 std::string Type::ToString(const std::string& rLabel, const Type* pContext) const {
00153   TokenWriter tw(TokenWriter::String);
00154   tw.Endl(false);
00155   DoWrite(tw,rLabel,pContext);
00156   return tw.Str();
00157 }
00158 
00159 // ToSText()
00160 std::string Type::ToSText(void) const {
00161   TokenWriter tw(TokenWriter::String);
00162   tw.Endl(true);
00163   DoSWrite(tw);
00164   return tw.Str();
00165 }
00166 
00167 // ToString(label, context)
00168 std::string Type::ToText(const std::string& rLabel, const Type* pContext) const {
00169   TokenWriter tw(TokenWriter::String);
00170   tw.Endl(true);
00171   DoWrite(tw,rLabel,pContext);
00172   return tw.Str();
00173 }
00174 
00175 // DWrite(context)
00176 void Type::DWrite(const Type* pContext) const {
00177   TokenWriter tw(TokenWriter::Stdout);
00178   DoDWrite(tw,"",pContext);
00179 }
00180 
00181 // DWrite(rFileName, label, context, openmode)
00182 void Type::DWrite(const std::string& rFileName, const std::string& rLabel, 
00183       const Type* pContext, std::ios::openmode openmode) const {
00184   try {
00185     TokenWriter tw(rFileName, openmode);
00186     DoDWrite(tw, rLabel, pContext);
00187   }
00188   catch (std::ios::failure&) {
00189     std::stringstream errstr;
00190     errstr << "Exception opening/writing file \"" << rFileName << "\"";
00191     throw Exception("Type::DWrite", errstr.str(), 2);
00192   }
00193 }
00194 
00195 // DWrite(tw,label,context)
00196 void Type::DWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
00197   DoDWrite(rTw,rLabel,pContext);
00198 }
00199 
00200 // SWrite()
00201 void Type::SWrite(void) const {
00202   TokenWriter tw(TokenWriter::Stdout);
00203   DoSWrite(tw);
00204 }
00205 
00206 // SWrite(tw)
00207 void Type::SWrite(TokenWriter& rTw) const {
00208   DoSWrite(rTw);
00209 }
00210 
00211 // Read(rFilename, rLabel,context)
00212 void Type::Read(const std::string& rFilename, const std::string& rLabel, const Type* pContext) {
00213   Clear();
00214   TokenReader tr(rFilename);
00215   DoRead(tr,rLabel,pContext);
00216 }
00217 
00218 // Read(rTr, rLabel, context)
00219 void Type::Read(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
00220   Clear();
00221   DoRead(rTr,rLabel,pContext);
00222 }
00223 
00224 // FromString(rString, rLabel, context)
00225 void Type::FromString(const std::string& rString, const std::string& rLabel, const Type* pContext) {
00226   Clear();
00227   TokenReader tr(TokenReader::String,rString);
00228   DoRead(tr,rLabel,pContext);
00229 }
00230   
00231 //DoWrite(rTr,rLabel,pContext)
00232 void Type::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
00233   (void) rTw; (void) rLabel; (void) pContext;
00234   FD_DC("Type::DoWrite(): not re-implemented in " << typeid(*this).name());
00235 }
00236 
00237 //DoDWrite(rTr,rLabel,pContext)
00238 void Type::DoDWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const {
00239   FD_DC("Type::DoDWrite(): not re-implemented in " << typeid(*this).name() << ", using DoDWrite instead");
00240   DoWrite(rTw,rLabel,pContext);
00241 }
00242 
00243 //DoSWrite(rTr,rLabel,pContext)
00244 void Type::DoSWrite(TokenWriter& rTw) const {
00245   FD_DC("Type::DoSWrite(): not re-implemented in " << typeid(*this).name());
00246   rTw.WriteComment("");
00247   rTw.WriteComment(" Statistics for " + Name());
00248   rTw.WriteComment("");
00249 }
00250 
00251 //DoRead(rTr,rLabel,pContext)
00252 void Type::DoRead(TokenReader& rTr, const std::string& rLabel, const Type* pContext) {
00253   (void) rLabel; (void) pContext; (void) rTr;
00254   FD_DC("Type::DoRead(): not re-implemented in " << typeid(*this).name());
00255 }
00256 
00257 
00258 
00259 /*
00260 ********************************************************************
00261 ********************************************************************
00262 ********************************************************************
00263 
00264 Implementation of class Documentation
00265 
00266 ********************************************************************
00267 ********************************************************************
00268 ********************************************************************
00269 */
00270 
00271 // faudes type
00272 FAUDES_TYPE_IMPLEMENTATION(Documentation,Type)
00273 
00274 // construct
00275 Documentation::Documentation(void) : Type() {
00276 }
00277 
00278 // construct
00279 Documentation::Documentation(const Documentation& rOther) : Type() {
00280   DoAssign(rOther);
00281 }
00282 
00283 // std faudes type
00284 Documentation& Documentation::DoAssign(const Documentation& rSrc) {
00285   // assign my members
00286   mName=rSrc.mName;
00287   mPlugIn=rSrc.mPlugIn;
00288   mHtmlDoc=rSrc.mHtmlDoc;
00289   mTextDoc=rSrc.mTextDoc;
00290   mKeywords=rSrc.mKeywords;
00291   return *this;
00292 }
00293 
00294 // std faudes type
00295 bool Documentation::DoEqual(const Documentation& rOther) const {
00296   // test my members
00297   if(mName!=rOther.mName) return false;
00298   if(mPlugIn!=rOther.mPlugIn) return false;
00299   if(mHtmlDoc!=rOther.mHtmlDoc) return false;
00300   if(mTextDoc!=rOther.mTextDoc) return false;
00301   if(mKeywords!=rOther.mKeywords) return false;
00302   return true;
00303 }
00304 
00305 // clear all
00306 void Documentation::Clear(void){
00307   FD_DRTI("Documentation::Clear()");
00308   mName="";
00309   mPlugIn="CoreFaudes";
00310   mHtmlDoc="";
00311   mTextDoc="";
00312   mKeywords="";
00313 }
00314 
00315 //  read access members
00316 const std::string& Documentation::Name(void) const{ return mName; }
00317 const std::string& Documentation::PlugIn(void) const{ return mPlugIn; }
00318 const std::string& Documentation::CType(void) const{ return mCType; }
00319 const std::string& Documentation::TextDoc() const{ return mTextDoc; }
00320 const std::string& Documentation::HtmlDoc() const{ return mHtmlDoc; }
00321 const std::string& Documentation::Keywords() const{ return mKeywords; }
00322 
00323 //  write access members
00324 void Documentation::Name(const std::string& name){mName = name; }
00325 void Documentation::PlugIn(const std::string& plugin){mPlugIn = plugin; }
00326 void Documentation::CType(const std::string& name){mCType = name; }
00327 void Documentation::TextDoc(const std::string& textdoc){mTextDoc = textdoc;}
00328 void Documentation::HtmlDoc(const std::string& fname){mHtmlDoc = fname;}
00329 
00330 //  write access keyword
00331 void Documentation::AddKeyword(const std::string& rKeyword){
00332   if(mKeywords.empty()){
00333     mKeywords = rKeyword;
00334   } else{
00335     mKeywords.append(1, mDelim);
00336     mKeywords.append(rKeyword);
00337   }
00338 }
00339 
00340 // regex match keywords (todo)
00341 std::string Documentation::MatchKeyword(const std::string& rPattern) const{
00342   FD_DRTI("Documentation::MatchKeyword(" << rPattern << ")");
00343   // be case insensitive (inefficient)
00344   std::string keys = mKeywords;
00345   std::transform(keys.begin(), keys.end(), keys.begin(), tolower);
00346   std::string match = rPattern;
00347   std::transform(match.begin(), match.end(), match.begin(), tolower);
00348   // find match
00349   std::string res="";
00350   std::size_t posk=keys.find(match);
00351   if(posk==std::string::npos) return res;
00352   // find delimiters 
00353   std::size_t posa=0;
00354   std::size_t posb=keys.length();
00355   while(true) {
00356     posb=keys.find(mDelim,posa);
00357     if(posb==std::string::npos) posb=mKeywords.length();
00358     if(posb>posk) break;
00359     posa=posb+1;
00360   };
00361   // return original case match
00362   FD_DRTI("Documentation::MatchKeyword(" << rPattern << "): " << mKeywords.substr(posa,posb-posa));
00363   return mKeywords.substr(posa,posb-posa);
00364 }
00365 
00366 
00367 // positional keyword
00368 std::string Documentation::KeywordAt(int pos) const{
00369   FD_DRTI("Documentation::KeywordAt(" << pos << ")");
00370   // default result
00371   std::string res="";
00372   // find delimiter
00373   std::size_t posa=0;
00374   std::size_t posb=mKeywords.length();
00375   while(pos>0) {
00376     if(posa>=mKeywords.length()) return res;
00377     posb=mKeywords.find(mDelim,posa);
00378     if(posb==std::string::npos) return res;
00379     posa=posb+1;
00380     pos--;
00381   };
00382   // default result
00383   res=mKeywords.substr(posa);
00384   // find next delimiter
00385   posb=res.find(mDelim);
00386   if(posb==std::string::npos) return res;
00387   res=res.substr(0,posb);
00388   return res;
00389 }
00390 
00391 // keyword
00392 int Documentation::KeywordsSize(void) const{
00393   int cnt=0;
00394   // find delimiter
00395   std::size_t pos=0;
00396   while(pos<mKeywords.length()) {
00397     cnt++;
00398     pos=mKeywords.find(mDelim,pos);
00399     if(pos==std::string::npos) break;
00400     pos++;
00401   };
00402   return cnt;
00403 }
00404 
00405 
00406 // merge with docu from token io
00407 void Documentation::MergeDocumentationBody(TokenReader& rTr) {
00408   FD_DRTI("Documentation::MergeDocumentationBody(): file " << rTr.FileName() << " for " << mName);
00409   // peek type ident
00410   Token token;
00411   rTr.Peek(token);
00412   // separate name
00413   std::string name = token.StringValue();
00414   size_t pos=name.find("::");
00415   if(pos!=std::string::npos) name=name.substr(pos+2);
00416   // match?
00417   if(mName!=name && mName!="") {
00418     std::stringstream errstr;
00419     errstr << "Documentation mismatch in file \"" << rTr.FileName() << "\" : " << mName << "!=" << name;
00420     throw Exception("Documentation::MergeDocumentationBody", errstr.str(), 48);
00421   };
00422   // read core
00423   DoReadCore(rTr);  
00424 }
00425 
00426 // token io
00427 void Documentation::DoRead(TokenReader& rTr,  const std::string& rLabel, const Type* pContext) {
00428   FD_DRTI("Documentation::DoRead(): file " << rTr.FileName());
00429   // ignore
00430   (void) pContext;
00431   // figure ma section
00432   std::string label = rLabel;
00433   if(label=="") label="Documentation";
00434   // doit
00435   rTr.ReadBegin(label);
00436   // peek and set name
00437   Token token;
00438   rTr.Peek(token);
00439   // extract name 
00440   mName=token.StringValue();
00441   size_t pos=mName.find("::");
00442   if(pos!=std::string::npos) mName=mName.substr(pos+2);
00443   // do read
00444   DoReadCore(rTr);
00445   // done
00446   rTr.ReadEnd(label);
00447 }
00448 
00449 // token read
00450 void Documentation::DoReadCore(TokenReader& rTr) {
00451   FD_DRTI("Documentation::DoReadCore()");
00452   Token token;
00453   rTr.Peek(token);
00454   // my faudes entity (eg type name or function name)
00455   mName=token.StringValue();
00456   size_t pos=mName.find("::");
00457   if(pos!=std::string::npos) mName=mName.substr(pos+2);
00458   // my faudes plugin
00459   mPlugIn=token.StringValue();
00460   pos=mPlugIn.find("::");
00461   if(pos!=std::string::npos) mPlugIn=mPlugIn.substr(0,pos);
00462   else mPlugIn = "CoreFaudes";
00463   // flush and token check
00464   rTr.ReadString();
00465   // corresponding c type
00466   mCType="";
00467   rTr.Peek(token);
00468   if(token.Type()==Token::Option)
00469     mCType = rTr.ReadOption();
00470   // loop other known entries
00471   while(true) {
00472     rTr.Peek(token);
00473     // do the read: text
00474     if(token.Type()==Token::Begin)  
00475     if(token.StringValue()=="TextDoc") {
00476       rTr.ReadBegin("TextDoc");
00477       mTextDoc = rTr.ReadString();
00478       rTr.ReadEnd("TextDoc");
00479       continue;
00480     }
00481     // do the read: html
00482     if(token.Type()==Token::Begin)  
00483     if(token.StringValue()=="HtmlDoc") {
00484       rTr.ReadBegin("HtmlDoc");
00485       mHtmlDoc = rTr.ReadString();
00486       rTr.ReadEnd("HtmlDoc");
00487       continue;
00488     }
00489     // do the read: keys
00490     if(token.Type()==Token::Begin)  
00491     if(token.StringValue()=="Keywords") {
00492       rTr.ReadBegin("Keywords");
00493       while(!rTr.Eos("Keywords")) 
00494         AddKeyword(rTr.ReadString());
00495       rTr.ReadEnd("Keywords");
00496       continue;
00497     }
00498     // unknown -> derived class take over
00499     break;
00500   }
00501 }
00502 
00503 
00504 // token write
00505 void Documentation::DoWrite(TokenWriter& rTw,  const std::string& rLabel, const Type* pContext) const {
00506   // ignore
00507   (void) pContext;
00508   // figure my section
00509   std::string label = rLabel;
00510   if(label=="") label="Documentation";
00511   // doit
00512   rTw.WriteBegin(label);
00513   DoWriteCore(rTw); 
00514   rTw.WriteEnd(label);
00515 }
00516 
00517 // token write
00518 void Documentation::DoWriteCore(TokenWriter& rTw) const {
00519   // write name
00520   rTw << mPlugIn + "::" + mName ;
00521   // write c type
00522   if(mCType!="") rTw.WriteOption(mCType);
00523   // write text doc
00524   rTw.WriteBegin("TextDoc");
00525   rTw << mTextDoc;
00526   rTw.WriteEnd("TextDoc");
00527   // write html doc
00528   rTw.WriteBegin("HtmlDoc");
00529   rTw << mHtmlDoc;
00530   rTw.WriteEnd("HtmlDoc");
00531   // write keys
00532   rTw.WriteBegin("Keywords");
00533   rTw << mKeywords;
00534   rTw.WriteEnd("Keywords");
00535 }
00536 
00537 
00538 
00539 /*
00540 ********************************************************************
00541 ********************************************************************
00542 ********************************************************************
00543 
00544 Implementation of class TypeDefinition
00545 
00546 ********************************************************************
00547 ********************************************************************
00548 ********************************************************************
00549 */
00550 
00551 
00552 // typedefinition constructor function
00553 TypeDefinition* TypeDefinition::Constructor(Type* pProto, const std::string& rTypeName){
00554   FD_DRTI("TypeDefinition::Construct(" << typeid(pProto).name() << ", " << rTypeName << ")");
00555   TypeDefinition* td = new TypeDefinition();
00556   td->Prototype(pProto);
00557   std::string name=rTypeName;
00558   if(name=="") name=typeid(pProto).name();
00559   td->Name(name);
00560   return(td);
00561 }
00562 
00563 
00564 // token io
00565 void TypeDefinition::DoRead(TokenReader& rTr,  const std::string& rLabel, const Type* pContext) {
00566   FD_DRTI("TypeDefinition::DoRead(): file " << rTr.FileName());
00567   // ignore
00568   (void) pContext;
00569   // figure my section
00570   std::string label = rLabel;
00571   if(label=="") label="TypeDefinition";
00572   // base can handle this
00573   Documentation::DoRead(rTr,label,pContext);
00574 }
00575 
00576 // token io
00577 void TypeDefinition::DoReadCore(TokenReader& rTr) {
00578   FD_DRTI("TypeDefinition::DoReadCore(): file " << rTr.FileName());
00579   // call base core
00580   Documentation::DoReadCore(rTr);  
00581   // no extra data for type definition
00582 }
00583 
00584 // token io
00585 void TypeDefinition::DoWrite(TokenWriter& rTw,  const std::string& rLabel, const Type* pContext) const {
00586   // label
00587   std::string label=rLabel;
00588   if(label=="") label="TypeDefinition";
00589   // base can handle
00590   Documentation::DoWrite(rTw,label,pContext); 
00591 }
00592 
00593 // token io
00594 void TypeDefinition::DoWriteCore(TokenWriter& rTw) const {
00595   FD_DRTI("TypeDefinition::DoWriteCore(): file " << rTw.FileName());
00596   // call base core
00597   Documentation::DoWriteCore(rTw);  
00598   // no extra data for type definition
00599 }
00600 
00601 // access prototype
00602 const Type* TypeDefinition::Prototype(void) const{
00603   return(mpType);
00604 }
00605 
00606 // set prototype
00607 void TypeDefinition::Prototype(faudes::Type* pType){
00608   if(mpType) delete mpType;
00609   mpType = pType;
00610 }
00611 
00612 // construct new object with faudes type
00613 Type* TypeDefinition::NewObject() const{
00614   FD_DRTI("TypeDefinition::NewObject()");
00615   // bail out 
00616   if(!mpType) return NULL;
00617   // use prototype
00618   return mpType->New();
00619 }
00620 
00621 
00622 // clear (all except prototype)
00623 void TypeDefinition::Clear(){
00624   FD_DRTI("TypeDefinition::Clear()");
00625   // call base
00626   Documentation::Clear();
00627 }
00628 
00629 
00630 
00631 
00632 } //namspace
00633 

libFAUDES 2.16b --- 2010-9-8 --- c++ source docu by doxygen 1.6.3