def __jni_get_jobjects_array_by_core_objects_implementation(self): object_name = string_utils.cpp_class_name_to_cpp_file_name(self.__class_name) impl = 'jobjectArray JniHelper::GetJ{0}sArrayByCore{0}s(const std::vector<std::unique_ptr<{0}>>& {1}s) {{'.format( self.__class_name, object_name) impl += '\n' impl += indent(2) + 'jclass {0}Jclass = JniReferenceCache::SharedCache()->{1}_jclass();\n'.format( string_utils.first_char_to_lower(self.__class_name), object_name) impl += indent(2) + 'JNIEnv* env = GetJniEnv();\n' impl += indent(2) + 'if (!env) {\n' impl += indent(4) + 'return env->NewObjectArray(0, {0}Jclass, NULL);\n'.format( string_utils.first_char_to_lower(self.__class_name)) impl += indent(2) + '}\n\n' impl += indent(2) + 'jobjectArray jobjs = env->NewObjectArray({0}s.size(), {1}Jclass, NULL);\n\n'.format( object_name, string_utils.first_char_to_lower(self.__class_name)) impl += indent(2) + 'jsize i = 0;\n' impl += indent(2) + 'for (auto it = {0}s.begin(); it != {0}s.end(); ++it) {{\n'.format(object_name) impl += indent(4) + 'jobject j{0} = GetJ{0}ByCore{0}(**it);\n'.format(self.__class_name) impl += indent(4) + 'env->SetObjectArrayElement(jobjs, i, j{0});\n'.format(self.__class_name) impl += indent(4) + 'env->DeleteLocalRef(j{0});\n'.format(self.__class_name) impl += indent(4) + '++i;\n' impl += indent(2) + '}\n' impl += indent(2) + 'return jobjs;\n' impl += '}' return impl
def __web_api_declaration(self, api, config): declaration = '' declaration += '- (void){0}'.format(string_utils.first_char_to_lower(api.alias)) if len(api.input_var_list) > 0: if len(api.input_var_list) == 1: declaration += 'By' else: declaration += 'With' for i, input_var in enumerate(api.input_var_list): input_name = string_utils.to_objc_property_name(input_var.name) if i == 0: input_name = string_utils.first_char_to_upper(input_name) declaration += '{0}:({1}){2} '.format(input_name, input_var.var_type.to_objc_getter_string(config), string_utils.first_char_to_lower(input_name)) declaration += 'success:(void (^)(' else: declaration += 'Success:(void (^)(' if len(api.output_var_list) > 0: for i, output_var in enumerate(api.output_var_list): declaration += output_var.var_type.to_objc_getter_string(config) declaration += string_utils.to_objc_property_name(output_var.name) if i != len(api.output_var_list) - 1: declaration += ', ' declaration += '))successBlock failure:(void (^)(NSError *error))failureBlock' return declaration
def release_impl(self): method_name = self.release_method_name() para_name = ' (JNIEnv *env, jobject thiz, jlong handler)' step_1 = 'lesschat::{0}* {1} = reinterpret_cast<lesschat::{0}*>(handler);'\ .format(self.class_name, string_utils.first_char_to_lower(self.class_name)) step_2 = 'LCC_SAFE_DELETE({0});'.format(string_utils.first_char_to_lower(self.class_name)) return method_name + '\n' + para_name + '{{\n {0}\n {1}\n}}'.format(step_1, step_2)
def __release_impl(self): method_name = self.__release_method_name() para_name = ' (JNIEnv *env, jobject thiz, jlong handler)' step_1 = 'lesschat::{0}* {1} = reinterpret_cast<lesschat::{0}*>(handler);'\ .format(self.__class_name, string_utils.first_char_to_lower(self.__class_name)) step_2 = 'LCC_SAFE_DELETE({0});'.format(string_utils.first_char_to_lower(self.__class_name)) return method_name + '\n' + para_name + '{{\n {0}\n {1}\n}}'.format(step_1, step_2)
def __web_api_declaration(self, api): declaration = '' declaration += '- (void){0}'.format( string_utils.first_char_to_lower(api.alias)) if len(api.input_var_list) > 0: if len(api.input_var_list) == 1: declaration += 'By' else: declaration += 'With' for i, input_var in enumerate(api.input_var_list): input_name = string_utils.to_objc_property_name(input_var.name) if i == 0: input_name = string_utils.first_char_to_upper(input_name) declaration += '{0}:({1}){2} '.format( input_name, input_var.var_type.to_objc_getter_string(), string_utils.first_char_to_lower(input_name)) declaration += 'success:(void (^)(' else: declaration += 'Success:(void (^)(' if len(api.output_var_list) > 0: for i, output_var in enumerate(api.output_var_list): declaration += output_var.var_type.to_objc_getter_string() declaration += string_utils.to_objc_property_name( output_var.name) if i != len(api.output_var_list) - 1: declaration += ', ' declaration += '))successBlock failure:(void (^)(NSError *error))failureBlock' return declaration
def generate_implementation(self): file_name = 'LCC{0}.mm'.format(self.class_name) file_path = _OBJC_BUILD_PATH + string_utils.cpp_group_name_to_objc_group_name(self.group_name) + '/' + file_name output_impl = open(file_path, 'w') output_impl.write('#if !defined(__has_feature) || !__has_feature(objc_arc)\n#error "This file requires ARC support."\n#endif') output_impl.write(_OBJC_BR) output_impl.write('#import "LCC{0}.h"\n#import "LCC{0}_CoreAddition.h"\n\n#import "LCCObjcAdapter.h"'.format(self.class_name)) output_impl.write(_OBJC_BR) output_impl.write('@implementation LCC{0}'.format(self.class_name)) output_impl.write(_OBJC_BR) output_impl.write('#pragma mark - Property') output_impl.write(_OBJC_BR) for objc_var in self.objc_var_list: output_impl.write(objc_var.getter_impl()) output_impl.write(_OBJC_BR) output_impl.write(objc_var.setter_impl()) output_impl.write(_OBJC_BR) output_impl.write('#pragma mark - Core Addition') output_impl.write(_OBJC_BR) output_impl.write('+ (instancetype){0}WithCore{1}:(const lesschat::{1}&)core{1} {{\n' .format(string_utils.first_char_to_lower(self.class_name), self.class_name)) output_impl.write(_OBJC_SPACE) output_impl.write('LCC{0} *{1} = [[LCC{0} alloc] init];\n'.format(self.class_name, string_utils.first_char_to_lower(self.class_name))) output_impl.write(_OBJC_SPACE) output_impl.write('{0}->_coreHandle = core{1}.Clone();\n'.format(string_utils.first_char_to_lower(self.class_name), self.class_name)) output_impl.write(_OBJC_SPACE) output_impl.write('return {0};\n}}'.format(string_utils.first_char_to_lower(self.class_name))) output_impl.write(_OBJC_BR) output_impl.write('@end\n')
def getter_impl(self): method_name = self.getter_method_name() para_name = ' (JNIEnv *env, jobject thiz, jlong handler)' step_1 = 'lesschat::{0}* {1} = reinterpret_cast<lesschat::{0}*>(handler);'\ .format(self.class_name, string_utils.first_char_to_lower(self.class_name)) cpp_return = '{0}->{1}()'.format(string_utils.first_char_to_lower(self.class_name), self.cpp_method()) step_2 = 'return {0};'.format(self.jni_variable_from_cpp_variable(cpp_return)) return method_name + '\n' + para_name + '{{\n {0}\n {1}\n}}'.format(step_1, step_2)
def __fetch_implementation(self, fetch_command, config): """Generates Objective-C++ fetch implementation. Args: fetch_command: A <FetchCommand> object represents necessary info for generating fetch implementation. config: A <Config> object represents user-defined info. Returns: A string which is Objective-C++ fetch implementation. """ by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if not fetch_command.is_plural: impl = '- (nullable {2}{0} *)fetch{0}FromCache{1} {{\n'\ .format(self.object_name, self.__convert_bys_to_string(by_list), config.objc_prefix) impl += string_utils.indent(2) impl += 'std::unique_ptr<{2}::{0}> core{0} = _coreManagerHandler->{1};\n'\ .format(self.object_name, self.__cpp_fetch_method_name(fetch_command), config.cpp_namespace) impl += string_utils.indent(2) impl += 'if (core{0}) {{\n'.format(self.object_name) impl += string_utils.indent(4) impl += 'return [{2}{0} {1}WithCore{0}:*core{0}];\n'\ .format(self.object_name, string_utils.first_char_to_lower(self.object_name), config.objc_prefix) impl += string_utils.indent(2) impl += '}\n' impl += string_utils.indent(2) impl += 'return nil;\n' impl += '}' return impl else: impl = '- (NSArray<LCC{0} *> *)fetch{1}FromCache{2} {{\n'\ .format(self.object_name, self.plural_object_name, self.__convert_bys_to_string(by_list)) impl += string_utils.indent(2) impl += 'NSMutableArray *{0} = [NSMutableArray array];\n'.format(string_utils.first_char_to_lower(self.plural_object_name)) impl += string_utils.indent(2) impl += 'std::vector<std::unique_ptr<{3}::{0}>> core{1} = _coreManagerHandler->{2};\n'\ .format(self.object_name, self.plural_object_name, self.__cpp_fetch_method_name(fetch_command), config.objc_prefix) impl += string_utils.indent(2) impl += 'for (auto it = core{0}.begin(); it != core{0}.end(); ++it) {{\n'.format(self.plural_object_name) impl += string_utils.indent(4) impl += '[{0} addObject:[LCC{1} {2}WithCore{1}:(**it)]];\n'\ .format(string_utils.first_char_to_lower(self.plural_object_name), self.object_name, string_utils.first_char_to_lower(self.object_name)) impl += string_utils.indent(2) impl += '}\n' impl += string_utils.indent(2) impl += 'return [{0} copy];\n'.format(string_utils.first_char_to_lower(self.plural_object_name)) impl += '}\n' self.impl = impl return self.impl
def __jni_get_jobject_by_core_object_implementation(self, config): impl = 'jobject JniHelper::GetJ{0}ByCore{0}(const {0}& {1}) {{\n'.format( self.__class_name, string_utils.cpp_class_name_to_cpp_file_name(self.__class_name)) impl += indent(2) + 'JNIEnv* env = GetJniEnv();\n' impl += indent(2) + 'if (!env) {\n' impl += indent(4) + 'sakura::log_error("Failed to get JNIEnv");\n' impl += indent(4) + 'return nullptr;\n' impl += indent(2) + '}\n\n' impl += indent(2) + 'jclass {0}Jclass = JniReferenceCache::SharedCache()->{1}_jclass();\n'.format( string_utils.first_char_to_lower(self.__class_name), string_utils.cpp_class_name_to_cpp_file_name(self.__class_name)) impl += indent(2) + 'jmethodID {0}ConstructorMethodID = env->GetMethodID({0}Jclass, "<init>", "('.format( string_utils.first_char_to_lower(self.__class_name)) for jni_var in self.__jni_var_list: impl += jni_var.var_type.to_jni_signature() impl += ')V");\n\n' for jni_var in self.__jni_var_list: impl += indent(2) + jni_var.jni_var_assignment_by_cpp_variable(config) + '\n' impl += '\n' constructor_fst_line = indent(2) + 'jobject j{0}Object = env->NewObject('.format(self.__class_name) num_constructor_indent = len(constructor_fst_line) impl += constructor_fst_line parameters = [] jclass_instance_name = '{0}Jclass'.format(string_utils.first_char_to_lower(self.__class_name)) constructor_method_id = '{0}ConstructorMethodID'.format(string_utils.first_char_to_lower(self.__class_name)) parameters.append(constructor_method_id) for jni_var in self.__jni_var_list: parameters.append('j{0}'.format(string_utils.to_title_style_name(jni_var.name))) impl += jclass_instance_name + ',\n' for parameter in parameters: impl += indent(num_constructor_indent) + parameter + ',\n' impl = impl[:-2] impl += ');' impl += '\n' for jni_var in self.__jni_var_list: delete_method = jni_var.jni_delete_local_ref() if delete_method != '': impl += indent(2) + delete_method + '\n' impl += '\n' impl += indent(2) + 'return j{0}Object;'.format(self.__class_name) impl += '\n' impl += '}\n' impl += '\n' return impl
def getter_impl(self): """Generate getter implementation before 5.0. Returns: A string which is native getter implementation before 5.0. """ method_name = self.__getter_method_name() para_name = ' (JNIEnv *env, jobject thiz, jlong handler)' step_1 = 'lesschat::{0}* {1} = reinterpret_cast<lesschat::{0}*>(handler);' \ .format(self.__class_name, string_utils.first_char_to_lower(self.__class_name)) cpp_return = '{0}->{1}()'.format(string_utils.first_char_to_lower(self.__class_name), self.cpp_method()) step_2 = 'return {0};'.format(self.jni_variable_from_cpp_variable(cpp_return)) return method_name + '\n' + para_name + '{{\n {0}\n {1}\n}}'.format(step_1, step_2)
def __fetch_implementation(self, fetch_command): by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if not fetch_command.is_plural: impl = '- (nullable LCC{0} *)fetch{0}FromCache{1} {{\n'\ .format(self.object_name, self.__convert_bys_to_string(by_list)) impl += string_utils.indent(2) impl += 'std::unique_ptr<lesschat::{0}> core{0} = _coreManagerHandler->{1};\n'.format( self.object_name, self.__cpp_fetch_method_name(fetch_command)) impl += string_utils.indent(2) impl += 'if (core{0}) {{\n'.format(self.object_name) impl += string_utils.indent(4) impl += 'return [LCC{0} {1}WithCore{0}:*core{0}];\n'.format( self.object_name, string_utils.first_char_to_lower(self.object_name)) impl += string_utils.indent(2) impl += '}\n' impl += string_utils.indent(2) impl += 'return nil;\n' impl += '}' return impl else: impl = '- (NSArray<LCC{0} *> *)fetch{1}FromCache{2} {{\n'\ .format(self.object_name, self.plural_object_name, self.__convert_bys_to_string(by_list)) impl += string_utils.indent(2) impl += 'NSMutableArray *{0} = [NSMutableArray array];\n'.format( string_utils.first_char_to_lower(self.plural_object_name)) impl += string_utils.indent(2) impl += 'std::vector<std::unique_ptr<lesschat::{0}>> core{1} = _coreManagerHandler->{2};\n'.format( self.object_name, self.plural_object_name, self.__cpp_fetch_method_name(fetch_command)) impl += string_utils.indent(2) impl += 'for (auto it = core{0}.begin(); it != core{0}.end(); ++it) {{\n'.format( self.plural_object_name) impl += string_utils.indent(4) impl += '[{0} addObject:[LCC{1} {2}WithCore{1}:(**it)]];\n'.format( string_utils.first_char_to_lower(self.plural_object_name), self.object_name, string_utils.first_char_to_lower(self.object_name)) impl += string_utils.indent(2) impl += '}\n' impl += string_utils.indent(2) impl += 'return [{0} copy];\n'.format( string_utils.first_char_to_lower(self.plural_object_name)) impl += '}\n' self.impl = impl return self.impl
def objc_form_cpp_parameter(self, indent, config): """From coreCalendar to LCCCallendar *calendar = [LCCCalendar calendarWithCoreCalendar:coreCalendar]; (Objc Array also is) """ objc_code = '' if self.var_type == VarType.cpp_object_array: objc_code += string_utils.indent(indent) objc_code += 'NSMutableArray *{0} = [NSMutableArray array];\n'.format( self.__objc_name()) objc_code += string_utils.indent(indent) objc_code += 'for (auto it = core{0}.begin(); it != core{0}.end(); ++it) {{\n'\ .format(self.to_title_style_name()) objc_code += string_utils.indent(2 + indent) objc_code += '[{0} addObject:[{3}{1} {2}WithCore{1}:**it]];\n'\ .format(self.__objc_name(), self.var_type.object_class_name, string_utils.first_char_to_lower(self.var_type.object_class_name), config.objc_prefix) objc_code += string_utils.indent(indent) objc_code += '}' elif self.var_type == VarType.cpp_object: objc_code += string_utils.indent(indent) objc_code += '{2}{0} *{1} = [LCC{0} {1}WithCore{0}:*core{0}];'\ .format(self.var_type.object_class_name, self.__objc_name(), config.objc_prefix) return objc_code
def generate_core_addition_header(self, config): """Generates Objective-C++ private header file. (eg: LCCTopic_CoreAddition.h) Args: config: A <Config> object represents user-defined configs, in this method, only apple/prefix is used. """ file_name = '{1}{0}_CoreAddition.h'.format(self.class_name, config.objc_prefix) file_path = _OBJC_BUILD_PATH + string_utils.cpp_group_name_to_objc_group_name(self.group_name) + '/' + file_name output_header = open(file_path, 'w') output_header.write('#include "{0}.h"'.format(cpp_class_name_to_cpp_file_name(self.class_name))) output_header.write(_OBJC_BR) output_header.write('@interface {1}{0} () {{'.format(self.class_name, config.objc_prefix)) output_header.write('\n') output_header.write(' @package') output_header.write('\n') output_header.write(_OBJC_SPACE) output_header.write('std::unique_ptr<{1}::{0}> _coreHandle;'.format(self.class_name, config.cpp_namespace)) output_header.write('\n') output_header.write('}') output_header.write(_OBJC_BR) output_header.write('+ (instancetype){0}WithCore{1}:(const {2}::{1}&)core{1};' .format(string_utils.first_char_to_lower(self.class_name), self.class_name, config.cpp_namespace)) output_header.write(_OBJC_BR) output_header.write('@end')
def __http_function(self, api): http_function = indent(4) + 'public void ' + string_utils.first_char_to_lower(api.function_name) input_variable = self.__input_variable_declarations(api.input_var_list) http_function += '({0}{1}response){{\n'\ .format(input_variable, self.__variable_type_from_var_list(api.output_var_list)) http_function += indent(8) + 'm{0}Response'.format(api.function_name) + ' = response;\n' for variable in api.input_var_list: if variable.var_type == VarType.cpp_enum: http_function += indent(8) + 'int {0} = {1}.getValue();\n'\ .format(variable.name_str + "_int", variable.name_str) if variable.var_type == VarType.cpp_object: http_function += indent(8) + 'long {0} = {1}.getNativeHandler();\n'\ .format(variable.name_str + '_handler', variable.name_str) if variable.var_type == VarType.cpp_object_array: http_function += indent(8) + 'long[] {0} = new long[{1}.size()];\n'\ .format(variable.name_str + '_handler', variable.name_str) http_function += indent(8) + 'for (int i = 0; i < {0}.size(); i++){{\n'.format(variable.name_str) http_function += indent(12) + '{0}[i] = {1}.get(i).getNativeHandler();\n'\ .format(variable.name_str + '_handler', variable.name_str) http_function += indent(8) + '}' input_variable_call = self.__input_variable_call(api.input_var_list) http_function += indent(8) + 'native{0}(mNativeHandler{1});\n'\ .format(api.function_name, input_variable_call) http_function += indent(4) + "}" return http_function
def generate_core_addition_header(self): file_name = 'LCC{0}_CoreAddition.h'.format(self.class_name) file_path = _OBJC_BUILD_PATH + string_utils.cpp_group_name_to_objc_group_name( self.group_name) + '/' + file_name output_header = open(file_path, 'w') output_header.write('#include "{0}.h"'.format( cpp_class_name_to_cpp_file_name(self.class_name))) output_header.write(_OBJC_BR) output_header.write('@interface LCC{0} () {{'.format(self.class_name)) output_header.write('\n') output_header.write('@package') output_header.write('\n') output_header.write(_OBJC_SPACE) output_header.write( 'std::unique_ptr<lesschat::{0}> _coreHandle;'.format( self.class_name)) output_header.write('\n') output_header.write('}') output_header.write(_OBJC_BR) output_header.write( '+ (instancetype){0}WithCore{1}:(const lesschat::{1}&)core{1};'. format(string_utils.first_char_to_lower(self.class_name), self.class_name)) output_header.write(_OBJC_BR) output_header.write('@end')
def generate_fetch_v2(self): """Gets fetch method implementation code. Paris with <generate_fetch_native_v2>. Returns: A string describes Java fetch method implementation code. """ fetch_function = '' for fetch_command in self.__fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if fetch_command.alias != '': fetch_fun_name = string_utils.first_char_to_lower( fetch_command.alias) fetch_fun_name_native = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'fetch{0}FromCache'.format( self.__java_object_name()) fetch_fun_name_native = 'Fetch{0}FromCache'.format( self.__object_name) else: fetch_fun_name = 'fetch{0}FromCache'.format( self.__plural_object_name) fetch_fun_name_native = 'Fetch{0}FromCache'.format( self.__plural_object_name) if not fetch_command.is_plural: # singular implementation if len(by_list) == 0: skr_log_warning( 'Singular often comes with at least one by parameter') fetch_function += indent(4) + '@Nullable\n' fetch_function += indent(4) + 'public {0} '.format( self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string( by_list, False, False) + '{\n' parameters = self.__convert_bys_to_input_parameters(by_list) parameters_with_type = self.__convert_bys_to_string( by_list, False, False) parameters_for_func = re.sub('\([^]]*\)', parameters, parameters_with_type) fetch_function += indent(8) + 'return native{0}{1};\n'.format( fetch_fun_name_native, parameters_for_func) fetch_function += indent(4) + '}' + _JAVA_BR else: # regular implementation fetch_function += indent(4) + 'public {0}[] '.format( self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string( by_list, False, False) + ' {\n' parameters = self.__convert_bys_to_input_parameters(by_list) parameters_with_type = self.__convert_bys_to_string( by_list, False, False) parameters_for_func = re.sub('\([^]]*\)', parameters, parameters_with_type) fetch_function += indent(8) + 'return native{0}{1};\n'.format( fetch_fun_name_native, parameters_for_func) fetch_function += indent(4) + '}' + _JAVA_BR return fetch_function
def generate_default_manager_implementation(self): impl = '+ (instancetype)defaultManager {\n' impl += _OBJC_SPACE impl += 'return [LCCDirector defaultDirector].{0}Manager;\n'.format( string_utils.first_char_to_lower(self.object_name)) impl += '}' return impl
def __http_function(self, api): http_function = indent( 4) + 'public void ' + string_utils.first_char_to_lower( api.function_name) input_variable = self.__input_variable_declarations(api.input_var_list) http_function += '({0} @NonNull {1}response){{\n'\ .format(input_variable, self.__variable_type_from_var_list(api.output_var_list)) http_function += indent(8) + 'm{0}Response'.format( api.function_name) + ' = response;\n' for variable in api.input_var_list: if variable.var_type == VarType.cpp_enum: http_function += indent(8) + 'int {0} = {1}.getValue();\n'\ .format(variable.name_str + "_int", variable.name_str) if variable.var_type == VarType.cpp_object: http_function += indent(8) + 'long {0} = {1}.getNativeHandler();\n'\ .format(variable.name_str + '_handler', variable.name_str) if variable.var_type == VarType.cpp_string_array: http_function += indent(2) + 'String[] {0} = new String[{1}.size()];\n'\ .format(variable.name_str + '_strs', variable.name_str) http_function += indent(2) + '{0}.toArray({1});\n'\ .format(variable.name_str, variable.name_str + '_strs') if variable.var_type == VarType.cpp_object_array: http_function += indent(8) + 'long[] {0} = new long[{1}.size()];\n'\ .format(variable.name_str + '_handler', variable.name_str) http_function += indent( 8) + 'for (int i = 0; i < {0}.size(); i++){{\n'.format( variable.name_str) http_function += indent(12) + '{0}[i] = {1}.get(i).getNativeHandler();\n'\ .format(variable.name_str + '_handler', variable.name_str) http_function += indent(8) + '}' input_variable_call = self.__input_variable_call(api.input_var_list) http_function += indent(8) + 'native{0}(mNativeHandler{1});\n'\ .format(api.function_name, input_variable_call) http_function += indent(4) + "}" return http_function
def assignment(self): """Gets assignment implementation. Returns: A string which is assignment. For example: mTaskId = taskId; """ return 'm{0} = {1};'.format(self.__title_style_name, first_char_to_lower(self.__title_style_name))
def generate_implementation(self): file_name = 'LCC{0}.mm'.format(self.class_name) file_path = _OBJC_BUILD_PATH + string_utils.cpp_group_name_to_objc_group_name( self.group_name) + '/' + file_name output_impl = open(file_path, 'w') output_impl.write( '#if !defined(__has_feature) || !__has_feature(objc_arc)\n#error "This file requires ARC support."\n#endif' ) output_impl.write(_OBJC_BR) output_impl.write( '#import "LCC{0}.h"\n#import "LCC{0}_CoreAddition.h"\n\n#import "LCCObjcAdapter.h"' .format(self.class_name)) output_impl.write(_OBJC_BR) output_impl.write('@implementation LCC{0}'.format(self.class_name)) output_impl.write(_OBJC_BR) output_impl.write('#pragma mark - Property') output_impl.write(_OBJC_BR) for objc_var in self.objc_var_list: output_impl.write(objc_var.getter_impl()) output_impl.write(_OBJC_BR) output_impl.write(objc_var.setter_impl()) output_impl.write(_OBJC_BR) output_impl.write('#pragma mark - Core Addition') output_impl.write(_OBJC_BR) output_impl.write( '+ (instancetype){0}WithCore{1}:(const lesschat::{1}&)core{1} {{\n' .format(string_utils.first_char_to_lower(self.class_name), self.class_name)) output_impl.write(_OBJC_SPACE) output_impl.write('LCC{0} *{1} = [[LCC{0} alloc] init];\n'.format( self.class_name, string_utils.first_char_to_lower(self.class_name))) output_impl.write(_OBJC_SPACE) output_impl.write('{0}->_coreHandle = core{1}.Clone();\n'.format( string_utils.first_char_to_lower(self.class_name), self.class_name)) output_impl.write(_OBJC_SPACE) output_impl.write('return {0};\n}}'.format( string_utils.first_char_to_lower(self.class_name))) output_impl.write(_OBJC_BR) output_impl.write('@end\n')
def generate_implementation(self, config): """Generates Objective-C++ object header file. Args: config: A <Config> object represents user-defined configs, in this method, only apple/prefix is used. """ file_name = '{1}{0}.mm'.format(self.class_name, config.objc_prefix) file_path = _OBJC_BUILD_PATH + string_utils.cpp_group_name_to_objc_group_name(self.group_name) + '/' + file_name output_impl = open(file_path, 'w') output_impl.write('#if !defined(__has_feature) || !__has_feature(objc_arc)\n#error "This file requires ARC support."\n#endif') output_impl.write(_OBJC_BR) output_impl.write('#import "{1}{0}.h"\n#import "{1}{0}_CoreAddition.h"\n\n#import "{1}ObjcAdapter.h"'.format( self.class_name, config.objc_prefix)) output_impl.write(_OBJC_BR) output_impl.write('@implementation {1}{0}'.format(self.class_name, config.objc_prefix)) output_impl.write(_OBJC_BR) output_impl.write('#pragma mark - Property') output_impl.write(_OBJC_BR) for objc_var in self.objc_var_list: output_impl.write(objc_var.getter_impl(config)) output_impl.write(_OBJC_BR) output_impl.write(objc_var.setter_impl(config)) output_impl.write(_OBJC_BR) output_impl.write('#pragma mark - Core Addition') output_impl.write(_OBJC_BR) output_impl.write('+ (instancetype){0}WithCore{1}:(const {2}::{1}&)core{1} {{\n' .format(string_utils.first_char_to_lower(self.class_name), self.class_name, config.cpp_namespace)) output_impl.write(_OBJC_SPACE) output_impl.write('{2}{0} *{1} = [[{2}{0} alloc] init];\n' .format(self.class_name, string_utils.first_char_to_lower(self.class_name), config.objc_prefix)) output_impl.write(_OBJC_SPACE) output_impl.write('{0}->_coreHandle = core{1}.Clone();\n' .format(string_utils.first_char_to_lower(self.class_name), self.class_name)) output_impl.write(_OBJC_SPACE) output_impl.write('return {0};\n}}'.format(string_utils.first_char_to_lower(self.class_name))) output_impl.write(_OBJC_BR) output_impl.write('@end\n')
def input_parameter_name(self): """Gets Android style Java input parameter name. Returns: A string which is Java input parameter name. For example: String teamName """ return '{0} {1}'.format(self.__var_type.to_java_getter_setter_string_v2(), first_char_to_lower(self.__title_style_name))
def generate_fetch(self): """Gets fetch method implementation code. Paris with <generate_fetch_native>. New development should use <generate_fetch_v2>. Returns: A string describes Java fetch method implementation code. """ fetch_function = '' for fetch_command in self.__fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if fetch_command.alias != '': fetch_fun_name = string_utils.first_char_to_lower(fetch_command.alias) fetch_fun_name_native = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'fetch{0}FromCache'.format(self.__java_object_name()) fetch_fun_name_native = 'Fetch{0}FromCache'.format(self.__object_name) else: fetch_fun_name = 'fetch{0}FromCache'.format(self.__plural_object_name) fetch_fun_name_native = 'Fetch{0}FromCache'.format(self.__plural_object_name) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') fetch_function += indent(4) + 'public {0} '.format(self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string(by_list, False, False) + '{\n' fetch_function += indent(8) + 'long handler = native' + fetch_fun_name_native fetch_function += self.__convert_bys_to_string(by_list, False, True) + ';\n\n' fetch_function += indent(8) + 'if (handler == JniHelper.sNullPointer) {\n' fetch_function += indent(12) + 'return null;\n' fetch_function += indent(8) + '}\n\n' fetch_function += indent(8) + 'return new {0}(handler);\n'.format(self.__java_object_name()) fetch_function += indent(4) + '}' + _JAVA_BR else: fetch_function += indent(4) + 'public List<{0}> '.format(self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string(by_list, False, False) + ' {\n' fetch_function += indent(8) + 'long[] handlers = native' + fetch_fun_name_native fetch_function += self.__convert_bys_to_string(by_list, False, True) + ';\n\n' fetch_function += indent(8) + 'List<{0}> {1} = new ArrayList<>();\n'\ .format(self.__java_object_name(), self.__to_object_name_java_style() + 's') fetch_function += indent(8) + 'for (long handler: handlers) {\n' fetch_function += indent(12) + '{0}.add(new {1}(handler));\n'\ .format(self.__to_object_name_java_style() + 's', self.__java_object_name()) fetch_function += indent(8) + '}\n\n' fetch_function += indent(8) + 'return {0};\n'\ .format(self.__to_object_name_java_style() + 's') fetch_function += indent(4) + '}' + _JAVA_BR return fetch_function
def __fetch_implementation(self, fetch_command): by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if not fetch_command.is_plural: impl = '- (nullable LCC{0} *)fetch{0}FromCache{1} {{\n'\ .format(self.object_name, self.__convert_bys_to_string(by_list)) impl += string_utils.indent(2) impl += 'std::unique_ptr<lesschat::{0}> core{0} = _coreManagerHandler->{1};\n'.format(self.object_name, self.__cpp_fetch_method_name(fetch_command)) impl += string_utils.indent(2) impl += 'if (core{0}) {{\n'.format(self.object_name) impl += string_utils.indent(4) impl += 'return [LCC{0} {1}WithCore{0}:*core{0}];\n'.format(self.object_name, string_utils.first_char_to_lower(self.object_name)) impl += string_utils.indent(2) impl += '}\n' impl += string_utils.indent(2) impl += 'return nil;\n' impl += '}' return impl else: impl = '- (NSArray<LCC{0} *> *)fetch{1}FromCache{2} {{\n'\ .format(self.object_name, self.plural_object_name, self.__convert_bys_to_string(by_list)) impl += string_utils.indent(2) impl += 'NSMutableArray *{0} = [NSMutableArray array];\n'.format(string_utils.first_char_to_lower(self.plural_object_name)) impl += string_utils.indent(2) impl += 'std::vector<std::unique_ptr<lesschat::{0}>> core{1} = _coreManagerHandler->{2};\n'.format(self.object_name, self.plural_object_name, self.__cpp_fetch_method_name(fetch_command)) impl += string_utils.indent(2) impl += 'for (auto it = core{0}.begin(); it != core{0}.end(); ++it) {{\n'.format(self.plural_object_name) impl += string_utils.indent(4) impl += '[{0} addObject:[LCC{1} {2}WithCore{1}:(**it)]];\n'.format(string_utils.first_char_to_lower(self.plural_object_name), self.object_name, string_utils.first_char_to_lower(self.object_name)) impl += string_utils.indent(2) impl += '}\n' impl += string_utils.indent(2) impl += 'return [{0} copy];\n'.format(string_utils.first_char_to_lower(self.plural_object_name)) impl += '}\n' self.impl = impl return self.impl
def generate_default_manager_implementation(self, config): """Generates Objective-C++ default manager method. Args: config: A <Config> object represents user-defined info. Returns: Objective-C++ default manager method. """ impl = '+ (instancetype)defaultManager {\n' impl += _OBJC_SPACE impl += 'return [{1}Director defaultDirector].{0}Manager;\n'.format( string_utils.first_char_to_lower(self.object_name), config.objc_prefix) impl += '}' return impl
def generate_fetch_v2(self): """Gets fetch method implementation code. Paris with <generate_fetch_native_v2>. Returns: A string describes Java fetch method implementation code. """ fetch_function = '' for fetch_command in self.__fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if fetch_command.alias != '': fetch_fun_name = string_utils.first_char_to_lower(fetch_command.alias) fetch_fun_name_native = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'fetch{0}FromCache'.format(self.__java_object_name()) fetch_fun_name_native = 'Fetch{0}FromCache'.format(self.__object_name) else: fetch_fun_name = 'fetch{0}FromCache'.format(self.__plural_object_name) fetch_fun_name_native = 'Fetch{0}FromCache'.format(self.__plural_object_name) if not fetch_command.is_plural: # singular implementation if len(by_list) == 0: skr_log_warning('Singular often comes with at least one by parameter') fetch_function += indent(4) + '@Nullable\n' fetch_function += indent(4) + 'public {0} '.format(self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string(by_list, False, False) + '{\n' parameters = self.__convert_bys_to_input_parameters(by_list) parameters_with_type = self.__convert_bys_to_string(by_list, False, False) parameters_for_func = re.sub('\([^]]*\)', parameters, parameters_with_type) fetch_function += indent(8) + 'return native{0}{1};\n'.format(fetch_fun_name_native, parameters_for_func) fetch_function += indent(4) + '}' + _JAVA_BR else: # regular implementation fetch_function += indent(4) + 'public {0}[] '.format(self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string(by_list, False, False) + ' {\n' parameters = self.__convert_bys_to_input_parameters(by_list) parameters_with_type = self.__convert_bys_to_string(by_list, False, False) parameters_for_func = re.sub('\([^]]*\)', parameters, parameters_with_type) fetch_function += indent(8) + 'return native{0}{1};\n'.format(fetch_fun_name_native, parameters_for_func) fetch_function += indent(4) + '}' + _JAVA_BR return fetch_function
def generate_core_addition_header(self): file_name = 'LCC{0}_CoreAddition.h'.format(self.class_name) file_path = _OBJC_BUILD_PATH + string_utils.cpp_group_name_to_objc_group_name(self.group_name) + '/' + file_name output_header = open(file_path, 'w') output_header.write('#include "{0}.h"'.format(CppClass.convert_class_name_to_file_name(self.class_name))) output_header.write(_OBJC_BR) output_header.write('@interface LCC{0} () {{'.format(self.class_name)) output_header.write('\n') output_header.write('@package') output_header.write('\n') output_header.write(_OBJC_SPACE) output_header.write('std::unique_ptr<lesschat::{0}> _coreHandle;'.format(self.class_name)) output_header.write('\n') output_header.write('}') output_header.write(_OBJC_BR) output_header.write('+ (instancetype){0}WithCore{1}:(const lesschat::{1}&)core{1};' .format(string_utils.first_char_to_lower(self.class_name), self.class_name)) output_header.write(_OBJC_BR) output_header.write('@end')
def objc_form_cpp_parameter(self, indent): objc_code = '' if self.var_type == VarType.cpp_object_array: objc_code += string_utils.indent(indent) objc_code += 'NSMutableArray *{0} = [NSMutableArray array];\n'.format( self.__objc_name()) objc_code += string_utils.indent(indent) objc_code += 'for (auto it = core{0}.begin(); it != core{0}.end(); ++it) {{\n'.format( self.to_title_style_name()) objc_code += string_utils.indent(2 + indent) objc_code += '[{0} addObject:[LCC{1} {2}WithCore{1}:**it]];\n'.format( self.__objc_name(), self.var_type.object_class_name, string_utils.first_char_to_lower( self.var_type.object_class_name)) objc_code += string_utils.indent(indent) objc_code += '}' elif self.var_type == VarType.cpp_object: objc_code += string_utils.indent(indent) objc_code += 'LCC{0} *{1} = [LCC{0} {1}WithCore{0}:*core{0}];'.format( self.var_type.object_class_name, self.__objc_name()) return objc_code
def objc_form_cpp_parameter(self, indent, config): """From coreCalendar to LCCCallendar *calendar = [LCCCalendar calendarWithCoreCalendar:coreCalendar]; (Objc Array also is) """ objc_code = '' if self.var_type == VarType.cpp_object_array: objc_code += string_utils.indent(indent) objc_code += 'NSMutableArray *{0} = [NSMutableArray array];\n'.format(self.__objc_name()) objc_code += string_utils.indent(indent) objc_code += 'for (auto it = core{0}.begin(); it != core{0}.end(); ++it) {{\n'\ .format(self.to_title_style_name()) objc_code += string_utils.indent(2 + indent) objc_code += '[{0} addObject:[{3}{1} {2}WithCore{1}:**it]];\n'\ .format(self.__objc_name(), self.var_type.object_class_name, string_utils.first_char_to_lower(self.var_type.object_class_name), config.objc_prefix) objc_code += string_utils.indent(indent) objc_code += '}' elif self.var_type == VarType.cpp_object: objc_code += string_utils.indent(indent) objc_code += '{2}{0} *{1} = [LCC{0} {1}WithCore{0}:*core{0}];'\ .format(self.var_type.object_class_name, self.__objc_name(), config.objc_prefix) return objc_code
def __http_function_v2(self, api): http_function = indent( 4) + 'public void ' + string_utils.first_char_to_lower( api.function_name) input_variable = self.__input_variable_declarations(api.input_var_list) listener_name = self.__http_response_listener(api) input_variable += listener_name + ' success' input_variable += ', OnFailureListener failure' http_function += '(' + input_variable + ') {\n' for variable in api.input_var_list: if variable.var_type == VarType.cpp_enum: http_function += indent(8) + 'int {0} = {1}.getValue();\n' \ .format(variable.name_str + "_int", variable.name_str) if variable.var_type == VarType.cpp_object: http_function += indent(8) + 'long {0} = {1}.getNativeHandler();\n' \ .format(variable.name_str + '_handler', variable.name_str) if variable.var_type == VarType.cpp_string_array: http_function += indent(2) + 'String[] {0} = new String[{1}.size()];\n' \ .format(variable.name_str + '_strs', variable.name_str) http_function += indent(2) + '{0}.toArray({1});\n' \ .format(variable.name_str, variable.name_str + '_strs') if variable.var_type == VarType.cpp_object_array: http_function += indent(8) + 'long[] {0} = new long[{1}.size()];\n' \ .format(variable.name_str + '_handler', variable.name_str) http_function += indent( 8) + 'for (int i = 0; i < {0}.size(); i++){{\n'.format( variable.name_str) http_function += indent(12) + '{0}[i] = {1}.get(i).getNativeHandler();\n' \ .format(variable.name_str + '_handler', variable.name_str) http_function += indent(8) + '}' input_variable_call = self.__input_variable_call(api.input_var_list) input_variable_call += ', success, failure' http_function += indent(8) + 'native{0}(mNativeHandler{1});\n' \ .format(api.function_name, input_variable_call) http_function += indent(4) + "}" return http_function
def generate_default_manager_implementation(self): impl = '+ (instancetype)defaultManager {\n' impl += _OBJC_SPACE impl += 'return [LCCDirector defaultDirector].{0}Manager;\n'.format(string_utils.first_char_to_lower(self.object_name)) impl += '}' return impl
def generate_fetch(self): """Gets fetch method implementation code. Paris with <generate_fetch_native>. New development should use <generate_fetch_v2>. Returns: A string describes Java fetch method implementation code. """ fetch_function = '' for fetch_command in self.__fetch_commands: by_list = [] if fetch_command.where != '': by_list = re.split(',', fetch_command.where) if fetch_command.alias != '': fetch_fun_name = string_utils.first_char_to_lower( fetch_command.alias) fetch_fun_name_native = fetch_command.alias elif not fetch_command.is_plural: fetch_fun_name = 'fetch{0}FromCache'.format( self.__java_object_name()) fetch_fun_name_native = 'Fetch{0}FromCache'.format( self.__object_name) else: fetch_fun_name = 'fetch{0}FromCache'.format( self.__plural_object_name) fetch_fun_name_native = 'Fetch{0}FromCache'.format( self.__plural_object_name) if not fetch_command.is_plural: if len(by_list) == 0: skr_log_warning( 'Singular often comes with at least one by parameter') fetch_function += indent(4) + 'public {0} '.format( self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string( by_list, False, False) + '{\n' fetch_function += indent( 8) + 'long handler = native' + fetch_fun_name_native fetch_function += self.__convert_bys_to_string( by_list, False, True) + ';\n\n' fetch_function += indent( 8) + 'if (handler == JniHelper.sNullPointer) {\n' fetch_function += indent(12) + 'return null;\n' fetch_function += indent(8) + '}\n\n' fetch_function += indent( 8) + 'return new {0}(handler);\n'.format( self.__java_object_name()) fetch_function += indent(4) + '}' + _JAVA_BR else: fetch_function += indent(4) + 'public List<{0}> '.format( self.__java_object_name()) fetch_function += fetch_fun_name + self.__convert_bys_to_string( by_list, False, False) + ' {\n' fetch_function += indent( 8) + 'long[] handlers = native' + fetch_fun_name_native fetch_function += self.__convert_bys_to_string( by_list, False, True) + ';\n\n' fetch_function += indent(8) + 'List<{0}> {1} = new ArrayList<>();\n'\ .format(self.__java_object_name(), self.__to_object_name_java_style() + 's') fetch_function += indent( 8) + 'for (long handler: handlers) {\n' fetch_function += indent(12) + '{0}.add(new {1}(handler));\n'\ .format(self.__to_object_name_java_style() + 's', self.__java_object_name()) fetch_function += indent(8) + '}\n\n' fetch_function += indent(8) + 'return {0};\n'\ .format(self.__to_object_name_java_style() + 's') fetch_function += indent(4) + '}' + _JAVA_BR return fetch_function
def objc_form_cpp_parameter(self, indent): objc_code = '' if self.var_type == VarType.cpp_object_array: objc_code += string_utils.indent(indent) objc_code += 'NSMutableArray *{0} = [NSMutableArray array];\n'.format(self.__objc_name()) objc_code += string_utils.indent(indent) objc_code += 'for (auto it = core{0}.begin(); it != core{0}.end(); ++it) {{\n'.format(self.to_title_style_name()) objc_code += string_utils.indent(2 + indent) objc_code += '[{0} addObject:[LCC{1} {2}WithCore{1}:**it]];\n'.format(self.__objc_name(), self.var_type.object_class_name, string_utils.first_char_to_lower(self.var_type.object_class_name)) objc_code += string_utils.indent(indent) objc_code += '}' elif self.var_type == VarType.cpp_object: objc_code += string_utils.indent(indent) objc_code += 'LCC{0} *{1} = [LCC{0} {1}WithCore{0}:*core{0}];'.format(self.var_type.object_class_name, self.__objc_name()) return objc_code