[Bf-blender-cvs] [90a27d5aa91] master: Cleanup: Use enum for return values in context callbacks

Sybren A. Stüvel noreply at git.blender.org
Fri Oct 2 18:58:02 CEST 2020


Commit: 90a27d5aa91a1b6a25ea14e11c889d47f77f4cf7
Author: Sybren A. Stüvel
Date:   Fri Oct 2 18:56:25 2020 +0200
Branches: master
https://developer.blender.org/rB90a27d5aa91a1b6a25ea14e11c889d47f77f4cf7

Cleanup: Use enum for return values in context callbacks

Define enum `eContextResult` and use its values for returns, instead of
just returning 1, 0, or -1 (and always having some comment that explains
what -1 means).

This also cleans up the mixup between returning `0` and `false`, and `1`
and `true`. An inconsistency was discovered during this cleanup, and
marked with `TODO(sybren)`. It's not fixed here, as it would consititute
a functional change.

The enum isn't used everywhere, as enums in C and C++ can have different
storage sizes. To prevent issues, callback functions are still declared
as returning`int`. To at least make things easier to understand for
humans, I marked those with `int /*eContextResult*/`.

This is a followup of D9090, and is intended to unify how context
callbacks return values. This will make it easier to extend the approach
in D9090 to those functions.

No functional changes.

Differential Revision: https://developer.blender.org/D9095

===================================================================

M	source/blender/blenkernel/BKE_context.h
M	source/blender/blenkernel/BKE_screen.h
M	source/blender/blenkernel/intern/context.c
M	source/blender/editors/screen/screen_context.c
M	source/blender/editors/space_buttons/buttons_context.c
M	source/blender/editors/space_clip/space_clip.c
M	source/blender/editors/space_image/space_image.c
M	source/blender/editors/space_node/space_node.c
M	source/blender/editors/space_sequencer/space_sequencer.c
M	source/blender/editors/space_text/space_text.c
M	source/blender/makesdna/DNA_screen_types.h
M	source/blender/python/intern/bpy_rna.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_context.h b/source/blender/blenkernel/BKE_context.h
index f3e4a18b9bd..5c534803781 100644
--- a/source/blender/blenkernel/BKE_context.h
+++ b/source/blender/blenkernel/BKE_context.h
@@ -74,9 +74,26 @@ typedef struct bContext bContext;
 struct bContextDataResult;
 typedef struct bContextDataResult bContextDataResult;
 
