torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Frame.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the Frame class.
18 
19 #ifndef TORC_BITSTREAM_FRAME_HPP
20 #define TORC_BITSTREAM_FRAME_HPP
21 
22 #include <boost/cstdint.hpp>
23 #include <boost/shared_ptr.hpp>
24 #include <boost/shared_array.hpp>
25 #include <vector>
26 
27 namespace torc {
28 namespace bitstream {
29 
30  /// \brief Bitstream frame.
31  template <typename WORD_TYPE> class Frame {
32  protected:
33  // typedefs
34  /// \brief Imported type name.
35  typedef boost::uint32_t uint32_t;
36  // members
37  /// \brief Flag to indicate whether the frame is in use.
38  bool mIsUsed;
39  /// \brief Flag to indicate whether the frame has been modified.
40  bool mIsDirty;
41  /// \brief Frame length in words.
43  /// \brief Array of frame words.
44  WORD_TYPE* mWords;
45  public:
46  // typedefs
47  /// \brief Frame word type.
48  typedef WORD_TYPE word_t;
49  // constructors
50  /// \brief Basic constructor.
51  Frame(uint32_t inLength, word_t* inWords = 0) : mIsUsed(true), mIsDirty(false),
52  mLength(inLength) {
53  if(inWords && inLength) {
54  mWords = new word_t[inLength];
55  word_t* p = mWords;
56  for(uint32_t i = 0; i < inLength; i++) *p++ = *inWords++;
57  } else {
58  mWords = 0;
59  }
60  }
61  ~Frame(void) {
62  if(mWords) delete mWords;
63  mWords = 0;
64  }
65  // accessors
66  /// \brief Returns the length of the frame in words.
67  uint32_t getLength(void) const { return mLength; }
68  /// \brief Returns a const raw word pointer.
69  const word_t* getWords(void) const { return mWords; }
70  /// \brief Returns a non-const raw word pointer.
71  //word_t* getWords(void) { return mWords; }
72  /// \brief Returns true if the frame is in use in the bitstream, or false otherwise.
73  bool isUsed(void) const { return mIsUsed; }
74  /// \brief Returns true if the frame has been modified, or false otherwise.
75  bool isDirty(void) const { return mIsDirty; }
76  /// \brief Marks the frame used or unused, and if unused automatically marks it clean.
77  void setUsed(bool inIsUsed = true) { mIsUsed = inIsUsed; if(!mIsUsed) mIsDirty = false; }
78  /// \brief Marks the frame dirty or clean, and if dirty automatically marks it used.
79  void setDirty(bool inIsDirty = true) { mIsDirty = inIsDirty; if(mIsDirty) mIsUsed = true; }
80  /// \brief Returns the specified frame word, or 0 if out of bounds.
81  word_t operator[] (int index) {
82  if(mWords == 0 || index < 0 || static_cast<uint32_t>(index) >= mLength) return 0;
83  return mWords[index];
84  }
85  /// \brief Sets the value of the specified frame word.
86  void setWord(int index, word_t inWord) {
87  // do nothing if the index is out of bounds
88  if(index < 0 || static_cast<uint32_t>(index) >= mLength) return;
89  // allocate and zero out the words if they were previously unallocated
90  if(mWords == 0) {
91  mWords = new word_t[mLength];
92  word_t* p = mWords;
93  for(uint32_t i = 0; i < mLength; i++) *p++ = 0;
94  setDirty();
95  }
96  // if the new value is different, update the word and mark the frame dirty
97  if(mWords[index] != inWord) {
98  mWords[index] = inWord;
99  setDirty();
100  }
101  }
102  };
103 
104  typedef Frame<uint32_t> VirtexFrame; ///< \brief Virtex frame type.
105  typedef Frame<uint32_t> SpartanFrame; ///< \brief Spartan frame type.
106  typedef Frame<uint16_t> Spartan6Frame; ///< \brief Spartan6 frame type.
107 
108  typedef boost::shared_ptr<VirtexFrame> VirtexFrameSharedPtr; ///< \brief Virtex frame type.
109  typedef boost::shared_ptr<SpartanFrame> SpartanFrameSharedPtr; ///< \brief Spartan frame type.
110  typedef boost::shared_ptr<Spartan6Frame> Spartan6FrameSharedPtr;///< \brief Spartan6 frame type.
111 
112 } // namespace bitstream
113 } // namespace torc
114 
115 #endif // TORC_BITSTREAM_FRAME_HPP
boost::shared_ptr< SpartanFrame > SpartanFrameSharedPtr
Spartan frame type.
Definition: Frame.hpp:109
bool mIsUsed
Flag to indicate whether the frame is in use.
Definition: Frame.hpp:38
bool isUsed(void) const
Returns a non-const raw word pointer.
Definition: Frame.hpp:73
WORD_TYPE * mWords
Array of frame words.
Definition: Frame.hpp:44
void setUsed(bool inIsUsed=true)
Marks the frame used or unused, and if unused automatically marks it clean.
Definition: Frame.hpp:77
word_t operator[](int index)
Returns the specified frame word, or 0 if out of bounds.
Definition: Frame.hpp:81
const word_t * getWords(void) const
Returns a const raw word pointer.
Definition: Frame.hpp:69
Frame< uint32_t > VirtexFrame
Virtex frame type.
Definition: Frame.hpp:104
void setDirty(bool inIsDirty=true)
Marks the frame dirty or clean, and if dirty automatically marks it used.
Definition: Frame.hpp:79
uint32_t mLength
Frame length in words.
Definition: Frame.hpp:42
Frame< uint16_t > Spartan6Frame
Spartan6 frame type.
Definition: Frame.hpp:106
boost::shared_ptr< Spartan6Frame > Spartan6FrameSharedPtr
Spartan6 frame type.
Definition: Frame.hpp:110
void setWord(int index, word_t inWord)
Sets the value of the specified frame word.
Definition: Frame.hpp:86
uint32_t getLength(void) const
Returns the length of the frame in words.
Definition: Frame.hpp:67
boost::shared_ptr< VirtexFrame > VirtexFrameSharedPtr
Virtex frame type.
Definition: Frame.hpp:108
bool mIsDirty
Flag to indicate whether the frame has been modified.
Definition: Frame.hpp:40
bool isDirty(void) const
Returns true if the frame has been modified, or false otherwise.
Definition: Frame.hpp:75
Frame(uint32_t inLength, word_t *inWords=0)
Basic constructor.
Definition: Frame.hpp:51
Bitstream frame.
Definition: Frame.hpp:31
boost::uint32_t uint32_t
Imported type name.
Definition: Frame.hpp:35
WORD_TYPE word_t
Frame word type.
Definition: Frame.hpp:48
Frame< uint32_t > SpartanFrame
Spartan frame type.
Definition: Frame.hpp:105