xref: /llvm-project/lldb/source/Host/macosx/cfcpp/CFCData.cpp (revision 80b476a902a8e2ab662b2e2a38e2afc55ce85c37)
1 //===-- CFCData.cpp -------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CFCData.h"
10 
11 // CFCData constructor
CFCData(CFDataRef data)12 CFCData::CFCData(CFDataRef data) : CFCReleaser<CFDataRef>(data) {}
13 
14 // CFCData copy constructor
15 CFCData::CFCData(const CFCData &rhs) = default;
16 
17 // CFCData copy constructor
operator =(const CFCData & rhs)18 CFCData &CFCData::operator=(const CFCData &rhs)
19 
20 {
21   if (this != &rhs)
22     *this = rhs;
23   return *this;
24 }
25 
26 // Destructor
27 CFCData::~CFCData() = default;
28 
GetLength() const29 CFIndex CFCData::GetLength() const {
30   CFDataRef data = get();
31   if (data)
32     return CFDataGetLength(data);
33   return 0;
34 }
35 
GetBytePtr() const36 const uint8_t *CFCData::GetBytePtr() const {
37   CFDataRef data = get();
38   if (data)
39     return CFDataGetBytePtr(data);
40   return NULL;
41 }
42 
Serialize(CFPropertyListRef plist,CFPropertyListFormat format)43 CFDataRef CFCData::Serialize(CFPropertyListRef plist,
44                              CFPropertyListFormat format) {
45   CFAllocatorRef alloc = kCFAllocatorDefault;
46   reset();
47   CFCReleaser<CFWriteStreamRef> stream(
48       ::CFWriteStreamCreateWithAllocatedBuffers(alloc, alloc));
49   ::CFWriteStreamOpen(stream.get());
50   CFIndex len = ::CFPropertyListWrite(plist, stream.get(), format, 0, nullptr);
51   if (len > 0)
52     reset((CFDataRef)::CFWriteStreamCopyProperty(stream.get(),
53                                                  kCFStreamPropertyDataWritten));
54   ::CFWriteStreamClose(stream.get());
55   return get();
56 }
57