From 93eecb4250dd91a3791253b17bdd5342c648f04b Mon Sep 17 00:00:00 2001 From: godotg Date: Sun, 15 Oct 2023 11:07:08 +0800 Subject: [PATCH] feat[cpp]: compatible field of inside protocol class --- .../serializer/cpp/CppCharSerializer.java | 50 ---------- .../serializer/cpp/GenerateCppUtils.java | 14 ++- protocol/src/main/resources/cpp/ByteBuffer.h | 92 ++++--------------- 3 files changed, 26 insertions(+), 130 deletions(-) delete mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppCharSerializer.java diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppCharSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppCharSerializer.java deleted file mode 100644 index b4209e33..00000000 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppCharSerializer.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2020 The zfoo Authors - * - * 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. - */ - -package com.zfoo.protocol.serializer.cpp; - -import com.zfoo.protocol.generate.GenerateProtocolFile; -import com.zfoo.protocol.model.Pair; -import com.zfoo.protocol.registration.field.IFieldRegistration; -import com.zfoo.protocol.util.StringUtils; - -import java.lang.reflect.Field; - -import static com.zfoo.protocol.util.FileUtils.LS; - -/** - * @author godotg - */ -public class CppCharSerializer implements ICppSerializer { - - @Override - public Pair field(Field field, IFieldRegistration fieldRegistration) { - return new Pair<>("char", field.getName()); - } - - @Override - public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { - GenerateProtocolFile.addTab(builder, deep); - builder.append(StringUtils.format("buffer.writeChar({});", objectStr)).append(LS); - } - - @Override - public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { - String result = "result" + GenerateProtocolFile.index.getAndIncrement(); - - GenerateProtocolFile.addTab(builder, deep); - builder.append(StringUtils.format("char {} = buffer.readChar();", result)).append(LS); - return result; - } - -} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java index 54b7b9b1..6e490bb5 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java @@ -48,8 +48,9 @@ import static com.zfoo.protocol.util.StringUtils.TAB; */ 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 cppSerializerMap; @@ -58,10 +59,13 @@ public abstract class GenerateCppUtils { } 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()); diff --git a/protocol/src/main/resources/cpp/ByteBuffer.h b/protocol/src/main/resources/cpp/ByteBuffer.h index 07825277..2287c78d 100644 --- a/protocol/src/main/resources/cpp/ByteBuffer.h +++ b/protocol/src/main/resources/cpp/ByteBuffer.h @@ -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; @@ -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); @@ -863,70 +869,6 @@ namespace zfoo { return set; } - //---------------------------------char-------------------------------------- - inline void writeCharArray(const vector &array) { - if (array.empty()) { - writeByte(0); - return; - } - int32_t length = array.size(); - writeInt(length); - for (auto value : array) { - writeChar(value); - } - } - - inline vector readCharArray() { - int32_t length = readInt(); - vector array; - for (auto i = 0; i < length; i++) { - array.emplace_back(readChar()); - } - return array; - } - - inline void writeCharList(const list &list) { - if (list.empty()) { - writeByte(0); - return; - } - int32_t length = list.size(); - writeInt(length); - for (auto value : list) { - writeChar(value); - } - } - - inline list readCharList() { - int32_t length = readInt(); - list list; - for (auto i = 0; i < length; i++) { - list.emplace_back(readChar()); - } - return list; - } - - inline void writeCharSet(const set &set) { - if (set.empty()) { - writeByte(0); - return; - } - int32_t length = set.size(); - writeInt(length); - for (auto value : set) { - writeChar(value); - } - } - - inline set readCharSet() { - int32_t length = readInt(); - set set; - for (auto i = 0; i < length; i++) { - set.emplace(readChar()); - } - return set; - } - //---------------------------------string-------------------------------------- inline void writeStringArray(const vector &array) { if (array.empty()) {