{"thread":{"id":"18075","subject":"coding style: #ifdef blocks and real C blocks","startedAt":"2009-03-01T08:52:49Z","lastAt":"2009-03-01T09:33:02Z","messageCount":4,"participants":["Tay Ray Chuan","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"106591","messageId":"be6fef0d0903010052t50551f3w74352b69afdee620@mail.gmail.com","threadId":"18075","inReplyTo":null,"subject":"coding style: #ifdef blocks and real C blocks","fromName":"Tay Ray Chuan","fromEmail":"rctay89@gmail.com","sentAt":"2009-03-01T08:52:49Z","receivedAt":"2009-03-01T08:52:49Z","isPatch":false,"sender":{"key":"rctay89@gmail.com","avatar":"https://avatars.githubusercontent.com/u/61553?v=4"},"body":"Hi,\n\nI couldn't find any mention of #ifdef usage nor any example of how I'm\nthinking of using #ifdef, so I'm posing this here.\n\nRight now here's what I have on my local branch:\n\n---\n#ifdef USE_CURL_MULTI\n\tslot = get_active_multi_slot();\n#else\n\tslot = get_active_slot();\n#endif\n\tslot->callback_func = process_response;\n\tslot->callback_data = request;\n\trequest->slot = slot;\n---\n\nI want to implement an option that's stored in the variable\n\"persistent_connection\", which basically makes the code behave as if\nUSE_CURL_MULTI isn't defined. (This would make git open/close less\nconnections throughout the lifespan of its execution, which would\nremove/minimize the number of credential prompting if authentication\nis required, among other advantages.)\n\nHere's what I thought of:\n\n---\n#ifdef USE_CURL_MULTI\n\tif (!persistent_connection)\n\t\tslot = get_active_multi_slot();\n\telse\n\t\tslot = get_active_slot();\n#else\n\tslot = get_active_slot();\n#endif\n---\n\nI thought of shortening this further to\n\n---\n#ifdef USE_CURL_MULTI\nif (!persistent_connection)\n\tslot = get_active_multi_slot();\nelse\n#else\n\tslot = get_active_slot();\n#endif\n\tslot->callback_func = process_response;\n\tslot->callback_data = request;\n\trequest->slot = slot;\n---\n\nWhat would you suggest?\n\n-- \nCheers,\nRay Chuan\n"},{"id":"106593","messageId":"7vbpsl8v7r.fsf@gitster.siamese.dyndns.org","threadId":"18075","inReplyTo":"be6fef0d0903010052t50551f3w74352b69afdee620@mail.gmail.com","subject":"Re: coding style: #ifdef blocks and real C blocks","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-03-01T09:10:16Z","receivedAt":"2009-03-01T09:10:16Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Tay Ray Chuan <rctay89@gmail.com> writes:\n\n> #ifdef USE_CURL_MULTI\n> \tslot = get_active_multi_slot();\n> #else\n> \tslot = get_active_slot();\n> #endif\n> \tslot->callback_func = process_response;\n> \tslot->callback_data = request;\n> \trequest->slot = slot;\n\nHow about doing something like this:\n\n    #ifdef USE_CURL_MULTI\n    #define active_slot_get get_active_multi_slot\n    #else\n    #define active_slot_get get_active_slot\n    #endif\n\nso that the code itself would not have to have any #ifdef?\n\n    slot = active_slot_get()\n    slot->callback_func = process_response;\n    slot->callback_data = request;\n    request->slot = slot;\n    \n> #ifdef USE_CURL_MULTI\n> \tif (!persistent_connection)\n> \t\tslot = get_active_multi_slot();\n> \telse\n> \t\tslot = get_active_slot();\n> #else\n> \tslot = get_active_slot();\n> #endif\n\nSimilarly, with something like this:\n\n    #ifdef USE_CURL_MULTI\n    slot active_persistent_slot() {\n            return persistent_connection ? get_active_slot() : get_active_multi_slot();\n    }\n    #else\n    slot active_persistent_slot() {\n            return get_active_slot();\n    }\n    #endif\n\nthe call site can be #ifdef free, no?\n"},{"id":"106594","messageId":"be6fef0d0903010127q7b1491d6o8cdb48acdc674603@mail.gmail.com","threadId":"18075","inReplyTo":"7vbpsl8v7r.fsf@gitster.siamese.dyndns.org","subject":"Re: coding style: #ifdef blocks and real C blocks","fromName":"Tay Ray Chuan","fromEmail":"rctay89@gmail.com","sentAt":"2009-03-01T09:27:25Z","receivedAt":"2009-03-01T09:27:25Z","isPatch":false,"sender":{"key":"rctay89@gmail.com","avatar":"https://avatars.githubusercontent.com/u/61553?v=4"},"body":"Hi,\n\nOn Sun, Mar 1, 2009 at 5:10 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> How about doing something like this:\n>\n>    #ifdef USE_CURL_MULTI\n>    #define active_slot_get get_active_multi_slot\n>    #else\n>    #define active_slot_get get_active_slot\n>    #endif\n>\n\nNice.\n\n> Similarly, with something like this:\n>\n>    #ifdef USE_CURL_MULTI\n>    slot active_persistent_slot() {\n>            return persistent_connection ? get_active_slot() : get_active_multi_slot();\n>    }\n>    #else\n>    slot active_persistent_slot() {\n>            return get_active_slot();\n>    }\n>    #endif\n>\n> the call site can be #ifdef free, no?\n>\n\nHmm, so I just do \"slot = active_persistent_slot()\" ?\n\n-- \nCheers,\nRay Chuan\n"},{"id":"106595","messageId":"be6fef0d0903010133s7343a96ekd8939695a65ba4a2@mail.gmail.com","threadId":"18075","inReplyTo":"7vbpsl8v7r.fsf@gitster.siamese.dyndns.org","subject":"Re: coding style: #ifdef blocks and real C blocks","fromName":"Tay Ray Chuan","fromEmail":"rctay89@gmail.com","sentAt":"2009-03-01T09:33:02Z","receivedAt":"2009-03-01T09:33:02Z","isPatch":false,"sender":{"key":"rctay89@gmail.com","avatar":"https://avatars.githubusercontent.com/u/61553?v=4"},"body":"On Sun, Mar 1, 2009 at 5:10 PM, Junio C Hamano <gitster@pobox.com> wrote:\n>    #ifdef USE_CURL_MULTI\n>    #define active_slot_get get_active_multi_slot\n>    #else\n>    #define active_slot_get get_active_slot\n>    #endif\n>\n> so that the code itself would not have to have any #ifdef?\n\nOn further thought, wouldn't it be a better idea to make this\nuppercase, ie. GET_ACTIVE_SLOT, to make it more obvious this is a\nmacro? Then we wouldn't have to jumble the name into \"active_slot_get\"\nto differentiate it from the two slot functions.\n\n-- \nCheers,\nRay Chuan\n"}]}