Apollo  6.0
Open source self driving car software
py_message.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #ifndef CYBER_MESSAGE_PY_MESSAGE_H_
18 #define CYBER_MESSAGE_PY_MESSAGE_H_
19 
20 #include <iostream>
21 #include <sstream>
22 #include <string>
23 
24 #include "cyber/common/macros.h"
26 
27 namespace apollo {
28 namespace cyber {
29 namespace message {
30 
31 const char* const PY_MESSAGE_FULLNAME = "apollo.cyber.message.PyMessage";
32 
34  public:
35  PyMessageWrap() : type_name_("") {}
36  PyMessageWrap(const std::string& msg, const std::string& type_name)
37  : data_(msg), type_name_(type_name) {}
39  : data_(msg.data_), type_name_(msg.type_name_) {}
40  virtual ~PyMessageWrap() {}
41 
42  class Descriptor {
43  public:
44  std::string full_name() const { return PY_MESSAGE_FULLNAME; }
45  std::string name() const { return PY_MESSAGE_FULLNAME; }
46  };
47 
48  static const Descriptor* descriptor();
49  static std::string TypeName();
50 
51  bool SerializeToArray(void* data, int size) const;
52  bool SerializeToString(std::string* output) const;
53  bool ParseFromArray(const void* data, int size);
54  bool ParseFromString(const std::string& msgstr);
55  int ByteSize() const;
56  static void GetDescriptorString(const std::string& type,
57  std::string* desc_str);
58 
59  const std::string& data() const;
60  void set_data(const std::string& msg);
61  const std::string& type_name();
62  void set_type_name(const std::string& type_name);
63 
64  private:
65  std::string data_;
66  std::string type_name_;
67 };
68 
69 inline void PyMessageWrap::GetDescriptorString(const std::string& type,
70  std::string* desc_str) {
71  ProtobufFactory::Instance()->GetDescriptorString(type, desc_str);
72 }
73 
74 inline void PyMessageWrap::set_data(const std::string& msg) { data_ = msg; }
75 
76 inline const std::string& PyMessageWrap::data() const { return data_; }
77 
78 inline bool PyMessageWrap::ParseFromArray(const void* data, int size) {
79  if (data == nullptr || size <= 0) {
80  return false;
81  }
82 
83  data_.assign(reinterpret_cast<const char*>(data), size);
84  return true;
85 }
86 
87 inline bool PyMessageWrap::ParseFromString(const std::string& msgstr) {
88  // todo : will use submsg type ywf
89  // std::size_t pos = msgstr.rfind(data_split_pattern);
90  // if (pos != std::string::npos) {
91  // std::size_t split_count = data_split_pattern.size();
92  // data_ = msgstr.substr(0, pos);
93  // type_name_ = msgstr.substr(pos + split_count);
94  // return true;
95  // }
96  data_ = msgstr;
97  return true;
98 }
99 
100 inline bool PyMessageWrap::SerializeToArray(void* data, int size) const {
101  if (data == nullptr || size < ByteSize()) {
102  return false;
103  }
104 
105  memcpy(data, data_.data(), data_.size());
106  return true;
107 }
108 
109 inline bool PyMessageWrap::SerializeToString(std::string* output) const {
110  if (!output) {
111  return false;
112  }
113  // todo : will use submsg type ywf
114  // *output = data_ + data_split_pattern + type_name_;
115  *output = data_;
116  return true;
117 }
118 
119 inline int PyMessageWrap::ByteSize() const {
120  return static_cast<int>(data_.size());
121 }
122 
123 inline const std::string& PyMessageWrap::type_name() { return type_name_; }
124 
125 inline void PyMessageWrap::set_type_name(const std::string& type_name) {
126  type_name_ = type_name;
127 }
128 
130  static Descriptor desc;
131  return &desc;
132 }
133 
134 inline std::string PyMessageWrap::TypeName() { return PY_MESSAGE_FULLNAME; }
135 
136 } // namespace message
137 } // namespace cyber
138 } // namespace apollo
139 
140 #endif // CYBER_MESSAGE_PY_MESSAGE_H_
const std::string & type_name()
Definition: py_message.h:123
PyMessageWrap(const PyMessageWrap &msg)
Definition: py_message.h:38
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
const char *const PY_MESSAGE_FULLNAME
Definition: py_message.h:31
bool SerializeToString(std::string *output) const
Definition: py_message.h:109
static const Descriptor * descriptor()
Definition: py_message.h:129
PyMessageWrap(const std::string &msg, const std::string &type_name)
Definition: py_message.h:36
std::string name() const
Definition: py_message.h:45
virtual ~PyMessageWrap()
Definition: py_message.h:40
void set_type_name(const std::string &type_name)
Definition: py_message.h:125
const std::string & data() const
Definition: py_message.h:76
void set_data(const std::string &msg)
Definition: py_message.h:74
bool ParseFromArray(const void *data, int size)
Definition: py_message.h:78
Definition: py_message.h:33
int ByteSize() const
Definition: py_message.h:119
static void GetDescriptorString(const std::string &type, std::string *desc_str)
Definition: py_message.h:69
static std::string TypeName()
Definition: py_message.h:134
std::string full_name() const
Definition: py_message.h:44
bool ParseFromString(const std::string &msgstr)
Definition: py_message.h:87
PyMessageWrap()
Definition: py_message.h:35
bool SerializeToArray(void *data, int size) const
Definition: py_message.h:100