00001 #include "typedisplay.hh" 00002 #include <string> 00003 #include <iostream> 00004 00005 using namespace Typelib; 00006 using namespace std; 00007 00008 namespace 00009 { 00010 struct Indent 00011 { 00012 std::string& m_indent; 00013 std::string m_save; 00014 Indent(std::string& current) 00015 : m_indent(current), m_save(current) 00016 { m_indent += " "; } 00017 ~Indent() { m_indent = m_save; } 00018 }; 00019 } 00020 00021 TypeDisplayVisitor::TypeDisplayVisitor(std::ostream& stream, std::string const& base_indent) 00022 : m_stream(stream), m_indent(base_indent) {} 00023 00024 bool TypeDisplayVisitor::visit_(NullType const& type) 00025 { 00026 m_stream << "null\n"; 00027 return true; 00028 } 00029 00030 bool TypeDisplayVisitor::visit_(OpaqueType const& type) 00031 { 00032 m_stream << "opaque " << type.getName() << "\n"; 00033 return true; 00034 } 00035 00036 bool TypeDisplayVisitor::visit_(Compound const& type) 00037 { 00038 m_stream << "compound " << type.getName() << " [" << type.getSize() << "] {\n"; 00039 00040 { Indent indenter(m_indent); 00041 TypeVisitor::visit_(type); 00042 } 00043 00044 m_stream << m_indent 00045 << "};"; 00046 return true; 00047 } 00048 bool TypeDisplayVisitor::visit_(Compound const& type, Field const& field) 00049 { 00050 m_stream << m_indent << "(+" << field.getOffset() << ") "; 00051 TypeVisitor::visit_(type, field); 00052 m_stream << "\n"; 00053 return true; 00054 } 00055 00056 bool TypeDisplayVisitor::visit_(Numeric const& type) 00057 { 00058 char const* name = ""; 00059 switch (type.getNumericCategory()) 00060 { 00061 case Numeric::SInt: 00062 name = "sint"; 00063 break; 00064 case Numeric::UInt: 00065 name = "uint"; 00066 break; 00067 case Numeric::Float: 00068 name = "float"; 00069 break; 00070 default: 00071 throw UnsupportedType(type, "unsupported numeric category"); 00072 }; 00073 00074 m_stream 00075 << name << "(" << type.getSize() << ")"; 00076 return true; 00077 } 00078 00079 bool TypeDisplayVisitor::visit_(Enum const& type) 00080 { 00081 m_stream << "enum " << type.getName(); 00082 Enum::ValueMap::const_iterator it; 00083 00084 for (it = type.values().begin(); it != type.values().end(); ++it) 00085 m_stream << "\n " << it->first << " -> " << it->second; 00086 00087 return true; 00088 } 00089 00090 bool TypeDisplayVisitor::visit_(Pointer const& type) 00091 { 00092 m_stream << "pointer on " << type.getIndirection().getName() << "\n"; 00093 return true; 00094 } 00095 00096 bool TypeDisplayVisitor::visit_(Array const& type) 00097 { 00098 m_stream << "array[" << type.getDimension() << "] of\n"; 00099 { Indent indenter(m_indent); 00100 m_stream << m_indent; 00101 TypeVisitor::visit_(type); 00102 } 00103 return true; 00104 } 00105 00106