Changeset 262 for trunk/src


Ignore:
Timestamp:
Mar 2, 2006 5:52:05 PM (18 years ago)
Author:
dkearney
Message:

adding new core units function getType() and getCompatible()
adding new tcl bindings related to getType() and getCompatible()
adjusted test files for testing.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/RpUnits.cc

    r147 r262  
    1818// dict pointer
    1919RpDict<std::string,RpUnits*>* RpUnits::dict = new RpDict<std::string,RpUnits*>();
     20
     21// install predefined units
    2022static RpUnitsPreset loader;
    2123
     
    2729
    2830RpUnits *
    29 RpUnits::define( const std::string units, const RpUnits* basis) {
     31RpUnits::define(    const std::string units,
     32                    const RpUnits* basis,
     33                    const std::string type  ) {
    3034
    3135    RpUnits* newRpUnit = NULL;
     
    3741    double exponent = 1;
    3842
    39     if (units == "") {
     43    if (units.empty()) {
    4044        // raise error, user sent null units!
    4145        return NULL;
     
    7579    }
    7680
    77     newRpUnit = new RpUnits(sendStr, exponent, basis);
     81    newRpUnit = new RpUnits(sendStr, exponent, basis, type);
    7882    if (newRpUnit) {
    7983        insert(newRpUnit->getUnitsName(),newRpUnit);
     
    163167
    164168
     169/**********************************************************************/
     170// METHOD: getType()
     171/// Return the type of an RpUnits object.
     172/**
     173 */
     174std::string
     175RpUnits::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 */
     184std::list<std::string>
     185RpUnits::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}
    165219
    166220/**********************************************************************/
     
    419473
    420474    name = "c" + basisName;
    421     RpUnits * centi = RpUnits::define(name, basis);
     475    RpUnits * centi = RpUnits::define(name, basis, basis->type);
    422476    RpUnits::define(centi, basis, centi2base, base2centi);
    423477
    424478    name = "m" + basisName;
    425     RpUnits * milli = RpUnits::define(name, basis);
     479    RpUnits * milli = RpUnits::define(name, basis, basis->type);
    426480    RpUnits::define(milli, basis, milli2base, base2milli);
    427481
    428482    name = "u" + basisName;
    429     RpUnits * micro = RpUnits::define(name, basis);
     483    RpUnits * micro = RpUnits::define(name, basis, basis->type);
    430484    RpUnits::define(micro, basis, micro2base, base2micro);
    431485
    432486    name = "n" + basisName;
    433     RpUnits * nano  = RpUnits::define(name, basis);
     487    RpUnits * nano  = RpUnits::define(name, basis, basis->type);
    434488    RpUnits::define(nano, basis, nano2base, base2nano);
    435489
    436490    name = "p" + basisName;
    437     RpUnits * pico  = RpUnits::define(name, basis);
     491    RpUnits * pico  = RpUnits::define(name, basis, basis->type);
    438492    RpUnits::define(pico, basis, pico2base, base2pico);
    439493
    440494    name = "f" + basisName;
    441     RpUnits * femto = RpUnits::define(name, basis);
     495    RpUnits * femto = RpUnits::define(name, basis, basis->type);
    442496    RpUnits::define(femto, basis, femto2base, base2femto);
    443497
    444498    name = "a" + basisName;
    445     RpUnits * atto  = RpUnits::define(name, basis);
     499    RpUnits * atto  = RpUnits::define(name, basis, basis->type);
    446500    RpUnits::define(atto, basis, atto2base, base2atto);
    447501
    448502    name = "k" + basisName;
    449     RpUnits * kilo  = RpUnits::define(name, basis);
     503    RpUnits * kilo  = RpUnits::define(name, basis, basis->type);
    450504    RpUnits::define(kilo, basis, kilo2base, base2kilo);
    451505
    452506    name = "M" + basisName;
    453     RpUnits * mega  = RpUnits::define(name, basis);
     507    RpUnits * mega  = RpUnits::define(name, basis, basis->type);
    454508    RpUnits::define(mega, basis, mega2base, base2mega);
    455509
    456510    name = "G" + basisName;
    457     RpUnits * giga  = RpUnits::define(name, basis);
     511    RpUnits * giga  = RpUnits::define(name, basis, basis->type);
    458512    RpUnits::define(giga, basis, giga2base, base2giga);
    459513
    460514    name = "T" + basisName;
    461     RpUnits * tera  = RpUnits::define(name, basis);
     515    RpUnits * tera  = RpUnits::define(name, basis, basis->type);
    462516    RpUnits::define(tera, basis, tera2base, base2tera);
    463517
    464518    name = "P" + basisName;
    465     RpUnits * peta  = RpUnits::define(name, basis);
     519    RpUnits * peta  = RpUnits::define(name, basis, basis->type);
    466520    RpUnits::define(peta, basis, peta2base, base2peta);
    467521
     
    504558
    505559/**********************************************************************/
    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
    508562/**
    509563 */
     
    524578}
    525579
    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
    530583/**
    531584 */
     
    537590}
    538591
    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
    543595/**
    544596 */
     
    551603}
    552604
    553 // provide the caller with the basis of the RpUnits object being stored
    554605/**********************************************************************/
    555606// 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
    557608/**
    558609 */
     
    15051556RpUnitsPreset::addPresetTime () {
    15061557
    1507     RpUnits* seconds    = RpUnits::define("s", NULL);
     1558    RpUnits* seconds    = RpUnits::define("s", NULL, RP_TYPE_TIME);
    15081559
    15091560    RpUnits::makeMetric(seconds);
     
    15301581RpUnitsPreset::addPresetTemp () {
    15311582
    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);
    15361587
    15371588    // add temperature definitions
     
    15611612RpUnitsPreset::addPresetLength () {
    15621613
    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);
    15681619
    15691620    RpUnits::makeMetric(meters);
     
    15931644RpUnitsPreset::addPresetEnergy () {
    15941645
    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);
    15981649
    15991650    RpUnits::makeMetric(volt);
     
    16211672RpUnitsPreset::addPresetVolume () {
    16221673
    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);
    16271678
    16281679    RpUnits::makeMetric(cubic_meter);
  • trunk/src/tcl/src/RpUnitsTclInterface.cc

    r225 r262  
    2828                                                const char *argv[]    ));
    2929
     30static int RpTclUnitsDesc      _ANSI_ARGS_((    ClientData cdata,
     31                                                Tcl_Interp *interp,
     32                                                int argc,
     33                                                const char *argv[]    ));
     34
     35static int RpTclUnitsSysFor   _ANSI_ARGS_((    ClientData cdata,
     36                                                Tcl_Interp *interp,
     37                                                int argc,
     38                                                const char *argv[]    ));
     39
     40static int RpTclUnitsSysAll   _ANSI_ARGS_((    ClientData cdata,
     41                                                Tcl_Interp *interp,
     42                                                int argc,
     43                                                const char *argv[]    ));
     44
    3045#ifdef __cplusplus
    3146}
     
    4762    Tcl_CreateCommand(interp, "::Rappture::Units::convert",
    4863        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);
    4973
    5074    return TCL_OK;
     
    125149                    // unrecognized value for -units option
    126150                    Tcl_AppendResult(interp,
    127                         "value for -units must be \"yes\" or \"no\"",
     151                        "value for -units must be \"on\" or \"off\"",
    128152                        (char*)NULL);
    129153                    return TCL_ERROR;
     
    190214}
    191215
     216int
     217RpTclUnitsDesc      (   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
     274int
     275RpTclUnitsSysFor    (   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
     311int
     312RpTclUnitsSysAll    (   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.