Tawara  0.1.0
attachments.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_ATTACHMENTS_H_)
40 #define TAWARA_ATTACHMENTS_H_
41 
42 #include <boost/operators.hpp>
43 #include <boost/shared_ptr.hpp>
44 #include <tawara/binary_element.h>
45 #include <tawara/master_element.h>
46 #include <tawara/string_element.h>
47 #include <tawara/uint_element.h>
48 #include <tawara/win_dll.h>
49 
52 
53 namespace tawara
54 {
61  {
62  public:
64  FileData(std::vector<char> data)
65  : BinaryElement(ids::FileData, data)
66  {
67  }
68 
70  typedef boost::shared_ptr<FileData> Ptr;
72  typedef boost::shared_ptr<FileData const> ConstPtr;
73  }; // class FileData
74 
81  public boost::equality_comparable<AttachedFile>
82  {
83  public:
85  AttachedFile();
86 
94  AttachedFile(std::string const& name,
95  std::string const& mime_type,
96  FileData::Ptr data,
97  uint64_t uid);
98 
104  std::string description() const { return desc_; }
106  void description(std::string const& desc) { desc_ = desc; }
107 
109  std::string name() const { return name_; }
111  void name(std::string const& name) { name_ = name; }
112 
119  std::string mime_type() const { return mime_; }
121  void mime_type(std::string const& mime_type) { mime_ = mime_type; }
122 
129  FileData::ConstPtr data() const { return data_; }
131  void data(FileData::Ptr& data);
132 
138  uint64_t uid() const { return uid_; }
140  void uid(uint64_t uid);
141 
143  friend bool operator==(AttachedFile const& lhs,
144  AttachedFile const& rhs);
145 
146  protected:
152 
154  // Element interface
156 
158  virtual std::streamsize body_size() const;
159 
161  virtual std::streamsize read_body(std::istream& input,
162  std::streamsize size);
163 
165  virtual std::streamsize write_body(std::ostream& output);
166 
168  void reset();
169  }; // class AttachedFile
170 
172  bool operator==(AttachedFile const& lhs, AttachedFile const& rhs);
173 
174 
186  public boost::equality_comparable<Attachments>
187  {
188  public:
190  typedef std::vector<AttachedFile>::value_type value_type;
192  typedef std::vector<AttachedFile>::size_type size_type;
194  typedef std::vector<AttachedFile>::reference reference;
196  typedef std::vector<AttachedFile>::const_reference const_reference;
198  typedef std::vector<AttachedFile>::iterator iterator;
200  typedef std::vector<AttachedFile>::const_iterator const_iterator;
202  typedef std::vector<AttachedFile>::reverse_iterator reverse_iterator;
204  typedef std::vector<AttachedFile>::const_reverse_iterator
206 
208  Attachments();
209 
216  virtual value_type& at(size_type pos)
217  { return files_.at(pos); }
224  virtual value_type const& at(size_type pos) const
225  { return files_.at(pos); }
226 
234  { return files_[pos]; }
241  virtual value_type const& operator[](size_type pos) const
242  { return files_[pos]; }
243 
245  virtual iterator begin() { return files_.begin(); }
247  virtual const_iterator begin() const { return files_.begin(); }
249  virtual iterator end() { return files_.end(); }
251  virtual const_iterator end() const { return files_.end(); }
253  virtual reverse_iterator rbegin() { return files_.rbegin(); }
256  { return files_.rbegin(); }
260  virtual reverse_iterator rend() { return files_.rend(); }
264  virtual const_reverse_iterator rend() const { return files_.rend(); }
265 
271  virtual bool empty() const { return files_.empty(); }
273  virtual size_type count() const { return files_.size(); }
275  virtual size_type max_count() const { return files_.max_size(); }
276 
278  virtual void clear() { files_.clear(); }
279 
284  virtual void erase(iterator position) { files_.erase(position); }
290  virtual void erase(iterator first, iterator last)
291  { files_.erase(first, last); }
292 
294  virtual void push_back(value_type const& value)
295  { files_.push_back(value); }
296 
298  virtual void resize(size_type count) { files_.resize(count); }
299 
305  virtual void swap(Attachments& other) { files_.swap(other.files_); }
306 
308  friend bool operator==(Attachments const& lhs,
309  Attachments const& rhs);
310 
311  protected:
312  std::vector<AttachedFile> files_;
313 
315  // Element interface
317 
319  virtual std::streamsize body_size() const;
320 
322  virtual std::streamsize write_body(std::ostream& output);
323 
325  virtual std::streamsize read_body(std::istream& input,
326  std::streamsize size);
327  }; // class Attachments
328 
330  bool operator==(Attachments const& lhs, Attachments const& rhs);
331 }; // namespace tawara
332 
334 // group elements
335 
336 #endif // TAWARA_ATTACHMENTS_H_
337 
virtual size_type count() const
Get the number of attachments.
Definition: attachments.h:273
FileData::ConstPtr data() const
Get the file data.
Definition: attachments.h:129
virtual bool empty() const
Check if there are no attachments.
Definition: attachments.h:271
virtual iterator end()
Get an iterator to the position past the last attachment.
Definition: attachments.h:249
std::vector< AttachedFile >::const_reverse_iterator const_reverse_iterator
The constant reversed random access iterator type.
Definition: attachments.h:205
The Attachments element stores opaque data attached to a segment.
Definition: attachments.h:185
virtual value_type & operator[](size_type pos)
Get a reference to an attachment.
Definition: attachments.h:233
virtual void erase(iterator position)
Erase the attachment at the specified iterator.
Definition: attachments.h:284
virtual const_iterator begin() const
Get an iterator to the first attachment.
Definition: attachments.h:247
virtual value_type const & operator[](size_type pos) const
Get a reference to an attachment.
Definition: attachments.h:241
std::streamsize size(ID id)
Get the number of bytes required by an ID.
#define TAWARA_EXPORT
Definition: win_dll.h:51
boost::shared_ptr< FileData > Ptr
Type of a pointer to a FileData instance.
Definition: attachments.h:70
StringElement mime_
Definition: attachments.h:149
std::vector< AttachedFile >::reverse_iterator reverse_iterator
The reversed random access iterator type.
Definition: attachments.h:202
virtual const_iterator end() const
Get an iterator to the position past the last attachment.
Definition: attachments.h:251
virtual const_reverse_iterator rend() const
Get a reverse iterator to the position before the first attachment.
Definition: attachments.h:264
The MasterElement interface.
virtual const_reverse_iterator rbegin() const
Get a reverse iterator to the last attachment.
Definition: attachments.h:255
Binary primitive element.
virtual reverse_iterator rend()
Get a reverse iterator to the position before the first attachment.
Definition: attachments.h:260
std::vector< AttachedFile > files_
Definition: attachments.h:312
std::string description() const
Get the attachment&#39;s description.
Definition: attachments.h:104
uint64_t uid() const
Get the attached file&#39;s UID.
Definition: attachments.h:138
bool operator==(AttachedFile const &lhs, AttachedFile const &rhs)
Equality operator for the AttachedFile object.
virtual void clear()
Remove all attachments.
Definition: attachments.h:278
std::vector< AttachedFile >::value_type value_type
The value type of this container.
Definition: attachments.h:190
StringElement desc_
Definition: attachments.h:147
FileData(std::vector< char > data)
Constructor.
Definition: attachments.h:64
Unsigned integer primitive element.
Definition: uint_element.h:57
std::vector< AttachedFile >::const_iterator const_iterator
The constant random access iterator type.
Definition: attachments.h:200
String primitive element.
virtual void erase(iterator first, iterator last)
Erase a range of attachments.
Definition: attachments.h:290
std::vector< AttachedFile >::const_reference const_reference
The constant reference type.
Definition: attachments.h:196
void name(std::string const &name)
Set the attachment&#39;s file name.
Definition: attachments.h:111
std::vector< AttachedFile >::iterator iterator
The random access iterator type.
Definition: attachments.h:198
The data of an attached file.
Definition: attachments.h:60
FileData::Ptr data_
Definition: attachments.h:150
virtual void swap(Attachments &other)
Swaps the contents of this Attachments element with another.
Definition: attachments.h:305
const ID Attachments(0x1941A469)
std::string name() const
Get the attachment&#39;s file name.
Definition: attachments.h:109
StringElement name_
Definition: attachments.h:148
virtual reverse_iterator rbegin()
Get a reverse iterator to the last attachment.
Definition: attachments.h:253
An attachment is a binary blob attached to a segment.
Definition: attachments.h:80
virtual void resize(size_type count)
Resizes the attachments storage.
Definition: attachments.h:298
virtual size_type max_count() const
Get the maximum number of attachments.
Definition: attachments.h:275
virtual void push_back(value_type const &value)
Add an attachment.
Definition: attachments.h:294
virtual value_type & at(size_type pos)
Get the attachment at the given position, with bounds checking.
Definition: attachments.h:216
void description(std::string const &desc)
Set the attachment&#39;s description.
Definition: attachments.h:106
std::string mime_type() const
Get the MIME type of the file.
Definition: attachments.h:119
const ID AttachedFile(0x61A7)
std::vector< AttachedFile >::reference reference
The reference type.
Definition: attachments.h:194
void mime_type(std::string const &mime_type)
Set the MIME type of the file.
Definition: attachments.h:121
std::vector< AttachedFile >::size_type size_type
The size type of this container.
Definition: attachments.h:192
virtual value_type const & at(size_type pos) const
Get the attachment at the given position, with bounds checking.
Definition: attachments.h:224
boost::shared_ptr< FileData const > ConstPtr
Type of a pointer to a const FileData instance.
Definition: attachments.h:72
virtual iterator begin()
Get an iterator to the first attachment.
Definition: attachments.h:245