mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-24 02:26:23 +00:00
feat[cpp]: compatible field of inside protocol class
This commit is contained in:
@@ -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<String, String> 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<ISerializer, ICppSerializer> 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());
|
||||
|
||||
@@ -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<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()) {
|
||||
|
||||
Reference in New Issue
Block a user