PH Logo
Logo intepreter modeled after UCB Logo.
/Users/paul/Documents/phlogo/core/Thing.cpp
00001 /*
00002  *  Thing.cpp
00003  *
00004  *  Author: Paul Hamilton
00005  *      Date: 8 Jun 2011
00006  *
00007  */
00008 
00009 #include "Thing.h"
00010 
00011 #include "Exceptions.h"
00012 #include "List.h"
00013 #include "Array.h"
00014 
00015 #include <boost/lexical_cast.hpp>
00016 
00017 using namespace std;
00018 using namespace boost;
00019 
00020 namespace phlogo {
00021 
00022 Thing::Thing(const std::string &s) {
00023     string s1 = s;
00024     _data = s1; 
00025 }
00026 
00027 void Thing::set(pThing thing) {
00028         _data = thing->_data;
00029 }
00030 
00031 void Thing::set(const string &s) { 
00032         _data = s;
00033 }
00034 
00035 void Thing::set(long num) { 
00036         _data = num;
00037 }
00038 
00039 void Thing::set(double num) { 
00040     _data = num;
00041 }
00042 
00043 List Thing::allocList() {
00044         _data = make_tuple(false, tListArray());
00045         return List(&(get<tListData>(_data).get<1>()));
00046 }
00047 
00048 Array Thing::allocArray(int size) {
00049         _data = make_tuple(true, tListArray());
00050         return Array(&(get<tListData>(_data).get<1>()), size);
00051 }
00052 
00053 List Thing::allocList(const tListArray &list) {
00054         _data = make_tuple(false, list);
00055         return List(&(get<tListData>(_data).get<1>()));
00056 }
00057 
00058 Array Thing::allocArray(const tListArray &array) {
00059         _data = make_tuple(true, array);
00060         return Array(&(get<tListData>(_data).get<1>()));
00061 }
00062 
00063 std::string Thing::type() {
00064     
00065     if (isWord())
00066        return "word";
00067     else if (isList())
00068         return "list";
00069     else if (isArray())
00070         return "array";
00071     else if (isLong())
00072         return "long";
00073     else if (isDouble())
00074         return "double";
00075     else
00076         return "unknown";
00077 
00078 }
00079 
00080 bool Thing::isWord() { 
00081         try {
00082                 get<string>(_data);
00083                 return true;
00084         }
00085         catch (bad_get &x) {
00086         }
00087         return false;
00088 }
00089 
00090 bool Thing::isLong() { 
00091         try {
00092                 get<long>(_data);
00093                 return true;
00094         }
00095         catch (bad_get &x) {
00096         }
00097         return false;
00098 }
00099 
00100 bool Thing::isDouble() { 
00101     try {
00102         get<double>(_data);
00103         return true;
00104     }
00105     catch (bad_get &x) {
00106     }
00107     return false;
00108 }
00109 
00110 bool Thing::isList() { 
00111         try {
00112                 tListData &d = get<tListData>(_data);
00113                 return !d.get<0>();
00114         }
00115         catch (bad_get &x) {
00116         }
00117         return false;
00118 }
00119 
00120 bool Thing::isArray() { 
00121         try {
00122                 tListData &d = get<tListData>(_data);
00123                 return d.get<0>();
00124         }
00125         catch (bad_get &x) {
00126         }
00127         return false;
00128 }
00129 
00130 string Thing::getWord()  { 
00131 
00132         try {
00133                 return get<string>(_data);
00134         }
00135         catch (bad_get &x) {
00136                 BOOST_THROW_EXCEPTION( no_word_in_thing_exception() );
00137         }
00138 }
00139 
00140 long Thing::getLong()  { 
00141 
00142         try {
00143                 return get<long>(_data);
00144         }
00145         catch (bad_get &x) {
00146                 BOOST_THROW_EXCEPTION( no_word_in_thing_exception() );
00147         }
00148 }
00149 
00150 double Thing::getDouble()  { 
00151     
00152     try {
00153         return get<double>(_data);
00154     }
00155     catch (bad_get &x) {
00156         BOOST_THROW_EXCEPTION( no_word_in_thing_exception() );
00157     }
00158 }
00159 
00160 List Thing::getList() {
00161 
00162         try {
00163                 tListData &d = get<tListData>(_data);
00164                 if (d.get<0>())
00165                         BOOST_THROW_EXCEPTION( no_list_in_thing_exception() );
00166                 
00167                 return List(&d.get<1>());
00168         }
00169         catch (bad_get &x) {
00170                 BOOST_THROW_EXCEPTION( no_list_in_thing_exception() );
00171         }
00172         
00173 }
00174 
00175 Array Thing::getArray() {
00176 
00177         try {
00178                 tListData &d = get<tListData>(_data);
00179                 if (!d.get<0>())
00180                         BOOST_THROW_EXCEPTION( no_array_in_thing_exception() );
00181                 
00182                 return Array(&d.get<1>());
00183         }
00184         catch (bad_get &x) {
00185                 BOOST_THROW_EXCEPTION( no_array_in_thing_exception() );
00186         }
00187 
00188 }
00189 
00190 string Thing::str() {
00191 
00192         if (isWord())
00193                 return getWord();
00194         else if (isList())
00195                 return getList().str();
00196         else if (isLong())
00197         return lexical_cast<string>(getLong());
00198         else if (isDouble())
00199         return lexical_cast<string>(getDouble());
00200     else
00201                 return getArray().str();
00202 
00203 }
00204 
00205 long Thing::asLong() {
00206     
00207     if (isWord())
00208         return lexical_cast<long>(getWord());
00209     else if (isLong())
00210         return getLong();
00211     else
00212         BOOST_THROW_EXCEPTION( bad_cast() );
00213 
00214 }
00215 
00216 double Thing::asDouble() {
00217     
00218     if (isWord())
00219         return lexical_cast<double>(getWord());
00220     else if (isLong())
00221         return (double)getLong();
00222     else if (isDouble())
00223         return getDouble();
00224     else
00225         BOOST_THROW_EXCEPTION( bad_cast() );
00226 }
00227 
00228 }
 All Classes Functions