fixed: auto_ptr -> unique_ptr
[opensg.git] / Tools / fcd2code / FieldContainer.py
blob2402e911728bc76db712e67e70d42af344c2c0c3
2 import logging;
4 from FCDElement import FCDElement;
6 class FieldContainer(FCDElement):
7 """Represents a <Field/> element from a .fcd file.
8 """
10 def __init__(self):
11 super(FieldContainer, self).__init__();
12 self.m_log = logging.getLogger("FieldContainer");
13 self.m_fields = [];
14 self.m_usedTypeInclude = {};
15 self.m_usedFieldInclude = {};
16 self.initFCDDict();
18 def initFCDDict(self):
19 """Sets the fcd dictionary to default values.
20 """
21 self.setFCD("name", "", True);
22 self.setFCD("parent", "", True);
23 self.setFCD("mixinparent", "", True);
24 self.setFCD("mixinheader", "", True);
25 self.setFCD("library", "", True);
26 self.setFCD("pointerfieldtypes", "", True);
27 self.setFCD("structure", "", True);
28 self.setFCD("systemcomponent", "true", True);
29 self.setFCD("parentsystemcomponent", "true", True);
30 self.setFCD("decoratable", "false", True);
31 self.setFCD("useLocalIncludes", "false", True);
32 self.setFCD("isNodeCore", "false", True);
33 self.setFCD("isBundle", "false", True);
34 self.setFCD("description", "", True);
35 self.setFCD("group", "", True);
36 # self.setFCD("namespace", "", True);
37 self.setFCD("decorateeFieldFlags", "", True);
38 self.setFCD("additionalIncludes", "", True);
39 self.setFCD("additionalPriorityIncludes", "", True);
40 self.setFCD("fcdFileLines", [], True);
41 self.setFCD("fieldsUnmarkedOnCreate", "0", True);
42 self.setFCD("libnamespace", "OSG", True);
43 self.setFCD("parentnamespace", "", True);
44 self.setFCD("childFields", "none", True);
45 self.setFCD("parentFields", "none", True);
46 self.setFCD("docGroupBase", "", True);
47 self.setFCD("realparent", "", True);
48 self.setFCD("authors", "", True);
49 self.setFCD("typeDescAddable", "false", True);
50 self.setFCD("dynFCDerived", "false", True);
51 self.setFCD("dynFCDerived", "false", True);
52 self.setFCD("package", "OSG", True);
54 # Access fields
56 def addField(self, field):
57 idx = len(self.m_fields);
58 field.setFieldContainer(self);
59 self.m_fields.append(field);
60 return idx;
62 def subField(self, field):
63 self.m_fields.remove(field)
66 # Common tests
68 def isSystemComponent(self):
69 return self["isSystemComponent"];
71 def isDecoratable(self):
72 return self["isDecoratable"];
74 def hasAuthors(self):
75 return len(self["authors"]) != 0;
77 def setupFieldContainer(self):
78 self["Classname"] = self.getFCD("name");
79 self["CLASSNAME"] = self.getFCD("name").upper();
80 self["Parent"] = self.getFCD("parent");
81 self["isDecorator"] = False;
82 self["nsFilePrefix"] = "";
84 if (self.getFCD("libnamespace") != "" and \
85 self.getFCD("libnamespace") != "OSG"):
86 self["nsFilePrefix"] = self.getFCD("libnamespace");
88 self["nsParentFilePrefix"] = ""
90 if self.getFCD("parentnamespace") == "" and \
91 self.getFCD("libnamespace") != "OSG" :
93 self["nsParentFilePrefix"] = self.getFCD("libnamespace")
94 else:
95 if self.getFCD("parentnamespace") != "OSG":
96 self["nsParentFilePrefix"] = self.getFCD("parentnamespace")
98 def setupDecorator(self):
99 self["Classname"] = self.getFCD("name") + "Decorator";
100 self["CLASSNAME"] = self.getFCD("name").upper() + "DECORATOR";
101 self["Parent"] = self.getFCD("name");
102 self["RealParent"] = self.getFCD("name");
103 self["isDecorator"] = True;
105 if (self.getFCD("libnamespace") != "" and \
106 self.getFCD("libnamespace") != "OSG"):
107 self["nsFilePrefix"] = self.getFCD("libnamespace");
109 def finalize(self):
110 if len(self.m_fields) > 0:
111 self["hasFields"] = True;
112 else:
113 self["hasFields"] = False;
116 if self.getFCD("library") != "":
117 self["isInLibrary"] = True;
118 self["Libname"] = self.getFCD("library");
119 self["LIBNAME"] = self.getFCD("library").upper();
120 else:
121 if self.getFCD("systemcomponent") == "true":
122 self.m_log.error("finalize: \"library\" not set for a systemcomponent.");
123 self["isInLibrary"] = True;
124 self["Libname"] = "<UNDEF>";
125 self["LIBNAME"] = "<UNDEF>";
126 else:
127 self["isInLibrary"] = False;
128 self["Libname"] = None;
129 self["LIBNAME"] = None;
131 if self.getFCD("docGroupBase") != "":
132 self["DocGroupBase"] = self.getFCD("docGroupBase")
133 else:
134 self["DocGroupBase"] = "Grp" + str(self["Libname"])
136 if self.getFCD("name") != "":
137 self["Classname"] = self.getFCD("name");
138 self["CLASSNAME"] = self.getFCD("name").upper();
139 else:
140 self.m_log.error("finalize: \"name\" has no valid value.");
141 self["Classname"] = "<UNDEF>";
142 self["CLASSNAME"] = "<UNDEF>";
144 if self.getFCD("authors") != "":
145 self["hasAuthors"] = True;
146 self["authors"] = self.getFCD("authors");
147 self["Authors"] = self.getFCD("authors");
148 else:
149 self["hasAuthors"] = False;
150 self["authors"] = "";
151 self["Authors"] = "";
153 self["Package"] = self.getFCD("package");
155 #Create the Authors comment text
156 self["AuthorsCommentText"] = "";
157 if self.hasAuthors():
158 MaxLineLength = 78;
159 ContactPreText = " * contact: ";
160 SubLinesPreText = " * ";
161 SubLinesPostText = "*";
163 AuthorTextLength = MaxLineLength - len(ContactPreText) - len(SubLinesPostText);
165 #Split the authors into a list
166 AuthorList = self["Authors"].split(",");
168 #Create a comment line for each author
169 for AuthorIndex in range(len(AuthorList)):
171 AuthorCommentLine = AuthorList[AuthorIndex];
172 AuthorCommentLine = AuthorCommentLine.strip();
174 if len(AuthorCommentLine) <= AuthorTextLength:
175 AuthorCommentLine += (" "*(AuthorTextLength-len(AuthorCommentLine)));
176 else:
177 AuthorCommentLine = AuthorCommentLine[0:AuthorTextLength];
178 self.m_log.error("finalize: \"authors\": Max size of per-author text cannot be greater than: "
179 + str(AuthorTextLength) + ". Truncating to " + AuthorCommentLine);
181 #If this is the first line
182 if AuthorIndex == 0 :
183 AuthorCommentLine = ContactPreText + AuthorCommentLine;
184 else :
185 AuthorCommentLine = SubLinesPreText + AuthorCommentLine;
187 AuthorCommentLine = AuthorCommentLine + SubLinesPostText;
189 #If this is not the first line
190 if AuthorIndex != 0 :
191 #Add a newline before this line
192 AuthorCommentLine = "\n" + AuthorCommentLine;
194 self["AuthorsCommentText"] += AuthorCommentLine;
197 if self.getFCD("parent") != "":
198 self["Parent"] = self.getFCD("parent");
199 else:
200 self.m_log.error("finalize: \"parent\" has no valid value.");
201 self["Parent"] = "<UNDEF>";
203 if self.getFCD("realparent") != "":
204 self["RealParent"] = self.getFCD("realparent");
205 else:
206 self["RealParent"] = self["Parent"];
210 if self.getFCD("decoratable") == "true":
211 self["isDecoratable"] = True;
212 else:
213 self["isDecoratable"] = False;
215 if self.getFCD("isBundle") == "true":
216 self["isBundle"] = True;
217 else:
218 self["isBundle"] = False;
220 if self.getFCD("systemcomponent") == "true":
221 self["isSystemComponent"] = True;
222 else:
223 self["isSystemComponent"] = False;
225 if self.getFCD("structure") == "concrete":
226 self["isAbstract"] = False;
227 elif self.getFCD("structure") == "abstract":
228 self["isAbstract"] = True;
229 else:
230 self.m_log.warning("finalize: \"structure\" has no valid value.");
232 if ((self.getFCD("parentsystemcomponent") == "true") and
233 (self.getFCD("systemcomponent") == "false") ):
234 self["ParentHeaderPrefix"] = "OpenSG/";
235 else:
236 self["ParentHeaderPrefix"] = "";
238 if self.getFCD("systemcomponent") == "false":
239 self["HeaderPrefix"] = "OpenSG/";
240 else:
241 self["HeaderPrefix"] = "";
243 if self.getFCD("description").strip() == "":
244 self["Description"] = "";
245 self["SafeDescription"] = "\"\"";
246 else:
247 self["Description"] = self._formatString (self.getFCD("description"), 4)
248 self["SafeDescription"] = self._formatSafeString(self.getFCD("description"), 4)
250 self["hasProtectedFields"] = False;
251 self["hasPublicFields"] = False;
252 self["hasPtrFields"] = False;
253 self["hasChildFields"] = False;
254 self["hasParentFields"] = False;
256 self["Fields"] = [];
257 self["SFields"] = [];
258 self["MFields"] = [];
260 self["hasValueMField"] = False;
262 foundChildField = False;
264 for i, field in enumerate(self.m_fields):
266 field.finalize();
268 if i == 0:
269 field["prevField"] = None;
270 field["isFirstField"] = True;
271 else:
272 field["prevField"] = self.m_fields[i - 1];
273 field["isFirstField"] = False;
275 if i == len(self.m_fields) - 1:
276 field["Separator"] = "";
277 field["nextField"] = None;
278 field["isLastField"] = True;
279 else:
280 field["Separator"] = ",";
281 field["nextField"] = self.m_fields[i + 1];
282 field["isLastField"] = False;
284 # only use type includes once
285 if self.m_usedTypeInclude.has_key(field["TypeInclude"]):
286 field["needTypeInclude"] = False;
287 else:
288 self.m_usedTypeInclude[field["TypeInclude"]] = True;
290 # only use field includes once
291 if self.m_usedFieldInclude.has_key(field["FieldInclude"]):
292 field["needFieldInclude"] = False;
293 else:
294 self.m_usedFieldInclude[field["FieldInclude"]] = True;
296 self["Fields"].append(field);
298 if field.isProtected():
299 self["hasProtectedFields"] = True;
301 if field.isPublic() or field.isPublicRead():
302 self["hasPublicFields"] = True;
304 if field.isPtrField() and field.hasAccess() and not field.isParentField():
305 self["hasPtrFields"] = True;
307 if field.isChildField():
309 if foundChildField == False:
310 field["isFirstChildField"] = True
311 foundChildField = True
312 else:
313 field["isFirstChildField"] = False
315 self["hasChildFields"] = True
317 if field["category"] == "pointer" and field["pointertype"] == "parent":
318 self["hasParentFields"] = True;
320 if field.isSField():
321 self["SFields"].append(field);
323 if field.isMField():
324 self["MFields"].append(field);
326 if not field.isPtrField():
327 self["hasValueMField"] = True;
329 if self.getFCD("isNodeCore") == "true" or self.getFCD("isNodeCore") == "True":
330 self["isNodeCore"] = True;
331 else:
332 self["isNodeCore"] = False;
334 self["PointerField"] = False;
335 self["SFPointerField"] = False;
336 self["MFPointerField"] = False;
337 self["ChildField"] = False;
338 self["ChildSFields"] = False;
339 self["ChildMFields"] = False;
340 self["ChildSParent"] = False;
341 self["ChildMParent"] = False;
342 self["ParentSFields"] = False;
343 self["ParentMFields"] = False;
344 self["ParentField"] = False;
346 TraitsNS = 1;
348 if self.getFCD("pointerfieldtypes") == "both":
349 self["PointerField"] = True;
350 self["SFPointerField"] = True;
351 self["MFPointerField"] = True;
352 elif self.getFCD("pointerfieldtypes") == "single":
353 self["PointerField"] = True;
354 self["SFPointerField"] = True;
355 elif self.getFCD("pointerfieldtypes") == "multi":
356 self["PointerField"] = True;
357 self["MFPointerField"] = True;
360 if self.getFCD("childFields") == "multi" or \
361 self.getFCD("childFields") == "both":
362 self["ChildMFields"] = True
363 self["ChildField"] = True
364 self["ChildNS"] = '%d' % TraitsNS
366 if self.getFCD("childFields") == "single" or \
367 self.getFCD("childFields") == "both":
368 self["ChildSFields"] = True
369 self["ChildField"] = True
370 self["ChildNS"] = '%d' % TraitsNS
372 if self.getFCD("childFields") == "single" or \
373 self.getFCD("childFields") == "multi" or \
374 self.getFCD("childFields") == "both":
375 TraitsNS += 1
378 if self.getFCD("parentFields") == "multi" or \
379 self.getFCD("parentFields") == "both":
380 self["ParentMFields"] = True
381 self["ParentField"] = True
382 self["ParentNS"] = '%d' % TraitsNS
384 if self.getFCD("parentFields") == "single" or \
385 self.getFCD("parentFields") == "both":
386 self["ParentSFields"] = True
387 self["ParentField"] = True
388 self["ParentNS"] = '%d' % TraitsNS
391 self["MethodType"] = "";
393 if self.getFCD("decoratable") == "true":
394 self["MethodType"] = "virtual";
396 if self.getFCD("group") != "":
397 self["Group"] = self.getFCD("group");
398 else:
399 if self["CLASSNAME"] .endswith("ATTACHMENT") == True or \
400 self["Parent"].upper().endswith("ATTACHMENT") == True:
401 self["Group"] = self["Classname"]
402 else:
403 self["Group"] = "NULL";
405 # if self.getFCD("namespace") != "":
406 # self["Namespace"] = self.getFCD("namespace");
407 # else:
408 # self["Namespace"] = "0";
410 self["inExternalNamespace"] = False;
411 self["LibNamespace"] = "OSG";
412 self["LIBNAMESPACE"] = "OSG";
413 self["IMPORTNAMESPACE"] = "";
414 self["nsConst"] = "nsOSG";
415 self["nsFilePrefix"] = "";
417 if self.getFCD("libnamespace") != "":
419 self["LibNamespace"] = self.getFCD("libnamespace");
420 self["LIBNAMESPACE"] = self.getFCD("libnamespace").upper();
422 if self["LIBNAMESPACE"] != "OSG":
423 self["IMPORTNAMESPACE"] = "OSG_IMPORT_NAMESPACE;\n";
424 self["inExternalNamespace"] = True;
425 self["nsConst"] = "ns" + self.getFCD("libnamespace");
426 self["nsFilePrefix"] = self.getFCD("libnamespace");
428 self["FieldsUnmarkedOnCreate"] = self.getFCD("fieldsUnmarkedOnCreate");
429 self["TypeDescAddable"] = self.getFCD("typeDescAddable");
430 self["DynFCDerived"] = self.getFCD("dynFCDerived");
432 if self.getFCD("dynFCDerived") == "true":
433 self["isDynFCDerived"] = True;
434 else:
435 self["isDynFCDerived"] = False;
437 if self.getFCD("mixinparent") != "":
438 self["MixinParent"] = self.getFCD("mixinparent");
439 self["hasMixinParent"] = True
441 if self.getFCD("mixinheader") != "":
442 self["MixinHeader"] = self.getFCD("mixinheader");
443 else:
444 self["MixinHeader"] = "OSG" + self["nsFilePrefix"] + self["MixinParent"] + ".h"
445 else:
446 self["MixinParent"] = ""
447 self["hasMixinParent"] = False
449 decorateeFieldFlags = self.getFCD("decorateeFieldFlags");
450 if decorateeFieldFlags == "":
451 self["DecorateeFieldFlags"] = "Field::SFDefaultFlags";
452 else:
453 dffList = decorateeFieldFlags.split(",");
454 numFlags = len(dffList);
455 decorateeFieldFlags = "(";
457 for i, flag in enumerate(dffList):
458 flag = flag.strip();
460 if (i == 0) and (i != numFlags - 1):
461 decorateeFieldFlags = decorateeFieldFlags + "Field::" + flag + " |";
462 continue;
464 if i == numFlags - 1:
465 if i == 0:
466 decorateeFieldFlags = decorateeFieldFlags + "Field::" + flag;
467 else:
468 decorateeFieldFlags = decorateeFieldFlags + " Field::" + flag;
469 continue;
471 decorateeFieldFlags = decorateeFieldFlags + " Field::" + flag + " |";
472 decorateeFieldFlags = decorateeFieldFlags + ")";
473 self["DecorateeFieldFlags"] = decorateeFieldFlags;
475 includesList = self.getFCD("additionalIncludes").split(",");
476 self["AdditionalIncludes"] = [];
477 for include in includesList:
478 include = include.strip();
479 if include != "":
480 self["AdditionalIncludes"].append(include);
482 includesList = self.getFCD("additionalPriorityIncludes").split(",");
483 self["AdditionalPriorityIncludes"] = [];
484 for include in includesList:
485 include = include.strip();
486 if include != "":
487 self["AdditionalPriorityIncludes"].append(include);
489 def setFCDContents(self, fcdContents):
490 self.setFCD("fcdFileLines", fcdContents, True);
491 if fcdContents == []:
492 self["Fcdxml"] = "\"\"";
493 else:
494 self["Fcdxml"] = self._formatXML(fcdContents, 4);
496 def _dumpValues(self):
497 self.m_log.info("FieldContainer dumpValues:");
498 super(FieldContainer, self)._dumpValues(self.m_log);
500 for field in self.m_fields:
501 field._dumpValues();