1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved. 2 // 3 // Use of this source code is governed by a BSD-style license 4 // that can be found in the LICENSE file in the root of the source 5 // tree. An additional intellectual property rights grant can be found 6 // in the file PATENTS. All contributing project authors may 7 // be found in the AUTHORS file in the root of the source tree. 8 #ifndef SRC_FLOAT_PARSER_H_ 9 #define SRC_FLOAT_PARSER_H_ 10 11 #include <cassert> 12 #include <cstdint> 13 14 #include "src/element_parser.h" 15 #include "webm/callback.h" 16 #include "webm/element.h" 17 #include "webm/reader.h" 18 #include "webm/status.h" 19 20 namespace webm { 21 22 // Parses an EBML float from a byte stream. 23 class FloatParser : public ElementParser { 24 public: 25 // Constructs a new parser which will use the given default_value as the 26 // value for the element if its size is zero. Defaults to the value zero (as 27 // the EBML spec indicates). 28 explicit FloatParser(double default_value = 0.0); 29 30 FloatParser(FloatParser&&) = default; 31 FloatParser& operator=(FloatParser&&) = default; 32 33 FloatParser(const FloatParser&) = delete; 34 FloatParser& operator=(const FloatParser&) = delete; 35 36 Status Init(const ElementMetadata& metadata, std::uint64_t max_size) override; 37 38 Status Feed(Callback* callback, Reader* reader, 39 std::uint64_t* num_bytes_read) override; 40 41 // Gets the parsed float. This must not be called until the parse had been 42 // successfully completed. value()43 double value() const { 44 assert(num_bytes_remaining_ == 0); 45 return value_; 46 } 47 48 // Gets the parsed float. This must not be called until the parse had been 49 // successfully completed. mutable_value()50 double* mutable_value() { 51 assert(num_bytes_remaining_ == 0); 52 return &value_; 53 } 54 55 private: 56 double value_; 57 double default_value_; 58 std::uint64_t uint64_value_; 59 int num_bytes_remaining_ = -1; 60 bool use_32_bits_; 61 }; 62 63 } // namespace webm 64 65 #endif // SRC_FLOAT_PARSER_H_ 66