pocolog_cpp
Write.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005-2006 LAAS/CNRS <openrobots@laas.fr>
3  * Sylvain Joyeux <sylvain.joyeux@m4x.org>
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29  * DAMAGE.
30  */
31 
32 
33 #ifndef POCOLOG_CPP_WRITE_H
34 #define POCOLOG_CPP_WRITE_H
35 
36 #include <pocolog_cpp/Format.hpp>
37 
38 #include <vector>
39 //#include <map>
40 #include <iosfwd>
41 #include <boost/thread/mutex.hpp>
42 #include <typelib/endian_swap.hh>
43 
44 namespace Typelib {
45  class Registry;
46 }
47 namespace pocolog_cpp
48 {
49  class Output
50  {
51  template<class T> friend Output& operator << (Output& output, const T& value);
52 
53  std::ostream& m_stream;
54  uint16_t m_stream_idx;
55 
56  private:
57  template<class T>
58  void write(const T& data)
59  {
60  T little_endian = Typelib::Endian::to_little(data);
61  m_stream.write( reinterpret_cast<const char*>(&little_endian), sizeof(T) );
62  }
63 
64 
65  public:
66  Output(std::ostream& stream);
67 
68  std::ostream& getStream();
69 
70  uint16_t newStreamIndex();
71 
72  void writeStreamDeclaration(uint16_t stream_index, StreamType type,
73  std::string const& name, std::string const& type_name,
74  std::string const& type_def,
75  std::vector<StreamMetadata> const& metadata);
76  void writeSampleHeader(uint16_t stream_index, base::Time const& realtime, base::Time const& logical, uint32_t payload_size);
77  void writeSample(uint16_t stream_index, base::Time const& realtime, base::Time const& logical, void* payload_data, uint32_t payload_size);
78  };
79 
80  namespace details
81  {
82  template <typename T> struct static_failure;
83  }
84 
86  void writePrologue(std::ostream& stream);
87 
88  template<class T>
89  Output& operator << (Output& output, const T& value)
90  {
91  details::static_failure<T> IF_YOU_HAVE_AN_ERROR_HERE_YOU_USED_THE_STREAM_OPERATOR_WITH_AN_INVALID_TYPE;
92  return output;
93  }
94 
95  template<>
96  inline Output& operator << (Output& output, const uint8_t& value)
97  { output.write(value); return output; }
98 
99  template<>
100  inline Output& operator << (Output& output, const uint16_t& value)
101  { output.write(value); return output; }
102 
103  template<>
104  inline Output& operator << (Output& output, const uint32_t& value)
105  { output.write(value); return output; }
106 
107  template<>
108  inline Output& operator << (Output& output, const std::string& value)
109  {
110  uint32_t length(value.length());
111  output.write(length);
112  output.m_stream.write(reinterpret_cast<const char*>(value.c_str()), length);
113  return output;
114  }
115 
116  template<>
117  inline Output& operator << (Output& output, const base::Time& time)
118  {
119  timeval tv = time.toTimeval();
120  output << (uint32_t)tv.tv_sec << (uint32_t)tv.tv_usec;
121  return output;
122  }
123 
124  template<>
125  inline Output& operator << (Output& output, const BlockHeader& header)
126  {
127  output
128  << header.type
129  << header.padding
130  << header.stream_idx
131  << header.data_size;
132  return output;
133  }
134 
135  template<>
136  inline Output& operator << (Output& output, const SampleHeader& header)
137  {
138  output
139  << header.realtime
140  << header.timestamp
141  << header.data_size
142  << header.compressed;
143  return output;
144  }
145 
146  template<>
147  inline Output& operator << (Output& output, const BlockType& type)
148  { return output << static_cast<uint8_t>(type); }
149 
150  template<>
151  inline Output& operator << (Output& output, const StreamType& type)
152  { return output << static_cast<uint8_t>(type); }
153 
154  template<>
155  inline Output& operator << (Output& output, const CommandType& type)
156  { return output << static_cast<uint8_t>(type); }
157 
158 
163  {
164  std::string const m_name;
165  std::string const m_type_name;
166  std::string const m_type_def;
167  std::vector<StreamMetadata> m_metadata;
168  // same types as in "struct BlockHeader"
169  uint16_t const m_stream_idx;
170  uint32_t const m_type_size;
171  base::Time m_sampling;
172  base::Time m_last;
173 
174  Output m_file;
175 
176  public:
186  StreamWriter(std::string const& name, std::string const& type_name, std::vector<StreamMetadata> const& metadata, Output& file);
187 
199  StreamWriter(std::string const& name, std::string const& type_name, Typelib::Registry const& type_def, std::vector<StreamMetadata> const& metadata, Output& file);
200 
202  void registerStream();
203 
207  void setSampling(base::Time const& period);
208 
209  bool writeSampleHeader(const base::Time& timestamp, uint32_t payload_size);
210 
211  std::ostream& getStream();
212  };
213 }
214 
215 #endif
216 
Definition: Write.hpp:49
Definition: Format.hpp:122
Output & operator<<(Output &output, const CommandType &type)
Definition: Write.hpp:155
uint32_t data_size
Definition: Format.hpp:145
uint8_t compressed
Definition: Format.hpp:146
StreamType
Definition: Format.hpp:157
uint8_t type
Definition: Format.hpp:106
void writePrologue(std::ostream &stream)
Definition: Write.cpp:44
uint16_t stream_idx
Definition: Format.hpp:126
CommandType
Definition: Format.hpp:163
Definition: Format.hpp:141
uint8_t type
Definition: Format.hpp:124
base::Time realtime
Definition: Format.hpp:143
Definition: InputDataStream.hpp:8
BlockType
Definition: Format.hpp:150
Definition: Write.hpp:82
Definition: FileStream.hpp:7
base::Time timestamp
Definition: Format.hpp:144
Stream * stream
Definition: MultiFileIndex.hpp:150
uint32_t data_size
Definition: Format.hpp:127
Definition: Write.hpp:162
uint8_t padding
Definition: Format.hpp:125