OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GOOGLE_APIS_GCM_BASE_MCS_MESSAGE_H_ | 5 #ifndef GOOGLE_APIS_GCM_BASE_MCS_MESSAGE_H_ |
6 #define GOOGLE_APIS_GCM_BASE_MCS_MESSAGE_H_ | 6 #define GOOGLE_APIS_GCM_BASE_MCS_MESSAGE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
| 10 #include <memory> |
10 #include <string> | 11 #include <string> |
11 | 12 |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "google_apis/gcm/base/gcm_export.h" | 15 #include "google_apis/gcm/base/gcm_export.h" |
16 | 16 |
17 namespace google { | 17 namespace google { |
18 namespace protobuf { | 18 namespace protobuf { |
19 class MessageLite; | 19 class MessageLite; |
20 } // namespace protobuf | 20 } // namespace protobuf |
21 } // namespace google | 21 } // namespace google |
22 | 22 |
23 namespace gcm { | 23 namespace gcm { |
24 | 24 |
25 // A wrapper for MCS protobuffers that encapsulates their tag, size and data | 25 // A wrapper for MCS protobuffers that encapsulates their tag, size and data |
26 // in an immutable and thread-safe format. If a mutable version is desired, | 26 // in an immutable and thread-safe format. If a mutable version is desired, |
27 // CloneProtobuf() should use used to create a new copy of the protobuf. | 27 // CloneProtobuf() should use used to create a new copy of the protobuf. |
28 // | 28 // |
29 // Note: default copy and assign welcome. | 29 // Note: default copy and assign welcome. |
30 class GCM_EXPORT MCSMessage { | 30 class GCM_EXPORT MCSMessage { |
31 public: | 31 public: |
32 // Creates an invalid MCSMessage. | 32 // Creates an invalid MCSMessage. |
33 MCSMessage(); | 33 MCSMessage(); |
34 // Infers tag from |message|. | 34 // Infers tag from |message|. |
35 explicit MCSMessage(const google::protobuf::MessageLite& protobuf); | 35 explicit MCSMessage(const google::protobuf::MessageLite& protobuf); |
36 // |tag| must match |protobuf|'s message type. | 36 // |tag| must match |protobuf|'s message type. |
37 MCSMessage(uint8_t tag, const google::protobuf::MessageLite& protobuf); | 37 MCSMessage(uint8_t tag, const google::protobuf::MessageLite& protobuf); |
38 // |tag| must match |protobuf|'s message type. Takes ownership of |protobuf|. | 38 // |tag| must match |protobuf|'s message type. Takes ownership of |protobuf|. |
39 MCSMessage(uint8_t tag, | 39 MCSMessage(uint8_t tag, |
40 scoped_ptr<const google::protobuf::MessageLite> protobuf); | 40 std::unique_ptr<const google::protobuf::MessageLite> protobuf); |
41 MCSMessage(const MCSMessage& other); | 41 MCSMessage(const MCSMessage& other); |
42 ~MCSMessage(); | 42 ~MCSMessage(); |
43 | 43 |
44 // Returns whether this message is valid or not (whether a protobuf was | 44 // Returns whether this message is valid or not (whether a protobuf was |
45 // provided at construction time or not). | 45 // provided at construction time or not). |
46 bool IsValid() const; | 46 bool IsValid() const; |
47 | 47 |
48 // Getters for serialization. | 48 // Getters for serialization. |
49 uint8_t tag() const { return tag_; } | 49 uint8_t tag() const { return tag_; } |
50 int size() const {return size_; } | 50 int size() const {return size_; } |
51 std::string SerializeAsString() const; | 51 std::string SerializeAsString() const; |
52 | 52 |
53 // Getter for accessing immutable probotuf fields. | 53 // Getter for accessing immutable probotuf fields. |
54 const google::protobuf::MessageLite& GetProtobuf() const; | 54 const google::protobuf::MessageLite& GetProtobuf() const; |
55 | 55 |
56 // Getter for creating a mutated version of the protobuf. | 56 // Getter for creating a mutated version of the protobuf. |
57 scoped_ptr<google::protobuf::MessageLite> CloneProtobuf() const; | 57 std::unique_ptr<google::protobuf::MessageLite> CloneProtobuf() const; |
58 | 58 |
59 private: | 59 private: |
60 class Core : public base::RefCountedThreadSafe<MCSMessage::Core> { | 60 class Core : public base::RefCountedThreadSafe<MCSMessage::Core> { |
61 public: | 61 public: |
62 Core(); | 62 Core(); |
63 Core(uint8_t tag, const google::protobuf::MessageLite& protobuf); | 63 Core(uint8_t tag, const google::protobuf::MessageLite& protobuf); |
64 Core(uint8_t tag, scoped_ptr<const google::protobuf::MessageLite> protobuf); | 64 Core(uint8_t tag, |
| 65 std::unique_ptr<const google::protobuf::MessageLite> protobuf); |
65 | 66 |
66 const google::protobuf::MessageLite& Get() const; | 67 const google::protobuf::MessageLite& Get() const; |
67 | 68 |
68 private: | 69 private: |
69 friend class base::RefCountedThreadSafe<MCSMessage::Core>; | 70 friend class base::RefCountedThreadSafe<MCSMessage::Core>; |
70 ~Core(); | 71 ~Core(); |
71 | 72 |
72 // The immutable protobuf. | 73 // The immutable protobuf. |
73 scoped_ptr<const google::protobuf::MessageLite> protobuf_; | 74 std::unique_ptr<const google::protobuf::MessageLite> protobuf_; |
74 | 75 |
75 DISALLOW_COPY_AND_ASSIGN(Core); | 76 DISALLOW_COPY_AND_ASSIGN(Core); |
76 }; | 77 }; |
77 | 78 |
78 // These are cached separately to avoid having to recompute them. | 79 // These are cached separately to avoid having to recompute them. |
79 const uint8_t tag_; | 80 const uint8_t tag_; |
80 const int size_; | 81 const int size_; |
81 | 82 |
82 // The refcounted core, containing the protobuf memory. | 83 // The refcounted core, containing the protobuf memory. |
83 scoped_refptr<const Core> core_; | 84 scoped_refptr<const Core> core_; |
84 }; | 85 }; |
85 | 86 |
86 } // namespace gcm | 87 } // namespace gcm |
87 | 88 |
88 #endif // GOOGLE_APIS_GCM_BASE_MCS_MESSAGE_H_ | 89 #endif // GOOGLE_APIS_GCM_BASE_MCS_MESSAGE_H_ |
OLD | NEW |