ICE 3.4.2
[php5-ice-freebsdport.git] / cpp / src / Slice / Checksum.cpp
bloba30c36a9a6c8e7b012baf01ddac508780ca7ccf5
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 #include <Slice/Checksum.h>
11 #include <Slice/MD5.h>
13 using namespace std;
14 using namespace Slice;
16 namespace Slice
19 class ChecksumVisitor : public ParserVisitor
21 public:
23 ChecksumVisitor(ChecksumMap&);
25 virtual bool visitClassDefStart(const ClassDefPtr&);
26 virtual bool visitExceptionStart(const ExceptionPtr&);
27 virtual bool visitStructStart(const StructPtr&);
28 virtual void visitSequence(const SequencePtr&);
29 virtual void visitDictionary(const DictionaryPtr&);
30 virtual void visitEnum(const EnumPtr&);
31 virtual void visitConst(const ConstPtr&);
33 private:
35 string typeToString(const TypePtr&);
36 void updateMap(const string&, const string&);
38 ChecksumMap& _map;
43 Slice::ChecksumVisitor::ChecksumVisitor(ChecksumMap& m) :
44 _map(m)
48 bool
49 Slice::ChecksumVisitor::visitClassDefStart(const ClassDefPtr& p)
51 if(p->isLocal())
53 return false;
56 ClassList bases = p->bases();
58 ostringstream ostr;
60 if(p->isInterface())
62 ostr << "interface ";
64 else
66 ostr << "class ";
68 ostr << p->name();
70 if(!bases.empty())
72 if(!bases.front()->isInterface())
74 ostr << " extends " << bases.front()->scoped();
75 bases.erase(bases.begin());
77 if(!bases.empty())
79 if(p->isInterface())
81 ostr << " extends ";
83 else
85 ostr << " implements ";
87 for(ClassList::iterator q = bases.begin(); q != bases.end(); ++q)
89 if(q != bases.begin())
91 ostr << ", ";
93 ostr << (*q)->scoped();
97 ostr << endl;
99 if(p->hasDataMembers())
101 DataMemberList members = p->dataMembers();
102 for(DataMemberList::iterator q = members.begin(); q != members.end(); ++q)
104 ostr << typeToString((*q)->type()) << ' ' << (*q)->name() << endl;
108 if(p->hasOperations())
110 OperationList ops = p->operations();
111 for(OperationList::iterator q = ops.begin(); q != ops.end(); ++q)
113 ostr << typeToString((*q)->returnType()) << ' ' << (*q)->name() << '(';
114 ParamDeclList params = (*q)->parameters();
115 for(ParamDeclList::iterator r = params.begin(); r != params.end(); ++r)
117 if(r != params.begin())
119 ostr << ", ";
121 if((*r)->isOutParam())
123 ostr << "out ";
125 ostr << typeToString((*r)->type()) << ' ' << (*r)->name();
127 ostr << ')';
128 ExceptionList ex = (*q)->throws();
129 if(!ex.empty())
131 ostr << " throws ";
132 for(ExceptionList::iterator s = ex.begin(); s != ex.end(); ++s)
134 if(s != ex.begin())
136 ostr << ", ";
138 ostr << (*s)->scoped();
141 ostr << endl;
145 updateMap(p->scoped(), ostr.str());
147 return false;
150 bool
151 Slice::ChecksumVisitor::visitExceptionStart(const ExceptionPtr& p)
153 if(p->isLocal())
155 return false;
158 ExceptionPtr base = p->base();
160 ostringstream ostr;
162 ostr << "exception " << p->name();
163 if(base)
165 ostr << " extends " << base->scoped();
167 ostr << endl;
169 DataMemberList members = p->dataMembers();
170 for(DataMemberList::iterator q = members.begin(); q != members.end(); ++q)
172 ostr << typeToString((*q)->type()) << ' ' << (*q)->name() << endl;
175 updateMap(p->scoped(), ostr.str());
177 return false;
180 bool
181 Slice::ChecksumVisitor::visitStructStart(const StructPtr& p)
183 if(p->isLocal())
185 return false;
188 ostringstream ostr;
190 ostr << "struct " << p->name() << endl;
192 DataMemberList members = p->dataMembers();
193 for(DataMemberList::iterator q = members.begin(); q != members.end(); ++q)
195 ostr << typeToString((*q)->type()) << ' ' << (*q)->name() << endl;
198 updateMap(p->scoped(), ostr.str());
200 return false;
203 void
204 Slice::ChecksumVisitor::visitSequence(const SequencePtr& p)
206 if(p->isLocal())
208 return;
211 ostringstream ostr;
212 ostr << "sequence<" << typeToString(p->type()) << "> " << p->name() << endl;
213 updateMap(p->scoped(), ostr.str());
216 void
217 Slice::ChecksumVisitor::visitDictionary(const DictionaryPtr& p)
219 if(p->isLocal())
221 return;
224 ostringstream ostr;
225 ostr << "dictionary<" << typeToString(p->keyType()) << ", " << typeToString(p->valueType()) << "> " << p->name()
226 << endl;
227 updateMap(p->scoped(), ostr.str());
230 void
231 Slice::ChecksumVisitor::visitEnum(const EnumPtr& p)
233 if(p->isLocal())
235 return;
238 ostringstream ostr;
240 ostr << "enum " << p->name() << endl;
242 EnumeratorList enums = p->getEnumerators();
243 for(EnumeratorList::iterator q = enums.begin(); q != enums.end(); ++q)
245 ostr << (*q)->name() << endl;
248 updateMap(p->scoped(), ostr.str());
251 void
252 Slice::ChecksumVisitor::visitConst(const ConstPtr& p)
254 ostringstream ostr;
255 ostr << "const " << typeToString(p->type()) << ' ' << p->name() << " = " << p->value() << endl;
256 updateMap(p->scoped(), ostr.str());
259 string
260 Slice::ChecksumVisitor::typeToString(const TypePtr& type)
262 static const char* builtinTable[] =
264 "byte",
265 "boolean",
266 "short",
267 "int",
268 "long",
269 "float",
270 "double",
271 "string",
272 "Object",
273 "Object*",
274 "LocalObject"
277 if(!type)
279 return "void";
282 BuiltinPtr builtin = BuiltinPtr::dynamicCast(type);
283 if(builtin)
285 return builtinTable[builtin->kind()];
288 ProxyPtr proxy = ProxyPtr::dynamicCast(type);
289 if(proxy)
291 return proxy->_class()->scoped() + "*";
294 ContainedPtr cont = ContainedPtr::dynamicCast(type);
295 assert(cont);
296 return cont->scoped();
299 void
300 Slice::ChecksumVisitor::updateMap(const string& scoped, const string& data)
302 MD5 md5(reinterpret_cast<const unsigned char*>(data.c_str()), static_cast<int>(data.size()));
303 vector<unsigned char> bytes;
304 bytes.resize(16);
305 md5.getDigest(reinterpret_cast<unsigned char*>(&bytes[0]));
306 _map.insert(ChecksumMap::value_type(scoped, bytes));
309 Slice::ChecksumMap
310 Slice::createChecksums(const UnitPtr& u)
312 ChecksumMap result;
314 ChecksumVisitor visitor(result);
315 u->visit(&visitor, false);
317 return result;