diff options
Diffstat (limited to 'src/mbgl/util/blob.cpp')
-rw-r--r-- | src/mbgl/util/blob.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/mbgl/util/blob.cpp b/src/mbgl/util/blob.cpp new file mode 100644 index 0000000000..53d7644002 --- /dev/null +++ b/src/mbgl/util/blob.cpp @@ -0,0 +1,46 @@ +#include <mbgl/util/blob.hpp> +#include <mbgl/util/compression.hpp> + +namespace mbgl { + +Blob::Blob() = default; + +Blob::Blob(std::shared_ptr<const std::string> bytes_, bool compressed_) + : bytes(std::move(bytes_)), compressed(compressed_) { +} + +Blob::Blob(std::string&& bytes_, bool compressed_) + : bytes(std::make_shared<const std::string>(std::move(bytes_))), compressed(compressed_) { +} + +std::shared_ptr<const std::string> Blob::uncompressedData() const { + if (!bytes) { + throw std::runtime_error("invalid blob"); + } + if (compressed) { + return std::make_shared<const std::string>(util::decompress(*bytes)); + } else { + return bytes; + } +} + +std::shared_ptr<const std::string> Blob::compressedData() const { + if (!bytes) { + throw std::runtime_error("invalid blob"); + } + if (compressed) { + return bytes; + } else { + return std::make_shared<const std::string>(util::compress(*bytes)); + } +} + +void Blob::uncompress() { + if (compressed) { + bytes = uncompressedData(); + compressed = false; + } +} + + +} // namespace mbgl |