Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  // $Id: CPPType.java 6776 2012-02-03 21:25:13Z mdb $
  //
  // Narya library - tools for developing networked games
  // Copyright (C) 2002-2012 Three Rings Design, Inc., All Rights Reserved
  // http://code.google.com/p/narya/
  //
  // This library is free software; you can redistribute it and/or modify it
  // under the terms of the GNU Lesser General Public License as published
 // by the Free Software Foundation; either version 2.1 of the License, or
 // (at your option) any later version.
 //
 // 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.
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 package com.threerings.presents.tools.cpp;
 
 import java.util.List;
 
 
 
 public class CPPType
 {
     public static final String JAVA_LIST_FIXED = "JAVA_LIST_NAME()";

    
The full cpp type.
 
     public final String type;

    
The method on ObjectInputStream and ObjectOutputStream to interpret this type.
 
     public final String interpreter;

    
CPP code to cast from this object to its reader type.
 
     public final String cast;

    
If this is a fixed type and thereby doesn't need a type encoding on the wire.
 
     public final String fixed;

    
Another type embedded in this type, or null if it doesn't have an embedded type.
 
     public final CPPType dependent;
 
     public final String representationImport;
 
     public final Class<?> rawType;
 
     public final boolean primitive;
 
     public CPPType (Type javaType)
     {
         if (javaType instanceof ParameterizedType) {
              = (Class<?>)((ParameterizedType)javaType).getRawType();
         } else if (javaType instanceof TypeVariable<?>) {
              = (Class<?>)((TypeVariable<?>)javaType).getBounds()[0];
         } else {
              = (Class<?>)javaType;
         }
 
         if (.equals(List.class)) {
              = ;
         } else {
              = null;
         }
 
         if (.isArray()) {
             Class<?> componentType = .getComponentType();
              = new CPPType(componentType);
              = "Shared< std::vector< " + . + " > >";
              = componentType.equals(Object.class) ||
                 componentType.isPrimitive() ? "Field" : "Object";
              = null;
 
         } else {
              = CPPUtil.getCPPType(javaType);
              = getCPPInterpreter();
             if (javaType instanceof ParameterizedType) {
                 Type[] typeArguments = ((ParameterizedType)javaType).getActualTypeArguments();
                 if (.equals(Comparable.class)) {
                      = new CPPType(Streamable.class);
                      = "presents/Streamable.h";
                 } else if ( == List.class) {
                     Class<?> param;
                     if (typeArguments[0] instanceof ParameterizedType) {
                         param = (Class<?>)((ParameterizedType)typeArguments[0]).getRawType();
                     } else {
                         param = (Class<?>)typeArguments[0];
                     }
                     if (!Streamable.class.isAssignableFrom(param)) {
                        throw new IllegalArgumentException(
                            "Lists may only contain Streamables in C++, not '" + typeArguments[0]
                                + "'");
                    }
                     = new CPPType(param);
                     = null;
                } else if (.equals(DSet.class)) {
                     = null;
                     = CPPUtil.makePath(".h");
                } else {
                    throw new IllegalArgumentException("Don't know how to handle " + );
                }
            } else if (javaType instanceof TypeVariable<?>
                || javaType.equals(com.threerings.presents.dobj.DSet.Entry.class)) {
                 = new CPPType(Streamable.class);
                 = "presents/Streamable.h";
            } else {
                if (!Streamable.class.equals() && Streamable.class.isAssignableFrom()) {
                     = CPPUtil.makePath(".h");
                } else {
                     = null;
                }
                 = null;
            }
        }
         = .isPrimitive();
        Matcher m = .matcher();
         = m.matches() ? "boost::static_pointer_cast<" + m.group(1) + ">" : ;
    }
    protected String getCPPInterpreter (Class<?> ftype)
    {
        if (ftype.equals(String.class) || ftype.equals(List.class)) {
            return "Field";
        } else if (ftype.equals(.)) {
            return "Boolean";
        } else if (ftype.equals(.)) {
            return "Byte";
        } else if (ftype.equals(.)) {
            return "Short";
        } else if (ftype.equals(.)) {
            return "Int";
        } else if (ftype.equals(.)) {
            return "Long";
        } else if (ftype.equals(.)) {
            return "Float";
        } else if (ftype.equals(.)) {
            return "Double";
        } else {
            return "Object";
        }
    }
    public String getCastFromStreamable (String name)
    {
        if () {
            return "boost::static_pointer_cast<presents::box::Boxed" +  + ">(" + name + ")->value";
        }
        return  + "(" + name + ")";
    }
    public String getAsStreamable (String name)
    {
        if () {
            return "presents::box::Boxed" +  + "::createShared(" + name + ")";
        }
        return name;
    }
    public String getWithoutShared ()
    {
        Matcher m = .matcher();
        return m.matches() ? m.group(1) : ;
    }
    protected static final Pattern TYPE_EXTRACT = Pattern.compile("Shared<(.*)>");
New to GrepCode? Check out our FAQ X