certi-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[certi-cvs] certi/libHLA CMakeLists.txt HLAtypesIEEE1516.hh...


From: certi-cvs
Subject: [certi-cvs] certi/libHLA CMakeLists.txt HLAtypesIEEE1516.hh...
Date: Thu, 17 Jul 2008 16:03:53 +0000

CVSROOT:        /sources/certi
Module name:    certi
Changes by:     Petr Gotthard <gotthardp>       08/07/17 16:03:53

Modified files:
        libHLA         : CMakeLists.txt HLAtypesIEEE1516.hh 
Added files:
        libHLA         : hlaomtdif2cpp.py 

Log message:
        Initial implementation of automatic datatype generation XML SOM/FOM --> 
C++

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/certi/libHLA/CMakeLists.txt?cvsroot=certi&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/certi/libHLA/HLAtypesIEEE1516.hh?cvsroot=certi&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/certi/libHLA/hlaomtdif2cpp.py?cvsroot=certi&rev=1.1

Patches:
Index: CMakeLists.txt
===================================================================
RCS file: /sources/certi/certi/libHLA/CMakeLists.txt,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- CMakeLists.txt      15 Jul 2008 10:54:11 -0000      1.1
+++ CMakeLists.txt      17 Jul 2008 16:03:53 -0000      1.2
@@ -27,6 +27,10 @@
     HLAtypesIEEE1516.hh
     DESTINATION include)
 
+INSTALL(PROGRAMS
+    hlaomtdif2cpp.py
+    DESTINATION bin)
+
 INSTALL(TARGETS HLA
     RUNTIME DESTINATION bin
     LIBRARY DESTINATION lib

Index: HLAtypesIEEE1516.hh
===================================================================
RCS file: /sources/certi/certi/libHLA/HLAtypesIEEE1516.hh,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- HLAtypesIEEE1516.hh 15 Jul 2008 10:54:11 -0000      1.1
+++ HLAtypesIEEE1516.hh 17 Jul 2008 16:03:53 -0000      1.2
@@ -11,7 +11,7 @@
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Lesser General Public License for more details.
 //
-// $Id: HLAtypesIEEE1516.hh,v 1.1 2008/07/15 10:54:11 gotthardp Exp $
+// $Id: HLAtypesIEEE1516.hh,v 1.2 2008/07/17 16:03:53 gotthardp Exp $
 // ----------------------------------------------------------------------------
 
 #ifndef _HLATYPES_IEEE1516_HH
@@ -28,6 +28,8 @@
  * access to IEEE 1516.2 compliant data buffers.
  * The data are manipulated "in situ", no temporary variables are created.
  *
+ * Use hlaomtdif2cpp -i <file> to generate FOM/SOM specific datatypes.
+ *
  * The extensive use of template metaprogramming allows many operations to
  * be pre-calculated during compile-time.
  * See http://aszt.inf.elte.hu/~gsd/halado_cpp/ch06s09.html
@@ -59,7 +61,7 @@
 class __HLAbuffer
 {
 private:
-    // static buffer for all instantiations of the HLAbuffer template
+    // static buffer for all instantiations of the HLAdata template
     // indexed by pointers after the last element, the end() pointers
     typedef std::map<char*,__HLAbuffer*> BufferList;
     static BufferList gBuffers;
@@ -100,7 +102,7 @@
         // find the first pointer not less than "this", what is the end() 
pointer
         BufferList::iterator result = gBuffers.lower_bound((char*)__this);
         if (result == gBuffers.end())
-            throw std::runtime_error("HLAbuffer: bad pointer");
+            throw std::runtime_error("HLAdata: bad pointer");
         return result;
     }
 
@@ -125,7 +127,7 @@
                     gBuffers[mBegin + mCapacity] = this;
                 }
                 else
-                    throw std::length_error("HLAbuffer: data buffer overflow");
+                    throw std::length_error("HLAdata: data buffer overflow");
             }
 
             char* ptr = (char*)mBegin + offset;
@@ -143,7 +145,7 @@
     {
         const __HLAbuffer& buffer = __buffer(__this);
         if ((char*)__this + size > (char*)buffer.mBegin + buffer.mCapacity)
-            throw std::length_error("HLAbuffer: data buffer overflow");
+            throw std::length_error("HLAdata: data buffer overflow");
     }
 
     virtual const size_t size() const = 0;
