Tawara  0.1.0
memory_cluster.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, 2012, Geoffrey Biggs, geoffrey.biggs@aist.go.jp
5  * RT-Synthesis Research Group
6  * Intelligent Systems Research Institute,
7  * National Institute of Advanced Industrial Science and Technology (AIST),
8  * Japan
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * * Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  * * Redistributions in binary form must reproduce the above
18  * copyright notice, this list of conditions and the following
19  * disclaimer in the documentation and/or other materials provided
20  * with the distribution.
21  * * Neither the name of Geoffrey Biggs nor AIST, nor the names of its
22  * contributors may be used to endorse or promote products derived
23  * from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #if !defined(TAWARA_MEMORY_CLUSTER_H_)
40 #define TAWARA_MEMORY_CLUSTER_H_
41 
42 #include <boost/iterator/iterator_facade.hpp>
43 #include <boost/type_traits/is_convertible.hpp>
44 #include <boost/utility/enable_if.hpp>
45 #include <tawara/block_element.h>
46 #include <tawara/cluster.h>
47 #include <tawara/win_dll.h>
48 
51 
52 namespace tawara
53 {
62  {
63  protected:
65  typedef std::vector<BlockElement::Ptr> BlockStore;
66 
67  public:
69  typedef boost::shared_ptr<MemoryCluster> Ptr;
70 
76  MemoryCluster(uint64_t timecode=0);
77 
79  // Iterator types
81 
82  template <typename BlockType, typename IterType>
84  : public boost::iterator_facade<
85  IteratorBase<BlockType, IterType>,
86  BlockType, boost::bidirectional_traversal_tag>
87  {
88  private:
89  struct enabler {};
90 
91  public:
94  {
95  }
96 
101  IteratorBase(IterType iter)
102  : iter_(iter)
103  {
104  }
105 
111  template <typename OtherType, typename OtherIterType>
113  : iter_(other.iter_)
114  {
115  }
116 
117  protected:
118  // Necessary for Boost::iterator implementation.
119  friend class boost::iterator_core_access;
120 
121  // Integrate with owning container
122  friend class MemoryCluster;
123 
124  IterType iter_;
125 
127  void increment()
128  {
129  ++iter_;
130  }
131 
133  void decrement()
134  {
135  --iter_;
136  }
137 
142  template <typename OtherType, typename OtherIterType>
143  bool equal(
144  IteratorBase<OtherType, OtherIterType> const& other) const
145  {
146  return iter_ == other.iter_;
147  }
148 
152  BlockType& dereference() const
153  {
154  return *iter_;
155  }
156  }; // class IteratorBase
157 
170 
172  // Iterator access
174 
179  Iterator begin();
184  ConstIterator begin() const;
189  Iterator end();
194  ConstIterator end() const;
195 
196 
198  // Cluster interface
200 
202  virtual bool empty() const { return blocks_.empty(); }
204  virtual size_type count() const { return blocks_.size(); }
206  virtual void clear() { blocks_.clear(); }
207 
212  virtual void erase(Iterator position)
213  { blocks_.erase(position.iter_); }
219  virtual void erase(Iterator first, Iterator last)
220  { blocks_.erase(first.iter_, last.iter_); }
221 
227  virtual void push_back(value_type const& value)
228  { blocks_.push_back(value); }
229 
231  std::streamsize finalise(std::ostream& output);
232 
233  protected:
236 
238  std::streamsize blocks_size() const;
239 
241  std::streamsize read_blocks(std::istream& input,
242  std::streamsize size);
243  }; // class MemoryCluster
244 }; // namespace tawara
245 
247 // group elements
248 
249 #endif // TAWARA_MEMORY_CLUSTER_H_
250 
virtual bool empty() const
Check if there are no blocks.
bool equal(IteratorBase< OtherType, OtherIterType > const &other) const
Test for equality with another Iterator.
void decrement()
Decrement the Iterator to the previous block.
std::streamsize size(ID id)
Get the number of bytes required by an ID.
#define TAWARA_EXPORT
Definition: win_dll.h:51
BlockElement::Ptr value_type
The value type of this container.
Definition: cluster.h:156
void increment()
Increment the Iterator to the next block.
virtual void erase(Iterator position)
Erase the block at the specified iterator.
The in-memory Cluster implementation.
BlockType & dereference() const
Dereference the iterator to get the Block pointer.
IteratorBase(IterType iter)
Constructor.
IteratorBase(IteratorBase< OtherType, OtherIterType > const &other)
Templated base constructor.
The base Cluster, defining the common interface for Cluster element implementations.
Definition: cluster.h:150
virtual size_type count() const
Get the number of blocks.
virtual void clear()
Remove all blocks.
size_t size_type
The size type of this container.
Definition: cluster.h:158
virtual void erase(Iterator first, Iterator last)
Erase a range of blocks.
BlockStore blocks_
Block storage.
virtual void push_back(value_type const &value)
Add a block to this cluster.
boost::shared_ptr< MemoryCluster > Ptr
Pointer to a memory-based cluster.
std::vector< BlockElement::Ptr > BlockStore
Block storage type.
IteratorBase< BlockElement::Ptr, BlockStore::iterator > Iterator
Block iterator interface.
IteratorBase< Block::ConstPtr, BlockStore::const_iterator > ConstIterator
Block const iterator interface.