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

ShManipulator.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 #ifndef SHMANIPULATOR_HPP
00025 #define SHMANIPULATOR_HPP
00026 
00027 #include "ShProgram.hpp"
00028 #include <vector>
00029 #include <string>
00030 #include <sstream>
00031 
00032 namespace SH {
00033 
00044 template<typename T> 
00045 struct storage_trait {
00046   typedef T StorageType;
00047 };
00048 
00049 template<>
00050 struct storage_trait<const char*> {
00051   typedef std::string StorageType; 
00052 };
00053 
00054 enum OffsetRangeErrors {
00055   OFFSET_RANGE_BAD_OFFSET = -1,
00056   OFFSET_RANGE_BAD_INDEX = -2
00057 };
00058 
00059 template<typename T>
00060 class OffsetRange {
00061   public:
00062     OffsetRange(); 
00063     OffsetRange( T start, T end );
00064     OffsetRange( T start, int startOffset, T end, int endOffset );
00065 
00072     int absStartIndex( const ShProgramNode::VarList vars ) const; 
00073     int absEndIndex( const ShProgramNode::VarList vars ) const; 
00074 
00075     std::string toString() const; 
00076 
00077   private:
00078     T start, end;
00079     int startOffset, endOffset;
00080     int absIndex( T index, int offset, const ShProgramNode::VarList &vars ) const;
00081 };
00082 
00083 
00095 template<typename T>
00096 class ShManipulator {
00097   public:
00098     typedef typename storage_trait<T>::StorageType StorageType;
00099     typedef OffsetRange<StorageType> IndexRange;
00100     typedef std::vector<IndexRange> IndexRangeVector;
00101 
00104     ShManipulator();
00105     ~ShManipulator();
00106 
00107     ShManipulator<T>& operator()(T i);
00108     ShManipulator<T>& operator()(T start, T end);
00109     ShManipulator<T>& operator()(const IndexRange &range); 
00110 
00111     // converts ranges to a sequence of integer ranges using given var list 
00112     IndexRangeVector getRanges() const; 
00113 
00114     // converts to string for debugging/error messages
00115     std::string toString() const;
00116     
00117   protected:
00118     IndexRangeVector m_ranges; 
00119 
00120     // converts indices to positive integer indices. 
00121     // If it cannot be found, raises AlgebraException.
00122     // if the index has an offset that makes it invalid, then valid is set to false
00123     OffsetRange<int> convertRange(IndexRange range, const ShProgramNode::VarList &v) const;
00124 };
00125 
00134 template<typename T>
00135 ShProgram operator<<(const ShProgram &p, const ShManipulator<T> &m); 
00136 
00143 template<typename T>
00144 ShProgram operator<<(const ShManipulator<T> &m, const ShProgram &p);
00145 
00146 
00148 // shader outputs based on given indices
00149 //
00159 template<typename T>
00160 ShManipulator<T> shSwizzle(T i0);
00161 
00162 template<typename T>
00163 ShManipulator<T> shSwizzle(T i0, T i1);
00164 
00165 template<typename T>
00166 ShManipulator<T> shSwizzle(T i0, T i1, T i2);
00167 
00168 template<typename T>
00169 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3);
00170 
00171 template<typename T>
00172 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4);
00173 
00174 template<typename T>
00175 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5);
00176 
00177 template<typename T>
00178 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5, T i6);
00179 
00180 template<typename T>
00181 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5, T i6, T i7);
00182 
00183 template<typename T>
00184 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5, T i6, T i7, T i8);
00185 
00186 template<typename T>
00187 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5, T i6, T i7, T i8, T i9);
00188 
00189 template<typename T>
00190 ShManipulator<T> shSwizzle(std::vector<T> indices);
00191 
00193 // outputs based on given indices
00194 template<typename T>
00195 ShManipulator<T> shRange(T i);
00196 
00197 template<typename T>
00198 ShManipulator<T> shRange(T start, T end);
00199 
00210 template<typename T>
00211 ShManipulator<T> shExtract(T k); 
00212 
00224 template<typename T>
00225 ShManipulator<T> shInsert(T k); 
00226 
00235 template<typename T>
00236 ShManipulator<T> shDrop(T k);
00237 
00238 typedef ShManipulator<int> ShPositionManipulator;
00239 typedef ShManipulator<char*> ShNameManipulator;
00240 
00241 }
00242 
00243 #include "ShManipulatorImpl.hpp"
00244 
00245 #endif

Generated on Wed Jun 15 18:12:40 2005 for Sh by  doxygen 1.4.3-20050530