/[ascend]/trunk/pygtk/units.cpp
ViewVC logotype

Contents of /trunk/pygtk/units.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 966 - (show annotations) (download) (as text)
Thu Dec 14 14:04:54 2006 UTC (13 years, 10 months ago) by johnpye
File MIME type: text/x-c++src
File size: 2504 byte(s)
Cleaned up code comments in units.h
Fixed a bug with starting timesteps in LSODE (i think)
Added function to output a SampleList to the console (for debugging)
Removed debug output from base/generic/test/SConscript.
Removed 'custom' headers for UnitsM in ascpy.i
Made a sane default ctor for UnitsM
Added some more test cases to the Python unittest suite (still a problem running multiple solver-based tests in a single run)
1 #include <iostream>
2 #include <stdexcept>
3 #include <sstream>
4 using namespace std;
5
6 #include "units.h"
7 #include "dimensions.h"
8
9 UnitsM::UnitsM(){
10 int errcode;
11 long unsigned pos;
12 u = FindOrDefineUnits("?",&pos,&errcode);
13 if(errcode){
14 // naughty to throw exceptions during ctor.
15 throw runtime_error("Can't create wildcard (?)");
16 }
17 }
18
19 UnitsM::UnitsM(const struct Units *u) : u(u){
20 // nothing else
21 }
22
23 /*
24 exception
25 UnitsM::getException(const int &errorcode, const char *ustr, const int &pos){
26 const char *s;
27 switch(errorcode){
28 case 0: return runtime_error("This error should not have been thrown!");
29 case 1: s = "Undefined unit used in string"; break;
30 case 2: s = "Unbalanced parenthesis"; break;
31 case 3: s = "Illegal character"; break;
32 case 4: s = "Illegal real value"; break;
33 case 5: s = "Oversized identifier or real"; break;
34 case 6: s = "Missing operator in real followed by identifier"; break;
35 case 7: s = "Term missing after * or / or ("; break;
36 case 8: s = "Term missing before * or /"; break;
37 case 9: s = "Too many closing parentheses"; break;
38 case 10: s = "Bad fraction exponent"; break;
39 case 11: raise runtime_error("Invalid UnitsM::getException errorcode = 11");
40 }
41 exception e = runtime_error(s);
42 CONSOLE_DEBUG("Units error: %s",s);
43 CONSOLE_DEBUG("%s",ustr);
44 char indic[strlen(ustr)+1];
45 for(int i=0; i<pos; ++i){
46 indic[i] = ' ';
47 }
48 indic[pos] = '^'; indic[pos+1] = '\0';
49 CONSOLE_DEBUG(indic);
50 return e;
51 }
52 */
53
54 /**
55 Parse a units string, create a new UnitsM object from the string.
56
57 UnitsM doesn't allocate any storage so it's OK to throw
58 an exception in a ctor
59 */
60 UnitsM::UnitsM(const char *units){
61 const struct Units *u = LookupUnits(units);
62 if(u==NULL){
63 //cerr << "About to create new units '" << units << "'" << endl;
64 long unsigned pos;
65 int err;
66 u = FindOrDefineUnits(units, &pos, &err);
67 if(u==NULL){
68 char **errv = UnitsExplainError(units, err, pos);
69 stringstream ss;
70 ss << "Error parsing units: " << errv[0] << endl << errv[1] << endl << errv[2];
71 throw runtime_error(ss.str());
72 }
73 }/*else{
74 cerr << "Units '" << units << "' were found in lookup" << endl;
75 }*/
76 this->u = u;
77 }
78
79 const struct Units *
80 UnitsM::getInternalType() const{
81 return u;
82 }
83
84 const SymChar
85 UnitsM::getName() const{
86 return SymChar(SCP( UnitsDescription(u) ));
87 }
88
89 const Dimensions
90 UnitsM::getDimensions() const{
91 const dim_type *d = UnitsDimensions(u);
92 return Dimensions(d);
93 }
94
95 const double
96 UnitsM::getConversion() const{
97 return UnitsConvFactor(u);
98 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22