00001 // Torc - Copyright 2011 University of Southern California. All Rights Reserved. 00002 // $HeadURL: https://torc-isi.svn.sourceforge.net/svnroot/torc-isi/branches/staging/0.9/src/torc/bitstream/Frame.hpp $ 00003 // $Id: Frame.hpp 10 2011-10-12 18:40:16Z nsteiner $ 00004 00005 // This program is free software: you can redistribute it and/or modify it under the terms of the 00006 // GNU General Public License as published by the Free Software Foundation, either version 3 of the 00007 // License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 00010 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 00011 // the GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License along with this program. If 00014 // not, see <http://www.gnu.org/licenses/>. 00015 00016 /// \file 00017 /// \brief Header for the Frame class. 00018 00019 #ifndef TORC_BITSTREAM_FRAME_HPP 00020 #define TORC_BITSTREAM_FRAME_HPP 00021 00022 #include <boost/cstdint.hpp> 00023 #include <boost/shared_ptr.hpp> 00024 #include <boost/shared_array.hpp> 00025 #include <vector> 00026 00027 namespace torc { 00028 namespace bitstream { 00029 00030 /// \brief Bitstream frame. 00031 template <typename WORD_TYPE> class Frame { 00032 protected: 00033 // typedefs 00034 /// \brief Imported type name. 00035 typedef boost::uint32_t uint32_t; 00036 // members 00037 /// \brief Flag to indicate whether the frame is in use. 00038 bool mIsUsed; 00039 /// \brief Flag to indicate whether the frame has been modified. 00040 bool mIsDirty; 00041 /// \brief Frame length in words. 00042 uint32_t mLength; 00043 /// \brief Array of frame words. 00044 WORD_TYPE* mWords; 00045 public: 00046 // typedefs 00047 /// \brief Frame word type. 00048 typedef WORD_TYPE word_t; 00049 // constructors 00050 /// \brief Basic constructor. 00051 Frame(uint32_t inLength, word_t* inWords = 0) : mIsUsed(true), mIsDirty(false), 00052 mLength(inLength) { 00053 if(inWords && inLength) { 00054 mWords = new word_t[inLength]; 00055 word_t* p = mWords; 00056 for(uint32_t i = 0; i < inLength; i++) *p++ = *inWords++; 00057 } else { 00058 mWords = 0; 00059 } 00060 } 00061 ~Frame(void) { 00062 if(mWords) delete mWords; 00063 mWords = 0; 00064 } 00065 // functions 00066 /// \brief Touch the frame by marking it dirty. 00067 /// \details WARNING: This is a temporary convenience function that will not be retained. 00068 inline void touch(void) { mIsUsed = mIsDirty = true; } 00069 // accessors 00070 /// \brief Returns the length of the frame in words. 00071 uint32_t getLength(void) const { return mLength; } 00072 /// \brief Returns a const raw word pointer. 00073 const word_t* getWords(void) const { return mWords; } 00074 /// \brief Returns a non-const raw word pointer. 00075 //word_t* getWords(void) { return mWords; } 00076 /// \brief Returns true if the frame is in use in the bitstream, or false otherwise. 00077 bool isUsed(void) const { return mIsUsed; } 00078 /// \brief Returns true if the frame has been modified, or false otherwise. 00079 bool isDirty(void) const { return mIsDirty; } 00080 /// \brief Returns the specified frame word, or 0 if out of bounds. 00081 word_t operator[] (int index) { 00082 if(mWords == 0 || index < 0 || static_cast<uint32_t>(index) >= mLength) return 0; 00083 return mWords[index]; 00084 } 00085 /// \brief Sets the value of the specified frame word. 00086 void setWord(int index, int inWord) { 00087 // do nothing if the index is out of bounds 00088 if(index < 0 || static_cast<uint32_t>(index) >= mLength) return; 00089 // allocate and zero out the words if they were previously unallocated 00090 if(mWords == 0) { 00091 mWords = new word_t[mLength]; 00092 word_t* p = mWords; 00093 for(uint32_t i = 0; i < mLength; i++) *p++ = 0; 00094 } 00095 // set the new word value, and mark it used and dirty 00096 mWords + index = inWord; 00097 touch(); 00098 } 00099 /// \brief Sets the value of the specified frame words. 00100 void setWords(int inWord) { 00101 // allocate and zero out the words if they were previously unallocated 00102 if(mWords == 0) { 00103 mWords = new word_t[mLength]; 00104 word_t* p = mWords; 00105 for(uint32_t i = 0; i < mLength; i++) *p++ = 0; 00106 } 00107 // set the new word value, and mark it used and dirty 00108 mWords = inWord; 00109 touch(); 00110 } 00111 }; 00112 00113 typedef Frame<uint32_t> VirtexFrame; ///< \brief Virtex frame type. 00114 typedef Frame<uint32_t> SpartanFrame; ///< \brief Spartan frame type. 00115 typedef Frame<uint16_t> Spartan6Frame; ///< \brief Spartan6 frame type. 00116 00117 typedef boost::shared_ptr<VirtexFrame> VirtexFrameSharedPtr; ///< \brief Virtex frame type. 00118 typedef boost::shared_ptr<SpartanFrame> SpartanFrameSharedPtr; ///< \brief Spartan frame type. 00119 typedef boost::shared_ptr<Spartan6Frame> Spartan6FrameSharedPtr;///< \brief Spartan6 frame type. 00120 00121 } // namespace bitstream 00122 } // namespace torc 00123 00124 #endif // TORC_BITSTREAM_FRAME_HPP