Skip to content

Commit

Permalink
feat[cpp]: compatible field of inside protocol class
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Oct 15, 2023
1 parent 14c7551 commit 93eecb4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 130 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@
*/
public abstract class GenerateCppUtils {

private static String protocolOutputRootPath = "cppProtocol";
private static String protocolOutputPath = StringUtils.EMPTY;
// custom configuration
public static String protocolOutputRootPath = "zfoocpp";
public static String protocolOutputPath = StringUtils.EMPTY;

private static Map<ISerializer, ICppSerializer> cppSerializerMap;

Expand All @@ -58,10 +59,13 @@ public static ICppSerializer cppSerializer(ISerializer serializer) {
}

public static void init(GenerateOperation generateOperation) {
protocolOutputPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath);

// if not specify output path, then use current default path
if (StringUtils.isEmpty(generateOperation.getProtocolPath())) {
protocolOutputPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath);
} else {
protocolOutputPath = generateOperation.getProtocolPath();
}
FileUtils.deleteFile(new File(protocolOutputPath));
FileUtils.createDirectory(protocolOutputPath);

cppSerializerMap = new HashMap<>();
cppSerializerMap.put(BooleanSerializer.INSTANCE, new CppBooleanSerializer());
Expand Down
92 changes: 17 additions & 75 deletions protocol/src/main/resources/cpp/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,23 @@ namespace zfoo {
writerIndex(writeIndex);
}

inline int32_t writeIntCount(const int32_t &intValue) {
uint32_t v = (uint32_t) ((intValue << 1) ^ (intValue >> 31));
if (v >> 7 == 0) {
return 1;
}
if (v >> 14 == 0) {
return 2;
}
if (v >> 21 == 0) {
return 3;
}
if (v >> 28 == 0) {
return 4;
}
return 5;
}

inline int32_t readInt() {
int32_t readIndex = m_readerIndex;

Expand Down Expand Up @@ -384,17 +401,6 @@ namespace zfoo {
return str;
}

// 很多脚本语言没有char,所以这里使用string代替
inline void writeChar(const char &value) {
string str;
str.push_back(value);
writeString(str);
}

inline char readChar() {
return readString()[0];
}

inline bool writePacketFlag(const IProtocol *packet) {
bool flag = packet == nullptr;
writeBool(!flag);
Expand Down Expand Up @@ -863,70 +869,6 @@ namespace zfoo {
return set;
}

//---------------------------------char--------------------------------------
inline void writeCharArray(const vector<char> &array) {
if (array.empty()) {
writeByte(0);
return;
}
int32_t length = array.size();
writeInt(length);
for (auto value : array) {
writeChar(value);
}
}

inline vector<char> readCharArray() {
int32_t length = readInt();
vector<char> array;
for (auto i = 0; i < length; i++) {
array.emplace_back(readChar());
}
return array;
}

inline void writeCharList(const list<char> &list) {
if (list.empty()) {
writeByte(0);
return;
}
int32_t length = list.size();
writeInt(length);
for (auto value : list) {
writeChar(value);
}
}

inline list<char> readCharList() {
int32_t length = readInt();
list<char> list;
for (auto i = 0; i < length; i++) {
list.emplace_back(readChar());
}
return list;
}

inline void writeCharSet(const set<char> &set) {
if (set.empty()) {
writeByte(0);
return;
}
int32_t length = set.size();
writeInt(length);
for (auto value : set) {
writeChar(value);
}
}

inline set<char> readCharSet() {
int32_t length = readInt();
set<char> set;
for (auto i = 0; i < length; i++) {
set.emplace(readChar());
}
return set;
}

//---------------------------------string--------------------------------------
inline void writeStringArray(const vector<string> &array) {
if (array.empty()) {
Expand Down

0 comments on commit 93eecb4

Please sign in to comment.