summaryrefslogtreecommitdiffstats
path: root/ppapi/cpp/dev/string_wrapper_dev.cc
blob: 7f0976c02296cbe2ef8bf080057303b176be34db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/cpp/dev/string_wrapper_dev.h"

#include "ppapi/cpp/logging.h"
#include "ppapi/cpp/var.h"

namespace pp {
namespace internal {

OptionalStringWrapper::OptionalStringWrapper() {
}

OptionalStringWrapper::OptionalStringWrapper(const std::string& value) {
  *storage_ = Var(value).Detach();
}

OptionalStringWrapper::OptionalStringWrapper(PP_Var* storage, NotOwned)
    : storage_(storage, NOT_OWNED) {
  PP_DCHECK(storage_->type == PP_VARTYPE_STRING ||
            storage_->type == PP_VARTYPE_UNDEFINED);
}

OptionalStringWrapper::OptionalStringWrapper(
    const OptionalStringWrapper& other) {
  // Add one ref.
  *storage_ = Var(*other.storage_).Detach();
}

OptionalStringWrapper::~OptionalStringWrapper() {
  unset();
}

OptionalStringWrapper& OptionalStringWrapper::operator=(
    const OptionalStringWrapper& other) {
  return operator=(*other.storage_);
}

OptionalStringWrapper& OptionalStringWrapper::operator=(const PP_Var& other) {
  if (storage_.get() == &other)
    return *this;

  Var auto_release(PASS_REF, *storage_);
  // Add one ref.
  *storage_ = Var(other).Detach();
  return *this;
}

bool OptionalStringWrapper::is_set() const {
  PP_DCHECK(storage_->type == PP_VARTYPE_STRING ||
            storage_->type == PP_VARTYPE_UNDEFINED);
  return storage_->type == PP_VARTYPE_STRING;
}

void OptionalStringWrapper::unset() {
  Var auto_release(PASS_REF, *storage_);
  *storage_ = PP_MakeUndefined();
}

std::string OptionalStringWrapper::get() const {
  // TODO(yzshen): consider adding a cache.
  Var var(*storage_);
  if (var.is_string()) {
    return var.AsString();
  } else {
    PP_NOTREACHED();
    return std::string();
  }
}

void OptionalStringWrapper::set(const std::string& value) {
  Var auto_release(PASS_REF, *storage_);
  *storage_ = Var(value).Detach();
}

PP_Var* OptionalStringWrapper::StartRawUpdate() {
  unset();
  return storage_.get();
}

void OptionalStringWrapper::EndRawUpdate() {
  PP_DCHECK(storage_->type == PP_VARTYPE_STRING ||
            storage_->type == PP_VARTYPE_UNDEFINED);
}

StringWrapper::StringWrapper() : storage_(std::string()) {
}

StringWrapper::StringWrapper(const std::string& value) : storage_(value) {
}

StringWrapper::StringWrapper(PP_Var* storage, NotOwned)
    : storage_(storage, NOT_OWNED) {
  if (!storage_.is_set())
    storage_.set(std::string());
}

StringWrapper::StringWrapper(const StringWrapper& other)
    : storage_(other.storage_) {
}

StringWrapper::~StringWrapper() {
}

StringWrapper& StringWrapper::operator=(const StringWrapper& other) {
  storage_ = other.storage_;
  return *this;
}

StringWrapper& StringWrapper::operator=(const PP_Var& other) {
  PP_DCHECK(other.type == PP_VARTYPE_STRING);
  storage_ = other;
  return *this;
}

PP_Var* StringWrapper::StartRawUpdate() {
  return storage_.StartRawUpdate();
}

void StringWrapper::EndRawUpdate() {
  storage_.EndRawUpdate();
  if (!storage_.is_set())
    storage_.set(std::string());
}

}  // namespace internal
}  // namespace pp