port.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright 2016 The Cartographer Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef CARTOGRAPHER_COMMON_PORT_H_
  17. #define CARTOGRAPHER_COMMON_PORT_H_
  18. #include <boost/iostreams/device/back_inserter.hpp>
  19. #include <boost/iostreams/filter/gzip.hpp>
  20. #include <boost/iostreams/filtering_stream.hpp>
  21. #include <cinttypes>
  22. #include <cmath>
  23. #include <string>
  24. using int8 = int8_t;
  25. using int16 = int16_t;
  26. using int32 = int32_t;
  27. using int64 = int64_t;
  28. using uint8 = uint8_t;
  29. using uint16 = uint16_t;
  30. using uint32 = uint32_t;
  31. using uint64 = uint64_t;
  32. namespace common {
  33. inline int RoundToInt(const float x) { return std::lround(x); }
  34. inline int RoundToInt(const double x) { return std::lround(x); }
  35. inline int64 RoundToInt64(const float x) { return std::lround(x); }
  36. inline int64 RoundToInt64(const double x) { return std::lround(x); }
  37. inline void FastGzipString(const std::string& uncompressed,
  38. std::string* compressed) {
  39. boost::iostreams::filtering_ostream out;
  40. out.push(
  41. boost::iostreams::gzip_compressor(boost::iostreams::zlib::best_speed));
  42. out.push(boost::iostreams::back_inserter(*compressed));
  43. boost::iostreams::write(out,
  44. reinterpret_cast<const char*>(uncompressed.data()),
  45. uncompressed.size());
  46. }
  47. inline void FastGunzipString(const std::string& compressed,
  48. std::string* decompressed) {
  49. boost::iostreams::filtering_ostream out;
  50. out.push(boost::iostreams::gzip_decompressor());
  51. out.push(boost::iostreams::back_inserter(*decompressed));
  52. boost::iostreams::write(out, reinterpret_cast<const char*>(compressed.data()),
  53. compressed.size());
  54. }
  55. } // namespace common
  56. #endif // CARTOGRAPHER_COMMON_PORT_H_