Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

ShManipulator.cpp

00001 // Sh: A GPU metaprogramming language. 00002 // 00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory 00004 // Project administrator: Michael D. McCool 00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa, 00006 // Michael D. McCool 00007 // 00008 // This software is provided 'as-is', without any express or implied 00009 // warranty. In no event will the authors be held liable for any damages 00010 // arising from the use of this software. 00011 // 00012 // Permission is granted to anyone to use this software for any purpose, 00013 // including commercial applications, and to alter it and redistribute it 00014 // freely, subject to the following restrictions: 00015 // 00016 // 1. The origin of this software must not be misrepresented; you must 00017 // not claim that you wrote the original software. If you use this 00018 // software in a product, an acknowledgment in the product documentation 00019 // would be appreciated but is not required. 00020 // 00021 // 2. Altered source versions must be plainly marked as such, and must 00022 // not be misrepresented as being the original software. 00023 // 00024 // 3. This notice may not be removed or altered from any source 00025 // distribution. 00027 #include <cstdarg> 00028 #include <cassert> 00029 #include <sstream> 00030 #include "ShVariableNode.hpp" 00031 #include "ShError.hpp" 00032 #include "ShDebug.hpp" 00033 #include "ShAlgebra.hpp" 00034 #include "ShManipulator.hpp" 00035 00036 namespace SH { 00037 00038 /* TODO - clean up string 00039 * Currently these are hacked so that strings can encode some extra offset info other than just the name 00040 * When the string manipulators are applied, they are converted to int positional manipulators 00041 * (more code reuse) by finding which input/output channel has the given name. 00042 * "name" - use position that matches 00043 * "-name" - use position that matches - 1 (unless it's at beginning, in which case range is ignored) 00044 * "+name" - use position that matches + 1 (unless it's at end, in which case range is ignored) 00045 * "\0" - use beginnig 00046 * "\1" - use end 00047 */ 00048 /* int versions */ 00049 template<> 00050 SH_DLLEXPORT 00051 int OffsetRange<int>::absIndex( int index, int offset, const ShProgramNode::VarList &v ) const { 00052 if( offset == _FIRST ) return 0; 00053 else if( offset == _SECOND ) return 1; 00054 else if( offset == _LAST ) return v.size() - 1; 00055 if( index < 0 ) index += v.size(); 00056 if( index < 0 || index >= static_cast<int>(v.size()) ) return OFFSET_RANGE_BAD_INDEX; 00057 00058 index += offset; 00059 if( index < 0 || index >= static_cast<int>(v.size()) ) return OFFSET_RANGE_BAD_OFFSET; 00060 return index; 00061 } 00062 00063 /* string versions */ 00064 template<> 00065 SH_DLLEXPORT 00066 int OffsetRange<std::string>::absIndex( std::string index, int offset, 00067 const ShProgramNode::VarList &v ) const { 00068 if( offset == _FIRST ) return 0; 00069 else if( offset == _SECOND ) return 1; 00070 else if( offset == _LAST ) return v.size() - 1; 00071 00072 int result; 00073 result = OFFSET_RANGE_BAD_INDEX; 00074 00075 // match first occurrence of the index name 00076 ShProgramNode::VarList::const_iterator it = v.begin(); 00077 for( int i = 0; it != v.end(); ++it, ++i ) { 00078 if( (*it)->name() == index && result == OFFSET_RANGE_BAD_INDEX ) { 00079 result = i; 00080 break; 00081 } 00082 } 00083 00084 if( result == OFFSET_RANGE_BAD_INDEX ) { 00085 std::ostringstream os; 00086 os << "Could not find index " << index << (offset >= 0 ? "+" : "") 00087 << offset << " in variable channels:"; 00088 ShProgramNode::print(os, v) << std::endl; 00089 shError( ShAlgebraException( os.str())); 00090 } 00091 00092 result += offset; 00093 if( result < 0 || result >= static_cast<int>(v.size())) result = OFFSET_RANGE_BAD_OFFSET; 00094 return result; 00095 } 00096 00097 } 00098

Generated on Mon Oct 18 14:17:39 2004 for Sh by doxygen 1.3.7