1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MESSAGE_TRANSFORM_H 17 #define MESSAGE_TRANSFORM_H 18 19 #include <map> 20 #include <cstdint> 21 #include <functional> 22 #include "message.h" 23 24 namespace DistributedDB { 25 using ComputeLengthFunc = std::function<uint32_t(const Message *inMsg)>; 26 using SerializeFunc = std::function<int(uint8_t *buffer, uint32_t length, const Message *inMsg)>; 27 using DeserializeFunc = std::function<int(const uint8_t *buffer, uint32_t length, Message *inMsg)>; 28 29 struct TransformFunc { 30 ComputeLengthFunc computeFunc; 31 SerializeFunc serializeFunc; 32 DeserializeFunc deserializeFunc; 33 }; 34 35 class MessageTransform { 36 public: 37 // Must not be called in multi-thread 38 // Return E_ALREADY_REGISTER if msgId is already registered 39 // Return E_INVALID_ARGS if member of inFunc not all valid 40 // Calling ProtocolProto::RegTransformFunction 41 static int RegTransformFunction(uint32_t msgId, const TransformFunc &inFunc); 42 }; 43 } 44 45 #endif // MESSAGE_TRANSFORM_H