AaronCameron.net
Not Left, nor right. Just correct.
Not a Member? - Login or Create an Account
Wednesday the 23rd of May 2012 @ 06:18pm
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cTSUniStreamImp.h

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2002  Aaron Cameron
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 #ifndef _n2l4_cUniStreamImp_H
00026 #define _n2l4_cUniStreamImp_H
00027 
00028 #include "n2l/n2l.h"
00029 #include "n2l/threads.h"
00030 
00031 #include "cStreamFilterInterface.h"
00032 #include "cInsertionStreamInterface.h"
00033 #include "cStreamConverterInterface.h"
00034 
00035 #include <vector>
00036 
00037 namespace n2l
00038 {
00039 
00040 
00044     template <  class TIValue,
00045                 class TOValue,
00046                 class TVContainer >
00047     class cUniStreamImp
00048     {
00049     public:
00050         typedef cUniStreamImp<TIValue,TOValue,TVContainer> tThis;
00051         
00052         typedef TIValue tInValue;
00053         typedef TOValue tOutValue;
00054 
00055         typedef cAutoPtr<cStreamConverterInterface<tInValue,tOutValue> > tConverter;
00056 
00057         typedef cAutoPtr<cInsertionStreamInterface<tInValue> > tPreConvJoiner;
00058         typedef cAutoPtr<cInsertionStreamInterface<tInValue> > tPostConvJoiner;
00059 
00060         typedef TVContainer tValueContainer;
00061         typedef typename tValueContainer::size_type tSize;
00062 
00063         typedef cAutoPtr<cStreamFilterInterface<tInValue> > tPreFilter;
00064         typedef cAutoPtr<cStreamFilterInterface<tOutValue> > tPostFilter;
00065         
00066         typedef std::vector<tPreFilter> tPreFilterContainer;
00067         typedef std::vector<tPostFilter> tPostFilterContainer;
00068 
00069         typedef typename tPreFilterContainer::iterator tPreFilterIterator;
00070         typedef typename tPostFilterContainer::iterator tPostFilterIterator;
00071 
00072         typedef std::vector<tPreConvJoiner> tPreConvJoinContainer;
00073         typedef std::vector<tPostConvJoiner> tPostConvJoinContainer;
00074 
00075         typedef typename tPreConvJoinContainer::iterator tPreConvJoinIterator;
00076         typedef typename tPostConvJoinContainer::iterator tPostConvJoinIterator;
00077 
00078         /* ************************************************************ */
00079         cUniStreamImp( const tConverter & i_converter ) :
00080             m_converter(i_converter),
00081             m_connected(true)
00082         {
00083         }
00084 
00085 
00086         /* ************************************************************ */
00087         void push( const tInValue & i_value )
00088         {
00089             const cMutexLocker MutexLock(&m_mutex);
00090             if (!isConnected()) return;
00091 
00092             const tPreFilterIterator EndPreFilterIt = m_preFilters.end();
00093             for (   tPreFilterIterator filterIt = m_preFilters.begin();
00094                     EndPreFilterIt!=filterIt; ++filterIt )
00095                 if (!(*filterIt)->allow(i_value)) return;
00096 
00097             const tPreConvJoinIterator EndPreStreamIt = m_preConvJoiners.end();
00098             for (   tPreConvJoinIterator streamIt = m_preConvJoiners.begin();
00099                     EndPreStreamIt!=streamIt; ++streamIt )
00100                 (*streamIt)->push(i_value);
00101 
00102             tOutValue temp;
00103             m_converter->convert(temp,i_value);
00104 
00105             const tPostFilterIterator EndPostFilterIt = m_postFilters.end();
00106             for (   tPostFilterIterator filterIt = m_postFilters.begin();
00107                     EndPostFilterIt!=filterIt; ++filterIt )
00108                 if (!(*filterIt)->allow(temp)) return;
00109 
00110             const tPostConvJoinIterator EndPostStreamIt = m_postConvJoiners.end();
00111             for (   tPostConvJoinIterator streamIt = m_postConvJoiners.begin();
00112                     EndPostStreamIt!=streamIt; ++streamIt )
00113                 (*streamIt)->push(temp);
00114 
00115             m_values.push(temp);            
00116         }
00117 
00118 
00119         /* ************************************************************ */
00120         const bool pop( tOutValue & o_value )
00121         {
00122             const cMutexLocker MutexLock(&m_mutex);
00123             if (m_values.empty()) return false;
00124             o_value = m_values.front();
00125             m_values.pop();
00126             return true;
00127         }
00128 
00129 
00130         /* ************************************************************ */
00131         const tSize size() const
00132         {
00133             const cMutexLocker MutexLock(&m_mutex);
00134             return m_values.size();
00135         }
00136 
00137 
00138         /* ************************************************************ */
00139         const bool empty() const
00140         {
00141             const cMutexLocker MutexLock(&m_mutex);
00142             return m_values.empty();
00143         }
00144 
00145 
00146         /* ************************************************************ */
00147         void attachInsertionFilter( tPreFilter io_filter )
00148         {
00149             const cMutexLocker MutexLock(&m_mutex);
00150             m_preFilters.push_back( io_filter );
00151         }
00152 
00153 
00154         /* ************************************************************ */
00155         void attachExtractionFilter( tPostFilter io_filter )
00156         {
00157             const cMutexLocker MutexLock(&m_mutex);
00158             m_postFilters.push_back( io_filter );
00159         }
00160 
00161 
00162         /* ************************************************************ */
00163         void attachPreConversionStream( const tPreConvJoiner io_stream )
00164         {
00165             const cMutexLocker MutexLock(&m_mutex);
00166             m_preConvJoiners.push_back(io_stream);
00167         }
00168 
00169         /* ************************************************************ */
00170         void attachPostConversionStream( const tPostConvJoiner io_stream )
00171         {
00172             const cMutexLocker MutexLock(&m_mutex);
00173             m_postConvJoiners.push_back(io_stream);
00174         }
00175 
00176         /* ************************************************************ */
00177         const bool isConnected() const 
00178         {
00179             const cMutexLocker MutexLock(&m_mutex);
00180             return m_connected;
00181         }
00182 
00183         /* ************************************************************ */
00184         void disconnect()
00185         {
00186             const cMutexLocker MutexLock(&m_mutex);
00187             m_connected = false;
00188         }
00189 
00190         
00191     private:
00192         tValueContainer m_values;
00193         tPreFilterContainer m_preFilters;
00194         tPostFilterContainer m_postFilters;
00195         tPreConvJoinContainer m_preConvJoiners;
00196         tPostConvJoinContainer m_postConvJoiners;
00197 
00198         tConverter m_converter;
00199     
00200         bool m_connected;
00201     
00202         cMutex m_mutex;
00203 
00204     }; // class
00205 
00206 } // namespace n2l
00207 
00208 #endif
©2012 Aaron Cameron