itom 1.1.0
|
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 int messageLen = (int)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 int messageLen = (int)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 int messageLen = (int)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 int messageLen = (int)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 //int 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 int messageLen = 0; 00300 int messageLen2 = (int)strlen(addRetMessage); 00301 char *tempRetMessage = NULL; 00302 00303 if (m_pRetMessage != NULL) 00304 { 00305 messageLen = (int)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 ©Constr); //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 ito::RetVal addNameSuffix(const char *suffix) 00411 { 00412 if (suffix && m_pName) 00413 { 00414 int newSize = (int)strlen(m_pName) + (int)strlen(suffix) + 1; 00415 char *tmp = (char *)realloc(m_pName, newSize); 00416 if (tmp) 00417 { 00418 // m_pName = (char *)realloc(m_pName, newSize); 00419 // possible memleak when assigning value with realloc 00420 m_pName = tmp; //location of reallocated array can change 00421 strcat_s(m_pName, newSize, suffix); 00422 return ito::retOk; 00423 } 00424 else 00425 return ito::retError; 00426 } 00427 return ito::retWarning; 00428 } 00430 inline bool isValid(void) const { return m_type != 0; } 00431 00433 inline uint32 getType(bool filterFlags = true) const 00434 { 00435 if (filterFlags) 00436 return m_type & paramTypeMask; 00437 else 00438 return m_type; 00439 } 00440 00442 inline uint32 getFlags(void) const { return m_type & paramFlagMask; } 00443 00445 inline uint32 setFlags(const uint32 flags) { m_type = getType() | (flags & paramFlagMask); return 0; } 00446 00448 inline const char * getName(void) const { return m_pName; } 00451 inline uint32 getAutosave(void) const { return ((m_type & NoAutosave) > 0 ? 0 : 1); } 00454 inline void setAutosave(const uint32 autosave) { m_type = autosave > 0 ? m_type & ~NoAutosave : m_type | NoAutosave; return; } 00455 00456 inline int getLen(void) const 00457 { 00458 switch (m_type & paramTypeMask) 00459 { 00460 case DoubleArray: 00461 case IntArray: 00462 case CharArray: 00463 if (m_cVal) 00464 { 00465 return m_iVal; 00466 } 00467 else 00468 { 00469 return -1; 00470 } 00471 00472 case String: 00473 if (m_cVal) 00474 { 00475 return static_cast<int>(strlen(m_cVal)); 00476 } 00477 else 00478 { 00479 return 0; 00480 } 00481 00482 case Double: 00483 case Int: 00484 return 1; 00485 00486 default: 00487 return -1; 00488 } 00489 } 00490 00491 00498 template<typename _Tp> inline ito::RetVal setVal(_Tp val) 00499 { 00500 return ItomParamHelper<_Tp>::setVal(getType(), m_cVal, m_iVal, m_dVal, val, 0); 00501 } 00502 00510 template<typename _Tp> inline ito::RetVal setVal(_Tp val, int len) 00511 { 00512 return ItomParamHelper<_Tp>::setVal(getType(), m_cVal, m_iVal, m_dVal, val, len); 00513 } 00514 00521 template<typename _Tp> inline _Tp getVal(void) const 00522 { 00523 int len = 0; 00524 return ItomParamHelper<_Tp>::getVal(getType(), m_cVal, m_iVal, m_dVal, len); 00525 } 00526 00533 template<typename _Tp> inline _Tp getVal(int &len) const 00534 { 00535 return ItomParamHelper<_Tp>::getVal(getType(), m_cVal, m_iVal, m_dVal, len); 00536 } 00537 00538 }; 00539 00545 class ParamMeta 00546 { 00547 public: 00548 ParamMeta() : m_type(0) {} 00549 ParamMeta(uint32 type) : m_type(type) {} 00550 virtual ~ParamMeta() {} 00551 inline uint32 getType() const { return m_type; } 00552 protected: 00553 uint32 m_type; 00554 }; 00555 00561 class CharMeta : public ParamMeta 00562 { 00563 public: 00565 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); } 00566 static CharMeta* all() { return new CharMeta(std::numeric_limits<char>::min(), std::numeric_limits<char>::max() ); } 00567 inline char getMin() const { return m_minVal; } 00568 inline char getMax() const { return m_maxVal; } 00569 00571 00574 inline void setMin(char val) { m_minVal = val; m_maxVal = std::max(m_maxVal,m_minVal); } 00575 00577 00580 inline void setMax(char val) { m_maxVal = val; m_minVal = std::min(m_maxVal,m_minVal); } 00581 private: 00582 char m_minVal; 00583 char m_maxVal; 00584 }; 00585 00591 class IntMeta : public ParamMeta 00592 { 00593 public: 00595 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); } 00596 static IntMeta* all() { return new IntMeta(std::numeric_limits<int>::min(), std::numeric_limits<int>::max() ); } 00597 inline int getMin() const { return m_minVal; } 00598 inline int getMax() const { return m_maxVal; } 00599 00601 00604 inline void setMin(int val) { m_minVal = val; m_maxVal = std::max(m_maxVal,m_minVal); } 00605 00607 00610 inline void setMax(int val) { m_maxVal = val; m_minVal = std::min(m_maxVal,m_minVal); } 00611 private: 00612 int m_minVal; 00613 int m_maxVal; 00614 }; 00615 00621 class DoubleMeta : public ParamMeta 00622 { 00623 public: 00625 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); } 00626 static DoubleMeta* all() { return new DoubleMeta(-std::numeric_limits<double>::max(), std::numeric_limits<double>::max() ); } 00627 inline double getMin() const { return m_minVal; } 00628 inline double getMax() const { return m_maxVal; } 00629 00631 00634 inline void setMin(double val) { m_minVal = val; m_maxVal = std::max(m_maxVal,m_minVal); } 00635 00637 00640 inline void setMax(double val) { m_maxVal = val; m_minVal = std::min(m_maxVal,m_minVal); } 00641 private: 00642 double m_minVal; 00643 double m_maxVal; 00644 }; 00645 00651 class HWMeta : public ParamMeta 00652 { 00653 public: 00655 00661 explicit HWMeta(uint32 minType) : ParamMeta(ParamBase::HWRef), m_minType(minType), m_pHWAddInName(NULL) {} 00662 00664 00669 explicit HWMeta(const char *HWAddInName) : ParamMeta(ParamBase::HWRef), m_minType(0), m_pHWAddInName(NULL) 00670 { 00671 if(HWAddInName) m_pHWAddInName = _strdup(HWAddInName); 00672 } 00673 HWMeta(const HWMeta& cpy) : ParamMeta(ParamBase::HWRef), m_minType(cpy.m_minType), m_pHWAddInName(NULL) 00674 { 00675 if(cpy.m_pHWAddInName) m_pHWAddInName = _strdup(cpy.m_pHWAddInName); 00676 } 00677 ~HWMeta() { if(m_pHWAddInName) free(m_pHWAddInName); } 00678 inline uint32 getMinType() const { return m_minType; } 00679 inline char * getHWAddInName() const { return m_pHWAddInName; } 00680 private: 00681 uint32 m_minType; 00682 char *m_pHWAddInName; 00683 }; 00684 00690 class StringMeta : public ParamMeta 00691 { 00692 public: 00693 enum tType { 00694 String, 00695 Wildcard, 00696 RegExp 00697 }; 00698 00700 00705 StringMeta(tType type) : ParamMeta(ParamBase::String), m_stringType(type), m_len(0), m_val(NULL) {} 00706 00708 00714 StringMeta(tType type, const char* val) : ParamMeta(ParamBase::String), m_stringType(type), m_len(1) 00715 { 00716 if(val) 00717 { 00718 m_val = (char**) calloc(1, sizeof(char*)); 00719 m_val[0] = _strdup(val); 00720 } 00721 else 00722 { 00723 m_len = 0; 00724 m_val = NULL; 00725 } 00726 } 00727 00729 StringMeta(const StringMeta& cpy) : ParamMeta(ParamBase::String), m_stringType(cpy.m_stringType), m_len(cpy.m_len), m_val(NULL) 00730 { 00731 if(m_len > 0) 00732 { 00733 m_val = (char**) calloc(m_len, sizeof(char*)); 00734 for(int i=0;i<m_len;++i) m_val[i] = _strdup(cpy.m_val[i]); 00735 } 00736 } 00737 00739 ~StringMeta() 00740 { 00741 for(int i=0;i<m_len;++i) free(m_val[i]); 00742 free(m_val); 00743 } 00744 00745 inline tType getStringType() const { return m_stringType; } 00746 inline int getLen() const { return m_len; } 00747 inline const char* getString(int idx = 0) const { return (idx >= m_len) ? NULL : m_val[idx]; } 00748 bool addItem(const char *val) 00749 { 00750 if(m_val) 00751 { 00752 char **m_val_old = m_val; 00753 m_val = (char**)realloc(m_val, sizeof(char*) * (++m_len) ); //m_val can change its address. if NULL, reallocation failed and m_val_old still contains old values 00754 if (!m_val) 00755 { 00756 m_val = m_val_old; 00757 m_len--; //failed to add new value 00758 return false; 00759 } 00760 } 00761 else 00762 { 00763 m_val = (char**) calloc(++m_len, sizeof(char*)); 00764 } 00765 m_val[m_len-1] = _strdup(val); 00766 return true; 00767 } 00768 00769 StringMeta & operator += (const char *val) 00770 { 00771 addItem(val); 00772 return *this; 00773 } 00774 00775 private: 00776 tType m_stringType; 00777 int m_len; 00778 char **m_val; 00779 }; 00780 00786 class DObjMeta : public ParamMeta 00787 { 00788 public: 00789 explicit DObjMeta(uint32 allowedTypes = 0xFFFF, int minDim = 0, int maxDim = std::numeric_limits<int>::max()) : ParamMeta(ParamBase::DObjPtr), m_allowedTypes(allowedTypes), m_minDim(minDim), m_maxDim(maxDim) {} 00790 inline int getAllowedTypes() const { return m_allowedTypes; } 00791 inline int getMinDim() const { return m_minDim; } 00792 inline int getMaxDim() const { return m_maxDim; } 00793 00794 private: 00795 uint32 m_allowedTypes; 00796 int m_minDim; 00797 int m_maxDim; 00798 }; 00799 00800 00801 00802 //---------------------------------------------------------------------------------------------------------------------------------- 00809 class Param : public ParamBase 00810 { 00811 private: 00812 ParamMeta *m_pMeta; 00813 char *m_pInfo; 00814 00815 public: 00816 00817 //-------------------------------------------------------------------------------------------- 00818 // CONSTRUCTORS, COPY-CONSTRUCTOR, DESTRUCTOR 00819 //-------------------------------------------------------------------------------------------- 00821 Param() : ParamBase(), m_pMeta(NULL), m_pInfo(NULL) {} 00822 Param(const char *name) : ParamBase(name), m_pMeta(NULL), m_pInfo(NULL) {} // type-less Param with name only 00823 Param(const char *name, const uint32 type) : ParamBase(name, type), m_pMeta(NULL), m_pInfo(NULL) {} // constructor with type and name 00824 Param(const char *name, const uint32 type, const char *val, const char *info); // constructor with name and type, char val and optional info 00825 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 00826 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 00827 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 00828 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 00829 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 00830 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 00831 Param(const char *name, const uint32 type, const int val, ParamMeta *meta, const char *info); 00832 Param(const char *name, const uint32 type, const double val, ParamMeta *meta, const char *info); 00833 Param(const char *name, const uint32 type, const char val, ParamMeta *meta, const char *info); 00834 Param(const char *name, const uint32 type, const unsigned int size, const double *values, ParamMeta *meta, const char *info); 00835 Param(const char *name, const uint32 type, const unsigned int size, const int *values, ParamMeta *meta, const char *info); 00836 Param(const char *name, const uint32 type, const unsigned int size, const char *values, ParamMeta *meta, const char *info); 00837 ~Param(); 00838 Param(const Param ©Constr); 00839 00840 //-------------------------------------------------------------------------------------------- 00841 // ASSIGNMENT AND OPERATORS 00842 //-------------------------------------------------------------------------------------------- 00843 const Param operator [] (const int num) const; 00844 Param& operator = (const Param &rhs); 00845 ito::RetVal copyValueFrom(const ParamBase *rhs); 00846 00847 //-------------------------------------------------------------------------------------------- 00848 // SET/GET FURTHER PROPERTIES 00849 //-------------------------------------------------------------------------------------------- 00851 inline const char * getInfo(void) const { return m_pInfo; } 00853 inline int setInfo(const char *info) 00854 { 00855 if (m_pInfo) free(m_pInfo); 00856 if (info) 00857 { 00858 if (!(m_pInfo = _strdup(info))) 00859 { 00860 return ito::retError; 00861 } 00862 } 00863 else 00864 { 00865 m_pInfo = NULL; 00866 } 00867 return ito::retOk; 00868 } 00869 00870 inline const ParamMeta* getMeta(void) const { return m_pMeta; } 00871 inline ParamMeta* getMeta(void) { return m_pMeta; } 00872 00874 00879 bool setMeta(ParamMeta* meta, bool takeOwnership = false); 00880 00881 bool copyMetaFrom(const ParamMeta *meta); 00882 00883 double getMin() const; 00884 double getMax() const; 00885 }; 00886 00887 //--------------------------------------------------------------------------------------------------------------------- 00888 template<typename _Tp> 00889 struct ItomParamHelper 00890 { 00891 static ito::RetVal setVal(uint32 type, char *&cVal, int &iVal, double &/*dVal*/, _Tp val, int len = 0) 00892 { 00893 switch (type & paramTypeMask) 00894 { 00895 case (ito::ParamBase::HWRef & paramTypeMask): 00896 case (ito::ParamBase::DObjPtr & paramTypeMask): 00897 case ito::ParamBase::PointCloudPtr & paramTypeMask: 00898 case ito::ParamBase::PointPtr & paramTypeMask: 00899 case ito::ParamBase::PolygonMeshPtr & paramTypeMask: 00900 // case ito::ParamBase::Pointer & paramTypeMask: 00901 cVal = reinterpret_cast<char*>(val); 00902 return ito::retOk; 00903 00904 case ito::ParamBase::String & paramTypeMask: 00905 { 00906 char *cVal_ = cVal; 00907 if (val) 00908 { 00909 cVal = _strdup(const_cast<const char*>((char*)val)); 00910 iVal = static_cast<int>(strlen(cVal)); 00911 } 00912 else 00913 { 00914 cVal = 0; 00915 iVal = -1; 00916 } 00917 if (cVal_) 00918 { 00919 free(cVal_); 00920 } 00921 } 00922 return ito::retOk; 00923 00924 case ito::ParamBase::CharArray & ito::paramTypeMask: 00925 { 00926 char *cVal_ = cVal; 00927 if ((val) && (len > 0)) 00928 { 00929 cVal = (char*)malloc(len * sizeof(char)); 00930 memcpy(cVal, val, len * sizeof(char)); 00931 iVal = len; 00932 } 00933 else 00934 { 00935 cVal = NULL; 00936 iVal = -1; 00937 } 00938 if (cVal_) 00939 { 00940 free(cVal_); 00941 } 00942 } 00943 return ito::retOk; 00944 00945 case ito::ParamBase::IntArray & ito::paramTypeMask: 00946 { 00947 char *cVal_ = cVal; 00948 if ((val) && (len > 0)) 00949 { 00950 cVal = (char*)malloc(len * sizeof(int)); 00951 memcpy(cVal, val, len * sizeof(int)); 00952 iVal = len; 00953 } 00954 else 00955 { 00956 cVal = NULL; 00957 iVal = -1; 00958 } 00959 if (cVal_) 00960 { 00961 free(cVal_); 00962 } 00963 } 00964 return ito::retOk; 00965 00966 case ito::ParamBase::DoubleArray & ito::paramTypeMask: 00967 { 00968 char *cVal_ = cVal; 00969 if ((val) && (len > 0)) 00970 { 00971 cVal = (char*)malloc(len * sizeof(double)); 00972 memcpy(cVal, val, len * sizeof(double)); 00973 iVal = len; 00974 } 00975 else 00976 { 00977 cVal = NULL; 00978 iVal = -1; 00979 } 00980 if (cVal_) 00981 { 00982 free(cVal_); 00983 } 00984 } 00985 return ito::retOk; 00986 00987 default: 00988 return ito::retError; 00989 } 00990 } 00991 00992 static _Tp getVal(const uint32 type, const char *cVal, const int &iVal, const double &/*dVal*/, int &len) 00993 { 00994 switch (type & paramTypeMask) 00995 { 00996 case ito::ParamBase::String & paramTypeMask: 00997 if (cVal) 00998 { 00999 len = static_cast<int>(strlen(cVal)); 01000 //return reinterpret_cast<_Tp>(_strdup(cVal)); 01001 return reinterpret_cast<_Tp>(const_cast<char*>(cVal)); 01002 } 01003 else 01004 { 01005 len = 0; 01006 return 0; 01007 } 01008 01009 case ito::ParamBase::CharArray & ito::paramTypeMask: 01010 case ito::ParamBase::IntArray & ito::paramTypeMask: 01011 case ito::ParamBase::DoubleArray & ito::paramTypeMask: 01012 if (cVal) 01013 { 01014 len = iVal; 01015 return reinterpret_cast<_Tp>(const_cast<char*>(cVal)); 01016 } 01017 else 01018 { 01019 len = 0; 01020 return 0; 01021 } 01022 01023 case (ito::ParamBase::HWRef & paramTypeMask): 01024 case (ito::ParamBase::DObjPtr & paramTypeMask): 01025 case ito::ParamBase::PointCloudPtr & paramTypeMask: 01026 case ito::ParamBase::PointPtr & paramTypeMask: 01027 case ito::ParamBase::PolygonMeshPtr & paramTypeMask: 01028 return reinterpret_cast<_Tp>(const_cast<char*>(cVal)); 01029 01030 default: 01031 throw std::logic_error("Non-matching type!"); 01032 return (_Tp)0; 01033 } 01034 } 01035 }; 01036 01037 template<> 01038 struct ItomParamHelper<double> 01039 { 01040 static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, double val, int /*len = 0*/) 01041 { 01042 switch (type & paramTypeMask) 01043 { 01044 case ito::ParamBase::Double & ito::paramTypeMask: 01045 dVal = static_cast<double>(val); 01046 return ito::retOk; 01047 01048 case ito::ParamBase::Int & ito::paramTypeMask: 01049 case ito::ParamBase::Char & ito::paramTypeMask: 01050 iVal = static_cast<int>(val); 01051 return ito::retOk; 01052 01053 default: 01054 return ito::retError; 01055 } 01056 } 01057 01058 static double getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/) 01059 { 01060 switch (type & paramTypeMask) 01061 { 01062 case ito::ParamBase::Int & ito::paramTypeMask: 01063 case ito::ParamBase::Char & ito::paramTypeMask: 01064 return static_cast<double>(iVal); 01065 01066 case ito::ParamBase::Double & ito::paramTypeMask: 01067 return dVal; 01068 01069 default: 01070 throw std::logic_error("Non-matching type!"); 01071 return 0; 01072 } 01073 } 01074 }; 01075 01076 template<> 01077 struct ItomParamHelper<int> 01078 { 01079 static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, int val, int /*len = 0*/) 01080 { 01081 switch (type & paramTypeMask) 01082 { 01083 case ito::ParamBase::Double & ito::paramTypeMask: 01084 dVal = static_cast<int>(val); 01085 return ito::retOk; 01086 01087 case ito::ParamBase::Int & ito::paramTypeMask: 01088 case ito::ParamBase::Char & ito::paramTypeMask: 01089 iVal = val; 01090 return ito::retOk; 01091 01092 default: 01093 return ito::retError; 01094 } 01095 } 01096 01097 static int getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/) 01098 { 01099 switch (type & paramTypeMask) 01100 { 01101 case ito::ParamBase::Int & ito::paramTypeMask: 01102 case ito::ParamBase::Char & ito::paramTypeMask: 01103 return iVal; 01104 01105 case ito::ParamBase::Double & ito::paramTypeMask: 01106 return static_cast<int>(dVal); 01107 01108 case 0: 01109 throw std::invalid_argument("non existent parameter"); 01110 return 0; 01111 01112 default: 01113 throw std::logic_error("Non-matching type!"); 01114 return 0; 01115 } 01116 } 01117 }; 01118 01119 template<> 01120 struct ItomParamHelper<char> 01121 { 01122 static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, char val, int /*len = 0*/) 01123 { 01124 switch (type & paramTypeMask) 01125 { 01126 case ito::ParamBase::Double & ito::paramTypeMask: 01127 dVal = static_cast<char>(val); 01128 return ito::retOk; 01129 01130 case ito::ParamBase::Int & ito::paramTypeMask: 01131 case ito::ParamBase::Char & ito::paramTypeMask: 01132 iVal = static_cast<char>(val); 01133 return ito::retOk; 01134 01135 default: 01136 return ito::retError; 01137 } 01138 } 01139 01140 static char getVal(const uint32 type, const char * /*cVal*/, const int &iVal, const double &dVal, int & /*len*/) 01141 { 01142 switch (type & paramTypeMask) 01143 { 01144 case ito::ParamBase::Int & ito::paramTypeMask: 01145 case ito::ParamBase::Char & ito::paramTypeMask: 01146 return static_cast<char>(iVal); 01147 01148 case ito::ParamBase::Double & ito::paramTypeMask: 01149 return static_cast<char>(dVal); 01150 01151 case 0: 01152 throw std::invalid_argument("non existent parameter"); 01153 return 0; 01154 01155 default: 01156 throw std::logic_error("Non-matching type!"); 01157 return 0; 01158 } 01159 } 01160 }; 01161 01162 template<> 01163 struct ItomParamHelper<unsigned char> 01164 { 01165 static ito::RetVal setVal(uint32 type, char *&/*cVal*/, int &iVal, double &dVal, char val, int /*len = 0*/) 01166 { 01167 switch (type & paramTypeMask) 01168 { 01169 case ito::ParamBase::Double & ito::paramTypeMask: 01170 dVal = static_cast<unsigned char>(val); 01171 return ito::retOk; 01172 01173 case ito::ParamBase::Int & ito::paramTypeMask: 01174 case ito::ParamBase::Char & ito::paramTypeMask: 01175 iVal = static_cast<unsigned char>(val); 01176 return ito::retOk; 01177 01178 default: 01179 return ito::retError; 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 case ito::ParamBase::Char & ito::paramTypeMask: 01189 return static_cast<unsigned char>(iVal); 01190 01191 case ito::ParamBase::Double & ito::paramTypeMask: 01192 return static_cast<unsigned char>(dVal); 01193 01194 case 0: 01195 throw std::invalid_argument("non existent parameter"); 01196 return 0; 01197 01198 default: 01199 throw std::logic_error("Non-matching type!"); 01200 return 0; 01201 } 01202 } 01203 }; 01204 01205 } //end namespace ito 01206 01207 #endif