| OLD | NEW |
| (Empty) |
| 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 | |
| 5 """Utilies and constants specific to Chromium C++ code. | |
| 6 """ | |
| 7 | |
| 8 from datetime import datetime | |
| 9 from model import Property, PropertyType, Type | |
| 10 import os | |
| 11 | |
| 12 CHROMIUM_LICENSE = ( | |
| 13 """// Copyright (c) %d The Chromium Authors. All rights reserved. | |
| 14 // Use of this source code is governed by a BSD-style license that can be | |
| 15 // found in the LICENSE file.""" % datetime.now().year | |
| 16 ) | |
| 17 GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN | |
| 18 // %s | |
| 19 // DO NOT EDIT. | |
| 20 """ | |
| 21 GENERATED_BUNDLE_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITIONS IN | |
| 22 // %s | |
| 23 // DO NOT EDIT. | |
| 24 """ | |
| 25 | |
| 26 def Classname(s): | |
| 27 """Translates a namespace name or function name into something more | |
| 28 suited to C++. | |
| 29 | |
| 30 eg experimental.downloads -> Experimental_Downloads | |
| 31 updateAll -> UpdateAll. | |
| 32 """ | |
| 33 return '_'.join([x[0].upper() + x[1:] for x in s.split('.')]) | |
| 34 | |
| 35 def GetAsFundamentalValue(type_, src, dst): | |
| 36 """Returns the C++ code for retrieving a fundamental type from a | |
| 37 Value into a variable. | |
| 38 | |
| 39 src: Value* | |
| 40 dst: Property* | |
| 41 """ | |
| 42 return { | |
| 43 PropertyType.STRING: '%s->GetAsString(%s)', | |
| 44 PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)', | |
| 45 PropertyType.INTEGER: '%s->GetAsInteger(%s)', | |
| 46 PropertyType.DOUBLE: '%s->GetAsDouble(%s)', | |
| 47 }[type_.property_type] % (src, dst) | |
| 48 | |
| 49 def GetValueType(type_): | |
| 50 """Returns the Value::Type corresponding to the model.PropertyType. | |
| 51 """ | |
| 52 return { | |
| 53 PropertyType.STRING: 'Value::TYPE_STRING', | |
| 54 PropertyType.INTEGER: 'Value::TYPE_INTEGER', | |
| 55 PropertyType.BOOLEAN: 'Value::TYPE_BOOLEAN', | |
| 56 PropertyType.DOUBLE: 'Value::TYPE_DOUBLE', | |
| 57 PropertyType.ENUM: 'Value::TYPE_STRING', | |
| 58 PropertyType.OBJECT: 'Value::TYPE_DICTIONARY', | |
| 59 PropertyType.FUNCTION: 'Value::TYPE_DICTIONARY', | |
| 60 PropertyType.ARRAY: 'Value::TYPE_LIST', | |
| 61 PropertyType.BINARY: 'Value::TYPE_BINARY', | |
| 62 }[type_.property_type] | |
| 63 | |
| 64 def GetParameterDeclaration(param, type_): | |
| 65 """Gets a parameter declaration of a given model.Property and its C++ | |
| 66 type. | |
| 67 """ | |
| 68 if param.type_.property_type in (PropertyType.REF, | |
| 69 PropertyType.OBJECT, | |
| 70 PropertyType.ARRAY, | |
| 71 PropertyType.STRING, | |
| 72 PropertyType.ANY): | |
| 73 arg = 'const %(type)s& %(name)s' | |
| 74 else: | |
| 75 arg = '%(type)s %(name)s' | |
| 76 return arg % { | |
| 77 'type': type_, | |
| 78 'name': param.unix_name, | |
| 79 } | |
| 80 | |
| 81 def GenerateIfndefName(path, filename): | |
| 82 """Formats a path and filename as a #define name. | |
| 83 | |
| 84 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. | |
| 85 """ | |
| 86 return (('%s_%s_H__' % (path, filename)) | |
| 87 .upper().replace(os.sep, '_').replace('/', '_')) | |
| 88 | |
| 89 def PadForGenerics(var): | |
| 90 """Appends a space to |var| if it ends with a >, so that it can be compiled | |
| 91 within generic types. | |
| 92 """ | |
| 93 return ('%s ' % var) if var.endswith('>') else var | |
| OLD | NEW |