-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathEncoder.cpp
More file actions
148 lines (134 loc) · 3.63 KB
/
Copy pathEncoder.cpp
File metadata and controls
148 lines (134 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) 2009, Object Computing, Inc.
// All rights reserved.
// See the file license.txt for licensing information.
#include <Common/QuickFASTPch.h>
#include "Encoder.h"
#include <Codecs/DataDestination.h>
#include <Messages/Message.h>
#include <Codecs/PresenceMap.h>
#include <Codecs/TemplateRegistry.h>
#include <Codecs/FieldInstruction.h>
using namespace ::QuickFAST;
using namespace ::QuickFAST::Codecs;
Encoder::Encoder(Codecs::TemplateRegistryPtr registry)
: Context(registry)
{
}
void
Encoder::encodeMessage(
DataDestination & destination,
template_id_t templateId,
const Messages::Message & message)
{
encodeSegment(destination, templateId, message);
destination.endMessage();
}
void
Encoder::encodeSegment(
DataDestination & destination,
template_id_t templateId,
const Messages::FieldSet & fieldSet)
{
Codecs::TemplateCPtr templatePtr;
if(getTemplateRegistry()->getTemplate(templateId, templatePtr))
{
Codecs::PresenceMap pmap(templatePtr->presenceMapBitCount());
if(this->verboseOut_)
{
pmap.setVerbose(verboseOut_);
}
DestinationBufferPtr header = destination.startBuffer();
destination.startBuffer();
// can we "copy" the template ID?
if(templateId == templateId_)
{
pmap.setNextField(false);
}
else
{
pmap.setNextField(true);
FieldInstruction::encodeUnsignedInteger(destination, getWorkingBuffer(), templateId);
templateId_ = templateId;
}
encodeSegmentBody(destination, pmap, templatePtr, fieldSet);
destination.selectBuffer(header);
pmap.encode(destination);
}
else
{
throw EncodingError("[ERR D9] Unknown template ID.");
}
}
void
Encoder::encodeGroup(
DataDestination & destination,
Codecs::SegmentBodyCPtr group,
const Messages::FieldSet & fieldSet)
{
size_t presenceMapBits = group->presenceMapBitCount();
Codecs::PresenceMap pmap(presenceMapBits);
if(this->verboseOut_)
{
pmap.setVerbose(verboseOut_);
}
DestinationBufferPtr previousBuffer = destination.getBuffer();
DestinationBufferPtr header;
if(presenceMapBits > 0)
{
header = destination.startBuffer();
// start a buffer for the body of the group
destination.startBuffer();
}
encodeSegmentBody(destination, pmap, group, fieldSet);
if(presenceMapBits > 0)
{
destination.selectBuffer(header);
pmap.encode(destination);
}
destination.selectBuffer(previousBuffer);
}
#if 0
void
Encoder::encodeStaticTemplateRef(
DataSource & destination,
Codecs::PresenceMap & pmap,
const std::string & templateName,
const std::string & templateNamespace,
const Messages::FieldSet & fieldSet)
{
Codecs::TemplateCPtr templatePtr;
if(!templateRegistry_->findNamedTemplate(
templateName,
templateNamespace,
templatePtr))
{
throw EncodingError("[ERR D9] Unknown template name for static templateref.");
}
if(!encodeSegmentBody(destination, pmap, templatePtr, fieldSet))
{
throw EncodingError("Unexpected end of file during templateref decoding.");
}
}
#endif
void
Encoder::encodeSegmentBody(
DataDestination & destination,
Codecs::PresenceMap & pmap,
Codecs::SegmentBodyCPtr segment,
const Messages::FieldSet & fieldSet)
{
size_t instructionCount = segment->size();
for( size_t nField = 0; nField < instructionCount; ++nField)
{
PROFILE_POINT("emcode field");
Codecs::FieldInstructionCPtr instruction;
if(segment->getInstruction(nField, instruction))
{
if(verboseOut_)
{
(*verboseOut_) << "Encode instruction[" <<nField << "]: " << instruction->getIdentity()->name() << std::endl;
}
instruction->encode(destination, pmap, *this, fieldSet);
}
}
}