itom 2.0.0
D:/git-itom/sources/itom/common/param.h
00001 /* ********************************************************************
00002     itom software
00003     URL: http://www.uni-stuttgart.de/ito
00004     Copyright (C) 2013, Institut für Technische Optik (ITO),
00005     Universität Stuttgart, Germany
00006 
00007     This file is part of itom and its software development toolkit (SDK).
00008 
00009     itom is free software; you can redistribute it and/or modify it
00010     under the terms of the GNU Library General Public Licence as published by
00011     the Free Software Foundation; either version 2 of the Licence, or (at
00012     your option) any later version.
00013    
00014     In addition, as a special exception, the Institut für Technische
00015     Optik (ITO) gives you certain additional rights.
00016     These rights are described in the ITO LGPL Exception version 1.0,
00017     which can be found in the file LGPL_EXCEPTION.txt in this package.
00018 
00019     itom is distributed in the hope that it will be useful, but
00020     WITHOUT ANY WARRANTY; without even the implied warranty of
00021     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library
00022     General Public Licence for more details.
00023 
00024     You should have received a copy of the GNU Library General Public License
00025     along with itom. If not, see <http://www.gnu.org/licenses/>.
00026 *********************************************************************** */
00027 
00028 #ifndef PARAM_H
00029 #define PARAM_H
00030 
00031 /* includes */
00032 
00033 #include "commonGlobal.h"
00034 #include "typeDefs.h"
00035 #include "byteArray.h"
00036 #include "retVal.h"
00037 #include "paramMeta.h"
00038 
00039 #include <string.h>
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 #include <stdarg.h>
00043 #include <limits>
00044 #include <stdexcept>
00045 
00046 /* definition and macros */
00047 /* global variables (avoid) */
00048 /* content */
00049 
00050 namespace ito
00051 {
00052     class Param;
00053     class ParamHelper;
00054     template<typename _Tp> struct ItomParamHelper;
00055 
00056     const uint32 paramFlagMask = 0xFFFF0000; 
00057     const uint32 paramTypeMask = 0x0000FFFF; 
00058 
00059 
00060     class ITOMCOMMON_EXPORT ParamBase
00061     {
00062     protected:
00063         uint32 m_type;
00064         ByteArray m_name;      
00065 
00066         void InOutCheck();
00067 
00068     private:
00069         double m_dVal;      
00070         int m_iVal;         
00071         char *m_cVal;       
00072 
00073     public:
00074         enum Type {
00075             //flags (bit 17-32)
00076             NoAutosave      = 0x010000,
00077             Readonly        = 0x020000, // this flag is not used inside tParam but you can check for it
00078             In              = 0x040000,
00079             Out             = 0x080000,
00080 
00081             //type (bit 1-16)
00082             Pointer         = 0x000001,
00083             Char            = 0x000002,
00084             Int             = 0x000004,
00085             Double          = 0x000008,
00086             //DObj            = 0x000010,
00087             String          = 0x000020 | Pointer,
00088             HWRef           = 0x000040 | Pointer | NoAutosave,
00089             DObjPtr         = 0x000010 | Pointer | NoAutosave,
00090             CharArray       = Char     | Pointer,
00091             IntArray        = Int      | Pointer,
00092             DoubleArray     = Double   | Pointer,
00093             PointCloudPtr   = 0x000080 | Pointer | NoAutosave,
00094             PointPtr        = 0x000100 | Pointer | NoAutosave,
00095             PolygonMeshPtr  = 0x000200 | Pointer | NoAutosave
00096         };
00097 
00098         static inline uint32 typeFilter(uint32 type) { return type & paramTypeMask; }
00099 
00100         //--------------------------------------------------------------------------------------------
00101         //  CONSTRUCTORS, COPY-CONSTRUCTOR, DESTRUCTOR
00102         //--------------------------------------------------------------------------------------------
00104         ParamBase() : m_type(0), m_name(NULL), m_dVal(0.0), m_iVal(0), m_cVal(NULL) {}
00105         ParamBase(const ByteArray &name);                                                                  // type-less ParamBase with name only
00106         ParamBase(const ByteArray &name, const uint32 type);                                               // constructor with type and name
00107         ParamBase(const ByteArray &name, const uint32 type, const char *val);                              // constructor with name and type, char val
00108         ParamBase(const ByteArray &name, const uint32 type, const double val);                             // constructor with name and type, double val and optional info
00109         ParamBase(const ByteArray &name, const uint32 type, const int val);                                // constructor with name and type, int val and optional info
00110         ParamBase(const ByteArray &name, const uint32 type, const unsigned int size, const char *values);  // array constructor with name and type, size and array
00111         ParamBase(const ByteArray &name, const uint32 type, const unsigned int size, const int *values);   // array constructor with name and type, size and array
00112         ParamBase(const ByteArray &name, const uint32 type, const unsigned int size, const double *values);// array constructor with name and type, size and array
00113         virtual ~ParamBase(); //Destructor
00114         ParamBase(const ParamBase &copyConstr); //Copy-Constructor
00115 
00116         //--------------------------------------------------------------------------------------------
00117         //  ASSIGNMENT AND OPERATORS
00118         //--------------------------------------------------------------------------------------------
00119         const ParamBase operator [] (const int num) const;     
00120         ParamBase& operator = (const ParamBase &rhs);          
00121         ito::RetVal copyValueFrom(const ito::ParamBase *rhs);  
00122 
00123         //--------------------------------------------------------------------------------------------
00124         //  SET/GET FURTHER PROPERTIES
00125         //--------------------------------------------------------------------------------------------
00126 
00128         inline bool isNumeric(void) const
00129         {
00130             static int numericTypeMask = ito::ParamBase::Char | ParamBase::Int | ParamBase::Double;
00131             int type = getType();
00132             return (type & numericTypeMask) && !(type & ito::ParamBase::Pointer);
00133         }
00134 
00135         inline ito::RetVal addNameSuffix(const char *suffix) 
00136         { 
00137             if (suffix)  
00138             { 
00139                 m_name.append(suffix);
00140             }
00141             return ito::retWarning;
00142         }
00144         inline bool isValid(void) const { return m_type != 0; }
00145 
00147         inline uint32 getType(bool filterFlags = true) const 
00148         { 
00149             if (filterFlags) 
00150                 return m_type & paramTypeMask; 
00151             else
00152                 return m_type;
00153         }
00154 
00156         inline uint32 getFlags(void) const { return m_type & paramFlagMask; }
00157         
00159         inline uint32 setFlags(const uint32 flags) { m_type = getType() | (flags & paramFlagMask); return 0; }
00160    
00162         inline const char * getName(void) const { return m_name.data(); }
00165         inline uint32 getAutosave(void) const { return ((m_type & NoAutosave) > 0 ? 0 : 1); }
00168         inline void setAutosave(const uint32 autosave) { m_type = autosave > 0 ? m_type & ~NoAutosave : m_type | NoAutosave; return; }
00169 
00170         int getLen(void) const;
00171         
00172             
00179         template<typename _Tp> inline ito::RetVal setVal(_Tp val)
00180         {
00181             return ItomParamHelper<_Tp>::setVal(getType(), m_cVal, m_iVal, m_dVal, val, 0);
00182         }
00183 
00191         template<typename _Tp> inline ito::RetVal setVal(_Tp val, int len)
00192         {
00193             return ItomParamHelper<_Tp>::setVal(getType(), m_cVal, m_iVal, m_dVal, val, len);
00194         }
00195 
00202         template<typename _Tp> inline _Tp getVal(void) const
00203         {
00204             int len = 0;
00205             return ItomParamHelper<_Tp>::getVal(getType(), m_cVal, m_iVal, m_dVal, len);
00206         }
00207 
00214         template<typename _Tp> inline _Tp getVal(int &len) const
00215         {
00216             return ItomParamHelper<_Tp>::getVal(getType(), m_cVal, m_iVal, m_dVal, len);
00217         }
00218 
00219     };
00220 
00221     //----------------------------------------------------------------------------------------------------------------------------------
00228     class ITOMCOMMON_EXPORT Param : public ParamBase
00229     {
00230         private:
00231             ParamMeta *m_pMeta;
00232             ByteArray m_info;
00233 
00234         public:
00235 
00236             //--------------------------------------------------------------------------------------------
00237             //  CONSTRUCTORS, COPY-CONSTRUCTOR, DESTRUCTOR
00238             //--------------------------------------------------------------------------------------------
00240             Param() : ParamBase(), m_pMeta(NULL), m_info(NULL) {}
00241             Param(const ByteArray &name) : ParamBase(name), m_pMeta(NULL), m_info(NULL) {}                         // type-less Param with name only
00242             Param(const ByteArray &name, const uint32 type) : ParamBase(name, type), m_pMeta(NULL), m_info(NULL) {}   // constructor with type and name
00243             Param(const ByteArray &name, const uint32 type, const char *val, const char *info);                        // constructor with name and type, char val and optional info
00244             Param(const ByteArray &name, const uint32 type, const double minVal, const double maxVal, const double val, const char *info); // constructor with name and type, double val, double minVal, double maxVal and optional info
00245             Param(const ByteArray &name, const uint32 type, const int minVal, const int maxVal, const int val, const char *info); // constructor with name and type, int val, int minVal, int maxVal and optional info
00246             Param(const ByteArray &name, const uint32 type, const char minVal, const char maxVal, const char val, const char *info); // constructor with name and type, int val, int minVal, int maxVal and optional info
00247             Param(const ByteArray &name, const uint32 type, const unsigned int size, const char *values, const char *info);  // array constructor with name and type, size and array
00248             Param(const ByteArray &name, const uint32 type, const unsigned int size, const int *values, const char *info);   // array constructor with name and type, size and array
00249             Param(const ByteArray &name, const uint32 type, const unsigned int size, const double *values, const char *info);// array constructor with name and type, size and array
00250             Param(const ByteArray &name, const uint32 type, const int val, ParamMeta *meta, const char *info);
00251             Param(const ByteArray &name, const uint32 type, const double val, ParamMeta *meta, const char *info);
00252             Param(const ByteArray &name, const uint32 type, const char val, ParamMeta *meta, const char *info);
00253             Param(const ByteArray &name, const uint32 type, const unsigned int size, const double *values, ParamMeta *meta, const char *info);
00254             Param(const ByteArray &name, const uint32 type, const unsigned int size, const int *values, ParamMeta *meta, const char *info);
00255             Param(const ByteArray &name, const uint32 type, const unsigned int size, const char *values, ParamMeta *meta, const char *info);
00256             ~Param();                        
00257             Param(const Param &copyConstr); 
00258 
00259             //--------------------------------------------------------------------------------------------
00260             //  ASSIGNMENT AND OPERATORS
00261             //--------------------------------------------------------------------------------------------
00262             const Param operator [] (const int num) const;    
00263             Param& operator = (const Param &rhs);             
00264             ito::RetVal copyValueFrom(const ParamBase *rhs);  
00265 
00266             //--------------------------------------------------------------------------------------------
00267             //  SET/GET FURTHER PROPERTIES
00268             //--------------------------------------------------------------------------------------------
00270             inline const char * getInfo(void) const { return m_info.data(); }
00271 
00273             inline void setInfo(const char *info)
00274             {
00275                 m_info = info;
00276             }
00277 
00278             inline void setInfo(const ByteArray &info)
00279             {
00280                 m_info = info;
00281             }
00282 
00283             inline const ParamMeta* getMeta(void) const { return m_pMeta; } 
00284             inline ParamMeta* getMeta(void) { return m_pMeta; }                
00285 
00287 
00292             void setMeta(ParamMeta* meta, bool takeOwnership = false);
00293 
00294             double getMin() const;
00295             double getMax() const;
00296     };
00297 
00298     //---------------------------------------------------------------------------------------------------------------------
00299     template<typename _Tp>
00300     struct ItomParamHelper
00301     {
00302         static ito::RetVal setVal(uint32 type, char *&cVal, int &iVal, double &/*dVal*/, _Tp val, int len = 0)
00303         {
00304             switch (type & paramTypeMask)
00305             {
00306                 case (ito::ParamBase::HWRef & paramTypeMask):
00307                 case (ito::ParamBase::DObjPtr & paramTypeMask):
00308                 case ito::ParamBase::PointCloudPtr & paramTypeMask:
00309                 case ito::ParamBase::PointPtr & paramTypeMask:
00310                 case ito::ParamBase::PolygonMeshPtr & paramTypeMask:
00311 //                case ito::ParamBase::Pointer & paramTypeMask:
00312                     cVal = reinterpret_cast<char*>(val);
00313                     return ito::retOk;
00314 
00315                 case ito::ParamBase::String & paramTypeMask:
00316                     {
00317                         char *cVal_ = cVal;
00318                         if (val)
00319                         {
00320                             cVal = _strdup(const_cast<const char*>((char*)val));
00321                             iVal = static_cast<int>(strlen(cVal));
00322                         }
00323                         else
00324                         {
00325                             cVal = 0;
00326                             iVal = -1;
00327                         }
00328                         if (cVal_)
00329                         {
00330                             free(cVal_);
00331                         }
00332                     }
00333                     return ito::retOk;
00334 
00335                 case ito::ParamBase::CharArray & ito::paramTypeMask:
00336                     {
00337                         char *cVal_ = cVal;
00338                         if ((val) && (len > 0))
00339                         {
00340                             cVal = (char*)malloc(len * sizeof(char));
00341                             memcpy(cVal, val, len * sizeof(char));
00342                             iVal = len;
00343                         }
00344                         else
00345                         {
00346                             cVal = NULL;
00347                             iVal = -1;
00348                         }
00349                         if (cVal_)
00350                         {
00351                             free(cVal_);
00352                         }
00353                     }
00354                     return ito::retOk;
00355 
00356                 case ito::ParamBase::IntArray & ito::paramTypeMask:
00357                     {
00358                         char *cVal_ = cVal;
00359                         if ((val) && (len > 0))
00360                         {
00361                             cVal = (char*)malloc(len * sizeof(int));
00362                             memcpy(cVal, val, len * sizeof(int));
00363                             iVal = len;
00364                         }
00365                         else
00366                         {
00367                             cVal = NULL;
00368                             iVal = -1;
00369                         }
00370                         if (cVal_)
00371                         {
00372                             free(cVal_);
00373                         }
00374                     }
00375                     return ito::retOk;
00376 
00377                 case ito::ParamBase::DoubleArray & ito::paramTypeMask:
00378                     {
00379                         char *cVal_ = cVal;
00380                         if ((val) && (len > 0))
00381                         {
00382                             cVal = (char*)malloc(len * sizeof(double));
00383                             memcpy(cVal, val, len * sizeof(double));
00384                             iVal = len;
00385                         }
00386                         else
00387                         {
00388                             cVal = NULL;
00389                             iVal = -1;
00390                         }
00391                         if (cVal_)
00392                         {
00393                             free(cVal_);
00394                         }
00395                     }
00396                     return ito::retOk;
00397 
00398                 default:
00399                     return ito::RetVal(ito::retError, 0, "_Tp parameter of setVal<_Tp> does not match the type of the parameter");
00400             }
00401         }
00402 
00403         static _Tp getVal(const uint32 type, const char *cVal, const int &iVal, const double &/*dVal*/, int &len)
00404         {
00405             switch (type & paramTypeMask)
00406             {
00407                 case ito::ParamBase::String & paramTypeMask:
00408                     if (cVal)
00409                     {
00410                         len = static_cast<int>(strlen(cVal));
00411                         //return reinterpret_cast<_Tp>(_strdup(cVal));
00412                         return reinterpret_cast<_Tp>(const_cast<char*>(cVal));
00413                     }
00414                     else
00415                     {
00416                         len = 0;
00417                         return 0;
00418                     }
00419 
00420                 case ito::ParamBase::CharArray & ito::paramTypeMask:
00421                 case ito::ParamBase::IntArray & ito::paramTypeMask:
00422                 case ito::ParamBase::DoubleArray & ito::paramTypeMask:
00423                     if (cVal)
00424                     {
00425                         len = iVal;
00426                         return reinterpret_cast<_Tp>(const_cast<char*>(cVal));
00427                     }
00428                     else
00429                     {
00430                         len = 0;
00431                         return 0;
00432                     }
00433 
00434                 case (ito::ParamBase::HWRef & paramTypeMask):
00435                 case (ito::ParamBase::DObjPtr & paramTypeMask):
00436                 case ito::ParamBase::PointCloudPtr & paramTypeMask:
00437                 case ito::ParamBase::PointPtr & paramTypeMask:
00438                 case ito::ParamBase::PolygonMeshPtr & paramTypeMask:
00439                     return reinterpret_cast<_Tp>(const_cast<char*>(cVal));
00440 
00441                 default:
00442                     throw std::logic_error("Param::getVal<_Tp>: Non-matching type!");
00443             }
00444         }
00445     };
00446 
00447     template<>
00448     struct ItomParamHelper<double>
00449     {
00450         static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, double val, int /*len = 0*/)
00451         {
00452             switch (type & paramTypeMask)
00453             {
00454                 case ito::ParamBase::Double & ito::paramTypeMask:
00455                     dVal = static_cast<double>(val);
00456                     return ito::retOk;
00457 
00458                 case ito::ParamBase::Int & ito::paramTypeMask:
00459                 case ito::ParamBase::Char & ito::paramTypeMask:
00460                     iVal = static_cast<int>(val);
00461                     return ito::retOk;
00462 
00463                 default:
00464                     return ito::RetVal(ito::retError, 0, "double value passed to setVal<double> does not match the type of the parameter");
00465             }
00466         }
00467 
00468         static double getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/)
00469         {
00470             switch (type & paramTypeMask)
00471             {
00472                 case ito::ParamBase::Int & ito::paramTypeMask:
00473                 case ito::ParamBase::Char & ito::paramTypeMask:
00474                     return static_cast<double>(iVal);
00475 
00476                 case ito::ParamBase::Double & ito::paramTypeMask:
00477                     return dVal;
00478 
00479                 default:
00480                     throw std::logic_error("Param::getVal<double>: Non-matching type!");
00481             }
00482         }
00483     };
00484 
00485     template<>
00486     struct ItomParamHelper<int>
00487     {
00488         static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, int val, int /*len = 0*/)
00489         {
00490             switch (type & paramTypeMask)
00491             {
00492                 case ito::ParamBase::Double & ito::paramTypeMask:
00493                     dVal = static_cast<int>(val);
00494                     return ito::retOk;
00495 
00496                 case ito::ParamBase::Int & ito::paramTypeMask:
00497                 case ito::ParamBase::Char & ito::paramTypeMask:
00498                     iVal = val;
00499                     return ito::retOk;
00500 
00501                 default:
00502                     return ito::RetVal(ito::retError, 0, "int value passed to setVal<int> does not match the type of the parameter");
00503             }
00504         }
00505 
00506         static int getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/)
00507         {
00508             switch (type & paramTypeMask)
00509             {
00510                 case ito::ParamBase::Int & ito::paramTypeMask:
00511                 case ito::ParamBase::Char & ito::paramTypeMask:
00512                     return iVal;
00513 
00514                 case ito::ParamBase::Double & ito::paramTypeMask:
00515                     return static_cast<int>(dVal);
00516 
00517                 case 0:
00518                     throw std::invalid_argument("Param::getVal<int>: non existent parameter");
00519 
00520                 default:
00521                     throw std::logic_error("Param::getVal<int>: Non-matching type!");
00522             }
00523         }
00524     };
00525 
00526     template<>
00527     struct ItomParamHelper<char>
00528     {
00529         static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, char val, int /*len = 0*/)
00530         {
00531             switch (type & paramTypeMask)
00532             {
00533                 case ito::ParamBase::Double & ito::paramTypeMask:
00534                     dVal = static_cast<char>(val);
00535                     return ito::retOk;
00536 
00537                 case ito::ParamBase::Int & ito::paramTypeMask:
00538                 case ito::ParamBase::Char & ito::paramTypeMask:
00539                     iVal = static_cast<char>(val);
00540                     return ito::retOk;
00541 
00542                 default:
00543                     return ito::RetVal(ito::retError, 0, "char value passed to setVal<char> does not match the type of the parameter");
00544             }
00545         }
00546 
00547         static char getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/)
00548         {
00549             switch (type & paramTypeMask)
00550             {
00551                 case ito::ParamBase::Int & ito::paramTypeMask:
00552                 case ito::ParamBase::Char & ito::paramTypeMask:
00553                     return static_cast<char>(iVal);
00554 
00555                 case ito::ParamBase::Double & ito::paramTypeMask:
00556                     return static_cast<char>(dVal);
00557 
00558                 case 0:
00559                     throw std::invalid_argument("Param::getVal<char>: non existent parameter");
00560 
00561                 default:
00562                     throw std::logic_error("Param::getVal<char>: Non-matching type!");
00563             }
00564         }
00565     };
00566 
00567     template<>
00568     struct ItomParamHelper<unsigned char>
00569     {
00570         static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, char val, int /*len = 0*/)
00571         {
00572             switch (type & paramTypeMask)
00573             {
00574                 case ito::ParamBase::Double & ito::paramTypeMask:
00575                     dVal = static_cast<unsigned char>(val);
00576                     return ito::retOk;
00577 
00578                 case ito::ParamBase::Int & ito::paramTypeMask:
00579                 case ito::ParamBase::Char & ito::paramTypeMask:
00580                     iVal = static_cast<unsigned char>(val);
00581                     return ito::retOk;
00582 
00583                 default:
00584                     return ito::RetVal(ito::retError, 0, "unsigned char value passed to setVal<unsigned char> does not match the type of the parameter");
00585             }
00586         }
00587 
00588         static char getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/)
00589         {
00590             switch (type & paramTypeMask)
00591             {
00592                 case ito::ParamBase::Int & ito::paramTypeMask:
00593                 case ito::ParamBase::Char & ito::paramTypeMask:
00594                     return static_cast<unsigned char>(iVal);
00595 
00596                 case ito::ParamBase::Double & ito::paramTypeMask:
00597                     return static_cast<unsigned char>(dVal);
00598 
00599                 case 0:
00600                     throw std::invalid_argument("Param::getVal<uchar>: non existent parameter");
00601 
00602                 default:
00603                     throw std::logic_error("Param::getVal<uchar>: Non-matching type!");
00604             }
00605         }
00606     };
00607 
00608 } //end namespace ito
00609 
00610 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends