00001 #include "luapp_var.hpp" 00002 #include <utils_string.hpp> 00003 00004 namespace lua 00005 { 00006 const var_type& var::VALUE_NONE = typeid(void); 00007 const var_type& var::VALUE_INT = typeid(int); 00008 const var_type& var::VALUE_UINT = typeid(uint); 00009 const var_type& var::VALUE_FLOAT = typeid(float); 00010 const var_type& var::VALUE_DOUBLE = typeid(double); 00011 const var_type& var::VALUE_BOOL = typeid(bool); 00012 const var_type& var::VALUE_STRING = typeid(std::string); 00013 const var_type& var::VALUE_POINTER = typeid(void*); 00014 00015 var::var() : pValue_(nullptr) 00016 { 00017 } 00018 00019 var::var(const var& mVar) : pValue_(mVar.pValue_ ? mVar.pValue_->clone() : nullptr) 00020 { 00021 } 00022 00023 var& var::operator = (const var& mVar) 00024 { 00025 if (&mVar != this) 00026 { 00027 var mTemp(mVar); 00028 swap(mTemp); 00029 } 00030 return *this; 00031 } 00032 00033 bool var::operator == (const var& mVar) const 00034 { 00035 if (get_type() == mVar.get_type()) 00036 { 00037 if (is_of_type<std::string>()) 00038 { 00039 std::string s1 = get<std::string>(); 00040 std::string s2 = mVar.get<std::string>(); 00041 return s1 == s2; 00042 } 00043 else if (is_of_type<float>()) 00044 { 00045 float f1 = get<float>(); 00046 float f2 = mVar.get<float>(); 00047 return f1 == f2; 00048 } 00049 else if (is_of_type<double>()) 00050 { 00051 double d1 = get<double>(); 00052 double d2 = mVar.get<double>(); 00053 return d1 == d2; 00054 } 00055 else if (is_of_type<int>()) 00056 { 00057 int i1 = get<int>(); 00058 int i2 = mVar.get<int>(); 00059 return i1 == i2; 00060 } 00061 else if (is_of_type<uint>()) 00062 { 00063 uint ui1 = get<uint>(); 00064 uint ui2 = mVar.get<uint>(); 00065 return ui1 == ui2; 00066 } 00067 else if (is_of_type<bool>()) 00068 { 00069 bool b1 = get<bool>(); 00070 bool b2 = mVar.get<bool>(); 00071 return b1 == b2; 00072 } 00073 else if (is_of_type<void>()) 00074 return true; 00075 } 00076 00077 return false; 00078 } 00079 00080 bool var::operator != (const var& mVar) const 00081 { 00082 return !(operator == (mVar)); 00083 } 00084 00085 void var::swap(var& mVar) 00086 { 00087 std::swap(pValue_, mVar.pValue_); 00088 } 00089 00090 bool var::is_empty() const 00091 { 00092 return (pValue_ == nullptr); 00093 } 00094 00095 00096 const var_type& var::get_type() const 00097 { 00098 if (pValue_) 00099 { 00100 return pValue_->get_type(); 00101 } 00102 else 00103 { 00104 return typeid(void); 00105 } 00106 } 00107 00108 std::string var::to_string() const 00109 { 00110 const var_type& mType = get_type(); 00111 if (mType == VALUE_INT) return utils::to_string(get<int>()); 00112 else if (mType == VALUE_UINT) return utils::to_string(get<uint>())+"u"; 00113 else if (mType == VALUE_FLOAT) return utils::to_string(get<float>())+"f"; 00114 else if (mType == VALUE_DOUBLE) return utils::to_string(get<double>()); 00115 else if (mType == VALUE_BOOL) return utils::to_string(get<bool>()); 00116 else if (mType == VALUE_STRING) return "\""+get<std::string>()+"\""; 00117 else if (mType == VALUE_POINTER) return utils::to_string(get<void*>()); 00118 else return "<none>"; 00119 } 00120 }