-typedef int (*bContextDataCallback)(const bContext *C,
-                                    const char *member,
-                                    bContextDataResult *result);
+/* Result of context lookups.
+ * The specific values are important, and used implicitly in ctx_data_get(). Some functions also
+ * still accept/return `int` instead, to ensure that the compiler uses the correct storage size
+ * when mixing C/C++ code. */
+typedef enum eContextResult {
+  /* The context member was found, and its data is available. */
+  CTX_RESULT_OK = 1,
+
+  /* The context member was not found. */
+  CTX_RESULT_MEMBER_NOT_FOUND = 0,
+
+  /* The context member was found, but its data is not available.
+   * For example, "active_bone" is a valid context member, but has not data in Object mode. */
+  CTX_RESULT_NO_DATA = -1,
+} eContextResult;
+
+/* Function mapping a context member name to its value. */
+typedef int /*eContextResult*/ (*bContextDataCallback)(const bContext *C,
+                                                       const char *member,
+                                                       bContextDataResult *result);
 
 typedef struct bContextStoreEntry {
   struct bContextStoreEntry *next, *prev;
@@ -213,7 +230,7 @@ ListBase CTX_data_dir_get_ex(const bContext *C,
                              const bool use_rna,
                              const bool use_all);
 ListBase CTX_data_dir_get(const bContext *C);
-int CTX_data_get(
+int /*eContextResult*/ CTX_data_get(
     const bContext *C, const char *member, PointerRNA *r_ptr, ListBase *r_lb, short *r_type);
 
 void CTX_data_id_pointer_set(bContextDataResult *result, struct ID *id);
diff --git a/source/blender/blenkernel/BKE_screen.h b/source/blender/blenkernel/BKE_screen.h
index bcc58ecf2c5..35a3d0415a8 100644
--- a/source/blender/blenkernel/BKE_screen.h
+++ b/source/blender/blenkernel/BKE_screen.h
@@ -26,6 +26,8 @@
 
 #include "RNA_types.h"
 
+#include "BKE_context.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -106,7 +108,7 @@ typedef struct SpaceType {
   void (*gizmos)(void);
 
   /* return context data */
-  int (*context)(const struct bContext *C, const char *member, struct bContextDataResult *result);
+  bContextDataCallback context;
 
   /* Used when we want to replace an ID by another (or NULL). */
   void (*id_remap)(struct ScrArea *area,
@@ -181,7 +183,7 @@ typedef struct ARegionType {
   void (*cursor)(struct wmWindow *win, struct ScrArea *area, struct ARegion *region);
 
   /* return context data */
-  int (*context)(const struct bContext *C, const char *member, struct bContextDataResult *result);
+  bContextDataCallback context;
 
   /* Is called whenever the current visible View2D's region changes.
    *
diff --git a/source/blender/blenkernel/intern/context.c b/source/blender/blenkernel/intern/context.c
index 17ac8d7bedc..2002a49293f 100644
--- a/source/blender/blenkernel/intern/context.c
+++ b/source/blender/blenkernel/intern/context.c
@@ -294,7 +294,7 @@ static void *ctx_wm_python_context_get(const bContext *C,
   return fall_through;
 }
 
-static int ctx_data_get(bContext *C, const char *member, bContextDataResult *result)
+static eContextResult ctx_data_get(bContext *C, const char *member, bContextDataResult *result)
 {
   bScreen *screen;
   ScrArea *area;
@@ -374,7 +374,7 @@ static void *ctx_data_pointer_get(const bContext *C, const char *member)
 {
   bContextDataResult result;
 
-  if (C && ctx_data_get((bContext *)C, member, &result) == 1) {
+  if (C && ctx_data_get((bContext *)C, member, &result) == CTX_RESULT_OK) {
     BLI_assert(result.type == CTX_DATA_TYPE_POINTER);
     return result.ptr.data;
   }
@@ -391,7 +391,7 @@ static int ctx_data_pointer_verify(const bContext *C, const char *member, void *
     *pointer = NULL;
     return 1;
   }
-  if (ctx_data_get((bContext *)C, member, &result) == 1) {
+  if (ctx_data_get((bContext *)C, member, &result) == CTX_RESULT_OK) {
     BLI_assert(result.type == CTX_DATA_TYPE_POINTER);
     *pointer = result.ptr.data;
     return 1;
@@ -405,7 +405,7 @@ static int ctx_data_collection_get(const bContext *C, const char *member, ListBa
 {
   bContextDataResult result;
 
-  if (ctx_data_get((bContext *)C, member, &result) == 1) {
+  if (ctx_data_get((bContext *)C, member, &result) == CTX_RESULT_OK) {
     BLI_assert(result.type == CTX_DATA_TYPE_COLLECTION);
     *list = result.list;
     return 1;
@@ -453,7 +453,7 @@ PointerRNA CTX_data_pointer_get(const bContext *C, const char *member)
 {
   bContextDataResult result;
 
-  if (ctx_data_get((bContext *)C, member, &result) == 1) {
+  if (ctx_data_get((bContext *)C, member, &result) == CTX_RESULT_OK) {
     BLI_assert(result.type == CTX_DATA_TYPE_POINTER);
     return result.ptr;
   }
@@ -495,7 +495,7 @@ ListBase CTX_data_collection_get(const bContext *C, const char *member)
 {
   bContextDataResult result;
 
-  if (ctx_data_get((bContext *)C, member, &result) == 1) {
+  if (ctx_data_get((bContext *)C, member, &result) == CTX_RESULT_OK) {
     BLI_assert(result.type == CTX_DATA_TYPE_COLLECTION);
     return result.list;
   }
@@ -504,14 +504,13 @@ ListBase CTX_data_collection_get(const bContext *C, const char *member)
   return list;
 }
 
-/* 1:found,  -1:found but not set,  0:not found */
-int CTX_data_get(
+int /*eContextResult*/ CTX_data_get(
     const bContext *C, const char *member, PointerRNA *r_ptr, ListBase *r_lb, short *r_type)
 {
   bContextDataResult result;
-  int ret = ctx_data_get((bContext *)C, member, &result);
+  eContextResult ret = ctx_data_get((bContext *)C, member, &result);
 
-  if (ret == 1) {
+  if (ret == CTX_RESULT_OK) {
     *r_ptr = result.ptr;
     *r_lb = result.list;
     *r_type = result.type;
diff --git a/source/blender/editors/screen/screen_context.c b/source/blender/editors/screen/screen_context.c
index e32d374d094..07a571ae464 100644
--- a/source/blender/editors/screen/screen_context.c
+++ b/source/blender/editors/screen/screen_context.c
@@ -114,23 +114,16 @@ const char *screen_context_dir[] = {
 
 /* Each function `screen_ctx_XXX()` will be called when the screen context "XXX" is requested.
  * ensure_ed_screen_context_functions() is responsible for creating the hash map from context
- * member name to function.
- *
- * Each function returns:
- *    1 for "the member name was found and returned data is valid"
- *    0 for "the member name was not found"
- *   -1 for "the member name was found but data is not available"
- *
- * */
+ * member name to function. */
 
-static int screen_ctx_scene(const bContext *C, bContextDataResult *result)
+static eContextResult screen_ctx_scene(const bContext *C, bContextDataResult *result)
 {
   wmWindow *win = CTX_wm_window(C);
   Scene *scene = WM_window_get_active_scene(win);
   CTX_data_id_pointer_set(result, &scene->id);
-  return 1;
+  return CTX_RESULT_OK;
 }
-static int screen_ctx_visible_objects(const bContext *C, bContextDataResult *result)
+static eContextResult screen_ctx_visible_objects(const bContext *C, bContextDataResult *result)
 {
   wmWindow *win = CTX_wm_window(C);
   View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */
@@ -142,9 +135,9 @@ static int screen_ctx_visible_objects(const bContext *C, bContextDataResult *res
     }
   }
   CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
-  return 1;
+  return CTX_RESULT_OK;
 }
-static int screen_ctx_selectable_objects(const bContext *C, bContextDataResult *result)
+static eContextResult screen_ctx_selectable_objects(const bContext *C, bContextDataResult *result)
 {
   wmWindow *win = CTX_wm_window(C);
   View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */
@@ -156,9 +149,9 @@ static int screen_ctx_selectable_objects(const bContext *C, bContextDataResult *
     }
   }
   CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
-  return 1;
+  return CTX_RESULT_OK;
 }
-static int screen_ctx_selected_objects(const bContext *C, bContextDataResult *result)
+static eContextResult screen_ctx_selected_objects(const bContext *C, bContextDataResult *result)
 {
   wmWindow *win = CTX_wm_window(C);
   View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */
@@ -170,9 +163,10 @@ static int screen_ctx_selected_objects(const bContext *C, bContextDataResult *re
     }
   }
   CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
-  return 1;
+  return CTX_RESULT_OK;
 }
-static int screen_ctx_selected_editable_objects(const bContext *C, bContextDataResult *result)
+static eContextResult screen_ctx_selected_editable_objects(const bContext *C,
+                                                           bContextDataResult *result)
 {
   wmWindow *win = CTX_wm_window(C);
   View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */
@@ -184,9 +178,9 @@ static int screen_ctx_selected_editable_objects(const bContext *C, bContextDataR
     }
   }
   CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
-  return 1;
+  return CTX_RESULT_OK;
 }
-static int screen_ctx_editable_objects(const bContext *C, bContextDataResult *result)
+static eContextResult screen_ctx_editable_objects(const bContext *C, bContextDataResult *result)
 {
   wmWindow *win = CTX_wm_window(C);
   View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */
@@ -199,9 +193,9 @@ static int screen_ctx_editable_objects(const bContext *C, bContextDataResult *re
     }
   }
   CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
-  return 1;
+  return CTX_RESULT_OK;
 }
-static int screen_ctx_objects_in_mode(const bContext *C, bContextDataResult *result)
+static eContextResult screen_ctx_objects_in_mode(const bContext *C, bContextDataResult *result)
 {
   wmWindow *win = CTX_wm_window(C);
   View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */
@@ -215,9 +209,10 @@ static int screen_ctx_objects_in_mode(const bContext *C, bContextDataResult 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list