00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "jaus/core/types.h"
00041 #include <iostream>
00042 #include <math.h>
00043 #include <cstdio>
00044 #include <cstdlib>
00045 #include <cstring>
00046
00047 using namespace std;
00048 using namespace JAUS;
00049
00050 const char* Exception::what() const throw()
00051 {
00052 return std::string(std::string("[JAUS ++ Exception] - ") + mWhatString).c_str();
00053 }
00054
00055 void Exception::Print() const { std::cout << what() << std::endl; }
00056
00062 VarType::VarType() : mpData(0),
00063 mType(BYTE),
00064 mLength(1)
00065 {
00066 mpData = malloc(8);
00067 memset(mpData, 0, 8);
00068 }
00069
00070
00078 VarType::VarType(const Byte v) : mpData(0),
00079 mType(BYTE),
00080 mLength(1)
00081 {
00082 mpData = malloc(8);
00083 memset(mpData, 0, 8);
00084 memcpy(mpData, &v, 1);
00085 }
00086
00087
00095 VarType::VarType(const Short v) : mpData(0),
00096 mType(SHORT),
00097 mLength(2)
00098 {
00099 mpData = malloc(8);
00100 memset(mpData, 0, 8);
00101 memcpy(mpData, &v, mLength);
00102 }
00103
00104
00112 VarType::VarType(const Int v) : mpData(0),
00113 mType(INT),
00114 mLength(4)
00115 {
00116 mpData = malloc(8);
00117 memset(mpData, 0, 8);
00118 memcpy(mpData, &v, mLength);
00119 }
00120
00121
00129 VarType::VarType(const Long v) : mpData(0),
00130 mType(LONG),
00131 mLength(8)
00132 {
00133 mpData = malloc(8);
00134 memset(mpData, 0, 8);
00135 memcpy(mpData, &v, mLength);
00136 }
00137
00138
00146 VarType::VarType(const UShort v) : mpData(0),
00147 mType(USHORT),
00148 mLength(2)
00149 {
00150 mpData = malloc(8);
00151 memset(mpData, 0, 8);
00152 memcpy(mpData, &v, mLength);
00153 }
00154
00155
00163 VarType::VarType(const UInt v) : mpData(0),
00164 mType(UINT),
00165 mLength(4)
00166 {
00167 mpData = malloc(8);
00168 memset(mpData, 0, 8);
00169 memcpy(mpData, &v, mLength);
00170 }
00171
00172
00180 VarType::VarType(const ULong v) : mpData(0),
00181 mType(ULONG),
00182 mLength(8)
00183 {
00184 mpData = malloc(8);
00185 memset(mpData, 0, 8);
00186 memcpy(mpData, &v, mLength);
00187 }
00188
00189
00197 VarType::VarType(const Float v) : mpData(0),
00198 mType(FLOAT),
00199 mLength(4)
00200 {
00201 mpData = malloc(8);
00202 memset(mpData, 0, 8);
00203 memcpy(mpData, &v, mLength);
00204 }
00205
00206
00214 VarType::VarType(const LongFloat v) : mpData(0),
00215 mType(LONG_FLOAT),
00216 mLength(8)
00217 {
00218 mpData = malloc(8);
00219 memset(mpData, 0, 8);
00220 memcpy(mpData, &v, 8);
00221 }
00222
00223
00233 VarType::VarType(const Byte r, const Byte g, const Byte b) : mpData(0),
00234 mType(RGB),
00235 mLength(3)
00236 {
00237 mpData = malloc(8);
00238 Byte * temp = ( (Byte *)(mpData) );
00239 memset(mpData, 0, 8);
00240 if(temp)
00241 {
00242 temp[0] = r;
00243 temp[1] = g;
00244 temp[2] = b;
00245 }
00246 }
00247
00248
00256 VarType::VarType(const VarType& vt) : mpData(0),
00257 mType(BYTE),
00258 mLength(1)
00259 {
00260 mpData = malloc(8);
00261 memset(mpData, 0, 8);
00262 *this = vt;
00263 }
00264
00265
00271 VarType::~VarType()
00272 {
00273 free(mpData);
00274 }
00275
00276
00284 Byte VarType::Type() const
00285 {
00286 return mType;
00287 }
00288
00289
00295 Byte VarType::Size() const
00296 {
00297 return (Byte)(mLength);
00298 }
00299
00300
00312 Byte VarType::ToByte() const
00313 {
00314 return *(Byte *)(mpData);
00315 }
00316
00317
00329 Short VarType::ToShort() const
00330 {
00331 return *(Short *)(mpData);
00332 }
00333
00334
00346 Int VarType::ToInt() const
00347 {
00348 return *(Int *)(mpData);
00349 }
00350
00351
00363 Long VarType::ToLong() const
00364 {
00365 return *(Long *)(mpData);
00366 }
00367
00368
00380 UShort VarType::ToUShort() const
00381 {
00382 return *(UShort *)(mpData);
00383 }
00384
00385
00397 UInt VarType::ToUInt() const
00398 {
00399 return *(UInt *)(mpData);
00400 }
00401
00402
00414 ULong VarType::ToULong() const
00415 {
00416 return *(ULong *)(mpData);
00417 }
00418
00419
00431 Float VarType::ToFloat() const
00432 {
00433 return *(Float *)(mpData);
00434 }
00435
00436
00448 LongFloat VarType::ToLongFloat() const
00449 {
00450 return *(LongFloat *)(mpData);
00451 }
00452
00453
00466 Byte* VarType::ToRGB() const
00467 {
00468 return (Byte *)(mpData);
00469 }
00470
00471
00477 void VarType::Print() const
00478 {
00479 cout << ToString() << endl;
00480 }
00481
00482
00488 std::string VarType::ToString() const
00489 {
00490 std::string str;
00491 char buff[128];
00492 memset(buff, 0, 128);
00493
00494 switch(mType)
00495 {
00496 case BYTE:
00497 sprintf(buff, "%d", ToByte());
00498 break;
00499 case SHORT:
00500 sprintf(buff, "%d", ToShort());
00501 break;
00502 case INT:
00503 sprintf(buff, "%d", ToInt());
00504 break;
00505 case LONG:
00506 sprintf(buff, "%ld", (long)ToLong());
00507 break;
00508 case USHORT:
00509 sprintf(buff, "%u", ToUShort());
00510 break;
00511 case UINT:
00512 sprintf(buff, "%u", ToUInt());
00513 break;
00514 case ULONG:
00515 sprintf(buff, "%lu", (unsigned long)ToULong());
00516 break;
00517 case FLOAT:
00518 sprintf(buff, "%f", ToFloat());
00519 break;
00520 case LONG_FLOAT:
00521 sprintf(buff, "%lf", ToLongFloat());
00522 break;
00523 case RGB:
00524 sprintf(buff,
00525 "[%u,%u,%u]",
00526 ((Byte*)(mpData))[0],
00527 ((Byte*)(mpData))[1],
00528 ((Byte*)(mpData))[2]);
00529 break;
00530 default:
00531 str = "Invalid.";
00532 break;
00533 }
00534
00535 str = buff;
00536
00537 return str;
00538 }
00539
00540
00560 bool VarType::Equals(const VarType& vt, const double ferror) const
00561 {
00562 bool result = false;
00563
00564 if( mType == vt.mType )
00565 {
00566 if( mType == FLOAT)
00567 {
00568 if( fabs( ToFloat() - vt.ToFloat() ) < (float)ferror )
00569 {
00570 result = true;
00571 }
00572 }
00573 else if(mType == LONG_FLOAT)
00574 {
00575 if( fabs( ToLongFloat() - vt.ToLongFloat() ) < ferror )
00576 {
00577 result = true;
00578 }
00579 }
00580 else
00581 {
00582 result = memcmp(mpData, vt.mpData, mLength) == 0 ? true : false;
00583 }
00584 }
00585 return result;
00586 }
00587
00588
00596 int VarType::RunTestCase()
00597 {
00598 int result = 0;
00599
00600 VarType v1, v2;
00601
00602 v1 = 3.1415;
00603 v2 = 3.1415;
00604
00605 if( v1 == v2 )
00606 {
00607 v1 = (Byte)(250);
00608
00609 if( v1.Size() == 1 &&
00610 v1.Type() == BYTE &&
00611 v1.ToByte() == 250)
00612 {
00613
00614 v2 = (Byte)(255);
00615 if( v1 != v2 )
00616 {
00617
00618 v2 = (Int)(250);
00619 if( v1 != v2 )
00620 {
00621 v1 = (UInt)(555);
00622 if( v1.ToString() == "555" &&
00623 v1 == (UInt)(555))
00624 {
00625 result = 1;
00626 }
00627 }
00628 }
00629 }
00630 }
00631
00632 return result;
00633 }
00634
00635
00641 bool VarType::operator==(const VarType& vt) const
00642 {
00643 bool result = false;
00644
00645 if( mType == vt.mType )
00646 {
00647 if( mType == FLOAT)
00648 {
00649 if( fabs( ToFloat() - vt.ToFloat() ) < .000001 )
00650 {
00651 result = true;
00652 }
00653 }
00654 else if(mType == LONG_FLOAT)
00655 {
00656 if( fabs( ToLongFloat() - vt.ToLongFloat() ) < .000001 )
00657 {
00658 result = true;
00659 }
00660 }
00661 else
00662 {
00663 result = memcmp(mpData, vt.mpData, mLength) == 0 ? true : false;
00664 }
00665 }
00666 return result;
00667 }
00668
00669
00675 bool VarType::operator!=(const VarType& vt) const
00676 {
00677 bool result = true;
00678
00679 if( mType == vt.mType )
00680 {
00681 if( mType == FLOAT)
00682 {
00683 if( fabs( ToFloat() - vt.ToFloat() ) < .000001 )
00684 {
00685 result = false;
00686 }
00687 }
00688 else if(mType == LONG_FLOAT)
00689 {
00690 if( fabs( ToLongFloat() - vt.ToLongFloat() ) < .000001 )
00691 {
00692 result = false;
00693 }
00694 }
00695 else
00696 {
00697 result = memcmp(mpData, vt.mpData, mLength) == 0 ? false : true;
00698 }
00699 }
00700 return result;
00701 }
00702
00703
00713 bool VarType::operator==(const Byte v) const
00714 {
00715 return *this == VarType(v);
00716 }
00717
00718
00728 bool VarType::operator==(const Short v) const
00729 {
00730 return *this == VarType(v);
00731 }
00732
00733
00743 bool VarType::operator==(const Int v) const
00744 {
00745 return *this == VarType(v);
00746 }
00747
00748
00758 bool VarType::operator==(const Long v) const
00759 {
00760 return *this == VarType(v);
00761 }
00762
00763
00773 bool VarType::operator==(const UShort v) const
00774 {
00775 return *this == VarType(v);
00776 }
00777
00778
00788 bool VarType::operator==(const UInt v) const
00789 {
00790 return *this == VarType(v);
00791 }
00792
00793
00803 bool VarType::operator==(const ULong v) const
00804 {
00805 return *this == VarType(v);
00806 }
00807
00808
00818 bool VarType::operator==(const Float v) const
00819 {
00820 return *this == VarType(v);
00821 }
00822
00823
00833 bool VarType::operator==(const LongFloat v) const
00834 {
00835 return *this == VarType(v);
00836 }
00837
00838
00848 bool VarType::operator!=(const Byte v) const
00849 {
00850 return *this != VarType(v);
00851 }
00852
00853
00863 bool VarType::operator!=(const Short v) const
00864 {
00865 return *this != VarType(v);
00866 }
00867
00868
00878 bool VarType::operator!=(const Int v) const
00879 {
00880 return *this != VarType(v);
00881 }
00882
00883
00893 bool VarType::operator!=(const Long v) const
00894 {
00895 return *this != VarType(v);
00896 }
00897
00898
00908 bool VarType::operator!=(const UShort v) const
00909 {
00910 return *this != VarType(v);
00911 }
00912
00913
00923 bool VarType::operator!=(const UInt v) const
00924 {
00925 return *this != VarType(v);
00926 }
00927
00928
00938 bool VarType::operator!=(const ULong v) const
00939 {
00940 return *this != VarType(v);
00941 }
00942
00943
00953 bool VarType::operator!=(const Float v) const
00954 {
00955 return *this != VarType(v);
00956 }
00957
00958
00968 bool VarType::operator!=(const LongFloat v) const
00969 {
00970 return *this != VarType(v);
00971 }
00972
00973
00983 VarType& VarType::operator=(const Byte v)
00984 {
00985 memset(mpData, 0, 8);
00986 mType = BYTE;
00987 mLength = 1;
00988 memcpy(mpData, &v, mLength);
00989 return *this;
00990 }
00991
00992
01002 VarType& VarType::operator=(const Short v)
01003 {
01004 memset(mpData, 0, 8);
01005 mType = SHORT;
01006 mLength = 2;
01007 memcpy(mpData, &v, mLength);
01008 return *this;
01009 }
01010
01011
01021 VarType& VarType::operator=(const Int v)
01022 {
01023 memset(mpData, 0, 8);
01024 mType = INT;
01025 mLength = 4;
01026 memcpy(mpData, &v, mLength);
01027 return *this;
01028 }
01029
01030
01040 VarType& VarType::operator=(const Long v)
01041 {
01042 memset(mpData, 0, 8);
01043 mType = LONG;
01044 mLength = 8;
01045 memcpy(mpData, &v, mLength);
01046 return *this;
01047 }
01048
01049
01059 VarType& VarType::operator=(const UShort v)
01060 {
01061 memset(mpData, 0, 8);
01062 mType = USHORT;
01063 mLength = 2;
01064 memcpy(mpData, &v, mLength);
01065 return *this;
01066 }
01067
01068
01078 VarType& VarType::operator=(const UInt v)
01079 {
01080 memset(mpData, 0, 8);
01081 mType = UINT;
01082 mLength = 4;
01083 memcpy(mpData, &v, mLength);
01084 return *this;
01085 }
01086
01087
01097 VarType& VarType::operator=(const ULong v)
01098 {
01099 memset(mpData, 0, 8);
01100 mType = ULONG;
01101 mLength = 8;
01102 memcpy(mpData, &v, mLength);
01103 return *this;
01104 }
01105
01106
01116 VarType& VarType::operator=(const Float v)
01117 {
01118 memset(mpData, 0, 8);
01119 mType = FLOAT;
01120 mLength = 4;
01121 memcpy(mpData, &v, mLength);
01122 return *this;
01123 }
01124
01125
01135 VarType& VarType::operator=(const LongFloat v)
01136 {
01137 memset(mpData, 0, 8);
01138 mType = LONG_FLOAT;
01139 mLength = 8;
01140 memcpy(mpData, &v, mLength);
01141 return *this;
01142 }
01143
01144
01150 VarType& VarType::operator=(const VarType& vt)
01151 {
01152 if(this != &vt)
01153 {
01154 memcpy(mpData, vt.mpData, 8);
01155 mLength = vt.mLength;
01156 mType = vt.mType;
01157 }
01158 return *this;
01159 }
01160
01161
01173 VarType& VarType::operator()(const Byte r, const Byte g, const Byte b)
01174 {
01175 memset(mpData, 0, 8);
01176 mType = RGB;
01177 mLength = 3;
01178 ((Byte *)(mpData))[0] = r;
01179 ((Byte *)(mpData))[1] = g;
01180 ((Byte *)(mpData))[2] = b;
01181 return *this;
01182 }
01183
01184