1 #ifndef COIN_SBSTRINGCONVERT_H 2 #define COIN_SBSTRINGCONVERT_H 3 4 /**************************************************************************\ 5 * Copyright (c) Kongsberg Oil & Gas Technologies AS 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are 10 * met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * Neither the name of the copyright holder nor the names of its 20 * contributors may be used to endorse or promote products derived from 21 * this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 \**************************************************************************/ 35 36 #include <Inventor/SbBasic.h> 37 #include <Inventor/SbString.h> 38 #include "base/coinString.h" 39 40 class SbString; 41 class SbVec2s; 42 class SbVec2f; 43 class SbVec3f; 44 class SbVec3d; 45 class SbRotation; 46 47 template<size_t numArgs, typename Type, typename ArgumentType> 48 struct constructFromArray { 49 50 }; 51 52 template<typename Type, typename ArgumentType> 53 struct constructFromArray<2,Type,ArgumentType> { 54 static Type constructor(const ArgumentType * inArray) 55 { 56 return Type(inArray[0],inArray[1]); 57 } 58 }; 59 60 template<typename Type, typename ArgumentType> 61 struct constructFromArray<3,Type,ArgumentType> { 62 static Type constructor(const ArgumentType * inArray) 63 { 64 return Type(inArray[0],inArray[1],inArray[2]); 65 } 66 }; 67 68 template<typename Type, typename ArgumentType> 69 struct constructFromArray<4,Type,ArgumentType> { 70 static Type constructor(const ArgumentType * inArray) 71 { 72 return Type(inArray[0],inArray[1],inArray[2],inArray[3]); 73 } 74 }; 75 76 template <typename T> 77 struct ScXMLConvert { 78 static 79 T fromString(const SbString & str, SbBool * conversionOk = NULL) 80 { 81 typename SbTypeInfo<T>::PrimitiveType tmpVal[SbTypeInfo<T>::Dimensions]; 82 SbString substr; 83 int start = str.find("("); 84 int end = str.find(")"); 85 if (start == -1 || end == -1) { 86 if (conversionOk) *conversionOk = FALSE; 87 return T(); 88 } 89 substr = str.getSubString(0, start-1); 90 if (substr != SbTypeInfo<T>::getTypeName()) { 91 if (conversionOk) *conversionOk = FALSE; 92 return T(); 93 } 94 --end; 95 ++start; 96 assert(end>start); 97 substr = str.getSubString(start,end); 98 start = 0; 99 SbIntList indices; 100 substr.findAll(",",indices); 101 if (indices.getLength()!= SbTypeInfo<T>::Dimensions-1) { 102 if (conversionOk) *conversionOk = FALSE; 103 return T(); 104 } 105 for (int i=0;i<SbTypeInfo<T>::Dimensions-1;++i) { 106 end = indices[i]; 107 SbString token = substr.getSubString(start,end); 108 start=end+1; 109 typename SbTypeInfo<T>::PrimitiveType t = FromString< typename SbTypeInfo<T>::PrimitiveType > (token, conversionOk); 110 tmpVal[i]=t; 111 } 112 SbString token = substr.getSubString(start,-1); 113 typename SbTypeInfo<T>::PrimitiveType t = FromString< typename SbTypeInfo<T>::PrimitiveType > (token, conversionOk); 114 tmpVal[SbTypeInfo<T>::Dimensions-1]=t; 115 if (conversionOk) *conversionOk = TRUE; 116 return constructFromArray<SbTypeInfo<T>::Dimensions ,T, typename SbTypeInfo<T>::PrimitiveType >::constructor(tmpVal); 117 } 118 119 static SbString toString(const T & in) { 120 SbString retVal = SbTypeInfo<T>::getTypeName(); 121 retVal+="("; 122 retVal+=ToString<>(in[0]); 123 for (int i=1;i<SbTypeInfo<T>::Dimensions;++i) { 124 retVal+=", "; 125 retVal+=ToString<>(in[i]); 126 } 127 retVal+=")"; 128 return retVal; 129 } 130 }; 131 132 template<typename T> 133 struct PrimitiveConvert { 134 static T fromString(const SbString & str, SbBool * conversionOk = NULL) { 135 return FromString<T>(str, conversionOk); 136 } 137 static SbString toString(const T & in) { 138 return ToString<>(in); 139 } 140 }; 141 142 class SbStringConvert { 143 public: 144 enum TypeIdentity { 145 BOOLEAN, 146 NUMERIC, 147 SBVEC2S, 148 SBVEC2F, 149 SBVEC3F, 150 SBVEC3D, 151 SBROTATION, 152 153 UNKNOWN 154 }; 155 156 static TypeIdentity typeOf(const SbString & str); 157 158 template <typename T> 159 static 160 T fromString(const SbString & str, SbBool * conversionOk = NULL) 161 { 162 return 163 IF< SbTypeInfo<T>::isPrimitive, PrimitiveConvert<T>, ScXMLConvert<T> >::RET::fromString(str,conversionOk); 164 } 165 166 template <typename T> 167 static 168 SbString toString(const T & in) 169 { 170 SbString retVal = 171 IF< SbTypeInfo<T>::isPrimitive, PrimitiveConvert<T>, ScXMLConvert<T> >::RET::toString(in); 172 173 return retVal; 174 } 175 176 }; 177 178 #endif // !COIN_SBSTRINGCONVERT_H 179