Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / pdf / pdfium / pdfium_api_string_buffer_adapter.cc
blob9ac7425f55e4239942e87b60705e591b60f2b4fa
1 // Copyright 2015 The Chromium 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.
5 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h"
7 #include <string>
9 #include "base/logging.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_util.h"
13 namespace chrome_pdf {
15 template <class StringType>
16 PDFiumAPIStringBufferAdapter<StringType>::PDFiumAPIStringBufferAdapter(
17 StringType* str,
18 size_t expected_size,
19 bool check_expected_size)
20 : str_(str),
21 data_(base::WriteInto(str, expected_size + 1)),
22 expected_size_(expected_size),
23 check_expected_size_(check_expected_size),
24 is_closed_(false) {
27 template <class StringType>
28 PDFiumAPIStringBufferAdapter<StringType>::~PDFiumAPIStringBufferAdapter() {
29 DCHECK(is_closed_);
32 template <class StringType>
33 void* PDFiumAPIStringBufferAdapter<StringType>::GetData() {
34 DCHECK(!is_closed_);
35 return data_;
38 template <class StringType>
39 void PDFiumAPIStringBufferAdapter<StringType>::Close(size_t actual_size) {
40 DCHECK(!is_closed_);
41 is_closed_ = true;
43 if (check_expected_size_)
44 DCHECK_EQ(expected_size_, actual_size);
46 if (actual_size > 0) {
47 DCHECK((*str_)[actual_size - 1] == 0);
48 str_->resize(actual_size - 1);
49 } else {
50 str_->clear();
54 template <class StringType>
55 PDFiumAPIStringBufferSizeInBytesAdapter<StringType>::
56 PDFiumAPIStringBufferSizeInBytesAdapter(StringType* str,
57 size_t expected_size,
58 bool check_expected_size)
59 : adapter_(str,
60 expected_size / sizeof(typename StringType::value_type),
61 check_expected_size) {
62 DCHECK(expected_size % sizeof(typename StringType::value_type) == 0);
65 template <class StringType>
66 PDFiumAPIStringBufferSizeInBytesAdapter<
67 StringType>::~PDFiumAPIStringBufferSizeInBytesAdapter() = default;
69 template <class StringType>
70 void* PDFiumAPIStringBufferSizeInBytesAdapter<StringType>::GetData() {
71 return adapter_.GetData();
74 template <class StringType>
75 void PDFiumAPIStringBufferSizeInBytesAdapter<StringType>::Close(
76 size_t actual_size) {
77 DCHECK(actual_size % sizeof(typename StringType::value_type) == 0);
78 adapter_.Close(actual_size / sizeof(typename StringType::value_type));
81 // explicit instantiations
82 template class PDFiumAPIStringBufferAdapter<std::string>;
83 template class PDFiumAPIStringBufferAdapter<base::string16>;
84 template class PDFiumAPIStringBufferSizeInBytesAdapter<base::string16>;
86 } // namespace chrome_pdf