Flutter Engine
buffer_conversions.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #import "flutter/shell/platform/darwin/common/buffer_conversions.h"
6 
7 #include "flutter/fml/platform/darwin/scoped_nsobject.h"
8 
9 namespace flutter {
10 namespace {
11 class NSDataMapping : public fml::Mapping {
12  public:
13  NSDataMapping(NSData* data) : data_([data retain]) {}
14 
15  size_t GetSize() const override { return [data_.get() length]; }
16 
17  const uint8_t* GetMapping() const override {
18  return static_cast<const uint8_t*>([data_.get() bytes]);
19  }
20 
21  private:
23  FML_DISALLOW_COPY_AND_ASSIGN(NSDataMapping);
24 };
25 } // namespace
26 
28  const uint8_t* bytes = static_cast<const uint8_t*>(data.bytes);
29  return fml::MallocMapping::Copy(bytes, data.length);
30 }
31 
33  size_t size = buffer.GetSize();
34  return [NSData dataWithBytesNoCopy:buffer.Release() length:size];
35 }
36 
37 std::unique_ptr<fml::Mapping> ConvertNSDataToMappingPtr(NSData* data) {
38  return std::make_unique<NSDataMapping>(data);
39 }
40 
41 NSData* CopyMappingPtrToNSData(std::unique_ptr<fml::Mapping> mapping) {
42  return [NSData dataWithBytes:mapping->GetMapping() length:mapping->GetSize()];
43 }
44 
45 } // namespace flutter
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
uint8_t * Release()
Definition: mapping.cc:137
std::unique_ptr< fml::Mapping > ConvertNSDataToMappingPtr(NSData *data)
fml::MallocMapping CopyNSDataToMapping(NSData *data)
size_t GetSize() const override
Definition: mapping.cc:129
size_t length
static MallocMapping Copy(const T *begin, const T *end)
Definition: mapping.h:147
A Mapping like NonOwnedMapping, but uses Free as its release proc.
Definition: mapping.h:129
static const uint8_t buffer[]
NSData * CopyMappingPtrToNSData(std::unique_ptr< fml::Mapping > mapping)
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
NSData * ConvertMappingToNSData(fml::MallocMapping buffer)