- Timestamp:
- Mar 2, 2006 5:52:05 PM (18 years ago)
- Location:
- trunk/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/RpUnits.cc
r147 r262 18 18 // dict pointer 19 19 RpDict<std::string,RpUnits*>* RpUnits::dict = new RpDict<std::string,RpUnits*>(); 20 21 // install predefined units 20 22 static RpUnitsPreset loader; 21 23 … … 27 29 28 30 RpUnits * 29 RpUnits::define( const std::string units, const RpUnits* basis) { 31 RpUnits::define( const std::string units, 32 const RpUnits* basis, 33 const std::string type ) { 30 34 31 35 RpUnits* newRpUnit = NULL; … … 37 41 double exponent = 1; 38 42 39 if (units == "") {43 if (units.empty()) { 40 44 // raise error, user sent null units! 41 45 return NULL; … … 75 79 } 76 80 77 newRpUnit = new RpUnits(sendStr, exponent, basis );81 newRpUnit = new RpUnits(sendStr, exponent, basis, type); 78 82 if (newRpUnit) { 79 83 insert(newRpUnit->getUnitsName(),newRpUnit); … … 163 167 164 168 169 /**********************************************************************/ 170 // METHOD: getType() 171 /// Return the type of an RpUnits object. 172 /** 173 */ 174 std::string 175 RpUnits::getType() const { 176 return this->type; 177 } 178 179 /**********************************************************************/ 180 // METHOD: getCompatible() 181 /// Return a list of units compatible with this RpUnits object. 182 /** 183 */ 184 std::list<std::string> 185 RpUnits::getCompatible() const { 186 187 std::list<std::string> compatList; 188 std::list<std::string> basisCompatList; 189 std::string myName = getUnitsName(); 190 std::string otherName = getUnitsName(); 191 convEntry* myConversions = this->convList; 192 193 if (this->basis) { 194 basisCompatList = this->basis->getCompatible(); 195 compatList.merge(basisCompatList); 196 } 197 198 // run through the conversion list 199 // for each entry, look at the name 200 // if the name is not equal to the name of this RpUnits object, 201 // store the fromPtr->getUnitsName() into compatList 202 // else store the toPtr->getUnitsName() into compatList 203 // 204 while (myConversions != NULL) { 205 otherName = myConversions->conv->toPtr->getUnitsName(); 206 if (otherName == myName) { 207 otherName = myConversions->conv->fromPtr->getUnitsName(); 208 } 209 compatList.push_back(otherName); 210 myConversions = myConversions->next; 211 } 212 213 compatList.push_back(myName); 214 compatList.sort(); 215 compatList.unique(); 216 return compatList; 217 218 } 165 219 166 220 /**********************************************************************/ … … 419 473 420 474 name = "c" + basisName; 421 RpUnits * centi = RpUnits::define(name, basis );475 RpUnits * centi = RpUnits::define(name, basis, basis->type); 422 476 RpUnits::define(centi, basis, centi2base, base2centi); 423 477 424 478 name = "m" + basisName; 425 RpUnits * milli = RpUnits::define(name, basis );479 RpUnits * milli = RpUnits::define(name, basis, basis->type); 426 480 RpUnits::define(milli, basis, milli2base, base2milli); 427 481 428 482 name = "u" + basisName; 429 RpUnits * micro = RpUnits::define(name, basis );483 RpUnits * micro = RpUnits::define(name, basis, basis->type); 430 484 RpUnits::define(micro, basis, micro2base, base2micro); 431 485 432 486 name = "n" + basisName; 433 RpUnits * nano = RpUnits::define(name, basis );487 RpUnits * nano = RpUnits::define(name, basis, basis->type); 434 488 RpUnits::define(nano, basis, nano2base, base2nano); 435 489 436 490 name = "p" + basisName; 437 RpUnits * pico = RpUnits::define(name, basis );491 RpUnits * pico = RpUnits::define(name, basis, basis->type); 438 492 RpUnits::define(pico, basis, pico2base, base2pico); 439 493 440 494 name = "f" + basisName; 441 RpUnits * femto = RpUnits::define(name, basis );495 RpUnits * femto = RpUnits::define(name, basis, basis->type); 442 496 RpUnits::define(femto, basis, femto2base, base2femto); 443 497 444 498 name = "a" + basisName; 445 RpUnits * atto = RpUnits::define(name, basis );499 RpUnits * atto = RpUnits::define(name, basis, basis->type); 446 500 RpUnits::define(atto, basis, atto2base, base2atto); 447 501 448 502 name = "k" + basisName; 449 RpUnits * kilo = RpUnits::define(name, basis );503 RpUnits * kilo = RpUnits::define(name, basis, basis->type); 450 504 RpUnits::define(kilo, basis, kilo2base, base2kilo); 451 505 452 506 name = "M" + basisName; 453 RpUnits * mega = RpUnits::define(name, basis );507 RpUnits * mega = RpUnits::define(name, basis, basis->type); 454 508 RpUnits::define(mega, basis, mega2base, base2mega); 455 509 456 510 name = "G" + basisName; 457 RpUnits * giga = RpUnits::define(name, basis );511 RpUnits * giga = RpUnits::define(name, basis, basis->type); 458 512 RpUnits::define(giga, basis, giga2base, base2giga); 459 513 460 514 name = "T" + basisName; 461 RpUnits * tera = RpUnits::define(name, basis );515 RpUnits * tera = RpUnits::define(name, basis, basis->type); 462 516 RpUnits::define(tera, basis, tera2base, base2tera); 463 517 464 518 name = "P" + basisName; 465 RpUnits * peta = RpUnits::define(name, basis );519 RpUnits * peta = RpUnits::define(name, basis, basis->type); 466 520 RpUnits::define(peta, basis, peta2base, base2peta); 467 521 … … 504 558 505 559 /**********************************************************************/ 506 // METHOD: define()507 /// Define a unit type to be stored as a Rappture Unit.560 // METHOD: negateListExponents() 561 /// Negate the exponents on every element in unitsList 508 562 /** 509 563 */ … … 524 578 } 525 579 526 // negate the exponent 527 /**********************************************************************/ 528 // METHOD: define() 529 /// Define a unit type to be stored as a Rappture Unit. 580 /**********************************************************************/ 581 // METHOD: negateExponent() 582 /// Negate the exponent on the current RpUnitsListEntry 530 583 /** 531 584 */ … … 537 590 } 538 591 539 // provide the caller with the name of this object 540 /**********************************************************************/ 541 // METHOD: define() 542 /// Define a unit type to be stored as a Rappture Unit. 592 /**********************************************************************/ 593 // METHOD: name() 594 /// Provide the caller with the name of this object 543 595 /** 544 596 */ … … 551 603 } 552 604 553 // provide the caller with the basis of the RpUnits object being stored554 605 /**********************************************************************/ 555 606 // METHOD: define() 556 /// Define a unit type to be stored as a Rappture Unit.607 /// Provide the caller with the basis of the RpUnits object being stored 557 608 /** 558 609 */ … … 1505 1556 RpUnitsPreset::addPresetTime () { 1506 1557 1507 RpUnits* seconds = RpUnits::define("s", NULL );1558 RpUnits* seconds = RpUnits::define("s", NULL, RP_TYPE_TIME); 1508 1559 1509 1560 RpUnits::makeMetric(seconds); … … 1530 1581 RpUnitsPreset::addPresetTemp () { 1531 1582 1532 RpUnits* fahrenheit = RpUnits::define("F", NULL );1533 RpUnits* celcius = RpUnits::define("C", NULL );1534 RpUnits* kelvin = RpUnits::define("K", NULL );1535 RpUnits* rankine = RpUnits::define("R", NULL );1583 RpUnits* fahrenheit = RpUnits::define("F", NULL, RP_TYPE_TEMP); 1584 RpUnits* celcius = RpUnits::define("C", NULL, RP_TYPE_TEMP); 1585 RpUnits* kelvin = RpUnits::define("K", NULL, RP_TYPE_TEMP); 1586 RpUnits* rankine = RpUnits::define("R", NULL, RP_TYPE_TEMP); 1536 1587 1537 1588 // add temperature definitions … … 1561 1612 RpUnitsPreset::addPresetLength () { 1562 1613 1563 RpUnits* meters = RpUnits::define("m", NULL );1564 RpUnits* angstrom = RpUnits::define("A", NULL );1565 RpUnits* inch = RpUnits::define("in", NULL );1566 RpUnits* feet = RpUnits::define("ft", NULL );1567 RpUnits* yard = RpUnits::define("yd", NULL );1614 RpUnits* meters = RpUnits::define("m", NULL, RP_TYPE_LENGTH); 1615 RpUnits* angstrom = RpUnits::define("A", NULL, RP_TYPE_LENGTH); 1616 RpUnits* inch = RpUnits::define("in", NULL, RP_TYPE_LENGTH); 1617 RpUnits* feet = RpUnits::define("ft", NULL, RP_TYPE_LENGTH); 1618 RpUnits* yard = RpUnits::define("yd", NULL, RP_TYPE_LENGTH); 1568 1619 1569 1620 RpUnits::makeMetric(meters); … … 1593 1644 RpUnitsPreset::addPresetEnergy () { 1594 1645 1595 RpUnits* volt = RpUnits::define("V", NULL );1596 RpUnits* eVolt = RpUnits::define("eV", NULL );1597 RpUnits* joule = RpUnits::define("J", NULL );1646 RpUnits* volt = RpUnits::define("V", NULL, RP_TYPE_ENERGY); 1647 RpUnits* eVolt = RpUnits::define("eV", NULL, RP_TYPE_ENERGY); 1648 RpUnits* joule = RpUnits::define("J", NULL, RP_TYPE_ENERGY); 1598 1649 1599 1650 RpUnits::makeMetric(volt); … … 1621 1672 RpUnitsPreset::addPresetVolume () { 1622 1673 1623 RpUnits* cubic_meter = RpUnits::define("m3", NULL );1624 // RpUnits* pcubic_meter = RpUnits::define("/m3", NULL );1625 RpUnits* cubic_feet = RpUnits::define("ft3", NULL );1626 RpUnits* us_gallon = RpUnits::define("gal", NULL );1674 RpUnits* cubic_meter = RpUnits::define("m3", NULL, RP_TYPE_VOLUME); 1675 // RpUnits* pcubic_meter = RpUnits::define("/m3", NULL, RP_TYPE_VOLUME); 1676 RpUnits* cubic_feet = RpUnits::define("ft3", NULL, RP_TYPE_VOLUME); 1677 RpUnits* us_gallon = RpUnits::define("gal", NULL, RP_TYPE_VOLUME); 1627 1678 1628 1679 RpUnits::makeMetric(cubic_meter); -
trunk/src/tcl/src/RpUnitsTclInterface.cc
r225 r262 28 28 const char *argv[] )); 29 29 30 static int RpTclUnitsDesc _ANSI_ARGS_(( ClientData cdata, 31 Tcl_Interp *interp, 32 int argc, 33 const char *argv[] )); 34 35 static int RpTclUnitsSysFor _ANSI_ARGS_(( ClientData cdata, 36 Tcl_Interp *interp, 37 int argc, 38 const char *argv[] )); 39 40 static int RpTclUnitsSysAll _ANSI_ARGS_(( ClientData cdata, 41 Tcl_Interp *interp, 42 int argc, 43 const char *argv[] )); 44 30 45 #ifdef __cplusplus 31 46 } … … 47 62 Tcl_CreateCommand(interp, "::Rappture::Units::convert", 48 63 RpTclUnitsConvert, (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); 64 65 Tcl_CreateCommand(interp, "::Rappture::Units::description", 66 RpTclUnitsDesc, (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); 67 68 Tcl_CreateCommand(interp, "::Rappture::Units::System::for", 69 RpTclUnitsSysFor, (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); 70 71 Tcl_CreateCommand(interp, "::Rappture::Units::System::all", 72 RpTclUnitsSysAll, (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); 49 73 50 74 return TCL_OK; … … 125 149 // unrecognized value for -units option 126 150 Tcl_AppendResult(interp, 127 "value for -units must be \" yes\" or \"no\"",151 "value for -units must be \"on\" or \"off\"", 128 152 (char*)NULL); 129 153 return TCL_ERROR; … … 190 214 } 191 215 216 int 217 RpTclUnitsDesc ( ClientData cdata, 218 Tcl_Interp *interp, 219 int argc, 220 const char *argv[] ) 221 { 222 std::string unitsName = ""; // name of the units provided by user 223 std::string type = ""; // name of the units provided by user 224 std::string listStr = ""; // name of the units provided by user 225 const RpUnits* unitsObj = NULL; 226 std::list<std::string> compatList; 227 std::list<std::string>::iterator compatListIter; 228 229 int nextarg = 1; // start parsing using the '2'th argument 230 231 Tcl_ResetResult(interp); 232 233 // parse through command line options 234 if (argc != 2) { 235 Tcl_AppendResult(interp, "usage: ", argv[0], " <units>", (char*)NULL); 236 return TCL_ERROR; 237 } 238 239 unitsName = std::string(argv[nextarg]); 240 241 unitsObj = RpUnits::find(unitsName); 242 if (unitsObj == NULL) { 243 Tcl_AppendResult(interp, 244 "The units named: \"", unitsName.c_str(), 245 "\" is not a recognized unit for rappture", 246 (char*)NULL); 247 return TCL_ERROR; 248 } 249 250 type = unitsObj->getType(); 251 252 Tcl_AppendResult(interp, type.c_str(), (char*)NULL); 253 254 compatList = unitsObj->getCompatible(); 255 compatListIter = compatList.begin(); 256 257 while (compatListIter != compatList.end()) { 258 if ( listStr.empty() ) { 259 listStr = *compatListIter; 260 } 261 else { 262 listStr = listStr + "," + *compatListIter; 263 } 264 265 // increment the iterator 266 compatListIter++; 267 } 268 269 Tcl_AppendResult(interp, " (", listStr.c_str() ,")", (char*)NULL); 270 271 return TCL_OK; 272 } 273 274 int 275 RpTclUnitsSysFor ( ClientData cdata, 276 Tcl_Interp *interp, 277 int argc, 278 const char *argv[] ) 279 { 280 std::string unitsName = ""; // name of the units provided by user 281 std::string type = ""; // name of the units provided by user 282 const RpUnits* unitsObj = NULL; 283 int nextarg = 1; // start parsing using the '2'th argument 284 285 Tcl_ResetResult(interp); 286 287 // parse through command line options 288 if (argc != 2) { 289 Tcl_AppendResult(interp, "usage: ", argv[0], " <units>", (char*)NULL); 290 return TCL_ERROR; 291 } 292 293 unitsName = std::string(argv[nextarg]); 294 295 unitsObj = RpUnits::find(unitsName); 296 if (unitsObj == NULL) { 297 Tcl_AppendResult(interp, 298 "The units named: \"", unitsName.c_str(), 299 "\" is not a recognized unit for rappture", 300 (char*)NULL); 301 return TCL_ERROR; 302 } 303 304 type = unitsObj->getType(); 305 306 Tcl_AppendResult(interp, type.c_str(), (char*)NULL); 307 return TCL_OK; 308 309 } 310 311 int 312 RpTclUnitsSysAll ( ClientData cdata, 313 Tcl_Interp *interp, 314 int argc, 315 const char *argv[] ) 316 { 317 std::string unitsName = ""; // name of the units provided by user 318 const RpUnits* unitsObj = NULL; 319 std::list<std::string> compatList; 320 std::list<std::string>::iterator compatListIter; 321 int nextarg = 1; // start parsing using the '2'th argument 322 323 Tcl_ResetResult(interp); 324 325 // parse through command line options 326 if (argc != 2) { 327 Tcl_AppendResult(interp, "usage: ", argv[0], " <units>", (char*)NULL); 328 return TCL_ERROR; 329 } 330 331 unitsName = std::string(argv[nextarg]); 332 333 unitsObj = RpUnits::find(unitsName); 334 if (unitsObj == NULL) { 335 Tcl_AppendResult(interp, 336 "The units named: \"", unitsName.c_str(), 337 "\" is not a recognized unit for rappture", 338 (char*)NULL); 339 return TCL_ERROR; 340 } 341 342 compatList = unitsObj->getCompatible(); 343 compatListIter = compatList.begin(); 344 345 while (compatListIter != compatList.end()) { 346 Tcl_AppendElement(interp,(*compatListIter).c_str()); 347 // increment the iterator 348 compatListIter++; 349 } 350 351 return TCL_OK; 352 }
Note: See TracChangeset
for help on using the changeset viewer.