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

ShStream.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 "ShStream.hpp"
00028 #include "ShProgram.hpp"
00029 #include "ShChannelNode.hpp"
00030 #include "ShSyntax.hpp"
00031 #include "ShAlgebra.hpp"
00032 #include "ShContext.hpp"
00033 
00034 namespace SH {
00035 
00036 // May want to move this elsewhere
00037 ShProgram connect(const ShChannelNodePtr& node, const ShProgram& program)
00038 {
00039   ShProgram nibble = SH_BEGIN_PROGRAM() {
00040     ShVariable out(node->clone(SH_OUTPUT));
00041 
00042     ShVariable streamVar(node);
00043     ShStatement stmt(out, SH_OP_FETCH, streamVar);
00044     ShContext::current()->parsing()->tokenizer.blockList()->addStatement(stmt);
00045   } SH_END_PROGRAM;
00046   return connect(nibble, program);
00047 }
00048 
00049 ShStream::ShStream(const ShChannelNodePtr& channel)
00050 {
00051   append(channel);
00052 }
00053 
00054 ShStream::NodeList::const_iterator ShStream::begin() const
00055 {
00056   return m_nodes.begin();
00057 }
00058 
00059 ShStream::NodeList::const_iterator ShStream::end() const
00060 {
00061   return m_nodes.end();
00062 }
00063 
00064 ShStream::NodeList::iterator ShStream::begin() 
00065 {
00066   return m_nodes.begin();
00067 }
00068 
00069 ShStream::NodeList::iterator ShStream::end() 
00070 {
00071   return m_nodes.end();
00072 }
00073 
00074 int ShStream::size() const
00075 {
00076   return m_nodes.size();
00077 }
00078 
00079 void ShStream::append(const ShChannelNodePtr& node)
00080 {
00081   m_nodes.push_back(node);
00082 }
00083 
00084 void ShStream::prepend(const ShChannelNodePtr& node)
00085 {
00086   m_nodes.push_front(node);
00087 }
00088 
00089 ShStream combine(const ShStream& left, const ShStream& right)
00090 {
00091   ShStream stream = left;
00092   for (ShStream::NodeList::const_iterator I = right.begin(); I != right.end();
00093        ++I) {
00094     stream.append(*I);
00095   }
00096   return stream;
00097 }
00098 
00099 ShStream operator&(const ShStream& left, const ShStream& right)
00100 {
00101   return combine(left, right);
00102 }
00103 
00104 // Connecting (currying) streams onto programs
00105 ShProgram connect(const ShStream& stream, const ShProgram& program)
00106 {
00107   ShProgram p;
00108   p = program;
00109   for (ShStream::NodeList::const_iterator I = stream.begin();
00110        I != stream.end(); ++I) {
00111     p = connect(*I, p);
00112   }
00113   return p;
00114 }
00115 
00116 ShProgram operator<<(const ShProgram& program, const ShStream& stream)
00117 {
00118   return connect(stream, program);
00119 }
00120 
00121 ShStream& ShStream::operator=(const ShProgram& program)
00122 {
00123   ShEnvironment::backend->execute(program.node(), *this);
00124   return *this;
00125 }
00126 
00127 }

Generated on Mon Jan 24 18:36:35 2005 for Sh by  doxygen 1.4.1