WSF
UtPack.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2003-2015 The Boeing Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11//****************************************************************************
12// Updated by Infoscitex, a DCS Company
13//****************************************************************************
14
15#ifndef UTPACK_HPP
16#define UTPACK_HPP
17
18#include <cassert>
19#include <exception>
20#include <iosfwd>
21#include <map>
22#include <memory>
23#include <string>
24#include <vector>
25
26#include "UtBuffer.hpp"
27#include "UtPackMessage.hpp"
28
29// UtPack -
30// This code uses a schema to generate C++ classes for use with serialization.
31// 1. Create a schema file with the data types you need.
32// 2. Run pack_to_cpp to generate C++ header files containing class definitions.
33// 3. Use generated classes to pack messages, and write them to a file or socket
34// with UtPackMessageStreamO.
35// 4. Use UtPackMessageStdStreamI to reconstitute the messages from the binary stream
36//
37// Benefits
38// - stream objects in a self-describing format
39// - compact streaming format
40// -minimal type information
41// -unset fields aren't transmitted
42// - messages can be modified while retaining backwards compatibility
43//
44// Compatibility
45// Limited changes allow for backwards compatibility of messages. These kinds of
46// changes are allowed
47// - add new message types
48// - add new optional fields to existing data types
49// - existing fields' data types may be modified in a limited way:
50// - numeric types are interchangeable: double <-> int8 <-> int32. (one side will lose precision)
51//
52// - required fields may not be added or removed, therefore required should be used cautiously
53
54#include "WsfUtExport.hpp"
55
56class UtPackType;
58#include "UtPackSchema.hpp"
59#include "UtPackStream.hpp"
60
64{
65public:
66 struct Member
67 {
69 unsigned short mMemberSize;
71 const char* mName;
72 const char* mTypeName;
73 };
74
83
84 const Member* FindMember(const std::string& aMemberName) const
85 {
86 for (const auto& mem : mMembers)
87 {
88 if (aMemberName == mem.mName)
89 {
90 return &mem;
91 }
92 }
93 return nullptr;
94 }
95
96 int GetMemberIndex(const std::string& aMemberName) const
97 {
98 for (size_t i = 0; i < mMembers.size(); ++i)
99 {
100 if (aMemberName == mMembers[i].mName)
101 {
102 return (int)i;
103 }
104 }
105 return -1;
106 }
107
108 int GetOptionalFlagsBytes() const { return (int)mOptionalFlagsBytes; }
109
111 unsigned char mOptionalFlagsBytes;
112 const char* mStructName;
113 std::vector<Member> mMembers;
115 using Union_SetFieldTypeFuncPtr = void (*)(void* aUnion, int aFieldType);
117};
118
120{
121public:
123 : mId(0)
124 , mTypePtr(nullptr)
125 , mSerializer(nullptr)
126 , mStructLayout(nullptr)
127 {
128 }
129
130 int mId;
132 std::shared_ptr<UtPackSerializeFunctor> mSerializer;
134};
135
137{
138public:
139 static const int cMAXIMUM_OPTIONAL_BITS = 128;
142 friend class UtPackReflector;
143
144 UtPackSerializer() = default;
147 void RegisterBasicType(const std::string& aName, std::unique_ptr<UtPackSerializeFunctor> aSerializeFunctor);
148 void RegisterListType(const std::string& aTypeName, const std::string& aContainedTypeName);
149 void RegisterConversion(const std::string& aType1,
150 const std::string& aType2,
151 std::unique_ptr<UtPackSerializeFunctor> aSerializeToType1,
152 std::unique_ptr<UtPackSerializeFunctor> aSerializeToType2);
153 void RegisterMessage(int aMessageId, MessageFactoryFnPtr aFactoryFunctionPtr);
154 TypeData* FindTypeData(const std::string& aTypeName);
155 std::shared_ptr<UtPackSerializeFunctor> FindSerializeFunction(const std::string& aTypeInMemory,
156 const std::string& aSerializedType);
157 void Initialize(UtPackSchema& aSchema);
158 TypeData* FindLocalContainedListType(const std::string& aListTypeName);
159 std::string FindLocalContainedListTypeName(std::string aListTypeName);
160
162 {
166 {
167 }
169 std::shared_ptr<UtPackSerializeFunctor> mSerializeFunction;
170 };
171
172 const MessageInfo* GetMessageInfo(int aMessageId) const
173 {
174 auto it = mMessageInfoMap.find(aMessageId);
175 if (it != mMessageInfoMap.end())
176 {
177 return &(it->second);
178 }
179 return nullptr;
180 }
181
183 {
184 auto it = mMessageInfoMap.find(aMessageId);
185 if (it != mMessageInfoMap.end())
186 {
187 return &(it->second);
188 }
189 return nullptr;
190 }
191
192 void Print(std::ostream& aOstream, UtPackMessage& aMessage);
193
194private:
195 TypeData* UseType(UtPackType* aTypePtr);
196 void AddStructLayout(UtPackStructLayout* aLayoutPtr);
197 // FindTypeData(const std::string& aTypeName)
198
199 // int -> type data
200 using TypeIdMap = std::map<std::string, int>;
201 std::vector<TypeData> mTypes;
202 TypeIdMap mTypeIds;
203
204 using TypeConversionMap = std::map<std::string, std::map<std::string, std::shared_ptr<UtPackSerializeFunctor>>>;
205 TypeConversionMap mTypeConversions;
206 std::map<std::string, std::string> mListTypes;
207 std::map<int, MessageInfo> mMessageInfoMap;
208
209 UtPackSchema mSchema;
210};
211
212#endif // UTPACK_HPP
#define WSF_UT_EXPORT
Definition WsfUtExport.hpp:32
Definition UtPackMessage.hpp:119
Definition UtPackSchema.hpp:97
Definition UtPack.hpp:120
UtPackSerializerTypeData()
Definition UtPack.hpp:122
UtPackStructLayout * mStructLayout
Definition UtPack.hpp:133
UtPackType * mTypePtr
Definition UtPack.hpp:131
int mId
Definition UtPack.hpp:130
std::shared_ptr< UtPackSerializeFunctor > mSerializer
Definition UtPack.hpp:132
Definition UtPack.hpp:137
MessageInfo * GetMessageInfo(int aMessageId)
Definition UtPack.hpp:182
void Initialize(UtPackSchema &aSchema)
Definition UtPack.cpp:674
friend class UtPackReflector
Definition UtPack.hpp:142
std::shared_ptr< UtPackSerializeFunctor > FindSerializeFunction(const std::string &aTypeInMemory, const std::string &aSerializedType)
Definition UtPack.cpp:585
void RegisterMessage(int aMessageId, MessageFactoryFnPtr aFactoryFunctionPtr)
Definition UtPack.cpp:793
void RegisterBuiltinTypes()
Definition UtPack.cpp:609
UtPackSerializerTypeData TypeData
Definition UtPack.hpp:141
std::string FindLocalContainedListTypeName(std::string aListTypeName)
Definition UtPack.cpp:769
const MessageInfo * GetMessageInfo(int aMessageId) const
Definition UtPack.hpp:172
UtPackMessage *(*)() MessageFactoryFnPtr
Definition UtPack.hpp:140
void RegisterListType(const std::string &aTypeName, const std::string &aContainedTypeName)
Definition UtPack.cpp:788
void RegisterBasicType(const std::string &aName, std::unique_ptr< UtPackSerializeFunctor > aSerializeFunctor)
Definition UtPack.cpp:654
TypeData * FindLocalContainedListType(const std::string &aListTypeName)
Definition UtPack.cpp:760
static const int cMAXIMUM_OPTIONAL_BITS
Definition UtPack.hpp:139
UtPackSerializer()=default
TypeData * FindTypeData(const std::string &aTypeName)
Definition UtPack.cpp:778
void RegisterConversion(const std::string &aType1, const std::string &aType2, std::unique_ptr< UtPackSerializeFunctor > aSerializeToType1, std::unique_ptr< UtPackSerializeFunctor > aSerializeToType2)
Definition UtPack.cpp:567
Definition UtPack.hpp:64
Union_SetFieldTypeFuncPtr mSetFieldTypeFuncPtr
Definition UtPack.hpp:116
std::vector< Member > mMembers
Definition UtPack.hpp:113
const char * mStructName
Definition UtPack.hpp:112
const Member * FindMember(const std::string &aMemberName) const
Definition UtPack.hpp:84
UtPackStructLayout()
Definition UtPack.hpp:75
int mStructSizeBytes
Definition UtPack.hpp:114
int GetOptionalFlagsBytes() const
Definition UtPack.hpp:108
int GetMemberIndex(const std::string &aMemberName) const
Definition UtPack.hpp:96
int mOptionalFlagsOffset
Definition UtPack.hpp:110
void(*)(void *aUnion, int aFieldType) Union_SetFieldTypeFuncPtr
Definition UtPack.hpp:115
unsigned char mOptionalFlagsBytes
Definition UtPack.hpp:111
Definition UtPackSchema.hpp:118
Definition UtPack.hpp:162
MessageFactoryFnPtr mMessageFactory
Definition UtPack.hpp:168
MessageInfo()
Definition UtPack.hpp:163
std::shared_ptr< UtPackSerializeFunctor > mSerializeFunction
Definition UtPack.hpp:169
Definition UtPack.hpp:67
const char * mTypeName
Definition UtPack.hpp:72
unsigned short mMemberSize
Definition UtPack.hpp:69
const char * mName
Definition UtPack.hpp:71
short mOptionalIndex
Definition UtPack.hpp:70
int mOffset
Definition UtPack.hpp:68
Copyrights Multiple, All Rights Reserved