@@ -280,7 +282,7 @@
  *
  * For example:
  * typedef HLAbasicType<long, uint32_t, LittleEndian> HLAinteger32BE;
- * HLAbuffer<HLAinteger32BE> value;
+ * HLAdata<HLAinteger32BE> value;
  *
  * value = 42;
  */
@@ -333,23 +335,50 @@
 
 typedef HLAbasicType<char, char, BigEndian> HLAoctet;
 
-/* IEEE 1516.2, Table 25:
- * Simple datatype table
+/* Additional datatypes used by RPR-FOM
  */
-typedef HLAoctet HLAASCIIchar;
-typedef HLAoctetPairBE HLAunicodeChar;
-typedef HLAoctet HLAbyte;
+typedef HLAbasicType<unsigned short, uint16_t, BigEndian> Unsignedinteger16BE;
+typedef HLAbasicType<unsigned long, uint32_t, BigEndian> Unsignedinteger32BE;
 
-/* IEEE 1516.2, Table 27:
- * Enumeration datatype table
+//! HLA enumerated type, enumeration <E> with representation <R>.
+/*!
+ * HLAenumeratedType<ENUMERATION, REPRESENTATION>
+ * defines an user-convenient ENUMERATION stored using given REPRESENTATION.
+ *
+ * The data can be accessed in an usual way.
+ *
+ * For example:
+ * enum __HLAboolean
+ * {
+ *   HLAfalse = 0,
+ *   HLAtrue = 1
+ * };
+ * typedef HLAenumeratedType<__HLAboolean, HLAinteger32BE> HLAboolean;
+ * HLAdata<HLAboolean> value;
+ *
+ * value = HLAtrue;
  */
-enum __HLAboolean
+template<class E, class R>
+struct HLAenumeratedType
 {
-    HLAfalse = 0,
-    HLAtrue = 1
+    HLAenumeratedType& operator = (const E& data)
+    {
+        *(R*)this = data;
+        return *this;
+    }
+
+    operator E() const
+    { return *(R*)this; }
+
+    static const size_t empty_sizeof()
+    { return R::empty_sizeof(); }
+
+    static const size_t __sizeof()
+    { return R::__sizeof(); }
+
+    static const size_t m_octetBoundary = R::m_octetBoundary;
+    static const bool m_isVariable = false;
 };
-// As 64bit architectures may have 64bit enum, we need static-cast to uint32_t.
-typedef HLAbasicType<__HLAboolean, uint32_t, BigEndian> HLAboolean;
 
 #ifndef MAX
 #define MAX(a,b) (((a)>(b))?(a):(b))
@@ -370,7 +399,7 @@
  *
  * For example:
  * typedef HLAfixedArray<HLAinteger32BE,3> Coordinates;
- * HLAbuffer<Coordinates> value;
+ * HLAdata<Coordinates> value;
  *
  * (*value)[0] = 100;
  * (*value)[1] = 200;
@@ -460,7 +489,7 @@
  *
  * For example:
  * typedef HLAvariableArray<HLAinteger32BE> List;
- * HLAbuffer<List> value;
+ * HLAdata<List> value;
  *
  * (*value).set_size(2);
  * (*value)[0] = 100;
@@ -616,19 +645,21 @@
  * is a zero-based order of the field being accessed.
  *
  * For example:
+ * enum {
+ *   FIELD_X = 0,
+ *   FIELD_Y,
+ *   FIELD_Z
+ * };
  * typedef
- * #define FIELD_X 0
  *   HLAfixedRecord<HLAfloat23LE,
- * #define FIELD_Y 1
  *   HLAfixedRecord<HLAfloat32LE,
- * #define FIELD_Z 2
  *   HLAfixedRecord<HLAfloat32LE,
  *   HLAfixedRecordEnd> > > Coordinates;
- * HLAbuffer<Coordinates> value;
+ * HLAdata<Coordinates> value;
  *
- * (*value).field<FIELD_X>() = 3.14;
- * (*value).field<FIELD_Y>() = 6.28;
- * (*value).field<FIELD_Z>() = 9.42;
+ * value->field<FIELD_X>() = 3.14;
+ * value->field<FIELD_Y>() = 6.28;
+ * value->field<FIELD_Z>() = 9.42;
  */
 template<class M, class N, bool hasVariable = M::m_isVariable || 
N::m_isVariable>
 struct HLAfixedRecord;
