itom 1.4.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::retError;
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                     return (_Tp)0;
00444             }
00445         }
00446     };
00447 
00448     template<>
00449     struct ItomParamHelper<double>
00450     {
00451         static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, double val, int /*len = 0*/)
00452         {
00453             switch (type & paramTypeMask)
00454             {
00455                 case ito::ParamBase::Double & ito::paramTypeMask:
00456                     dVal = static_cast<double>(val);
00457                     return ito::retOk;
00458 
00459                 case ito::ParamBase::Int & ito::paramTypeMask:
00460                 case ito::ParamBase::Char & ito::paramTypeMask:
00461                     iVal = static_cast<int>(val);
00462                     return ito::retOk;
00463 
00464                 default:
00465                     return ito::retError;
00466             }
00467         }
00468 
00469         static double getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/)
00470         {
00471             switch (type & paramTypeMask)
00472             {
00473                 case ito::ParamBase::Int & ito::paramTypeMask:
00474                 case ito::ParamBase::Char & ito::paramTypeMask:
00475                     return static_cast<double>(iVal);
00476 
00477                 case ito::ParamBase::Double & ito::paramTypeMask:
00478                     return dVal;
00479 
00480                 default:
00481                     throw std::logic_error("Param::getVal<double>: Non-matching type!");
00482                     return 0;
00483             }
00484         }
00485     };
00486 
00487     template<>
00488     struct ItomParamHelper<int>
00489     {
00490         static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, int val, int /*len = 0*/)
00491         {
00492             switch (type & paramTypeMask)
00493             {
00494                 case ito::ParamBase::Double & ito::paramTypeMask:
00495                     dVal = static_cast<int>(val);
00496                     return ito::retOk;
00497 
00498                 case ito::ParamBase::Int & ito::paramTypeMask:
00499                 case ito::ParamBase::Char & ito::paramTypeMask:
00500                     iVal = val;
00501                     return ito::retOk;
00502 
00503                 default:
00504                     return ito::retError;
00505             }
00506         }
00507 
00508         static int getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/)
00509         {
00510             switch (type & paramTypeMask)
00511             {
00512                 case ito::ParamBase::Int & ito::paramTypeMask:
00513                 case ito::ParamBase::Char & ito::paramTypeMask:
00514                     return iVal;
00515 
00516                 case ito::ParamBase::Double & ito::paramTypeMask:
00517                     return static_cast<int>(dVal);
00518 
00519                 case 0:
00520                     throw std::invalid_argument("Param::getVal<int>: non existent parameter");
00521 
00522                 default:
00523                     throw std::logic_error("Param::getVal<int>: Non-matching type!");
00524             }
00525         }
00526     };
00527 
00528     template<>
00529     struct ItomParamHelper<char>
00530     {
00531         static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, char val, int /*len = 0*/)
00532         {
00533             switch (type & paramTypeMask)
00534             {
00535                 case ito::ParamBase::Double & ito::paramTypeMask:
00536                     dVal = static_cast<char>(val);
00537                     return ito::retOk;
00538 
00539                 case ito::ParamBase::Int & ito::paramTypeMask:
00540                 case ito::ParamBase::Char & ito::paramTypeMask:
00541                     iVal = static_cast<char>(val);
00542                     return ito::retOk;
00543 
00544                 default:
00545                     return ito::retError;
00546             }
00547         }
00548 
00549         static char getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/)
00550         {
00551             switch (type & paramTypeMask)
00552             {
00553                 case ito::ParamBase::Int & ito::paramTypeMask:
00554                 case ito::ParamBase::Char & ito::paramTypeMask:
00555                     return static_cast<char>(iVal);
00556 
00557                 case ito::ParamBase::Double & ito::paramTypeMask:
00558                     return static_cast<char>(dVal);
00559 
00560                 case 0:
00561                     throw std::invalid_argument("Param::getVal<char>: non existent parameter");
00562 
00563                 default:
00564                     throw std::logic_error("Param::getVal<char>: Non-matching type!");
00565             }
00566         }
00567     };
00568 
00569     template<>
00570     struct ItomParamHelper<unsigned char>
00571     {
00572         static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, char val, int /*len = 0*/)
00573         {
00574             switch (type & paramTypeMask)
00575             {
00576                 case ito::ParamBase::Double & ito::paramTypeMask:
00577                     dVal = static_cast<unsigned char>(val);
00578                     return ito::retOk;
00579 
00580                 case ito::ParamBase::Int & ito::paramTypeMask:
00581                 case ito::ParamBase::Char & ito::paramTypeMask:
00582                     iVal = static_cast<unsigned char>(val);
00583                     return ito::retOk;
00584 
00585                 default:
00586                     return ito::retError;
00587             }
00588         }
00589 
00590         static char getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/)
00591         {
00592             switch (type & paramTypeMask)
00593             {
00594                 case ito::ParamBase::Int & ito::paramTypeMask:
00595                 case ito::ParamBase::Char & ito::paramTypeMask:
00596                     return static_cast<unsigned char>(iVal);
00597 
00598                 case ito::ParamBase::Double & ito::paramTypeMask:
00599                     return static_cast<unsigned char>(dVal);
00600 
00601                 case 0:
00602                     throw std::invalid_argument("Param::getVal<uchar>: non existent parameter");
00603                     return 0;
00604 
00605                 default:
00606                     throw std::logic_error("Param::getVal<uchar>: Non-matching type!");
00607                     return 0;
00608             }
00609         }
00610     };
00611 
00612 } //end namespace ito
00613 
00614 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends