UVariant.cpp
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #include "rtabmap/utilite/UVariant.h"
00021 #include "rtabmap/utilite/UConversion.h"
00022 #include <limits>
00023 #include <string.h>
00024 
00025 UVariant::UVariant() :
00026         type_(kUndef)
00027 {
00028 }
00029 UVariant::UVariant(const bool & value) :
00030         type_(kBool),
00031         data_(1)
00032 {
00033         data_[0] = value?1:0;
00034 }
00035 UVariant::UVariant(const char & value) :
00036         type_(kChar),
00037         data_(sizeof(char))
00038 {
00039         memcpy(data_.data(), &value, sizeof(char));
00040 }
00041 UVariant::UVariant(const unsigned char & value) :
00042         type_(kUChar),
00043         data_(sizeof(unsigned char))
00044 {
00045         memcpy(data_.data(), &value, sizeof(unsigned char));
00046 }
00047 UVariant::UVariant(const short & value) :
00048         type_(kShort),
00049         data_(sizeof(short))
00050 {
00051         memcpy(data_.data(), &value, sizeof(short));
00052 }
00053 UVariant::UVariant(const unsigned short & value) :
00054         type_(kUShort),
00055         data_(sizeof(unsigned short))
00056 {
00057         memcpy(data_.data(), &value, sizeof(unsigned short));
00058 }
00059 UVariant::UVariant(const int & value) :
00060         type_(kInt),
00061         data_(sizeof(int))
00062 {
00063         memcpy(data_.data(), &value, sizeof(int));
00064 }
00065 UVariant::UVariant(const unsigned int & value) :
00066         type_(kUInt),
00067         data_(sizeof(unsigned int))
00068 {
00069         memcpy(data_.data(), &value, sizeof(unsigned int));
00070 }
00071 UVariant::UVariant(const float & value) :
00072         type_(kFloat),
00073         data_(sizeof(float))
00074 {
00075         memcpy(data_.data(), &value, sizeof(float));
00076 }
00077 UVariant::UVariant(const double & value) :
00078         type_(kDouble),
00079         data_(sizeof(double))
00080 {
00081         memcpy(data_.data(), &value, sizeof(double));
00082 }
00083 UVariant::UVariant(const char * value) :
00084         type_(kStr)
00085 {
00086         std::string str(value);
00087         data_.resize(str.size()+1);
00088         memcpy(data_.data(), str.data(), str.size()+1);
00089 }
00090 UVariant::UVariant(const std::string & value) :
00091         type_(kStr),
00092         data_(value.size()+1) // with null character
00093 {
00094         memcpy(data_.data(), value.data(), value.size()+1);
00095 }
00096 
00097 bool UVariant::toBool() const
00098 {
00099         if(type_ ==kStr)
00100         {
00101                 return uStr2Bool(toStr().c_str());
00102         }
00103         else if(data_.size())
00104         {
00105                 return memcmp(data_.data(), std::vector<unsigned char>(data_.size(), 0).data(), data_.size()) != 0;
00106         }
00107         return false;
00108 }
00109 
00110 char UVariant::toChar(bool * ok) const
00111 {
00112         if(ok)
00113         {
00114                 *ok = false;
00115         }
00116         char v = 0;
00117         if(type_ == kChar)
00118         {
00119                 memcpy(&v, data_.data(), sizeof(char));
00120                 if(ok)
00121                 {
00122                         *ok = true;
00123                 }
00124         }
00125         else if(type_ == kUChar)
00126         {
00127                 unsigned char tmp = toUChar();
00128                 if(tmp <= std::numeric_limits<char>::max())
00129                 {
00130                         v = (char)tmp;
00131                         if(ok)
00132                         {
00133                                 *ok = true;
00134                         }
00135                 }
00136         }
00137         else if(type_ == kShort)
00138         {
00139                 short tmp = toShort();
00140                 if(tmp >= std::numeric_limits<char>::min() && tmp <= std::numeric_limits<char>::max())
00141                 {
00142                         v = (char)tmp;
00143                         if(ok)
00144                         {
00145                                 *ok = true;
00146                         }
00147                 }
00148         }
00149         else if(type_ == kUShort)
00150         {
00151                 unsigned short tmp = toUShort();
00152                 if(tmp <= std::numeric_limits<char>::max())
00153                 {
00154                         v = (char)tmp;
00155                         if(ok)
00156                         {
00157                                 *ok = true;
00158                         }
00159                 }
00160         }
00161         else if(type_ == kInt)
00162         {
00163                 int tmp = toInt();
00164                 if(tmp >= std::numeric_limits<char>::min() && tmp <= std::numeric_limits<char>::max())
00165                 {
00166                         v = (char)tmp;
00167                         if(ok)
00168                         {
00169                                 *ok = true;
00170                         }
00171                 }
00172         }
00173         else if(type_ == kUInt)
00174         {
00175                 unsigned int tmp = toUInt();
00176                 if(tmp <= (unsigned int)std::numeric_limits<char>::max())
00177                 {
00178                         v = (char)tmp;
00179                         if(ok)
00180                         {
00181                                 *ok = true;
00182                         }
00183                 }
00184         }
00185         return v;
00186 }
00187 unsigned char UVariant::toUChar(bool * ok) const
00188 {
00189         if(ok)
00190         {
00191                 *ok = false;
00192         }
00193         unsigned char v = 0;
00194         if(type_ == kUChar)
00195         {
00196                 memcpy(&v, data_.data(), sizeof(unsigned char));
00197                 if(ok)
00198                 {
00199                         *ok = true;
00200                 }
00201         }
00202         else if(type_ == kChar)
00203         {
00204                 char tmp = toChar();
00205                 if(tmp >= std::numeric_limits<unsigned char>::min())
00206                 {
00207                         v = (unsigned char)tmp;
00208                         if(ok)
00209                         {
00210                                 *ok = true;
00211                         }
00212                 }
00213         }
00214         else if(type_ == kShort)
00215         {
00216                 short tmp = toShort();
00217                 if(tmp >= std::numeric_limits<unsigned char>::min() && tmp <= std::numeric_limits<unsigned char>::max())
00218                 {
00219                         v = (unsigned char)tmp;
00220                         if(ok)
00221                         {
00222                                 *ok = true;
00223                         }
00224                 }
00225         }
00226         else if(type_ == kUShort)
00227         {
00228                 unsigned short tmp = toUShort();
00229                 if(tmp >= std::numeric_limits<unsigned char>::min() && tmp <= std::numeric_limits<unsigned char>::max())
00230                 {
00231                         v = (unsigned char)tmp;
00232                         if(ok)
00233                         {
00234                                 *ok = true;
00235                         }
00236                 }
00237         }
00238         else if(type_ == kInt)
00239         {
00240                 int tmp = toInt();
00241                 if(tmp >= std::numeric_limits<unsigned char>::min() && tmp <= std::numeric_limits<unsigned char>::max())
00242                 {
00243                         v = (unsigned char)tmp;
00244                         if(ok)
00245                         {
00246                                 *ok = true;
00247                         }
00248                 }
00249         }
00250         else if(type_ == kUInt)
00251         {
00252                 unsigned int tmp = toUInt();
00253                 if(tmp <= std::numeric_limits<unsigned char>::max())
00254                 {
00255                         v = (unsigned char)tmp;
00256                         if(ok)
00257                         {
00258                                 *ok = true;
00259                         }
00260                 }
00261         }
00262         return v;
00263 }
00264 short UVariant::toShort(bool * ok) const
00265 {
00266         if(ok)
00267         {
00268                 *ok = false;
00269         }
00270         short v = 0;
00271         if(type_ == kShort)
00272         {
00273                 memcpy(&v, data_.data(), sizeof(short));
00274                 if(ok)
00275                 {
00276                         *ok = true;
00277                 }
00278         }
00279         else if(type_ == kChar)
00280         {
00281                 v = (short)toChar();
00282                 if(ok)
00283                 {
00284                         *ok = true;
00285                 }
00286         }
00287         else if(type_ == kUChar)
00288         {
00289                 v = (short)toUChar();
00290                 if(ok)
00291                 {
00292                         *ok = true;
00293                 }
00294         }
00295         else if(type_ == kUShort)
00296         {
00297                 unsigned short tmp = toUShort();
00298                 if(tmp <= std::numeric_limits<short>::max())
00299                 {
00300                         v = (short)tmp;
00301                         if(ok)
00302                         {
00303                                 *ok = true;
00304                         }
00305                 }
00306         }
00307         else if(type_ == kInt)
00308         {
00309                 int tmp = toInt();
00310                 if(tmp >= std::numeric_limits<short>::min() && tmp <= std::numeric_limits<short>::max())
00311                 {
00312                         v = (short)tmp;
00313                         if(ok)
00314                         {
00315                                 *ok = true;
00316                         }
00317                 }
00318         }
00319         else if(type_ == kUInt)
00320         {
00321                 unsigned int tmp = toUInt();
00322                 if(tmp <= (unsigned int)std::numeric_limits<short>::max())
00323                 {
00324                         v = (short)tmp;
00325                         if(ok)
00326                         {
00327                                 *ok = true;
00328                         }
00329                 }
00330         }
00331         return v;
00332 }
00333 unsigned short UVariant::toUShort(bool * ok) const
00334 {
00335         if(ok)
00336         {
00337                 *ok = false;
00338         }
00339         unsigned short v = 0;
00340         if(type_ == kUShort)
00341         {
00342                 memcpy(&v, data_.data(), sizeof(unsigned short));
00343                 if(ok)
00344                 {
00345                         *ok = true;
00346                 }
00347         }
00348         else if(type_ == kChar)
00349         {
00350                 char tmp = toChar();
00351                 if(tmp >= 0)
00352                 {
00353                         v = (unsigned short)tmp;
00354                         if(ok)
00355                         {
00356                                 *ok = true;
00357                         }
00358                 }
00359         }
00360         else if(type_ == kUChar)
00361         {
00362                 v = (unsigned short)toUChar();
00363                 if(ok)
00364                 {
00365                         *ok = true;
00366                 }
00367         }
00368         else if(type_ == kShort)
00369         {
00370                 short tmp = toShort();
00371                 if(tmp >= std::numeric_limits<unsigned short>::min())
00372                 {
00373                         v = (unsigned short)tmp;
00374                         if(ok)
00375                         {
00376                                 *ok = true;
00377                         }
00378                 }
00379         }
00380         else if(type_ == kInt)
00381         {
00382                 int tmp = toInt();
00383                 if(tmp >= std::numeric_limits<unsigned short>::min() && tmp <= std::numeric_limits<unsigned short>::max())
00384                 {
00385                         v = (unsigned short)tmp;
00386                         if(ok)
00387                         {
00388                                 *ok = true;
00389                         }
00390                 }
00391         }
00392         else if(type_ == kUInt)
00393         {
00394                 unsigned int tmp = toUInt();
00395                 if(tmp <= std::numeric_limits<unsigned short>::max())
00396                 {
00397                         v = (unsigned short)tmp;
00398                         if(ok)
00399                         {
00400                                 *ok = true;
00401                         }
00402                 }
00403         }
00404         return v;
00405 }
00406 int UVariant::toInt(bool * ok) const
00407 {
00408         if(ok)
00409         {
00410                 *ok = false;
00411         }
00412         int v = 0;
00413         if(type_ == kInt)
00414         {
00415                 memcpy(&v, data_.data(), sizeof(int));
00416                 if(ok)
00417                 {
00418                         *ok = true;
00419                 }
00420         }
00421         else if(type_ == kChar)
00422         {
00423                 v = (int)toChar();
00424                 if(ok)
00425                 {
00426                         *ok = true;
00427                 }
00428         }
00429         else if(type_ == kUChar)
00430         {
00431                 v = (int)toUChar();
00432                 if(ok)
00433                 {
00434                         *ok = true;
00435                 }
00436         }
00437         else if(type_ == kShort)
00438         {
00439                 v = (int)toShort();
00440                 if(ok)
00441                 {
00442                         *ok = true;
00443                 }
00444         }
00445         else if(type_ == kUShort)
00446         {
00447                 v = (int)toUShort();
00448                 if(ok)
00449                 {
00450                         *ok = true;
00451                 }
00452         }
00453         else if(type_ == kUInt)
00454         {
00455                 unsigned int tmp = toUInt();
00456                 if(tmp <= (unsigned int)std::numeric_limits<int>::max())
00457                 {
00458                         v = (int)tmp;
00459                         if(ok)
00460                         {
00461                                 *ok = true;
00462                         }
00463                 }
00464         }
00465         return v;
00466 }
00467 unsigned int UVariant::toUInt(bool * ok) const
00468 {
00469         if(ok)
00470         {
00471                 *ok = false;
00472         }
00473         unsigned int v = 0;
00474         if(type_ == kUInt)
00475         {
00476                 memcpy(&v, data_.data(), sizeof(unsigned int));
00477                 if(ok)
00478                 {
00479                         *ok = true;
00480                 }
00481         }
00482         else if(type_ == kChar)
00483         {
00484                 char tmp = toChar();
00485                 if(tmp >= 0)
00486                 {
00487                         v = (unsigned int)tmp;
00488                         if(ok)
00489                         {
00490                                 *ok = true;
00491                         }
00492                 }
00493         }
00494         else if(type_ == kUChar)
00495         {
00496                 v = (unsigned int)toUChar();
00497                 if(ok)
00498                 {
00499                         *ok = true;
00500                 }
00501         }
00502         else if(type_ == kShort)
00503         {
00504                 short tmp = toShort();
00505                 if(tmp >= 0)
00506                 {
00507                         v = (unsigned int)tmp;
00508                         if(ok)
00509                         {
00510                                 *ok = true;
00511                         }
00512                 }
00513         }
00514         else if(type_ == kUShort)
00515         {
00516                 v = (unsigned int)toUShort();
00517                 if(ok)
00518                 {
00519                         *ok = true;
00520                 }
00521         }
00522         else if(type_ == kInt)
00523         {
00524                 int tmp = toInt();
00525                 if(tmp >= 0)
00526                 {
00527                         v = (unsigned int)tmp;
00528                         if(ok)
00529                         {
00530                                 *ok = true;
00531                         }
00532                 }
00533         }
00534         return v;
00535 }
00536 float UVariant::toFloat(bool * ok) const
00537 {
00538         if(ok)
00539         {
00540                 *ok = false;
00541         }
00542         float v = 0;
00543         if(type_ == kFloat)
00544         {
00545                 memcpy(&v, data_.data(), sizeof(float));
00546                 if(ok)
00547                 {
00548                         *ok = true;
00549                 }
00550         }
00551         else if(type_ == kDouble)
00552         {
00553                 double tmp = toDouble();
00554                 if(tmp >= std::numeric_limits<float>::min() && tmp <= std::numeric_limits<float>::max())
00555                 {
00556                         v = (float)tmp;
00557                         if(ok)
00558                         {
00559                                 *ok = true;
00560                         }
00561                 }
00562         }
00563         return v;
00564 }
00565 double UVariant::toDouble(bool * ok) const
00566 {
00567         if(ok)
00568         {
00569                 *ok = false;
00570         }
00571         double v = 0;
00572         if(type_ == kDouble)
00573         {
00574                 memcpy(&v, data_.data(), sizeof(double));
00575                 if(ok)
00576                 {
00577                         *ok = true;
00578                 }
00579         }
00580         else if(type_ == kFloat)
00581         {
00582                 v = (double)toFloat(ok);
00583         }
00584         return v;
00585 }
00586 std::string UVariant::toStr(bool * ok) const
00587 {
00588         if(ok)
00589         {
00590                 *ok = false;
00591         }
00592         std::string v;
00593         if(type_ == kStr)
00594         {
00595                 v = std::string((const char *)data_.data());
00596                 if(ok)
00597                 {
00598                         *ok = true;
00599                 }
00600         }
00601         else if(type_ == kBool)
00602         {
00603                 v = toBool()?"true":"false";
00604                 if(ok)
00605                 {
00606                         *ok = true;
00607                 }
00608         }
00609         else if(type_ == kChar)
00610         {
00611                 v = " ";
00612                 v.at(0) = toChar(ok);
00613         }
00614         else if(type_ == kUChar)
00615         {
00616                 v = uNumber2Str(toUChar(ok));
00617         }
00618         else if(type_ == kShort)
00619         {
00620                 v = uNumber2Str(toShort(ok));
00621         }
00622         else if(type_ == kUShort)
00623         {
00624                 v = uNumber2Str(toUShort(ok));
00625         }
00626         else if(type_ == kInt)
00627         {
00628                 v = uNumber2Str(toInt(ok));
00629         }
00630         else if(type_ == kUInt)
00631         {
00632                 v = uNumber2Str(toUInt(ok));
00633         }
00634         else if(type_ == kFloat)
00635         {
00636                 v = uNumber2Str(toFloat(ok));
00637         }
00638         else if(type_ == kDouble)
00639         {
00640                 v = uNumber2Str(toDouble(ok));
00641         }
00642         return v;
00643 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:32