wiki:EFLWebKitCodingStyle

Version 12 (modified by gyuyoung.kim@samsung.com, 12 years ago) ( diff )

Add #!cpp in ...

Summary

  • 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
  • Place '*' operator to data type
  • Use C++ casting for type casting. For example, static_cast<...>, const_cast<...>, reinterpret_cast<...>
  • Use smart pointers
  • Use c++ new/delete operators
  • Use const keyword instead of #define when you define constant variable.
  • 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.

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

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;
}

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 const keyword instead of #define when you define constant variable

Right

 const int defaultTileWidth = 256;
 const int defaultTileHeigth = 256;

Wrong

 #define DEFAULT_TILE_W (256) 
 #define DEFAULT_TILE_H (256)

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;
}
Note: See TracWiki for help on using the wiki.