1 // Copyright 2013 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.
4
5 #include "mojo/public/cpp/bindings/lib/array_internal.h"
6
7 namespace mojo {
8 namespace internal {
9
~BitRef()10 ArrayDataTraits<bool>::BitRef::~BitRef() {
11 }
12
BitRef(uint8_t * storage,uint8_t mask)13 ArrayDataTraits<bool>::BitRef::BitRef(uint8_t* storage, uint8_t mask)
14 : storage_(storage),
15 mask_(mask) {
16 }
17
18 ArrayDataTraits<bool>::BitRef&
operator =(bool value)19 ArrayDataTraits<bool>::BitRef::operator=(bool value) {
20 if (value) {
21 *storage_ |= mask_;
22 } else {
23 *storage_ &= ~mask_;
24 }
25 return *this;
26 }
27
28 ArrayDataTraits<bool>::BitRef&
operator =(const BitRef & value)29 ArrayDataTraits<bool>::BitRef::operator=(const BitRef& value) {
30 return (*this) = static_cast<bool>(value);
31 }
32
operator bool() const33 ArrayDataTraits<bool>::BitRef::operator bool() const {
34 return (*storage_ & mask_) != 0;
35 }
36
37 // static
EncodePointersAndHandles(const ArrayHeader * header,ElementType * elements,std::vector<Handle> * handles)38 void ArraySerializationHelper<Handle, true>::EncodePointersAndHandles(
39 const ArrayHeader* header,
40 ElementType* elements,
41 std::vector<Handle>* handles) {
42 for (uint32_t i = 0; i < header->num_elements; ++i)
43 EncodeHandle(&elements[i], handles);
44 }
45
46 // static
DecodePointersAndHandles(const ArrayHeader * header,ElementType * elements,std::vector<Handle> * handles)47 void ArraySerializationHelper<Handle, true>::DecodePointersAndHandles(
48 const ArrayHeader* header,
49 ElementType* elements,
50 std::vector<Handle>* handles) {
51 for (uint32_t i = 0; i < header->num_elements; ++i)
52 DecodeHandle(&elements[i], handles);
53 }
54
55 // static
ValidateElements(const ArrayHeader * header,const ElementType * elements,BoundsChecker * bounds_checker)56 bool ArraySerializationHelper<Handle, true>::ValidateElements(
57 const ArrayHeader* header,
58 const ElementType* elements,
59 BoundsChecker* bounds_checker) {
60 for (uint32_t i = 0; i < header->num_elements; ++i) {
61 if (!bounds_checker->ClaimHandle(elements[i])) {
62 ReportValidationError(VALIDATION_ERROR_ILLEGAL_HANDLE);
63 return false;
64 }
65 }
66 return true;
67 }
68
69 } // namespace internal
70 } // namespace mojo
71