[Bf-blender-cvs] [f0ac5e8aec0] master: Cleanup: Move context.c to C++

Hans Goudey noreply at git.blender.org
Mon Dec 19 06:24:20 CET 2022


Commit: f0ac5e8aec03f7d0ca07d3792e50da929159478b
Author: Hans Goudey
Date:   Sun Dec 18 14:40:30 2022 -0600
Branches: master
https://developer.blender.org/rBf0ac5e8aec03f7d0ca07d3792e50da929159478b

Cleanup: Move context.c to C++

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

M	source/blender/blenkernel/BKE_context.h
M	source/blender/blenkernel/CMakeLists.txt
R075	source/blender/blenkernel/intern/context.c	source/blender/blenkernel/intern/context.cc
M	source/blender/python/BPY_extern.h

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

diff --git a/source/blender/blenkernel/BKE_context.h b/source/blender/blenkernel/BKE_context.h
index e1406e63ce1..90597a3e273 100644
--- a/source/blender/blenkernel/BKE_context.h
+++ b/source/blender/blenkernel/BKE_context.h
@@ -99,7 +99,7 @@ typedef struct bContextStore {
 } bContextStore;
 
 /* for the context's rna mode enum
- * keep aligned with data_mode_strings in context.c */
+ * keep aligned with data_mode_strings in context.cc */
 typedef enum eContextObjectMode {
   CTX_MODE_EDIT_MESH = 0,
   CTX_MODE_EDIT_CURVE,
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index d645408c6e9..c12c0c06ed8 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -97,7 +97,7 @@ set(SRC
   intern/colortools.c
   intern/compute_contexts.cc
   intern/constraint.c
-  intern/context.c
+  intern/context.cc
   intern/cpp_types.cc
   intern/crazyspace.cc
   intern/cryptomatte.cc
diff --git a/source/blender/blenkernel/intern/context.c b/source/blender/blenkernel/intern/context.cc
similarity index 75%
rename from source/blender/blenkernel/intern/context.c
rename to source/blender/blenkernel/intern/context.cc
index 0ddd53ccb99..4469f22346a 100644
--- a/source/blender/blenkernel/intern/context.c
+++ b/source/blender/blenkernel/intern/context.cc
@@ -4,9 +4,9 @@
  * \ingroup bke
  */
 
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
+#include <cstddef>
+#include <cstdlib>
+#include <cstring>
 
 #include "MEM_guardedalloc.h"
 
@@ -58,15 +58,15 @@ struct bContext {
 
   /* windowmanager context */
   struct {
-    struct wmWindowManager *manager;
-    struct wmWindow *window;
-    struct WorkSpace *workspace;
-    struct bScreen *screen;
-    struct ScrArea *area;
-    struct ARegion *region;
-    struct ARegion *menu;
-    struct wmGizmoGroup *gizmo_group;
-    struct bContextStore *store;
+    wmWindowManager *manager;
+    wmWindow *window;
+    WorkSpace *workspace;
+    bScreen *screen;
+    ScrArea *area;
+    ARegion *region;
+    ARegion *menu;
+    wmGizmoGroup *gizmo_group;
+    bContextStore *store;
 
     /* Operator poll. */
     /**
@@ -82,8 +82,8 @@ struct bContext {
 
   /* data context */
   struct {
-    struct Main *main;
-    struct Scene *scene;
+    Main *main;
+    Scene *scene;
 
     int recursion;
     /** True if python is initialized. */
@@ -101,14 +101,15 @@ struct bContext {
 
 bContext *CTX_create(void)
 {
-  bContext *C = MEM_callocN(sizeof(bContext), "bContext");
+  bContext *C = MEM_cnew<bContext>(__func__);
 
   return C;
 }
 
 bContext *CTX_copy(const bContext *C)
 {
-  bContext *newC = MEM_dupallocN((void *)C);
+  bContext *newC = MEM_new<bContext>(__func__);
+  *newC = *C;
 
   memset(&newC->wm.operator_poll_msg_dyn_params, 0, sizeof(newC->wm.operator_poll_msg_dyn_params));
 
@@ -129,22 +130,23 @@ bContextStore *CTX_store_add(ListBase *contexts, const char *name, const Pointer
 {
   /* ensure we have a context to put the entry in, if it was already used
    * we have to copy the context to ensure */
-  bContextStore *ctx = contexts->last;
+  bContextStore *ctx = static_cast<bContextStore *>(contexts->last);
 
   if (!ctx || ctx->used) {
     if (ctx) {
       bContextStore *lastctx = ctx;
-      ctx = MEM_dupallocN(lastctx);
+      ctx = MEM_new<bContextStore>(__func__);
+      *ctx = *lastctx;
       BLI_duplicatelist(&ctx->entries, &lastctx->entries);
     }
     else {
-      ctx = MEM_callocN(sizeof(bContextStore), "bContextStore");
+      ctx = MEM_cnew<bContextStore>(__func__);
     }
 
     BLI_addtail(contexts, ctx);
   }
 
-  bContextStoreEntry *entry = MEM_callocN(sizeof(bContextStoreEntry), "bContextStoreEntry");
+  bContextStoreEntry *entry = MEM_cnew<bContextStoreEntry>(__func__);
   BLI_strncpy(entry->name, name, sizeof(entry->name));
   entry->ptr = *ptr;
 
@@ -157,23 +159,25 @@ bContextStore *CTX_store_add_all(ListBase *contexts, bContextStore *context)
 {
   /* ensure we have a context to put the entries in, if it was already used
    * we have to copy the context to ensure */
-  bContextStore *ctx = contexts->last;
+  bContextStore *ctx = static_cast<bContextStore *>(contexts->last);
 
   if (!ctx || ctx->used) {
     if (ctx) {
       bContextStore *lastctx = ctx;
-      ctx = MEM_dupallocN(lastctx);
+      ctx = MEM_new<bContextStore>(__func__);
+      *ctx = *lastctx;
       BLI_duplicatelist(&ctx->entries, &lastctx->entries);
     }
     else {
-      ctx = MEM_callocN(sizeof(bContextStore), "bContextStore");
+      ctx = MEM_cnew<bContextStore>(__func__);
     }
 
     BLI_addtail(contexts, ctx);
   }
 
   LISTBASE_FOREACH (bContextStoreEntry *, tentry, &context->entries) {
-    bContextStoreEntry *entry = MEM_dupallocN(tentry);
+    bContextStoreEntry *entry = MEM_cnew<bContextStoreEntry>(__func__);
+    *entry = *tentry;
     BLI_addtail(&ctx->entries, entry);
   }
 
@@ -194,21 +198,22 @@ const PointerRNA *CTX_store_ptr_lookup(const bContextStore *store,
                                        const char *name,
                                        const StructRNA *type)
 {
-  bContextStoreEntry *entry = BLI_rfindstring(
-      &store->entries, name, offsetof(bContextStoreEntry, name));
+  bContextStoreEntry *entry = static_cast<bContextStoreEntry *>(
+      BLI_rfindstring(&store->entries, name, offsetof(bContextStoreEntry, name)));
   if (!entry) {
-    return NULL;
+    return nullptr;
   }
 
   if (type && !RNA_struct_is_a(entry->ptr.type, type)) {
-    return NULL;
+    return nullptr;
   }
   return &entry->ptr;
 }
 
 bContextStore *CTX_store_copy(bContextStore *store)
 {
-  bContextStore *ctx = MEM_dupallocN(store);
+  bContextStore *ctx = MEM_cnew<bContextStore>(__func__);
+  *ctx = *store;
   BLI_duplicatelist(&ctx->entries, &store->entries);
 
   return ctx;
@@ -223,7 +228,7 @@ void CTX_store_free(bContextStore *store)
 void CTX_store_free_list(ListBase *contexts)
 {
   bContextStore *ctx;
-  while ((ctx = BLI_pophead(contexts))) {
+  while ((ctx = static_cast<bContextStore *>(BLI_pophead(contexts)))) {
     CTX_store_free(ctx);
   }
 }
@@ -248,7 +253,7 @@ void *CTX_py_dict_get_orig(const bContext *C)
   return C->data.py_context_orig;
 }
 
-void CTX_py_state_push(bContext *C, struct bContext_PyState *pystate, void *value)
+void CTX_py_state_push(bContext *C, bContext_PyState *pystate, void *value)
 {
   pystate->py_context = C->data.py_context;
   pystate->py_context_orig = C->data.py_context_orig;
@@ -256,7 +261,7 @@ void CTX_py_state_push(bContext *C, struct bContext_PyState *pystate, void *valu
   C->data.py_context = value;
   C->data.py_context_orig = value;
 }
-void CTX_py_state_pop(bContext *C, struct bContext_PyState *pystate)
+void CTX_py_state_pop(bContext *C, bContext_PyState *pystate)
 {
   C->data.py_context = pystate->py_context;
   C->data.py_context_orig = pystate->py_context_orig;
@@ -300,7 +305,7 @@ static void *ctx_wm_python_context_get(const bContext *C,
 
   /* don't allow UI context access from non-main threads */
   if (!BLI_thread_is_main()) {
-    return NULL;
+    return nullptr;
   }
 
   return fall_through;
@@ -318,14 +323,14 @@ static eContextResult ctx_data_get(bContext *C, const char *member, bContextData
 #ifdef WITH_PYTHON
   if (CTX_py_dict_get(C)) {
     if (BPY_context_member_get(C, member, result)) {
-      return 1;
+      return CTX_RESULT_OK;
     }
   }
 #endif
 
   /* don't allow UI context access from non-main threads */
   if (!BLI_thread_is_main()) {
-    return done;
+    return CTX_RESULT_MEMBER_NOT_FOUND;
   }
 
   /* we check recursion to ensure that we do not get infinite
@@ -340,7 +345,7 @@ static eContextResult ctx_data_get(bContext *C, const char *member, bContextData
   if (done != 1 && recursion < 1 && C->wm.store) {
     C->data.recursion = 1;
 
-    const PointerRNA *ptr = CTX_store_ptr_lookup(C->wm.store, member, NULL);
+    const PointerRNA *ptr = CTX_store_ptr_lookup(C->wm.store, member, nullptr);
 
     if (ptr) {
       result->ptr = *ptr;
@@ -367,7 +372,7 @@ static eContextResult ctx_data_get(bContext *C, const char *member, bContextData
   }
 
   if (done != 1 && recursion < 4 && (screen = CTX_wm_screen(C))) {
-    bContextDataCallback cb = screen->context;
+    bContextDataCallback cb = reinterpret_cast<bContextDataCallback>(screen->context);
     C->data.recursion = 4;
     if (cb) {
       ret = cb(C, member, result);
@@ -379,7 +384,7 @@ static eContextResult ctx_data_get(bContext *C, const char *member, bContextData
 
   C->data.recursion = recursion;
 
-  return done;
+  return eContextResult(done);
 }
 
 static void *ctx_data_pointer_get(const bContext *C, const char *member)
@@ -390,14 +395,14 @@ static void *ctx_data_pointer_get(const bContext *C, const char *member)
     return result.ptr.data;
   }
 
-  return NULL;
+  return nullptr;
 }
 
 static bool ctx_data_pointer_verify(const bContext *C, const char *member, void **pointer)
 {
-  /* if context is NULL, pointer must be NULL too and that is a valid return */
-  if (C == NULL) {
-    *pointer = NULL;
+  /* if context is nullptr, pointer must be nullptr too and that is a valid return */
+  if (C == nullptr) {
+    *pointer = nullptr;
     return true;
   }
 
@@ -408,7 +413,7 @@ static bool ctx_data_pointer_verify(const bContext *C, const char *member, void
     return true;
   }
 
-  *pointer = NULL;
+  *pointer = nullptr;
   return false;
 }
 
@@ -445,10 +450,11 @@ static int ctx_data_base_collection_get(const bContext *C, const char *member, L
   bool ok = false;
 
   CollectionPointerLink *ctx_object;
-  for (ctx_object = ctx_object_list.first; ctx_object; ctx_object = ctx_object->next) {
-    Object *ob = ctx_object->ptr.data;
+  for (ctx_object = static_cast<CollectionPointerLink *>(ctx_object_list.first); ctx_object;
+       ctx_object = ctx_object->next) {
+    Object *ob = static_cast<Object *>(ctx_object->ptr.data);
     Base *base = BKE_view_layer_base_find(view_layer, ob);
-    if (base != NULL) {
+    if (base != nullptr) {
       CTX_data_list_add(&result, &scene->id, &RNA_ObjectBase, base);
       ok = true;
     }
@@ -509,7 +515,7 @@ ListBase CTX_data_collection_get(const bContext *C, const char *member)
     return result.list;
   }
 
-  List

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list