![]() |
GURLS++
2.0.00
C++ Implementation of GURLS Matlab Toolbox
|
00001 /* 00002 * The GURLS Package in C++ 00003 * 00004 * Copyright (C) 2011-1013, IIT@MIT Lab 00005 * All rights reserved. 00006 * 00007 * author: M. Santoro 00008 * email: msantoro@mit.edu 00009 * website: http://cbcl.mit.edu/IIT@MIT/IIT@MIT.html 00010 * 00011 * Redistribution and use in source and binary forms, with or without 00012 * modification, are permitted provided that the following conditions 00013 * are met: 00014 * 00015 * * Redistributions of source code must retain the above 00016 * copyright notice, this list of conditions and the following 00017 * disclaimer. 00018 * * Redistributions in binary form must reproduce the above 00019 * copyright notice, this list of conditions and the following 00020 * disclaimer in the documentation and/or other materials 00021 * provided with the distribution. 00022 * * Neither the name(s) of the copyright holders nor the names 00023 * of its contributors or of the Massacusetts Institute of 00024 * Technology or of the Italian Institute of Technology may be 00025 * used to endorse or promote products derived from this software 00026 * without specific prior written permission. 00027 * 00028 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00029 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00030 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00031 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00032 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00033 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00034 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00035 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00036 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00037 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00038 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00039 * POSSIBILITY OF SUCH DAMAGE. 00040 */ 00041 00042 #include <string> 00043 #include <sstream> 00044 #include <iostream> 00045 #include <algorithm> 00046 #include <numeric> 00047 #include <vector> 00048 00049 #include "gurls++/options.h" 00050 00051 using namespace std; 00052 00053 namespace gurls{ 00054 00058 GURLS_EXPORT std::ostream& operator<<(std::ostream& os, const GurlsOption& opt) 00059 { 00060 opt.operator <<(os); 00061 return os; 00062 } 00063 00064 GURLS_EXPORT std::ostream& OptString::operator<<(std::ostream& os) const 00065 { 00066 return os << this->getValue(); 00067 } 00068 00069 GURLS_EXPORT std::ostream& OptNumber::operator<<(std::ostream& os) const 00070 { 00071 return os << this->getValue(); 00072 } 00073 00074 GURLS_EXPORT std::ostream& OptStringList::operator<<(std::ostream& os) const 00075 { 00076 const std::vector<std::string>& V = this->getValue(); 00077 std::vector<std::string>::const_iterator it = V.begin(); 00078 std::vector<std::string>::const_iterator end = V.end(); 00079 00080 os << "("; 00081 if(!V.empty()) 00082 os << (*it++); 00083 00084 while( it != end) 00085 os << ", " << (*it++); 00086 00087 os << ")"; 00088 00089 return os; 00090 } 00091 00092 GURLS_EXPORT std::ostream& OptNumberList::operator<<(std::ostream& os) const 00093 { 00094 const std::vector<double>& V = this->getValue(); 00095 std::vector<double>::const_iterator it = V.begin(); 00096 std::vector<double>::const_iterator end = V.end(); 00097 00098 os << "("; 00099 if(!V.empty()) 00100 os << (*it++); 00101 00102 while( it != end) 00103 os << ", " << (*it++); 00104 os << ")"; 00105 00106 return os; 00107 } 00108 00109 GURLS_EXPORT std::ostream& OptProcess::operator<<(std::ostream& os) const 00110 { 00111 os << "( "; 00112 00113 if(!value->empty()) 00114 { 00115 ValueType::const_iterator end = value->end(); 00116 --end; 00117 00118 for(ValueType::const_iterator it = value->begin(); it != end; ++it) 00119 os << actionNames()[*it] << ", "; 00120 00121 os << actionNames()[*end]; 00122 } 00123 00124 os << " )"; 00125 00126 return os; 00127 } 00128 00129 GurlsOption::GurlsOption(OptTypes t):type(t) {} 00130 00131 OptTypes GurlsOption::getType() const 00132 { 00133 return type; 00134 } 00135 00136 GurlsOption::~GurlsOption(){} 00137 00138 bool GurlsOption::isA(OptTypes id) const 00139 { 00140 return (id == GenericOption); 00141 } 00142 00143 const std::type_info& GurlsOption::getDataID() 00144 { 00145 return typeid(GurlsOption); 00146 } 00147 00148 00149 00150 bool OptTaskSequence::isValid(const std::string & str, std::string& type, std::string& name) 00151 { 00152 size_t found = str.find(gurls::TASKDESC_SEPARATOR); 00153 00154 if (found==std::string::npos) 00155 return false; 00156 00157 type = str.substr(0, found); 00158 name = str.substr(found+1); 00159 00160 if (name.find(gurls::TASKDESC_SEPARATOR)!=std::string::npos) 00161 return false; 00162 00163 return true; 00164 } 00165 00166 OptTaskSequence::OptTaskSequence():OptStringList() 00167 { 00168 type = TaskSequenceOption; 00169 } 00170 00171 OptTaskSequence::OptTaskSequence(const char *str) 00172 { 00173 type = TaskSequenceOption; 00174 00175 value = new ValueType(); 00176 value->push_back(str); 00177 } 00178 00179 OptTaskSequence::OptTaskSequence(std::string& str): OptStringList(str) 00180 { 00181 type = TaskSequenceOption; 00182 } 00183 00184 OptTaskSequence::OptTaskSequence(const std::vector<std::string> &data): OptStringList(data) 00185 { 00186 type = TaskSequenceOption; 00187 } 00188 00189 void OptTaskSequence::addTask(const std::string newtask) 00190 { 00191 add(newtask); 00192 } 00193 00194 bool OptTaskSequence::isA(OptTypes id) const 00195 { 00196 return (id == TaskSequenceOption); 00197 } 00198 00199 OptTaskSequence *OptTaskSequence::dynacast(GurlsOption *opt) 00200 { 00201 if (opt->isA(TaskSequenceOption) ) 00202 return static_cast<OptTaskSequence*>(opt); 00203 00204 throw gException(gurls::Exception_Illegal_Dynamic_Cast); 00205 } 00206 00207 const OptTaskSequence *OptTaskSequence::dynacast(const GurlsOption *opt) 00208 { 00209 if (opt->isA(TaskSequenceOption) ) 00210 return static_cast<const OptTaskSequence*>(opt); 00211 00212 throw gException(gurls::Exception_Illegal_Dynamic_Cast); 00213 } 00214 00215 void OptTaskSequence::getTaskAt(int index, string &taskdesc, string &taskname) 00216 { 00217 if (!isValid((*value)[index], taskdesc, taskname)) 00218 throw gException(gurls::Exception_Invalid_TaskSequence); 00219 } 00220 00221 unsigned long OptTaskSequence::size() 00222 { 00223 return value->size(); 00224 } 00225 00226 00227 00228 00229 OptNumberList::OptNumberList(): GurlsOption(NumberListOption) 00230 { 00231 value = new std::vector<double>(); 00232 } 00233 00234 OptNumberList::OptNumberList(const std::vector<double>& vec): GurlsOption(NumberListOption) 00235 { 00236 value = new std::vector<double>(vec.begin(), vec.end()); 00237 } 00238 00239 OptNumberList::OptNumberList(double v): GurlsOption(NumberListOption) 00240 { 00241 value = new std::vector<double>(); 00242 value->push_back(v); 00243 } 00244 00245 OptNumberList::OptNumberList(double *v, int n):GurlsOption(NumberListOption), value() 00246 { 00247 value = new std::vector<double>(v, v+n); 00248 } 00249 00250 OptNumberList::~OptNumberList() 00251 { 00252 value->clear(); 00253 00254 delete value; 00255 } 00256 00257 void OptNumberList::setValue(const std::vector<double> newvalue) 00258 { 00259 value->clear(); 00260 delete value; 00261 00262 value = new std::vector<double>(newvalue.begin(), newvalue.end()); 00263 } 00264 00265 void OptNumberList::add(const double d) 00266 { 00267 value->push_back(d); 00268 } 00269 00270 std::vector<double> &OptNumberList::getValue() 00271 { 00272 return *value; 00273 } 00274 00275 const std::vector<double>& OptNumberList::getValue() const 00276 { 00277 return *value; 00278 } 00279 00280 bool OptNumberList::isA(OptTypes id) const 00281 { 00282 return (id == NumberListOption); 00283 } 00284 00285 OptNumberList *OptNumberList::dynacast(GurlsOption *opt) 00286 { 00287 if (opt->isA(NumberListOption) ) 00288 return static_cast<OptNumberList*>(opt); 00289 00290 throw gException(gurls::Exception_Illegal_Dynamic_Cast); 00291 } 00292 00293 const OptNumberList *OptNumberList::dynacast(const GurlsOption *opt) 00294 { 00295 if (opt->isA(NumberListOption) ) 00296 return static_cast<const OptNumberList*>(opt); 00297 00298 throw gException(gurls::Exception_Illegal_Dynamic_Cast); 00299 } 00300 00301 void OptNumberList::clear() 00302 { 00303 value->clear(); 00304 } 00305 00306 OptNumberList& OptNumberList::operator<<(double& d) 00307 { 00308 value->push_back(d); 00309 00310 return *this; 00311 } 00312 00313 00314 OptNumber::OptNumber(): GurlsOption(NumberOption), value(0.0) {} 00315 00316 OptNumber::OptNumber(double v): GurlsOption(NumberOption), value(v){} 00317 00318 OptNumber& OptNumber::operator=(double other) 00319 { 00320 this->type = NumberOption; 00321 this->value = other; 00322 return *this; 00323 } 00324 00325 void OptNumber::setValue(double newvalue) 00326 { 00327 value = newvalue; 00328 } 00329 00330 const double& OptNumber::getValue() const 00331 { 00332 return value; 00333 } 00334 00335 double& OptNumber::getValue() 00336 { 00337 return value; 00338 } 00339 00340 bool OptNumber::isA(OptTypes id) const 00341 { 00342 return (id == NumberOption); 00343 } 00344 00345 OptNumber* OptNumber::dynacast(GurlsOption* opt) 00346 { 00347 if (opt->isA(NumberOption) ) 00348 return static_cast<OptNumber*>(opt); 00349 00350 throw gException(gurls::Exception_Illegal_Dynamic_Cast); 00351 } 00352 00353 const OptNumber* OptNumber::dynacast(const GurlsOption* opt) 00354 { 00355 if (opt->isA(NumberOption) ) 00356 return static_cast<const OptNumber*>(opt); 00357 00358 throw gException(gurls::Exception_Illegal_Dynamic_Cast); 00359 } 00360 00361 00362 00363 OptStringList::OptStringList(): GurlsOption(StringListOption) 00364 { 00365 value = new std::vector<std::string>(); 00366 } 00367 00368 OptStringList::OptStringList(const std::vector<std::string>& vec): GurlsOption(StringListOption) 00369 { 00370 value = new std::vector<std::string>(vec.begin(), vec.end()); 00371 } 00372 00373 OptStringList::OptStringList(std::string& str): GurlsOption(StringListOption) 00374 { 00375 value = new std::vector<std::string>(); 00376 value->push_back(str); 00377 } 00378 00379 OptStringList::~OptStringList() 00380 { 00381 value->clear(); 00382 delete value; 00383 } 00384 00385 void OptStringList::setValue(const std::vector<std::string> newvalue) 00386 { 00387 value->clear(); 00388 delete value; 00389 00390 value = new std::vector<std::string>(newvalue.begin(), newvalue.end()); 00391 } 00392 00393 void OptStringList::add(const std::string str) 00394 { 00395 value->push_back(str); 00396 } 00397 00398 const std::vector<std::string>& OptStringList::getValue() const 00399 { 00400 return *value; 00401 } 00402 00403 bool OptStringList::isA(OptTypes id) const 00404 { 00405 return (id == StringListOption); 00406 } 00407 00408 OptStringList* OptStringList::dynacast(GurlsOption* opt) 00409 { 00410 if (opt->isA(StringListOption) ) 00411 return static_cast<OptStringList*>(opt); 00412 00413 throw gException(gurls::Exception_Illegal_Dynamic_Cast); 00414 } 00415 00416 00417 const OptStringList* OptStringList::dynacast(const GurlsOption* opt) 00418 { 00419 if (opt->isA(StringListOption) ) 00420 return static_cast<const OptStringList*>(opt); 00421 00422 throw gException(gurls::Exception_Illegal_Dynamic_Cast); 00423 } 00424 00425 void OptStringList::clear() 00426 { 00427 value->clear(); 00428 } 00429 00430 OptStringList &OptStringList::operator <<(std::string & str) 00431 { 00432 value->push_back(str); 00433 00434 return *this; 00435 } 00436 00437 OptStringList &OptStringList::operator <<(const char* str) 00438 { 00439 value->push_back(str); 00440 00441 return *this; 00442 } 00443 00444 00445 00446 00447 OptString::OptString(): GurlsOption(StringOption), value(""){} 00448 00449 //OptString::OptString(const char* str): GurlsOption(StringOption),value(str){} 00450 00451 OptString::OptString(const std::string& str): GurlsOption(StringOption),value(str){} 00452 00453 OptString::OptString(const std::wstring& str): GurlsOption(StringOption) 00454 { 00455 value = std::string(str.begin(), str.end()); 00456 } 00457 00458 OptString::~OptString() 00459 { 00460 value.clear(); 00461 } 00462 00463 OptString& OptString::operator=(const std::string& other) 00464 { 00465 this->type = StringOption; 00466 this->value = other; 00467 return *this; 00468 } 00469 00470 void OptString::setValue(const std::string& newvalue) 00471 { 00472 value = newvalue; 00473 } 00474 00475 std::string& OptString::getValue() 00476 { 00477 return value; 00478 } 00479 00480 const std::string& OptString::getValue() const 00481 { 00482 return value; 00483 } 00484 00485 bool OptString::isA(OptTypes id) const 00486 { 00487 return (id == StringOption); 00488 } 00489 00490 OptString* OptString::dynacast(GurlsOption* opt) 00491 { 00492 if (opt->isA(StringOption) ) 00493 return static_cast<OptString*>(opt); 00494 00495 throw gException(gurls::Exception_Illegal_Dynamic_Cast); 00496 } 00497 00498 const OptString* OptString::dynacast(const GurlsOption* opt) 00499 { 00500 if (opt->isA(StringOption) ) 00501 return static_cast<const OptString*>(opt); 00502 00503 throw gException(gurls::Exception_Illegal_Dynamic_Cast); 00504 } 00505 00506 00507 00508 00509 00510 std::vector<std::string> &OptProcess::actionNames() 00511 { 00512 static std::vector<std::string> ret; 00513 00514 if(ret.empty()) 00515 { 00516 ret.push_back("Ignore"); 00517 ret.push_back("Compute"); 00518 ret.push_back("ComputeNsave"); 00519 ret.push_back("Load"); 00520 ret.push_back("Remove"); 00521 } 00522 00523 return ret; 00524 } 00525 00526 OptProcess::OptProcess():GurlsOption(ProcessOption) 00527 { 00528 value = new ValueType(); 00529 } 00530 00531 OptProcess::OptProcess(const OptProcess& other):GurlsOption(ProcessOption) 00532 { 00533 value = new ValueType(other.value->begin(), other.value->end()); 00534 } 00535 00536 OptProcess::~OptProcess() 00537 { 00538 value->clear(); 00539 delete value; 00540 } 00541 00542 void OptProcess::addAction(const Action action) 00543 { 00544 value->push_back(action); 00545 } 00546 00547 OptProcess& OptProcess::operator<<(const Action action) 00548 { 00549 value->push_back(action); 00550 00551 return *this; 00552 } 00553 00554 const OptProcess::ValueType& OptProcess::getValue() const 00555 { 00556 return *value; 00557 } 00558 00559 OptProcess::Action OptProcess::operator[](unsigned long index) 00560 { 00561 return (*value)[index]; 00562 } 00563 00564 void OptProcess::clear() 00565 { 00566 value->clear(); 00567 } 00568 00569 unsigned long OptProcess::size() 00570 { 00571 return value->size(); 00572 } 00573 00574 bool OptProcess::isA(OptTypes id) const 00575 { 00576 return (id == ProcessOption); 00577 } 00578 00579 OptProcess* OptProcess::dynacast(GurlsOption* opt) 00580 { 00581 if (opt->isA(ProcessOption) ) 00582 return static_cast<OptProcess*>(opt); 00583 00584 throw gException(gurls::Exception_Illegal_Dynamic_Cast); 00585 } 00586 00587 const OptProcess* OptProcess::dynacast(const GurlsOption* opt) 00588 { 00589 if (opt->isA(ProcessOption) ) 00590 return static_cast<const OptProcess*>(opt); 00591 00592 throw gException(gurls::Exception_Illegal_Dynamic_Cast); 00593 } 00594 00595 } 00596