@@ -759,5 +790,5 @@
 
 #endif // _HLATYPES_IEEE1516_HH
 
-// $Id: HLAtypesIEEE1516.hh,v 1.1 2008/07/15 10:54:11 gotthardp Exp $
+// $Id: HLAtypesIEEE1516.hh,v 1.2 2008/07/17 16:03:53 gotthardp Exp $
 

Index: hlaomtdif2cpp.py
===================================================================
RCS file: hlaomtdif2cpp.py
diff -N hlaomtdif2cpp.py
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ hlaomtdif2cpp.py    17 Jul 2008 16:03:53 -0000      1.1
@@ -0,0 +1,214 @@
+#!/usr/bin/env python
+
+## ----------------------------------------------------------------------------
+## hlaomtdif2cpp.py - HLA OMT DIF document to C++ code convertor
+## Copyright (C) 2008  Petr Gotthard <address@hidden>
+##
+## This library is free software; you can redistribute it and/or
+## modify it under the terms of the GNU Lesser General Public
+## License version 2.1, as published by the Free Software Foundation.
+##
+## This library is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## Lesser General Public License for more details.
+##
+## $Id: hlaomtdif2cpp.py,v 1.1 2008/07/17 16:03:53 gotthardp Exp $
+## ----------------------------------------------------------------------------
+
+import getopt, sys
+import re
+import xml.sax.handler
+
+# name of the input file
+input = None
+
+def usage():
+  print "Usage: " + sys.argv[0] + " -i DIF"
+  print "Converts HLA OMT DIF document to C++ code."
+
+class CommentFormatter:
+  def __init__(self, indent=0, width=80):
+    self.lineCount = 0
+    self.indent = indent
+    self.width = width - indent
+
+  def addComment(self, text):
+    stack = [word for word in text.replace("\n", " ").split(" ") if word]
+    while stack:
+      line = ""
+      while stack:
+        if len(line) + len(" " + stack[0]) + 3 > self.width: break
+        if line: line += " "
+        line += stack.pop(0)
+      if self.lineCount == 0:
+        print " "*self.indent + "/* " + line
+      else:
+        print " "*self.indent + " * " + line
+      self.lineCount += 1
+
+  def addEmptyLine(self):
+    print " "*self.indent + " * "
+
+  def finish(self):
+    print " "*self.indent + " */"
+    self.lineCount = 0
+
+def printComment(text, indent=0, width=80):
+  if len(text) == 0 or text == "-NULL-": return
+
+  text = text.replace("\n", " ")
+  width = width - indent
+  # Single-line comment
+  if len(text) + 3 < width:
+    print " "*indent + "// " + text
+    return
+  # Multi-line comment
+  formatter = CommentFormatter(indent, width)
+  formatter.addComment(text)
+  formatter.finish()
+
+class CodeGenerator(xml.sax.handler.ContentHandler):
+#  def __init__(self):
+
+  def startElement(self, name, attributes):
+    if name == "objectModel":
+      print "#line", parser.getLineNumber(), "\"" + input + "\""
+      formatter = CommentFormatter()
+      formatter.addComment("Name: " + attributes["name"])
+      formatter.addComment("Version: " + attributes["version"])
+      formatter.addComment("Type: " + attributes["type"])
+      formatter.addEmptyLine()
+      formatter.addComment("Application domain:")
+      formatter.addComment(attributes["appDomain"])
+      formatter.addEmptyLine()
+      formatter.addComment("Purpose:")
+      formatter.addComment(attributes["purpose"])
+      formatter.addEmptyLine()
+      formatter.addComment("Sponsor:")
+      formatter.addComment(attributes["sponsor"])
+      formatter.addEmptyLine()
+      formatter.addComment("Point of contact:")
+      formatter.addComment("Name: " + attributes["pocName"])
+      formatter.addComment("Organization: " + attributes["pocOrg"])
+      formatter.addComment("Phone: " + attributes["pocPhone"])
+      formatter.addComment("E-mail: " + attributes["pocEmail"])
+      formatter.finish();
+      print
+
+    if name == "simpleData":
+      print "#line", parser.getLineNumber(), "\"" + input + "\""
+      self.buffer = ""
+      printComment(attributes["semantics"])
+      print "typedef " + attributes["representation"] + " " + 
attributes["name"] + ";"
+
+    elif name == "enumeratedData":
+      print "#line", parser.getLineNumber(), "\"" + input + "\""
+      self.enumCounter = 0
+      self.enumRepresentation = attributes["representation"]
+      self.enumName = attributes["name"]
+      printComment(attributes["semantics"])
+      # The enumerations must be isolated from other enumerations in an 
individual namespace
+      print "namespace __" + attributes["name"] + " {"
+      print "enum __enum {",
+
+    elif name == "enumerator":
+      if self.enumCounter > 0:
+        print ",",
+      self.enumCounter += 1
+      sys.stdout.write("\n  " + attributes["name"] + " = " + 
attributes["values"])
+
+    elif name == "arrayData":
+      print "#line", parser.getLineNumber(), "\"" + input + "\""
+      printComment(attributes["semantics"])
+      if attributes["encoding"] == "HLAfixedArray":
+        print "typedef HLAfixedArray<" + attributes["dataType"] + ", " + 
attributes["cardinality"] + "> " + attributes["name"] + ";"
+
+      elif attributes["encoding"] == "HLAvariableArray":
+        print "typedef HLAvariableArray<" + attributes["dataType"] + "> " + 
attributes["name"] + ";"
+
+    elif name == "fixedRecordData":
+      print "#line", parser.getLineNumber(), "\"" + input + "\""
+      printComment(attributes["semantics"])
+      # The enumerations must be isolated from other enumerations in an 
individual namespace
+      print "namespace __" + attributes["name"] + " {"
+      print "enum __enum {"
+      self.recordName = attributes["name"]
+      self.recordFields = []
+
+    elif name == "field":
+      if len(self.recordFields) > 0:
+        sys.stdout.write(",\n")
+      printComment(attributes["semantics"], 2)
+      sys.stdout.write("  " + attributes["name"])
+      if len(self.recordFields) == 0:
+        sys.stdout.write(" = 0")
+      tupple = (attributes["dataType"], attributes["name"])
+      self.recordFields.append(tupple)
+
+#  def characters(self, data):
+
+  def endElement(self, name):
+    if name == "simpleDataTypes":
+      print "\n",
+
+    if name == "enumeratedData":
+      print "\n};"
+      print "}"
+      print "typedef HLAenumeratedType<__" + self.enumName + "::__enum, " + 
self.enumRepresentation + "> " + self.enumName + ";\n"
+
+    if name == "arrayDataTypes":
+      print "\n",
+
+    elif name == "fixedRecordData":
+      print "\n};"
+      print "}"
+      print "typedef"
+      for field in self.recordFields:
+        print "  HLAfixedRecord<" + field[0] + ", // " + field[1]
+      sys.stdout.write("  HLAfixedRecordEnd")
+      for field in self.recordFields:
+        sys.stdout.write("> ")
+      print self.recordName + ";\n"
+
+try:
+  opts, args = getopt.getopt(sys.argv[1:], "i:h", ["help", "output="])
+except getopt.GetoptError, err:
+  print sys.argv[0] + ": " + str(err) # will print something like "option -a 
not recognized"
+  print "Try `" + sys.argv[0] + " --help' for more information."
+  sys.exit(2)
+for o, a in opts:
+  if o in ("-i", "--input"):
+    input = a
+    modulename = "__HLAOMTDIF2CPP_" + re.sub('[-._]', '', input.upper()) + 
"_HH__"
+  elif o in ("-h", "--help"):
+    usage()
+    sys.exit()
+  else:
+    assert False, "unhandled option"
+
+if input == None:
+  print sys.argv[0] + ": missing input file"
+  print "Try `" + sys.argv[0] + " --help' for more information."
+  sys.exit(2)
+
+print "// Do not edit! This file was automatically generated by",
+for arg in sys.argv:
+  print arg,
+print "\n"
+print "#ifndef " + modulename
+print "#define " + modulename
+print
+print "#include <HLAtypesIEEE1516.hh>\n"
+print "namespace libhla {\n"
+
+parser = xml.sax.make_parser()
+handler = CodeGenerator()
+parser.setContentHandler(handler)
+parser.parse(input)
+
+print "} // namespace libhla"
+print
+print "#endif // " + modulename
+
+# $Id: hlaomtdif2cpp.py,v 1.1 2008/07/17 16:03:53 gotthardp Exp $




reply via email to

[Prev in Thread] Current Thread [Next in Thread]