itom 1.0.14
D:/git-itom/sources/itom/Common/sharedStructures.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 SHAREDSTRUCTURES_H
00029 #define SHAREDSTRUCTURES_H
00030 
00031 /* includes */
00032 
00033 #include "../common/typeDefs.h"
00034 //#include "./helper/paramHelper.h"
00035 
00036 //#include <string>
00037 #include <string.h>
00038 //#include <cstdlib>
00039 #include <stdlib.h>
00040 #include <stdio.h>
00041 #include <stdarg.h>
00042 #include <limits>
00043 #include <stdexcept>
00044 
00045 /* definition and macros */
00046 /* global variables (avoid) */
00047 /* content */
00048 
00049 namespace ito
00050 {
00051     #define CREATEVERSION(major,minor,patch)    (major << 16) + (minor << 8) + patch
00052     #define MAJORVERSION(version)               version >> 16
00053     #define MINORVERSION(version)               (version >> 8) - (MAJORVERSION(version) << 8)
00054     #define PATCHVERSION(version)               version - ((version >> 8) << 8)
00055     #define MAXVERSION                          CREATEVERSION(999999,0,0)    //maximum possible version (that means no maximum version is indicated)
00056     #define MINVERSION                          CREATEVERSION(0,0,0)         //minimum possible version
00057 
00058     #define ItomDoc_VAR(name) static char name[]
00059     #define ItomDoc_STRVAR(name,str) ItomDoc_VAR(name) = str
00060 
00061 
00062     //class tParam;
00063     class Param;
00064     class ParamHelper;
00065     class RetVal;
00066     template<typename _Tp> struct ItomParamHelper;
00067    // template<typename _Tp> void tParam_setVal(ito::tParam *param, double &dVal, int &iVal, char *&cVal, _Tp val);
00068 
00069     const uint32 paramFlagMask = 0xFFFF0000; 
00070     const uint32 paramTypeMask = 0x0000FFFF; 
00071 
00072 
00073     //----------------------------------------------------------------------------------------------------------------------------------
00081     class RetVal
00082     {
00083         public:
00084             inline RetVal(tRetValue retValue = retOk) : m_retValue(retValue), m_retCode(0), m_pRetMessage(NULL) {}
00085             inline RetVal(int retValue) : m_retValue((tRetValue)retValue), m_retCode(0), m_pRetMessage(NULL) {}
00086             //RetVal(tRetValue retValue = retOk); //Auskommentiert sonstwer
00087 
00088             //----------------------------------------------------------------------------------------------------------------------------------
00089             //RetVal(tRetValue retValue, int retCode, char *pRetMessage) // Auskommentiert Ly bei umstellen wegen dataObj 05.10.2011
00097             RetVal(ito::tRetValue retValue, int retCode, const char *pRetMessage) : m_retValue(retValue), m_retCode(retCode), m_pRetMessage(NULL) // Copied from sharedStructure.cpp 05.10.2011
00098             {
00099                 if (pRetMessage != NULL)
00100                 {
00101                     size_t messageLen = strlen(pRetMessage);
00102                     m_pRetMessage = new char [messageLen + 1];
00103                     memcpy(m_pRetMessage, pRetMessage, sizeof(*pRetMessage) * messageLen);
00104                     m_pRetMessage[messageLen] = '\0';
00105                 }
00106             }
00107 
00108             //----------------------------------------------------------------------------------------------------------------------------------
00109             //RetVal(const RetVal& copyConstr); // Auskommentiert Ly bei umstellen wegen dataObj 05.10.2011
00114             RetVal(const RetVal& copyConstr) // Copied from sharedStructure.cpp 05.10.2011
00115             {
00116                 m_retValue = copyConstr.m_retValue;
00117                 m_retCode = copyConstr.m_retCode;
00118 
00119                 if (copyConstr.m_pRetMessage != NULL)
00120                 {
00121                     size_t messageLen = strlen(copyConstr.m_pRetMessage);
00122                     m_pRetMessage = new char [messageLen + 1];
00123                     memcpy(m_pRetMessage, copyConstr.m_pRetMessage, sizeof(*copyConstr.m_pRetMessage) * messageLen);
00124                     m_pRetMessage[messageLen] = '\0';
00125                 }
00126                 else
00127                 {
00128                     m_pRetMessage = NULL;
00129                 }
00130             }
00131 
00132             //----------------------------------------------------------------------------------------------------------------------------------
00133             //~RetVal(); // Auskommentiert Ly bei umstellen wegen dataObj 05.10.2011
00137             ~RetVal() // Copied from sharedStructure.cpp 05.10.2011
00138             {
00139                 if (this->m_pRetMessage)
00140                 {
00141                     delete [] this->m_pRetMessage;
00142                     this->m_pRetMessage = NULL;
00143                 }
00144             }
00145 
00146             //----------------------------------------------------------------------------------------------------------------------------------
00147             //RetVal & operator = (const RetVal rhs); // Auskommentiert Ly bei umstellen wegen dataObj 05.10.2011
00151             RetVal & operator = (const RetVal rhs) // Copied from sharedStructure.cpp 05.10.2011
00152             {
00153                 if (this == &rhs)
00154                 {
00155                     return *this;
00156                 }
00157 
00158                 this->m_retCode = rhs.m_retCode;
00159                 this->m_retValue = rhs.m_retValue;
00160 
00161                 if (this->m_pRetMessage)
00162                 {
00163                     delete [] this->m_pRetMessage;
00164                     this->m_pRetMessage = NULL;
00165                 }
00166 
00167                 if (rhs.m_pRetMessage != NULL)
00168                 {
00169                     size_t messageLen = strlen(rhs.m_pRetMessage);
00170                     this->m_pRetMessage = new char [messageLen + 1];
00171                     memcpy(this->m_pRetMessage, rhs.m_pRetMessage, sizeof(*rhs.m_pRetMessage) * messageLen);
00172                     this->m_pRetMessage[messageLen] = '\0';
00173                 }
00174 
00175                 return *this;
00176             }
00177 
00178             //----------------------------------------------------------------------------------------------------------------------------------
00179             //RetVal & operator += (const RetVal rhs); // Auskommentiert Ly bei umstellen wegen dataObj 05.10.2011
00185             RetVal & operator += (const RetVal rhs) // Copied from sharedStructure.cpp 05.10.2011
00186             {
00187                 if (rhs.m_retValue > this->m_retValue)
00188                 {
00189                     this->m_retCode = rhs.m_retCode;
00190                     if (this->m_pRetMessage)
00191                     {
00192                         delete [] this->m_pRetMessage;
00193                         this->m_pRetMessage = NULL;
00194                     }
00195 
00196                     if (rhs.m_pRetMessage != NULL)
00197                     {
00198                         size_t messageLen = strlen(rhs.m_pRetMessage);
00199                         this->m_pRetMessage = new char [messageLen + 1];
00200                         memcpy(this->m_pRetMessage, rhs.m_pRetMessage, sizeof(*rhs.m_pRetMessage) * messageLen);
00201                         this->m_pRetMessage[messageLen] = '\0';
00202                     }
00203                 }
00204                 this->m_retValue = static_cast<tRetValue>(this->m_retValue | rhs.m_retValue); // |= rhs.m_retValue;
00205                 return *this;
00206             }
00207 
00208             //----------------------------------------------------------------------------------------------------------------------------------
00209             //RetVal operator + (const RetVal rhs); // Auskommentiert Ly bei umstellen wegen dataObj 05.10.2011
00214             RetVal operator + (const RetVal rhs) // Copied from sharedStructure.cpp 05.10.2011
00215             {
00216                 static RetVal result = *this;
00217                 result += rhs;
00218                 return result;
00219             }
00220 
00221             //----------------------------------------------------------------------------------------------------------------------------------
00222             //char operator == (const RetVal rhs); // Auskommentiert Ly bei umstellen wegen dataObj 05.10.2011
00226             char operator == (const RetVal rhs) // Copied from sharedStructure.cpp 05.10.2011
00227             {
00228                 return m_retValue == rhs.m_retValue;
00229             }
00230 
00231             //----------------------------------------------------------------------------------------------------------------------------------
00232             //char operator != (const RetVal rhs); // Auskommentiert Ly bei umstellen wegen dataObj 05.10.2011
00236             char operator != (const RetVal rhs) // Copied from sharedStructure.cpp 05.10.2011
00237             {
00238                 return !(m_retValue == rhs.m_retValue);
00239             }
00240 
00241             //----------------------------------------------------------------------------------------------------------------------------------
00242             //char operator == (const tRetValue rhs); // Auskommentiert Ly bei umstellen wegen dataObj 05.10.2011
00246             char operator == (const tRetValue rhs) // Copied from sharedStructure.cpp 05.10.2011
00247             {
00248                 return m_retValue == rhs;
00249             }
00250 
00251             //----------------------------------------------------------------------------------------------------------------------------------
00252             //char operator != (const tRetValue rhs); // Auskommentiert Ly bei umstellen wegen dataObj 05.10.2011
00256             char operator != (const tRetValue rhs) // Copied from sharedStructure.cpp 05.10.2011
00257             {
00258                 return !(m_retValue == rhs);
00259             }
00260 
00261             //----------------------------------------------------------------------------------------------------------------------------------
00262             inline int containsWarning() { return (m_retValue & retWarning); }              
00263             inline int containsError() { return (m_retValue & retError); }                 
00264             inline int containsWarningOrError() { return (m_retValue & (retError | retWarning)); }  
00266             inline char *errorMessage() { return m_pRetMessage; }
00267             inline int errorCode() const { return m_retCode; }
00268 
00269             //----------------------------------------------------------------------------------------------------------------------------------
00270             static RetVal format(ito::tRetValue retValue, int retCode, const char *pRetMessage, ...)
00271             {
00272                 if (pRetMessage != NULL)
00273                 {
00274                     //size_t messageLen = strlen(pRetMessage);
00275                     va_list args;
00276                     va_start (args, pRetMessage);
00277                     char buffer[2048];
00278                     int len = 0;
00279                     len = vsprintf_s(buffer, 2048, pRetMessage, args);
00280                     va_end(args);
00281                     if (len < 0)
00282                     {
00283                         return RetVal(retValue, retCode, pRetMessage);
00284                     }
00285                     buffer[len] = '\0';
00286                     return  RetVal(retValue, retCode, buffer);
00287                 }
00288                 else
00289                 {
00290                     return RetVal(retValue, retCode, NULL);
00291                 }
00292             }
00293 
00294             //----------------------------------------------------------------------------------------------------------------------------------
00295             inline void appendRetMessage(const char *addRetMessage)
00296             {
00297                 if (addRetMessage != NULL)
00298                 {
00299                     size_t messageLen = 0;
00300                     size_t messageLen2 = strlen(addRetMessage);
00301                     char *tempRetMessage = NULL;
00302                     
00303                     if (m_pRetMessage != NULL)
00304                     {
00305                         messageLen = strlen(m_pRetMessage);
00306                         tempRetMessage = new char [messageLen + messageLen2 + 1];
00307                         memcpy(tempRetMessage, m_pRetMessage, sizeof(*m_pRetMessage) * messageLen);
00308                         delete [] m_pRetMessage;
00309                     }
00310                     else
00311                     {
00312                         tempRetMessage = new char [messageLen2 + 1];
00313                     }
00314 
00315                     memcpy(tempRetMessage + messageLen, addRetMessage, sizeof(*addRetMessage) * messageLen2);
00316                     tempRetMessage[messageLen2 + messageLen] = '\0';
00317                     
00318                     m_pRetMessage = tempRetMessage;
00319                     tempRetMessage = NULL;
00320                 }
00321             } 
00322 
00323         private:
00324             tRetValue m_retValue;    
00325             int m_retCode;           
00326             char *m_pRetMessage;  
00327     };
00328 
00329     class ParamBase
00330     {
00331     protected:
00332         uint32 m_type;
00333         char *m_pName;      
00334 
00335         void InOutCheck();
00336 
00337     private:
00338         double m_dVal;      
00339         int m_iVal;         
00340         char *m_cVal;       
00341 
00342     public:
00343         enum Type {
00344             //flags (bit 17-32)
00345             NoAutosave      = 0x010000,
00346             Readonly        = 0x020000, // this flag is not used inside tParam but you can check for it
00347             In              = 0x040000,
00348             Out             = 0x080000,
00349 
00350             //type (bit 1-16)
00351             Pointer         = 0x000001,
00352             Char            = 0x000002,
00353             Int             = 0x000004,
00354             Double          = 0x000008,
00355             //DObj            = 0x000010,
00356             String          = 0x000020 | Pointer,
00357             HWRef           = 0x000040 | Pointer | NoAutosave,
00358             DObjPtr         = 0x000010 | Pointer | NoAutosave,
00359             CharArray       = Char     | Pointer,
00360             IntArray        = Int      | Pointer,
00361             DoubleArray     = Double   | Pointer,
00362             PointCloudPtr   = 0x000080 | Pointer | NoAutosave,
00363             PointPtr        = 0x000100 | Pointer | NoAutosave,
00364             PolygonMeshPtr  = 0x000200 | Pointer | NoAutosave
00365         };
00366 
00367         static inline uint32 typeFilter(uint32 type) { return type & paramTypeMask; }
00368 
00369         //--------------------------------------------------------------------------------------------
00370         //  CONSTRUCTORS, COPY-CONSTRUCTOR, DESTRUCTOR
00371         //--------------------------------------------------------------------------------------------
00373         ParamBase() : m_type(0), m_pName(NULL), m_dVal(0.0), m_iVal(0), m_cVal(NULL) {}
00374         ParamBase(const char *name);                                                               // type-less ParamBase with name only
00375         ParamBase(const char *name, const uint32 type);                                               // constructor with type and name
00376         ParamBase(const char *name, const uint32 type, const char *val);                              // constructor with name and type, char val
00377         ParamBase(const char *name, const uint32 type, const double val);                             // constructor with name and type, double val and optional info
00378         ParamBase(const char *name, const uint32 type, const int val);                                // constructor with name and type, int val and optional info
00379         ParamBase(const char *name, const uint32 type, const unsigned int size, const char *values);  // array constructor with name and type, size and array
00380         ParamBase(const char *name, const uint32 type, const unsigned int size, const int *values);   // array constructor with name and type, size and array
00381         ParamBase(const char *name, const uint32 type, const unsigned int size, const double *values);// array constructor with name and type, size and array
00382         virtual ~ParamBase(); //Destructor
00383         ParamBase(const ParamBase &copyConstr); //Copy-Constructor
00384 
00385         //--------------------------------------------------------------------------------------------
00386         //  ASSIGNMENT AND OPERATORS
00387         //--------------------------------------------------------------------------------------------
00388         const ParamBase operator [] (const int num) const;     
00389         ParamBase& operator = (const ParamBase &rhs);          
00390         ito::RetVal copyValueFrom(const ito::ParamBase *rhs);  
00391 
00392         //--------------------------------------------------------------------------------------------
00393         //  SET/GET FURTHER PROPERTIES
00394         //--------------------------------------------------------------------------------------------
00395 
00397         inline bool isNumeric(void) const
00398         {
00399             switch(this->getType())
00400             {
00401                 case Char:
00402                 case Int:
00403                 case Double:
00404                     return true;
00405                 default:
00406                     return false;
00407             }
00408         }
00409 
00410         inline int addNameSuffix(const char *suffix) 
00411         { 
00412             if (suffix && m_pName)  
00413             { 
00414                 size_t newSize = strlen(m_pName) + strlen(suffix) + 1;
00415                 m_pName = (char *)realloc(m_pName, newSize);
00416                 strcat_s(m_pName, newSize, suffix);
00417                 return ito::retOk;
00418             }
00419             return ito::retWarning;
00420         }
00422         inline bool isValid(void) const { return m_type != 0; }
00423 
00425         inline uint32 getType(bool filterFlags = true) const 
00426         { 
00427             if (filterFlags) 
00428                 return m_type & paramTypeMask; 
00429             else
00430                 return m_type;
00431         }
00432 
00434         inline uint32 getFlags(void) const { return m_type & paramFlagMask; }
00435         
00437         inline uint32 setFlags(const uint32 flags) { m_type = getType() | (flags & paramFlagMask); return 0; }
00438    
00440         inline const char * getName(void) const { return m_pName; }
00443         inline uint32 getAutosave(void) const { return ((m_type & NoAutosave) > 0 ? 0 : 1); }
00446         inline void setAutosave(const uint32 autosave) { m_type = autosave > 0 ? m_type & ~NoAutosave : m_type | NoAutosave; return; }
00447 
00448         inline int getLen(void) const
00449         {
00450             switch (m_type & paramTypeMask)
00451             {
00452                 case DoubleArray:
00453                 case IntArray:
00454                 case CharArray:
00455                     if (m_cVal)
00456                     {
00457                         return m_iVal;
00458                     }
00459                     else
00460                     {
00461                         return -1;
00462                     }
00463                 break;
00464 
00465                 case String:
00466                     if (m_cVal)
00467                     {
00468                         return static_cast<int>(strlen(m_cVal));
00469                     }
00470                     else
00471                     {
00472                         return 0;
00473                     }
00474                 break;
00475 
00476                 case Double:
00477                 case Int:
00478                     return 1;
00479                 break;
00480 
00481                 default:
00482                     return -1;
00483                 break;
00484             }
00485         }
00486 
00487             
00494         template<typename _Tp> inline ito::RetVal setVal(_Tp val)
00495         {
00496             return ItomParamHelper<_Tp>::setVal(getType(), m_cVal, m_iVal, m_dVal, val, 0);
00497         }
00498 
00506         template<typename _Tp> inline ito::RetVal setVal(_Tp val, int len)
00507         {
00508             return ItomParamHelper<_Tp>::setVal(getType(), m_cVal, m_iVal, m_dVal, val, len);
00509         }
00510 
00517         template<typename _Tp> inline _Tp getVal(void) const
00518         {
00519             int len = 0;
00520             return ItomParamHelper<_Tp>::getVal(getType(), m_cVal, m_iVal, m_dVal, len);
00521         }
00522 
00529         template<typename _Tp> inline _Tp getVal(int &len) const
00530         {
00531             return ItomParamHelper<_Tp>::getVal(getType(), m_cVal, m_iVal, m_dVal, len);
00532         }
00533 
00534     };
00535 
00541     class ParamMeta
00542     {
00543     public:
00544         ParamMeta() : m_type(0) {}
00545         ParamMeta(uint32 type) : m_type(type) {}
00546         virtual ~ParamMeta() {}
00547         inline uint32 getType() const { return m_type; }
00548     protected:
00549         uint32 m_type;
00550     };
00551 
00557     class CharMeta : public ParamMeta
00558     {
00559     public:
00561         explicit CharMeta(char minVal, char maxVal) : ParamMeta(ParamBase::Char), m_minVal(minVal), m_maxVal(maxVal) { if(m_maxVal < m_minVal) std::swap(m_minVal,m_maxVal); }
00562         static CharMeta* all() { return new CharMeta(std::numeric_limits<char>::min(), std::numeric_limits<char>::max() ); } 
00563         inline char getMin() const { return m_minVal; } 
00564         inline char getMax() const { return m_maxVal; } 
00565 
00567 
00570         inline void setMin(char val) { m_minVal = val; m_maxVal = std::max(m_maxVal,m_minVal); }
00571         
00573 
00576                 inline void setMax(char val) { m_maxVal = val; m_minVal = std::min(m_maxVal,m_minVal); }
00577     private:
00578         char m_minVal;
00579         char m_maxVal;
00580     };
00581 
00587     class IntMeta : public ParamMeta
00588     {
00589     public:
00591         explicit IntMeta(int minVal, int maxVal) : ParamMeta(ParamBase::Int), m_minVal(minVal), m_maxVal(maxVal) { if(m_maxVal < m_minVal) std::swap(m_minVal,m_maxVal); }
00592         static IntMeta* all() { return new IntMeta(std::numeric_limits<int>::min(), std::numeric_limits<int>::max() ); } 
00593         inline int getMin() const { return m_minVal; } 
00594         inline int getMax() const { return m_maxVal; } 
00595         
00597 
00600                 inline void setMin(int val) { m_minVal = val; m_maxVal = std::max(m_maxVal,m_minVal); }
00601         
00603 
00606                 inline void setMax(int val) { m_maxVal = val; m_minVal = std::min(m_maxVal,m_minVal); }
00607     private:
00608         int m_minVal;
00609         int m_maxVal;
00610     };
00611 
00617     class DoubleMeta : public ParamMeta
00618     {
00619     public:
00621         explicit DoubleMeta(double minVal, double maxVal) : ParamMeta(ParamBase::Double), m_minVal(minVal), m_maxVal(maxVal) { if(m_maxVal < m_minVal) std::swap(m_minVal,m_maxVal); }
00622         static DoubleMeta* all() { return new DoubleMeta(-std::numeric_limits<double>::max(), std::numeric_limits<double>::max() ); } 
00623         inline double getMin() const { return m_minVal; } 
00624         inline double getMax() const { return m_maxVal; } 
00625         
00627 
00630                 inline void setMin(double val) { m_minVal = val; m_maxVal = std::max(m_maxVal,m_minVal); }
00631         
00633 
00636                 inline void setMax(double val) { m_maxVal = val; m_minVal = std::min(m_maxVal,m_minVal); }
00637     private:
00638         double m_minVal;
00639         double m_maxVal;
00640     };
00641 
00647     class HWMeta : public ParamMeta
00648     {
00649         public:
00651 
00657             explicit HWMeta(uint32 minType) : ParamMeta(ParamBase::HWRef), m_minType(minType), m_pHWAddInName(NULL) {}
00658 
00660 
00665             explicit HWMeta(const char *HWAddInName) : ParamMeta(ParamBase::HWRef), m_minType(0), m_pHWAddInName(NULL)
00666             {
00667                 if(HWAddInName) m_pHWAddInName = _strdup(HWAddInName);
00668             }
00669             HWMeta(const HWMeta& cpy) : ParamMeta(ParamBase::HWRef), m_minType(cpy.m_minType), m_pHWAddInName(NULL)
00670             {
00671                 if(cpy.m_pHWAddInName) m_pHWAddInName = _strdup(cpy.m_pHWAddInName);
00672             }
00673             ~HWMeta() { if(m_pHWAddInName) free(m_pHWAddInName); }                      
00674             inline uint32 getMinType() const { return m_minType; }                              
00675             inline char * getHWAddInName() const { return m_pHWAddInName; } 
00676         private:
00677             uint32 m_minType;                   
00678             char *m_pHWAddInName;       
00679     };
00680 
00686     class StringMeta : public ParamMeta
00687     {
00688         public:
00689             enum tType {
00690                 String, 
00691                 Wildcard, 
00692                 RegExp    
00693             };
00694 
00696 
00701             StringMeta(tType type) : ParamMeta(ParamBase::String), m_stringType(type), m_len(0), m_val(NULL) {}
00702 
00704 
00710             StringMeta(tType type, const char* val) : ParamMeta(ParamBase::String), m_stringType(type), m_len(1)
00711             {
00712                 if(val)
00713                 {
00714                     m_val = (char**) calloc(1, sizeof(char*));
00715                     m_val[0] = _strdup(val);
00716                 }
00717                 else
00718                 {
00719                     m_len = 0;
00720                     m_val = NULL;
00721                 }
00722             }
00723 
00725             StringMeta(const StringMeta& cpy) : ParamMeta(ParamBase::String), m_stringType(cpy.m_stringType), m_len(cpy.m_len), m_val(NULL)
00726             {
00727                 if(m_len > 0)
00728                 {
00729                     m_val = (char**) calloc(m_len, sizeof(char*));
00730                     for(size_t i=0;i<m_len;i++) m_val[i] = _strdup(cpy.m_val[i]);
00731                 }
00732             }
00733 
00735             ~StringMeta()
00736             {
00737                 for(size_t i=0;i<m_len;i++) free(m_val[i]);
00738                 free(m_val);
00739             }
00740 
00741             inline tType getStringType() const { return m_stringType; } 
00742             inline size_t getLen() const { return m_len; } 
00743             inline const char* getString(size_t idx = 0) const { return (idx >= m_len) ? NULL : m_val[idx]; } 
00744             void addItem(const char *val) 
00745             {
00746                 if(m_val)
00747                 {
00748                     m_val = (char**)realloc(m_val, sizeof(char*) * (++m_len) );
00749                 }
00750                 else
00751                 {
00752                     m_val = (char**) calloc(1, sizeof(char*));
00753                 }
00754                 m_val[m_len-1] = _strdup(val);
00755             }
00756             StringMeta & operator += (const char *val)
00757             {
00758                 addItem(val);
00759                 return *this;
00760             }
00761 
00762         private:
00763             tType m_stringType;
00764             size_t m_len;
00765             char **m_val;
00766     };
00767 
00773     class DObjMeta : public ParamMeta
00774     {
00775         public:
00776             explicit DObjMeta(uint32 allowedTypes = 0xFFFF, size_t minDim = 0, size_t maxDim = std::numeric_limits<int>::max()) : ParamMeta(ParamBase::DObjPtr), m_allowedTypes(allowedTypes), m_minDim(minDim), m_maxDim(maxDim) {}
00777             inline int getAllowedTypes() const { return m_allowedTypes; }
00778             inline size_t getMinDim() const { return m_minDim; } 
00779             inline size_t getMaxDim() const { return m_maxDim; } 
00780 
00781         private:
00782             uint32 m_allowedTypes;
00783             size_t m_minDim;
00784             size_t m_maxDim;
00785     };
00786 
00787 
00788 
00789     //----------------------------------------------------------------------------------------------------------------------------------
00796     class Param : public ParamBase
00797     {
00798         private:
00799             ParamMeta *m_pMeta;
00800             char *m_pInfo;
00801 
00802         public:
00803 
00804             //--------------------------------------------------------------------------------------------
00805             //  CONSTRUCTORS, COPY-CONSTRUCTOR, DESTRUCTOR
00806             //--------------------------------------------------------------------------------------------
00808             Param() : ParamBase(), m_pMeta(NULL), m_pInfo(NULL) {}
00809             Param(const char *name) : ParamBase(name), m_pMeta(NULL), m_pInfo(NULL) {}                         // type-less Param with name only
00810             Param(const char *name, const uint32 type) : ParamBase(name, type), m_pMeta(NULL), m_pInfo(NULL) {}   // constructor with type and name
00811             Param(const char *name, const uint32 type, const char *val, const char *info);                        // constructor with name and type, char val and optional info
00812             Param(const char *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
00813             Param(const char *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
00814             Param(const char *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
00815             Param(const char *name, const uint32 type, const unsigned int size, const char *values, const char *info);  // array constructor with name and type, size and array
00816             Param(const char *name, const uint32 type, const unsigned int size, const int *values, const char *info);   // array constructor with name and type, size and array
00817             Param(const char *name, const uint32 type, const unsigned int size, const double *values, const char *info);// array constructor with name and type, size and array
00818             Param(const char *name, const uint32 type, const int val, ParamMeta *meta, const char *info);
00819             Param(const char *name, const uint32 type, const double val, ParamMeta *meta, const char *info);
00820             Param(const char *name, const uint32 type, const char val, ParamMeta *meta, const char *info);
00821             Param(const char *name, const uint32 type, const unsigned int size, const double *values, ParamMeta *meta, const char *info);
00822             Param(const char *name, const uint32 type, const unsigned int size, const int *values, ParamMeta *meta, const char *info);
00823             Param(const char *name, const uint32 type, const unsigned int size, const char *values, ParamMeta *meta, const char *info);
00824             ~Param();                                           
00825             Param(const Param &copyConstr); 
00826 
00827             //--------------------------------------------------------------------------------------------
00828             //  ASSIGNMENT AND OPERATORS
00829             //--------------------------------------------------------------------------------------------
00830             const Param operator [] (const int num) const;    
00831             Param& operator = (const Param &rhs);             
00832             ito::RetVal copyValueFrom(const ParamBase *rhs);  
00833 
00834             //--------------------------------------------------------------------------------------------
00835             //  SET/GET FURTHER PROPERTIES
00836             //--------------------------------------------------------------------------------------------
00838             inline const char * getInfo(void) const { return m_pInfo; }
00840             inline int setInfo(const char *info)
00841             {
00842                 if (m_pInfo) free(m_pInfo);
00843                 if (info)
00844                 {
00845                     if (!(m_pInfo = _strdup(info)))
00846                     {
00847                         return ito::retError;
00848                     }
00849                 }
00850                 else
00851                 {
00852                     m_pInfo = NULL;
00853                 }
00854                 return ito::retOk;
00855             }
00856 
00857             inline const ParamMeta* getMeta(void) const { return m_pMeta; } 
00858             inline ParamMeta* getMeta(void) { return m_pMeta; }                         
00859 
00861 
00866             bool setMeta(ParamMeta* meta, bool takeOwnership = false);
00867 
00868             bool copyMetaFrom(const ParamMeta *meta);
00869 
00870             double getMin() const;
00871             double getMax() const;
00872     };
00873 
00874     //---------------------------------------------------------------------------------------------------------------------
00875     template<typename _Tp>
00876     struct ItomParamHelper
00877     {
00878         static ito::RetVal setVal(uint32 type, char *&cVal, int &iVal, double &/*dVal*/, _Tp val, int len = 0)
00879         {
00880             switch (type & paramTypeMask)
00881             {
00882                 case (ito::ParamBase::HWRef & paramTypeMask):
00883                 case (ito::ParamBase::DObjPtr & paramTypeMask):
00884                 case ito::ParamBase::PointCloudPtr & paramTypeMask:
00885                 case ito::ParamBase::PointPtr & paramTypeMask:
00886                 case ito::ParamBase::PolygonMeshPtr & paramTypeMask:
00887 //                case ito::ParamBase::Pointer & paramTypeMask:
00888                     cVal = reinterpret_cast<char*>(val);
00889                     return ito::retOk;
00890                 break;
00891 
00892                 case ito::ParamBase::String & paramTypeMask:
00893                     if (cVal)
00894                     {
00895                         free(cVal);
00896                     }
00897                     if (val)
00898                     {
00899                         cVal = _strdup(const_cast<const char*>((char*)val));
00900                         iVal = static_cast<int>(strlen(cVal));
00901                     }
00902                     else
00903                     {
00904                         cVal = 0;
00905                         iVal = -1;
00906                     }
00907                     return ito::retOk;
00908                 break;
00909 
00910                 case ito::ParamBase::CharArray & ito::paramTypeMask:
00911                     if (cVal)
00912                     {
00913                         free(cVal);
00914                     }
00915                     if ((val) && (len > 0))
00916                     {
00917                         cVal = (char*)malloc(len * sizeof(char));
00918                         memcpy(cVal, val, len * sizeof(char));
00919                         iVal = len;
00920                     }
00921                     else
00922                     {
00923                         cVal = NULL;
00924                         iVal = -1;
00925                     }
00926                     return ito::retOk;
00927                 break;
00928 
00929                 case ito::ParamBase::IntArray & ito::paramTypeMask:
00930                     if (cVal)
00931                     {
00932                         free(cVal);
00933                     }
00934                     if ((val) && (len > 0))
00935                     {
00936                         cVal = (char*)malloc(len * sizeof(int));
00937                         memcpy(cVal, val, len * sizeof(int));
00938                         iVal = len;
00939                     }
00940                     else
00941                     {
00942                         cVal = NULL;
00943                         iVal = -1;
00944                     }
00945                     return ito::retOk;
00946                 break;
00947 
00948                 case ito::ParamBase::DoubleArray & ito::paramTypeMask:
00949                     if (cVal)
00950                     {
00951                         free(cVal);
00952                     }
00953                     if ((val) && (len > 0))
00954                     {
00955                         cVal = (char*)malloc(len * sizeof(double));
00956                         memcpy(cVal, val, len * sizeof(double));
00957                         iVal = len;
00958                     }
00959                     else
00960                     {
00961                         cVal = NULL;
00962                         iVal = -1;
00963                     }
00964                     return ito::retOk;
00965                 break;
00966 
00967                 default:
00968                     return ito::retError;
00969                 break;
00970             }
00971         }
00972 
00973         static _Tp getVal(const uint32 type, const char *cVal, const int &iVal, const double &/*dVal*/, int &len)
00974         {
00975             switch (type & paramTypeMask)
00976             {
00977                 case ito::ParamBase::String & paramTypeMask:
00978                     if (cVal)
00979                     {
00980                         len = static_cast<int>(strlen(cVal));
00981                         //return reinterpret_cast<_Tp>(_strdup(cVal));
00982                         return reinterpret_cast<_Tp>(const_cast<char*>(cVal));
00983                     }
00984                     else
00985                     {
00986                         len = 0;
00987                         return 0;
00988                     }
00989                 break;
00990 
00991                 case ito::ParamBase::CharArray & ito::paramTypeMask:
00992                 case ito::ParamBase::IntArray & ito::paramTypeMask:
00993                 case ito::ParamBase::DoubleArray & ito::paramTypeMask:
00994                     if (cVal)
00995                     {
00996                         len = iVal;
00997                         return reinterpret_cast<_Tp>(const_cast<char*>(cVal));
00998                     }
00999                     else
01000                     {
01001                         len = 0;
01002                         return 0;
01003                     }
01004                 break;
01005 
01006                 case (ito::ParamBase::HWRef & paramTypeMask):
01007                 case (ito::ParamBase::DObjPtr & paramTypeMask):
01008                 case ito::ParamBase::PointCloudPtr & paramTypeMask:
01009                 case ito::ParamBase::PointPtr & paramTypeMask:
01010                 case ito::ParamBase::PolygonMeshPtr & paramTypeMask:
01011                     return reinterpret_cast<_Tp>(const_cast<char*>(cVal));
01012                 break;
01013 
01014                 default:
01015                     throw std::logic_error("Non-matching type!");
01016                     return (_Tp)0;
01017                 break;
01018             }
01019         }
01020     };
01021 
01022     template<>
01023     struct ItomParamHelper<double>
01024     {
01025         static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, double val, int /*len = 0*/)
01026         {
01027             switch (type & paramTypeMask)
01028             {
01029                 case ito::ParamBase::Double & ito::paramTypeMask:
01030                     dVal = static_cast<double>(val);
01031                     return ito::retOk;
01032                 break;
01033 
01034                 case ito::ParamBase::Int & ito::paramTypeMask:
01035                     iVal = static_cast<int>(val);
01036                     return ito::retOk;
01037                 break;
01038 
01039                 default:
01040                     return ito::retError;
01041                 break;
01042             }
01043         }
01044 
01045         static double getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/)
01046         {
01047             switch (type & paramTypeMask)
01048             {
01049                 case ito::ParamBase::Int & ito::paramTypeMask:
01050                     return static_cast<double>(iVal);
01051                 break;
01052                 case ito::ParamBase::Double & ito::paramTypeMask:
01053                     return dVal;
01054                 break;
01055 
01056                 default:
01057                     throw std::logic_error("Non-matching type!");
01058                     return 0;
01059                 break;
01060             }
01061         }
01062     };
01063 
01064     template<>
01065     struct ItomParamHelper<int>
01066     {
01067         static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, int val, int /*len = 0*/)
01068         {
01069             switch (type & paramTypeMask)
01070             {
01071                 case ito::ParamBase::Double & ito::paramTypeMask:
01072                     dVal = static_cast<int>(val);
01073                     return ito::retOk;
01074                 break;
01075 
01076                 case ito::ParamBase::Int & ito::paramTypeMask:
01077                     iVal = val;
01078                     return ito::retOk;
01079                 break;
01080 
01081                 default:
01082                     return ito::retError;
01083                 break;
01084             }
01085         }
01086 
01087         static int getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/)
01088         {
01089             switch (type & paramTypeMask)
01090             {
01091                 case ito::ParamBase::Int & ito::paramTypeMask:
01092                     return iVal;
01093                 break;
01094 
01095                 case ito::ParamBase::Double & ito::paramTypeMask:
01096                     return static_cast<int>(dVal);
01097                 break;
01098 
01099                 case 0:
01100                     throw std::invalid_argument("non existent parameter");
01101                     return 0;
01102                 break;
01103 
01104                 default:
01105                     throw std::logic_error("Non-matching type!");
01106                     return 0;
01107                 break;
01108             }
01109         }
01110     };
01111 
01112     template<>
01113     struct ItomParamHelper<char>
01114     {
01115         static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, char val, int /*len = 0*/)
01116         {
01117             switch (type & paramTypeMask)
01118             {
01119                 case ito::ParamBase::Double & ito::paramTypeMask:
01120                     dVal = static_cast<char>(val);
01121                     return ito::retOk;
01122                 break;
01123 
01124                 case ito::ParamBase::Int & ito::paramTypeMask:
01125                     iVal = static_cast<char>(val);
01126                     return ito::retOk;
01127                 break;
01128 
01129                 default:
01130                     return ito::retError;
01131                 break;
01132             }
01133         }
01134 
01135         static char getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/)
01136         {
01137             switch (type & paramTypeMask)
01138             {
01139                 case ito::ParamBase::Int & ito::paramTypeMask:
01140                     return static_cast<char>(iVal);
01141                 break;
01142 
01143                 case ito::ParamBase::Double & ito::paramTypeMask:
01144                     return static_cast<char>(dVal);
01145                 break;
01146 
01147                 case 0:
01148                     throw std::invalid_argument("non existent parameter");
01149                     return 0;
01150                 break;
01151 
01152                 default:
01153                     throw std::logic_error("Non-matching type!");
01154                     return 0;
01155                 break;
01156             }
01157         }
01158     };
01159 
01160     template<>
01161     struct ItomParamHelper<unsigned char>
01162     {
01163         static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, char val, int /*len = 0*/)
01164         {
01165             switch (type & paramTypeMask)
01166             {
01167                 case ito::ParamBase::Double & ito::paramTypeMask:
01168                     dVal = static_cast<unsigned char>(val);
01169                     return ito::retOk;
01170                 break;
01171 
01172                 case ito::ParamBase::Int & ito::paramTypeMask:
01173                     iVal = static_cast<unsigned char>(val);
01174                     return ito::retOk;
01175                 break;
01176 
01177                 default:
01178                     return ito::retError;
01179                 break;
01180             }
01181         }
01182 
01183         static char getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/)
01184         {
01185             switch (type & paramTypeMask)
01186             {
01187                 case ito::ParamBase::Int & ito::paramTypeMask:
01188                     return static_cast<unsigned char>(iVal);
01189                 break;
01190 
01191                 case ito::ParamBase::Double & ito::paramTypeMask:
01192                     return static_cast<unsigned char>(dVal);
01193                 break;
01194 
01195                 case 0:
01196                     throw std::invalid_argument("non existent parameter");
01197                     return 0;
01198                 break;
01199 
01200                 default:
01201                     throw std::logic_error("Non-matching type!");
01202                     return 0;
01203                 break;
01204             }
01205         }
01206     };
01207 
01208 } //end namespace ito
01209 
01210 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends