forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mojo bindings: Add string support to StructTraits
This CL enables string-typed fields in structs to be serialized and deserialized via StructTraits using base::StringPiece as an intermediate interface between the native type and the message buffer. BUG=577686 R=yzshen@chromium.org Review URL: https://codereview.chromium.org/1719183002 Cr-Commit-Position: refs/heads/master@{#376952}
- Loading branch information
Showing
14 changed files
with
279 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2016 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 "mojo/public/cpp/bindings/tests/struct_with_traits_impl.h" | ||
|
||
#include "mojo/public/interfaces/bindings/tests/struct_with_traits.mojom.h" | ||
|
||
namespace mojo { | ||
namespace test { | ||
|
||
StructWithTraitsImpl::StructWithTraitsImpl() {} | ||
|
||
StructWithTraitsImpl::~StructWithTraitsImpl() {} | ||
|
||
} // namespace test | ||
|
||
// static | ||
bool StructTraits<test::StructWithTraits, test::StructWithTraitsImpl>::Read( | ||
test::StructWithTraits_Reader r, | ||
test::StructWithTraitsImpl* out) { | ||
out->set_bool(r.f_bool()); | ||
out->set_uint32(r.f_uint32()); | ||
out->set_uint64(r.f_uint64()); | ||
out->set_string(r.f_string().as_string()); | ||
return true; | ||
} | ||
|
||
} // namespace mojo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2016 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. | ||
|
||
#ifndef MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_H_ | ||
#define MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_H_ | ||
|
||
#include <stdint.h> | ||
|
||
#include <string> | ||
|
||
#include "base/strings/string_piece.h" | ||
#include "mojo/public/cpp/bindings/struct_traits.h" | ||
|
||
namespace mojo { | ||
namespace test { | ||
|
||
// The mojom types are forward-declared to avoid circular dependencies between | ||
// this and generated headers. | ||
class StructWithTraits; | ||
class StructWithTraits_Reader; | ||
|
||
// A type which knows how to look like a mojo::test::StructWithTraits mojom type | ||
// by way of mojo::StructTraits. | ||
class StructWithTraitsImpl { | ||
public: | ||
StructWithTraitsImpl(); | ||
~StructWithTraitsImpl(); | ||
|
||
void set_bool(bool value) { bool_ = value; } | ||
bool get_bool() const { return bool_; } | ||
|
||
void set_uint32(uint32_t value) { uint32_ = value; } | ||
uint32_t get_uint32() const { return uint32_; } | ||
|
||
void set_uint64(uint64_t value) { uint64_ = value; } | ||
uint64_t get_uint64() const { return uint64_; } | ||
|
||
void set_string(std::string value) { string_ = value; } | ||
base::StringPiece get_string() const { return string_; } | ||
|
||
private: | ||
bool bool_ = false; | ||
uint32_t uint32_ = 0; | ||
uint64_t uint64_ = 0; | ||
std::string string_; | ||
}; | ||
|
||
} // namespace test | ||
|
||
template <> | ||
struct StructTraits<test::StructWithTraits, test::StructWithTraitsImpl> { | ||
// Deserialization to test::StructTraitsImpl. | ||
static bool Read(test::StructWithTraits_Reader r, | ||
test::StructWithTraitsImpl* out); | ||
|
||
// Fields in test::StructWithTraits. | ||
// See src/mojo/public/interfaces/bindings/tests/test_native_types.mojom. | ||
static bool f_bool(const test::StructWithTraitsImpl& value) { | ||
return value.get_bool(); | ||
} | ||
|
||
static uint32_t f_uint32(const test::StructWithTraitsImpl& value) { | ||
return value.get_uint32(); | ||
} | ||
|
||
static uint64_t f_uint64(const test::StructWithTraitsImpl& value) { | ||
return value.get_uint64(); | ||
} | ||
|
||
static base::StringPiece f_string(const test::StructWithTraitsImpl& value) { | ||
return value.get_string(); | ||
} | ||
}; | ||
|
||
} // namespace mojo | ||
|
||
#endif // MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
mojo/public/interfaces/bindings/tests/struct_with_traits.mojom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2016 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. | ||
|
||
module mojo.test; | ||
|
||
struct StructWithTraits { | ||
bool f_bool; | ||
uint32 f_uint32; | ||
uint64 f_uint64; | ||
string f_string; | ||
}; | ||
|
||
interface TraitsTestService { | ||
PassStructWithTraits(StructWithTraits s) => (StructWithTraits passed); | ||
}; |
14 changes: 14 additions & 0 deletions
14
mojo/public/interfaces/bindings/tests/struct_with_traits.typemap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2015 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. | ||
|
||
{ | ||
"c++": { | ||
"mojo.test.StructWithTraits": { | ||
"typename": "mojo::test::StructWithTraitsImpl", | ||
"headers": [ | ||
"mojo/public/cpp/bindings/tests/struct_with_traits_impl.h" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.