cpp_util.py revision 5821806d5e7f356e8fa4b058a389a808ea183019
1# Copyright (c) 2012 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4"""Utilies and constants specific to Chromium C++ code. 5""" 6 7from datetime import datetime 8from model import PropertyType 9import os 10 11CHROMIUM_LICENSE = ( 12"""// Copyright (c) %d The Chromium Authors. All rights reserved. 13// Use of this source code is governed by a BSD-style license that can be 14// found in the LICENSE file.""" % datetime.now().year 15) 16GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN 17// %s 18// DO NOT EDIT. 19""" 20GENERATED_BUNDLE_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITIONS IN 21// %s 22// DO NOT EDIT. 23""" 24 25def Classname(s): 26 """Translates a namespace name or function name into something more 27 suited to C++. 28 29 eg experimental.downloads -> Experimental_Downloads 30 updateAll -> UpdateAll. 31 """ 32 return '_'.join([x[0].upper() + x[1:] for x in s.split('.')]) 33 34def GetAsFundamentalValue(prop, src, dst): 35 """Returns the C++ code for retrieving a fundamental type from a 36 Value into a variable. 37 38 src: Value* 39 dst: Property* 40 """ 41 return { 42 PropertyType.STRING: '%s->GetAsString(%s)', 43 PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)', 44 PropertyType.INTEGER: '%s->GetAsInteger(%s)', 45 PropertyType.DOUBLE: '%s->GetAsDouble(%s)', 46 }[prop.type_] % (src, dst) 47 48def GetValueType(type_): 49 """Returns the Value::Type corresponding to the model.PropertyType. 50 """ 51 return { 52 PropertyType.STRING: 'Value::TYPE_STRING', 53 PropertyType.INTEGER: 'Value::TYPE_INTEGER', 54 PropertyType.BOOLEAN: 'Value::TYPE_BOOLEAN', 55 PropertyType.DOUBLE: 'Value::TYPE_DOUBLE', 56 PropertyType.ENUM: 'Value::TYPE_STRING', 57 PropertyType.OBJECT: 'Value::TYPE_DICTIONARY', 58 PropertyType.FUNCTION: 'Value::TYPE_DICTIONARY', 59 PropertyType.ARRAY: 'Value::TYPE_LIST', 60 PropertyType.BINARY: 'Value::TYPE_BINARY', 61 }[type_] 62 63def GetParameterDeclaration(param, type_): 64 """Gets a parameter declaration of a given model.Property and its C++ 65 type. 66 """ 67 if param.type_ in (PropertyType.REF, PropertyType.OBJECT, PropertyType.ARRAY, 68 PropertyType.STRING, PropertyType.ANY): 69 arg = '%(type)s& %(name)s' 70 else: 71 arg = '%(type)s %(name)s' 72 return arg % { 73 'type': type_, 74 'name': param.unix_name, 75 } 76 77def GenerateIfndefName(path, filename): 78 """Formats a path and filename as a #define name. 79 80 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. 81 """ 82 return (('%s_%s_H__' % (path, filename)) 83 .upper().replace(os.sep, '_').replace('/', '_')) 84 85def GenerateTypeToCompiledTypeConversion(prop, from_, to): 86 try: 87 return _GenerateTypeConversionHelper(prop.type_, prop.compiled_type, from_, 88 to) 89 except KeyError: 90 raise NotImplementedError('Conversion from %s to %s in %s not supported' % 91 (prop.type_, prop.compiled_type, prop.name)) 92 93def GenerateCompiledTypeToTypeConversion(prop, from_, to): 94 try: 95 return _GenerateTypeConversionHelper(prop.compiled_type, prop.type_, from_, 96 to) 97 except KeyError: 98 raise NotImplementedError('Conversion from %s to %s in %s not supported' % 99 (prop.compiled_type, prop.type_, prop.name)) 100 101def _GenerateTypeConversionHelper(from_type, to_type, from_, to): 102 """Converts from PropertyType from_type to PropertyType to_type. 103 104 from_type: The PropertyType to be converted from. 105 to_type: The PropertyType to be converted to. 106 from_: The variable name of the type to be converted from. 107 to: The variable name of the type to be converted to. 108 """ 109 # TODO(mwrosen): Add support for more from/to combinations as necessary. 110 return { 111 PropertyType.STRING: { 112 PropertyType.INTEGER: 'base::StringToInt(%(from)s, &%(to)s)', 113 PropertyType.INT64: 'base::StringToInt64(%(from)s, &%(to)s)', 114 }, 115 PropertyType.INTEGER: { 116 PropertyType.STRING: '%(to)s = base::IntToString(%(from)s)', 117 }, 118 PropertyType.INT64: { 119 PropertyType.STRING: '%(to)s = base::Int64ToString(%(from)s)', 120 } 121 }[from_type][to_type] % {'from': from_, 'to': to} 122