ShManipulator.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
00020 #ifndef SHMANIPULATOR_HPP
00021 #define SHMANIPULATOR_HPP
00022 
00023 #include "ShProgram.hpp"
00024 #include <vector>
00025 #include <string>
00026 #include <sstream>
00027 
00028 namespace SH {
00029 
00040 template<typename T> 
00041 struct storage_trait {
00042   typedef T StorageType;
00043 };
00044 
00045 template<>
00046 struct storage_trait<const char*> {
00047   typedef std::string StorageType; 
00048 };
00049 
00050 enum OffsetRangeErrors {
00051   OFFSET_RANGE_BAD_OFFSET = -1,
00052   OFFSET_RANGE_BAD_INDEX = -2
00053 };
00054 
00055 template<typename T>
00056 class OffsetRange {
00057   public:
00058     OffsetRange(); 
00059     OffsetRange( T start, T end );
00060     OffsetRange( T start, int startOffset, T end, int endOffset );
00061 
00068     int absStartIndex( const ShProgramNode::VarList vars ) const; 
00069     int absEndIndex( const ShProgramNode::VarList vars ) const; 
00070 
00071     std::string toString() const; 
00072 
00073   private:
00074     T start, end;
00075     int startOffset, endOffset;
00076     int absIndex( T index, int offset, const ShProgramNode::VarList &vars ) const;
00077 };
00078 
00079 
00091 template<typename T>
00092 class ShManipulator {
00093   public:
00094     typedef typename storage_trait<T>::StorageType StorageType;
00095     typedef OffsetRange<StorageType> IndexRange;
00096     typedef std::vector<IndexRange> IndexRangeVector;
00097 
00100     ShManipulator();
00101     ~ShManipulator();
00102 
00103     ShManipulator<T>& operator()(T i);
00104     ShManipulator<T>& operator()(T start, T end);
00105     ShManipulator<T>& operator()(const IndexRange &range); 
00106 
00107     // converts ranges to a sequence of integer ranges using given var list 
00108     IndexRangeVector getRanges() const; 
00109 
00110     // converts to string for debugging/error messages
00111     std::string toString() const;
00112     
00113   protected:
00114     IndexRangeVector m_ranges; 
00115 
00116     // converts indices to positive integer indices. 
00117     // If it cannot be found, raises AlgebraException.
00118     // if the index has an offset that makes it invalid, then valid is set to false
00119     OffsetRange<int> convertRange(IndexRange range, const ShProgramNode::VarList &v) const;
00120 };
00121 
00130 template<typename T>
00131 ShProgram operator<<(const ShProgram &p, const ShManipulator<T> &m); 
00132 
00139 template<typename T>
00140 ShProgram operator<<(const ShManipulator<T> &m, const ShProgram &p);
00141 
00142 
00144 // shader outputs based on given indices
00145 //
00155 template<typename T>
00156 ShManipulator<T> shSwizzle(T i0);
00157 
00158 template<typename T>
00159 ShManipulator<T> shSwizzle(T i0, T i1);
00160 
00161 template<typename T>
00162 ShManipulator<T> shSwizzle(T i0, T i1, T i2);
00163 
00164 template<typename T>
00165 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3);
00166 
00167 template<typename T>
00168 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4);
00169 
00170 template<typename T>
00171 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5);
00172 
00173 template<typename T>
00174 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5, T i6);
00175 
00176 template<typename T>
00177 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5, T i6, T i7);
00178 
00179 template<typename T>
00180 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5, T i6, T i7, T i8);
00181 
00182 template<typename T>
00183 ShManipulator<T> shSwizzle(T i0, T i1, T i2, T i3, T i4, T i5, T i6, T i7, T i8, T i9);
00184 
00185 template<typename T>
00186 ShManipulator<T> shSwizzle(std::vector<T> indices);
00187 
00189 // outputs based on given indices
00190 template<typename T>
00191 ShManipulator<T> shRange(T i);
00192 
00193 template<typename T>
00194 ShManipulator<T> shRange(T start, T end);
00195 
00206 template<typename T>
00207 ShManipulator<T> shExtract(T k); 
00208 
00220 template<typename T>
00221 ShManipulator<T> shInsert(T k); 
00222 
00231 template<typename T>
00232 ShManipulator<T> shDrop(T k);
00233 
00234 typedef ShManipulator<int> ShPositionManipulator;
00235 typedef ShManipulator<char*> ShNameManipulator;
00236 
00237 }
00238 
00239 #include "ShManipulatorImpl.hpp"
00240 
00241 #endif

Generated on Wed Nov 9 15:29:36 2005 for Sh by  doxygen 1.4.5