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

signalsequence_impl.h

Go to the documentation of this file.
00001 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This library is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this library; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
00024  *   MA  02111-1307  USA                                                   *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 
00046 #ifndef SIGNALSEQUENCE_IMPL_H
00047 #define SIGNALSEQUENCE_IMPL_H
00048 
00049 
00050 #include <iostream>
00051 #include <sstream>
00052 #include <complex>
00053 #include <vector>
00054 #include <stdexcept>
00055 #include <typeinfo>
00056 
00057 #include <simthetic/signals.h>
00058 #include <simthetic/phbib.h>
00059 
00060 namespace simth
00061 {
00062 
00063 template<class Val, class Attr, class Cont, class Stor, class Accs>
00064 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::SignalSequenceBase(iterator iterStart,
00065     iterator iterEnd, storage_type &seq)
00066     : storage(&seq)
00067     , iter1(iterStart)
00068     , iter2(iterEnd)
00069     , size_( std::distance(iter1,iter2) ), capacity_(size_)
00070 {
00071   invariante();
00072 }
00073 
00074 template<class Val, class Attr, class Cont, class Stor, class Accs>
00075 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::SignalSequenceBase(storage_type &seq)
00076     : storage(&seq), iter1(seq.begin()), iter2(seq.end()),
00077     size_( std::distance(iter1,iter2) ), capacity_(size_)
00078 {
00079   invariante();
00080 }
00081 
00082 /* Constructor that can be used only by derived classes.
00083    A derived class must call seek(iter1,iter1,stor) after using
00084   this constructor!
00085 */
00086 template<class Val, class Attr, class Cont, class Stor, class Accs>
00087 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::SignalSequenceBase( )
00088     : storage(NULL)
00089 {
00090   //invariante(); is not fulfilled here!
00091   //call seek(iter1,iter1,stor)  after using this constructor!
00092 }
00093 
00094 template<class Val, class Attr, class Cont, class Stor, class Accs>
00095 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::~SignalSequenceBase()
00096 {
00097   // nothing to delete here.
00098 }
00099 
00100 
00101 template<class Val, class Attr, class Cont, class Stor, class Accs>
00102 inline void SignalSequenceBase<Val, Attr, Cont, Stor, Accs>::invariante() const
00103 {
00104 #if DEBUG==1
00105   if(std::distance(iter1,iter2) != int(size())) {
00106     throw std::logic_error("void SignalSequenceBase<T,Sto>::invariante: "
00107                            "distance(iter1,iter2)=="
00108                            +simth::i2string(std::distance(iter1,iter2))
00109                            +" != size()=="+simth::i2string(size()));
00110   }
00111   if(capacity() < size()) {
00112     throw std::runtime_error("void SignalSequenceBase<T,Sto>::invariante: "
00113                              "capacity < size");
00114   }
00115 #endif
00116 }
00117 
00118 template<class Val, class Attr, class Cont, class Stor, class Accs>
00119 SignalSequenceBase<Val, Attr, Cont, Stor, Accs>&
00120 SignalSequenceBase<Val, Attr, Cont, Stor, Accs>::operator=(const SignalSequenceBase<value_type,attributes_type,container_type,storage_type,access_type>& seq)
00121 {
00122   if(&seq == this) {
00123     return *this;
00124   }
00125 
00126   resize( seq.size() );
00127 
00128   iterator thisIter = begin();
00129   for(const_iterator fromIter = seq.begin(); fromIter!=seq.end(); ++fromIter) {
00130     (*thisIter) = (*fromIter);
00131     ++thisIter;
00132   }
00133 
00134   if(&(rAttributes()) != &(seq.rAttributes()) &&
00135       rAttributes() != seq.rAttributes()) {
00136     wAttributes() = seq.rAttributes();
00137   }
00138 
00139   invariante();
00140   return *this;
00141 }
00142 
00143 template<class Val, class Attr, class Cont, class Stor, class Accs>
00144 void SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::resize(size_t newSize)
00145 {
00146 
00147   if(newSize == size()) {
00148     return;
00149   }
00150 
00151   if(newSize > capacity()) {
00152     std::ostringstream ost;
00153     ost<<"void SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::resize(size_t newSize) : Resizing to a size larger ";
00154     ost<<"than the capacity is not possible for a shallow copy sequence. Old size: "<<size()<<", capacity: "<<capacity();
00155     ost<<" new size: "<<newSize;
00156     throw std::runtime_error(ost.str());
00157   }
00158 
00159   iter2 = iter1+ newSize;
00160   size_ = newSize;
00161 
00162   invariante();
00163 }
00164 
00165 template<class Val, class Attr, class Cont, class Stor, class Accs>
00166 void SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::seek(iterator newStart, iterator newEnd)
00167 {
00168   iter1 = newStart;
00169   iter2 = newEnd;
00170   size_ = std::distance(iter1,iter2);
00171   capacity_  = size_;
00172   invariante();
00173 }
00174 
00175 template<class Val, class Attr, class Cont, class Stor, class Accs>
00176 void SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::seek(iterator newStart, iterator newEnd,storage_type& storage_)
00177 {
00178   iter1 = newStart;
00179   iter2 = newEnd;
00180   storage = &storage_;
00181   size_ = std::distance(iter1,iter2);
00182   capacity_  = size_;
00183   invariante();
00184 }
00185 
00186 
00187 template<class Val, class Attr, class Cont, class Stor, class Accs>
00188 inline typename SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::iterator
00189 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::begin()
00190 {
00191   invariante();
00192   return iter1;
00193 }
00194 
00195 template<class Val, class Attr, class Cont, class Stor, class Accs>
00196 inline typename SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::const_iterator
00197 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::begin() const
00198 {
00199   invariante();
00200   return iter1;
00201 }
00202 
00203 template<class Val, class Attr, class Cont, class Stor, class Accs>
00204 inline typename SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::iterator
00205 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::end()
00206 {
00207   invariante();
00208   return iter2;
00209 }
00210 
00211 template<class Val, class Attr, class Cont, class Stor, class Accs>
00212 inline typename SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::const_iterator
00213 SignalSequenceBase<Val,Attr,Cont,Stor,Accs>::end() const
00214 {
00215   invariante();
00216   return iter2;
00217 }
00218 
00219 
00220 
00221 
00222 /* ****************************************************+
00223     SignalSequence  (derived from SignalSequenceBase)
00224  
00225  */
00226 
00227 
00228 template<class Val, class Attr, class Cont>
00229 SignalSequence<Val,Attr,Cont>::SignalSequence(iterator iterStart,iterator iterEnd, storage_type &seq)
00230     :   SignalSequenceBase<Val,Attr,Cont,storage_type,access_type>(iterStart,iterEnd,seq)
00231 { }
00232 
00233 template<class Val, class Attr, class Cont>
00234 SignalSequence<Val,Attr,Cont>::SignalSequence(storage_type &seq)
00235     : SignalSequenceBase<Val,Attr,Cont,storage_type,access_type>(seq)
00236 { }
00237 
00238 /* Constructor that can be used only by derived classes.
00239    A derived class must call seek(iter1,iter1,stor) after using
00240   this constructor!
00241 */
00242 template<class Val, class Attr, class Cont>
00243 SignalSequence<Val,Attr,Cont>::SignalSequence( )
00244     : SignalSequenceBase<Val,Attr,Cont,storage_type,access_type>( )
00245 { //invariante(); is not fulfilled here!
00246   //call seek(iter1,iter1,stor)  after using this constructor!
00247 }
00248 
00249 template<class Val, class Attr, class Cont>
00250 SignalSequence<Val,Attr,Cont>::~SignalSequence()
00251 {
00252   // nothing to delete here.
00253 }
00254 
00255 
00256 
00257 
00258 
00259 /* ****************************************************+
00260     Storage class
00261  
00262 */
00263 
00264 
00265 template<class T, class A, class Cont>
00266 SignalSequenceStorage<T,A,Cont>::SignalSequenceStorage()
00267     : base_type( ),ownStorage(0), attributes_(), lock_(false)
00268 {
00269   seek(ownStorage.begin(),ownStorage.end(),*this);
00270   invariante();
00271 }
00272 
00273 template<class T, class A, class Cont>
00274 SignalSequenceStorage<T,A,Cont>::SignalSequenceStorage(size_t size, const value_type &val)
00275     : base_type( ),ownStorage(size,val), attributes_(), lock_(false)
00276 {
00277   seek(ownStorage.begin(),ownStorage.end(),*this);
00278   invariante();
00279 }
00280 
00281 template<class T, class A, class Cont>
00282 SignalSequenceStorage<T,A,Cont>::SignalSequenceStorage(const SignalSequence<value_type,attributes_type,container_type> &seq)
00283     : base_type( ), ownStorage(seq.size()), attributes_( seq.rAttributes() ), lock_(false)
00284 {
00285   seek(ownStorage.begin(),ownStorage.end(),*this);
00286   invariante();
00287 
00288   if(DEBUG) {
00289     if(base_type::size() != seq.size()) {
00290       throw std::logic_error("SignalSequenceStorage<T,A,Cont>::SignalSequenceStorage(..) : error 1a");
00291     }
00292   }
00293   iterator to = ownStorage.begin();
00294   for(const_iterator from = seq.begin();from!=seq.end();++from) {
00295     *to = *from;
00296     ++to;
00297   }
00298   invariante();
00299 }
00300 
00301 template<class T, class A, class Cont>
00302 SignalSequenceStorage<T,A,Cont>::~SignalSequenceStorage()
00303 { }
00304 
00305 template<class T, class A, class Cont>
00306 inline void SignalSequenceStorage<T,A,Cont>::invariante() const
00307 {
00308 #if DEBUG==1
00309   if(base_type::begin() != ownStorage.begin() || 
00310      base_type::end() != ownStorage.end()) {
00311     throw std::logic_error("inline void SignalSequenceStorage<T,A,Cont>::invariante() const : error 1");
00312   }
00313 #endif
00314 }
00315 
00316 template<class T, class A, class Cont>
00317 A& SignalSequenceStorage<T,A,Cont>::wAttributes()
00318 {
00319   //#if SECURITY==1
00320   if(base_type::isAttributesLocked()) {
00321     //assert(0);
00322     throw std::runtime_error("SignalSequenceStorage::wAttributes(): "
00323                              "write access to attributes requested "
00324                              "while attributes are locked, in class "
00325                              + std::string(typeid(*this).name()));
00326   }
00327   //#endif
00328   return attributes_ ;
00329 }
00330 
00331 template<class T, class A, class Cont>
00332 void SignalSequenceStorage<T,A,Cont>::resize(size_t newSize)
00333 {
00334   // Note: if this resizes to something bigger than capacity(), the
00335   // memory allocation probably will change. All existing iterators
00336   // and other shallow copies will then point to garbage.
00337   ownStorage.resize(newSize);
00338   // Set the iterators to the new begin and end.
00339   seek(ownStorage.begin(),ownStorage.end());
00340   invariante();
00341 }
00342 
00343 
00344 }
00345 
00346 #endif // SIGNALSEQUENCE_IMPL_H

Generated on Mon Apr 24 21:19:19 2006 for simthetic by  doxygen 1.4.1