You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

parcelable.h 2.6 kB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef PARCELABLE_H
  2. #define PARCELABLE_H
  3. #include <iostream>
  4. #include <sstream>
  5. #include <vector>
  6. using namespace std;
  7. class Parcel
  8. {
  9. private:
  10. class Head
  11. {
  12. public:
  13. Head(int size)
  14. {
  15. this->size = size;
  16. }
  17. Head(istream& ss)
  18. {
  19. read(ss);
  20. }
  21. int size;
  22. void myWriteInt(ostream& os, int val)
  23. {
  24. char buf[9] = {0};
  25. snprintf(buf, sizeof(buf), "%08d", val);
  26. os<<buf;
  27. }
  28. int myReadInt(istream& is)
  29. {
  30. char buf[9] = {0};
  31. is.read(buf, sizeof(buf)-1);
  32. return stoi(buf);
  33. }
  34. void write(ostream& os){
  35. myWriteInt(os, size);
  36. }
  37. void read(istream& is){
  38. size = myReadInt(is);
  39. }
  40. };
  41. public:
  42. template<typename T>
  43. void write(const T& val){
  44. writePtr(&val, 1);
  45. }
  46. template<typename T>
  47. void read(T& val){
  48. readPtr(&val);
  49. }
  50. template<typename T>
  51. void writePtr(const T* ptr, int n){
  52. Head head{(int)(n * sizeof(T))};
  53. head.write(ss);
  54. ss.write((const char*)ptr, head.size);
  55. }
  56. template<typename T>
  57. void readPtr(T* ptr){
  58. Head head(ss);
  59. unique_ptr<char[]> buf(new char[head.size]);
  60. ss.read(buf.get(), head.size);
  61. memcpy(ptr, buf.get(), head.size);
  62. }
  63. void writeString(const string& val)
  64. {
  65. writePtr(val.c_str(), val.length());
  66. }
  67. void readString(string& val)
  68. {
  69. auto size = nextSize();
  70. unique_ptr<char[]> buf(new char[size+1]);
  71. readPtr(buf.get());
  72. buf[size]=0;
  73. val=buf.get();
  74. }
  75. void resetForRead()
  76. {
  77. ss.seekg(0, ss.beg);
  78. }
  79. void fillWith(const void* data, int len)
  80. {
  81. ss.clear();
  82. ss.write(static_cast<const char*>(data), len);
  83. }
  84. public:
  85. streampos mark()
  86. {
  87. return ss.tellg();
  88. }
  89. void unmark(streampos markPos)
  90. {
  91. ss.seekg(markPos);
  92. }
  93. public:
  94. vector<char> raw()
  95. {
  96. auto size = ss.tellp();
  97. resetForRead();
  98. vector<char> buf(size);
  99. ss.read(buf.data(), size);
  100. return buf;
  101. }
  102. private:
  103. int nextSize()
  104. {
  105. auto pos = mark();
  106. Head head(ss);
  107. unmark(pos);
  108. return head.size;
  109. }
  110. private:
  111. stringstream ss;
  112. };
  113. class Parcelable
  114. {
  115. public:
  116. virtual void writeTo(Parcel& out) const =0;
  117. virtual void readFrom(Parcel& in) =0;
  118. };
  119. #endif // PARCELABLE_H

mac下的“飞秋”大多数只是飞鸽传书协议,而且未发现令人满意的开源项目,所以基于c++与qt实现了基础的飞秋协议。

Contributors (1)