wiki:EFLWebKitCodingStyle

Summary

C/C++

  • EFL port should adhere to the WebKit Coding Style basically.(http://www.webkit.org/coding/coding-style.html)
  • Do not use abbreviation except for public APIs
  • Do not use Eina_Bool type except for public APIs and callback function for EFL event handler.
  • Place '*' operator to data type
  • Use C++ casting for type casting. For example, static_cast<...>, const_cast<...>, reinterpret_cast<...>
  • Use smart pointers, like WKEinaSharedString, OwnPtr, RefPtr, etc.
  • Use c++ new/delete operators
  • Use NULL in public C headers instead of 0
  • Use a double negation (!!) when you modify or compare with a Eina Bool member variable which stores a value in a bit field.
  • *_ref() functions should return the ref'd object

CMake

  • Use spaces, not tabs.
  • Four spaces as indent.
  • Place one space between control statements and their parentheses. For eg, if (), else (), elseif (), endif (), foreach (), endforeach (), while (), endwhile (), break ().
  • Do not place spaces between function and macro statements and their parentheses. For eg, macro(), endmacro(), function(), endfunction().
  • Do not place spaces between a command or function or macro and its parentheses, or between a parenthesis and its content. For eg, message("testing") not message( "testing") or message ("testing" )
  • No space at line ending.
  • Lowercase when call commands macros and functions. For eg, add_executable() not ADD_EXECUTABLE(), set() not SET().

Example

Do not use abbreviation except for public APIs

Public APIs

 EAPI void ewk_view_bg_color_set(Evas_Object *o, int r, int g, int b, int a);

Implementation

 void ewk_view_bg_color_set(Evas_Object* ewkView, int red, int green, int blue, int alpha)
 {
     EWK_VIEW_SD_GET_OR_RETURN(ewkView, smartData);
     EINA_SAFETY_ON_NULL_RETURN(smartData->api);
     EINA_SAFETY_ON_NULL_RETURN(smartData->api->bg_color_set);
 
     if (alpha < 0) {
         WRN("Alpha less than zero (%d).", alpha);
         alpha = 0;
     } else if (alpha > 255) {
         WRN("Alpha is larger than 255 (%d).", alpha);
         alpha = 255;
     }
  ...
 }

Do not use Eina_Bool type except for public APIs and callback function for EFL event handler

Public APIs

EAPI Eina_Bool ewk_view_scale_set(Evas_Object *o, float scale_factor, Evas_Coord cx, Evas_Coord cy);

Implementation

Eina_Bool ewk_view_scale_set(Evas_Object* ewkView, float scaleFactor, Evas_Coord centerX, Evas_Coord centerY)
{
    EWK_VIEW_SD_GET_OR_RETURN(ewkView, smartData, false);
    EWK_VIEW_PRIV_GET_OR_RETURN(smartData, priv, false);
 
    float currentScaleFactor = ewk_view_scale_get(ewkView);
    if (currentScaleFactor == -1)
        return false;
    int x, y;
    ewk_frame_scroll_pos_get(smartData->main_frame, &x, &y);
 
    x = static_cast<int>(((x + centerX) / currentScaleFactor) * scaleFactor) - centerX;
    y = static_cast<int>(((y + centerY) / currentScaleFactor) * scaleFactor) - centerY;
    priv->page->setPageScaleFactor(scaleFactor, WebCore::LayoutPoint(x, y));
    return true;
}

Callback function for EFL event handler

Eina_Bool GamepadDeviceEfl::readCallback(void* userData, Ecore_Fd_Handler* fdHandler)
{
    GamepadDeviceEfl* gamepadDevice = static_cast<GamepadDeviceEfl*>(userData);
    ...
    return ECORE_CALLBACK_RENEW;
}

GamepadDeviceEfl::GamepadDeviceEfl(const String& deviceFile)
    : GamepadDeviceLinux(deviceFile)
    , m_fdHandler(0)
    , m_deviceFile(deviceFile)
{
    m_fdHandler = ecore_main_fd_handler_add(m_fileDescriptor, ECORE_FD_READ, readCallback, this, 0, 0);
   ...
}

Use "void" for empty parameter of public APIs

EAPI Ewk_Context *ewk_context_new(void);

Wrong

EAPI Ewk_Context *ewk_context_new();

Place '*' operator to data type

Right

static void _ewk_view_smart_show(Evas_Object* ewkView)

Wrong

static void _ewk_view_smart_show(Evas_Object *ewkView)

Use C++ casting for type casting

Right

 Ewk_View_Smart_Data* smartData = static_cast<Ewk_View_Smart_Data*>(data);

Wrong

 Ewk_View_Smart_Data* smartData = (Ewk_View_Smart_Data*)data;

Use smart pointers

Right

 RefPtr<cairo_region_t> dirtyRegion = adoptRef(cairo_region_create_rectangle(&rect));

 or 

 OwnPtr<WebCore::Page> page = adoptPtr(new WebCore::Page(pageClients));

Wrong

 cairo_region_t* dirtyRegion = cairo_region_create_rectangle(&rect);
 ...
 cairo_region_destroy(dirtyRegion);

 or

 WebCore::Page* page = new WebCore::Page(pageClients);
 ...
 delete page;

Use c++ new/delete operators

Right

Where it is not possible to use smart pointers. Use new/delete operators.

 unusedCacheEntry = new Ewk_Tile_Unused_Cache_Entry;
 ...
 delete unusedCacheEntry;

Wrong

 unusedCacheEntry = static_cast<Ewk_Tile_Unused_Cache_Entry*>(malloc(sizeof(Ewk_Tile_Unused_Cache_Entry)));
 ...
 free(unusedCacheEntry);

Use NULL in public C headers instead of 0 (but keep using 0 in implementations and in private headers).

Right

/**
 * Query action for this intent.
 *
 * @param intent intent object to query
 *
 * @return the intent action if successful or @c NULL otherwise
 */
EAPI const char *ewk_intent_action_get(const Ewk_Intent *intent);

Wrong

/**
 * Query action for this intent.
 *
 * @param intent intent object to query.
 *
 * @return the intent action if successful or @c 0 otherwise.
 */
EAPI const char *ewk_intent_action_get(const Ewk_Intent *intent);

Use a double negation when you modify or compare with a Eina Bool member variable which stores a value in a bit field.

Eina_Bool is an unsigned char typedef, which means one can in theory pass a value such as 3 or 42 to it. This can be problematic if one is storing an Eina_Bool in a bitfield, since only the last bit will be used. For example:

struct MyStruct {
    Eina_Bool b : 1;
};

void foo()
{
    Eina_Bool v1 = 2, v2 = 3;
    MyStruct s;

    s.b = v1; // b is 0, since the least significant bit is 0 (2 == 10 in binary)
    s.b = v2; // b is 1, since the least significant bit is 1 (3 == 11 in binary)
}

On the other hand, if an Eina_Bool is simply being obtained from some publicly-visible API and passed to another function that takes a bool, or if a bitfield is made from bools instead of Eina_Bools, this does not happen, since, according to the C++ standard, any value that is not zero, a null pointer or a null member pointer is converted to true.

Right

struct _Ewk_View_Private_Data {
    ...
    Eina_Bool pageCache : 1;
}

Eina_Bool ewk_view_setting_page_cache_set(Evas_Object* ewkView, Eina_Bool enable)
{
    EWK_VIEW_SD_GET_OR_RETURN(ewkView, smartData, false);
    EWK_VIEW_PRIV_GET_OR_RETURN(smartData, priv, false);
    enable = !!enable;
    if (priv->settings.pageCache != enable) {
        priv->pageSettings->setUsesPageCache(enable);
        priv->settings.pageCache = enable;
    }
    return true;
}

Wrong

Eina_Bool ewk_settings_auto_load_images_set(Ewk_Settings* settings, Eina_Bool automatic)
{
    EINA_SAFETY_ON_NULL_RETURN_VAL(settings, false);

    automatic = !!automatic;
    WKPreferencesSetLoadsImagesAutomatically(settings->preferences.get(), automatic);

    return true;
}

*_ref() functions should return the ref'd object.

This makes them more convenient to use and results in less code.

Right

EAPI Ewk_Web_Resource *ewk_web_resource_ref(Ewk_Web_Resource *resource);
[...]
resources.add(resourceID, ewk_web_resource_ref(ewkResource));

Wrong

EAPI void ewk_web_resource_ref(Ewk_Web_Resource *resource);
[...]
ewk_web_resource_ref(ewkResource)
resources.add(resourceID, ewkResource);
Last modified 12 years ago Last modified on Dec 3, 2012, 7:19:08 PM
Note: See TracWiki for help on using the wiki.