NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
ff_serialization.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef FF_UTIL_FF_SERIALIZATION_H_
20 #define FF_UTIL_FF_SERIALIZATION_H_
21 
22 // ROS includes
23 #include <ros/ros.h>
24 
25 // STL includes
26 #include <fstream>
27 #include <string>
28 
29 namespace ff_util {
30 
32  public:
33  // Write a ROS message to a file
34  template < class RosMessage >
35  static bool WriteFile(std::string const& file_name, RosMessage const& msg) {
36  std::ofstream ofs(file_name, std::ios::out | std::ios::binary);
37  if (!ofs.is_open())
38  return false;
39  uint32_t serial_size = ros::serialization::serializationLength(msg);
40  boost::shared_array < uint8_t > obuffer(new uint8_t[serial_size]);
41  ros::serialization::OStream ostream(obuffer.get(), serial_size);
42  ros::serialization::serialize(ostream, msg);
43  ofs.write(reinterpret_cast < char* > (obuffer.get()), serial_size);
44  ofs.close();
45  return true;
46  }
47  // Read a ROS message from a file
48  template < class RosMessage >
49  static bool ReadFile(std::string const& file_name, RosMessage & msg) {
50  std::ifstream ifs(file_name, std::ios::in | std::ios::binary);
51  if (!ifs.is_open())
52  return false;
53  ifs.seekg(0, std::ios::end);
54  std::streampos end = ifs.tellg();
55  ifs.seekg(0, std::ios::beg);
56  std::streampos begin = ifs.tellg();
57  uint32_t file_size = end - begin;
58  boost::shared_array<uint8_t> ibuffer(new uint8_t[file_size]);
59  ifs.read(reinterpret_cast<char*> (ibuffer.get()), file_size);
60  ros::serialization::IStream istream(ibuffer.get(), file_size);
61  ros::serialization::deserialize(istream, msg);
62  ifs.close();
63  return true;
64  }
65 };
66 
67 } // namespace ff_util
68 
69 #endif // FF_UTIL_FF_SERIALIZATION_H_
ff_util::Serialization::WriteFile
static bool WriteFile(std::string const &file_name, RosMessage const &msg)
Definition: ff_serialization.h:35
ff_util::Serialization::ReadFile
static bool ReadFile(std::string const &file_name, RosMessage &msg)
Definition: ff_serialization.h:49
ff_util::Serialization
Definition: ff_serialization.h:31
ff_util
Definition: config_client.h:31