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 #include <sstream>
8
9 namespace mojo {
10 namespace internal {
11
MakeMessageWithArrayIndex(const char * message,size_t size,size_t index)12 std::string MakeMessageWithArrayIndex(const char* message,
13 size_t size,
14 size_t index) {
15 std::ostringstream stream;
16 stream << message << ": array size - " << size << "; index - " << index;
17 return stream.str();
18 }
19
MakeMessageWithExpectedArraySize(const char * message,size_t size,size_t expected_size)20 std::string MakeMessageWithExpectedArraySize(const char* message,
21 size_t size,
22 size_t expected_size) {
23 std::ostringstream stream;
24 stream << message << ": array size - " << size << "; expected size - "
25 << expected_size;
26 return stream.str();
27 }
28
~BitRef()29 ArrayDataTraits<bool>::BitRef::~BitRef() {
30 }
31
BitRef(uint8_t * storage,uint8_t mask)32 ArrayDataTraits<bool>::BitRef::BitRef(uint8_t* storage, uint8_t mask)
33 : storage_(storage),
34 mask_(mask) {
35 }
36
37 ArrayDataTraits<bool>::BitRef&
operator =(bool value)38 ArrayDataTraits<bool>::BitRef::operator=(bool value) {
39 if (value) {
40 *storage_ |= mask_;
41 } else {
42 *storage_ &= ~mask_;
43 }
44 return *this;
45 }
46
47 ArrayDataTraits<bool>::BitRef&
operator =(const BitRef & value)48 ArrayDataTraits<bool>::BitRef::operator=(const BitRef& value) {
49 return (*this) = static_cast<bool>(value);
50 }
51
operator bool() const52 ArrayDataTraits<bool>::BitRef::operator bool() const {
53 return (*storage_ & mask_) != 0;
54 }
55
56 // static
EncodePointersAndHandles(const ArrayHeader * header,ElementType * elements,std::vector<Handle> * handles)57 void ArraySerializationHelper<Handle, true>::EncodePointersAndHandles(
58 const ArrayHeader* header,
59 ElementType* elements,
60 std::vector<Handle>* handles) {
61 for (uint32_t i = 0; i < header->num_elements; ++i)
62 EncodeHandle(&elements[i], handles);
63 }
64
65 // static
DecodePointersAndHandles(const ArrayHeader * header,ElementType * elements,std::vector<Handle> * handles)66 void ArraySerializationHelper<Handle, true>::DecodePointersAndHandles(
67 const ArrayHeader* header,
68 ElementType* elements,
69 std::vector<Handle>* handles) {
70 for (uint32_t i = 0; i < header->num_elements; ++i)
71 DecodeHandle(&elements[i], handles);
72 }
73
74 } // namespace internal
75 } // namespace mojo
76