Implement internal class for ipc-port-parcel APIs 57/161857/2
authorJunghoon Park <jh9216.park@samsung.com>
Tue, 28 Nov 2017 02:42:44 +0000 (11:42 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Tue, 28 Nov 2017 02:46:12 +0000 (11:46 +0900)
Change-Id: Iffff2ac8d991106d3a81cc8902a24aa07ba9d6ab
Signed-off-by: Junghoon Park <jh9216.park@samsung.com>
src/parcel-internal.cc [new file with mode: 0644]
src/parcel-internal.h [new file with mode: 0644]
src/rpc-port-parcel.cc [moved from src/rpc_port_parcel.cc with 100% similarity]
src/rpc-port.cc [moved from src/rpc_port.cc with 100% similarity]

diff --git a/src/parcel-internal.cc b/src/parcel-internal.cc
new file mode 100644 (file)
index 0000000..9868c57
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <cstring>
+
+#include "parcel-internal.h"
+
+namespace rpc_port {
+
+Parcel::Parcel(const unsigned char* buf, unsigned int size)
+    : data_(buf, buf + size) {
+  reader_ = data_.begin();
+}
+
+void Parcel::WriteByte(char b) {
+  Write<char>(b);
+}
+
+void Parcel::WriteInt16(short i) {
+  Write<short>(i);
+}
+
+void Parcel::WriteInt32(int i) {
+  Write<int>(i);
+}
+
+void Parcel::WriteInt64(long long i) {
+  Write<long long>(i);
+}
+
+void Parcel::WriteFloat(float f) {
+  Write<float>(f);
+}
+
+void Parcel::WriteDouble(double d) {
+  Write<double>(d);
+}
+
+void Parcel::WriteString(const char* str) {
+  int l = std::strlen(str) + 1;
+
+  Write<int>(l);
+  std::copy(str, str + l, std::back_inserter(data_));
+}
+
+void Parcel::WriteBool(bool b) {
+  Write<bool>(b);
+}
+
+void Parcel::WriteBundle(bundle* b) {
+  bundle_raw *r;
+  int len;
+  bundle_encode(b, &r, &len);
+  unsigned char* c = reinterpret_cast<unsigned char*>(r);
+
+  Write<int>(len + 1);
+  std::copy(c, c + len + 1, std::back_inserter(data_));
+}
+
+void Parcel::WriteArrayCount(int count) {
+  Write<int>(count);
+}
+
+char Parcel::ReadByte() {
+  return Read<char>();
+}
+
+short Parcel::ReadInt16() {
+  return Read<short>();
+}
+
+int Parcel::ReadInt32() {
+  return Read<int>();
+}
+
+long long Parcel::ReadInt64() {
+  return Read<long long>();
+}
+
+float Parcel::ReadFloat() {
+  return Read<float>();
+}
+
+double Parcel::ReadDouble() {
+  return Read<double>();
+}
+
+std::string Parcel::ReadString() {
+  int l = Read<int>();
+
+  std::string str(reader_, reader_ + l);
+  reader_ += l;
+
+  return str;
+}
+
+bool Parcel::ReadBool() {
+  return Read<bool>();
+}
+
+bundle* Parcel::ReadBundle() {
+  int l = Read<int>();
+
+  std::string str(reader_, reader_ + l);
+  reader_ += l;
+
+  bundle* b = bundle_decode(
+      reinterpret_cast<const bundle_raw*>(str.c_str()), l - 1);
+
+  return b;
+}
+
+int Parcel::ReadArrayCount() {
+  return Read<int>();
+}
+
+const std::vector<unsigned char>& Parcel::GetRaw() {
+  return data_;
+}
+
+}  // namespace rpc_port
\ No newline at end of file
diff --git a/src/parcel-internal.h b/src/parcel-internal.h
new file mode 100644 (file)
index 0000000..f63960e
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __PARCEL_INTERNAL_H__
+#define __PARCEL_INTERNAL_H__
+
+#include <bundle.h>
+
+#include <vector>
+#include <iterator>
+#include <algorithm>
+#include <string>
+
+namespace rpc_port {
+
+class Parcel {
+ public:
+  Parcel() = default;
+  Parcel(const unsigned char* buf, unsigned int size);
+
+  void WriteByte(char b);
+  void WriteInt16(short i);
+  void WriteInt32(int i);
+  void WriteInt64(long long i);
+  void WriteFloat(float f);
+  void WriteDouble(double d);
+  void WriteString(const char* str);
+  void WriteBool(bool b);
+  void WriteBundle(bundle* b);
+  void WriteArrayCount(int count);
+  char ReadByte();
+  short ReadInt16();
+  int ReadInt32();
+  long long ReadInt64();
+  float ReadFloat();
+  double ReadDouble();
+  std::string ReadString();
+  bool ReadBool();
+  bundle* ReadBundle();
+  int ReadArrayCount();
+  const std::vector<unsigned char>& GetRaw();
+
+ private:
+  template<typename T>
+  void Write(T d) {
+    unsigned char* p = reinterpret_cast<unsigned char*>(&d);
+
+    std::copy(p, p + sizeof(T), std::back_inserter(data_));
+  }
+
+  template<typename T>
+  T Read() {
+    T d;
+    unsigned char* p = reinterpret_cast<unsigned char*>(&d);
+
+    std::copy(reader_, reader_ + sizeof(T), p);
+    reader_ += sizeof(T);
+    return d;
+  }
+
+ private:
+  std::vector<unsigned char> data_;
+  std::vector<unsigned char>::iterator reader_;
+};
+
+}
+
+#endif  // __PARCEL_INTERNAL_H__
\ No newline at end of file
similarity index 100%
rename from src/rpc_port.cc
rename to src/rpc-port.cc