Add missing `CustomData_duplicate_referenced_layer_n` and deduplicate code.
authorBastien Montagne <montagne29@wanadoo.fr>
Tue, 4 Nov 2014 09:06:08 +0000 (10:06 +0100)
committerBastien Montagne <montagne29@wanadoo.fr>
Tue, 4 Nov 2014 09:06:08 +0000 (10:06 +0100)
CustomData_duplicate_referenced_layer_n not used in master currently, but need it
in mesh tranfer branch.

source/blender/blenkernel/BKE_customdata.h
source/blender/blenkernel/intern/customdata.c

index 51c78948c701300f5b2b8a1b1a213814c2c2f2e2..30a58891bda6e32dfbc3711853b4c20fd55ee427 100644 (file)
@@ -173,6 +173,7 @@ int CustomData_number_of_layers_typemask(const struct CustomData *data, CustomDa
 /* duplicate data of a layer with flag NOFREE, and remove that flag.
  * returns the layer data */
 void *CustomData_duplicate_referenced_layer(struct CustomData *data, const int type, const int totelem);
+void *CustomData_duplicate_referenced_layer_n(struct CustomData *data, const int type, const int n, const int totelem);
 void *CustomData_duplicate_referenced_layer_named(struct CustomData *data,
                                                   const int type, const char *name, const int totelem);
 bool CustomData_is_referenced_layer(struct CustomData *data, int type);
index 7684c5a3a42e53a7ddf14638e3fbcce858435759..fb0135bf16396bfbcdc024e78ea20c7e45997ac8 100644 (file)
@@ -1873,14 +1873,14 @@ int CustomData_number_of_layers_typemask(const CustomData *data, CustomDataMask
        return number;
 }
 
-void *CustomData_duplicate_referenced_layer(struct CustomData *data, const int type, const int totelem)
+static void *customData_duplicate_referenced_layer_index(CustomData *data, const int type,
+                                                         const int layer_index, const int totelem)
 {
        CustomDataLayer *layer;
-       int layer_index;
 
-       /* get the layer index of the first layer of type */
-       layer_index = CustomData_get_active_layer_index(data, type);
-       if (layer_index == -1) return NULL;
+       if (layer_index == -1) {
+               return NULL;
+       }
 
        layer = &data->layers[layer_index];
 
@@ -1896,8 +1896,9 @@ void *CustomData_duplicate_referenced_layer(struct CustomData *data, const int t
                        typeInfo->copy(layer->data, dest_data, totelem);
                        layer->data = dest_data;
                }
-               else
+               else {
                        layer->data = MEM_dupallocN(layer->data);
+               }
 
                layer->flag &= ~CD_FLAG_NOFREE;
        }
@@ -1905,37 +1906,37 @@ void *CustomData_duplicate_referenced_layer(struct CustomData *data, const int t
        return layer->data;
 }
 
-void *CustomData_duplicate_referenced_layer_named(struct CustomData *data,
-                                                  const int type, const char *name, const int totelem)
+void *CustomData_duplicate_referenced_layer(CustomData *data, const int type, const int totelem)
 {
        CustomDataLayer *layer;
        int layer_index;
 
-       /* get the layer index of the desired layer */
-       layer_index = CustomData_get_named_layer_index(data, type, name);
-       if (layer_index == -1) return NULL;
+       /* get the layer index of the first layer of type */
+       layer_index = CustomData_get_active_layer_index(data, type);
 
-       layer = &data->layers[layer_index];
+       return customData_duplicate_referenced_layer_index(data, type, layer_index, totelem);
+}
 
-       if (layer->flag & CD_FLAG_NOFREE) {
-               /* MEM_dupallocN won't work in case of complex layers, like e.g.
-                * CD_MDEFORMVERT, which has pointers to allocated data...
-                * So in case a custom copy function is defined, use it!
-                */
-               const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type);
+void *CustomData_duplicate_referenced_layer_n(CustomData *data, const int type, const int n, const int totelem)
+{
+       CustomDataLayer *layer;
+       int layer_index;
 
-               if (typeInfo->copy) {
-                       char *dest_data = MEM_mallocN(typeInfo->size * totelem, "CD duplicate ref layer");
-                       typeInfo->copy(layer->data, dest_data, totelem);
-                       layer->data = dest_data;
-               }
-               else
-                       layer->data = MEM_dupallocN(layer->data);
+       /* get the layer index of the desired layer */
+       layer_index = CustomData_get_layer_index_n(data, type, n);
 
-               layer->flag &= ~CD_FLAG_NOFREE;
-       }
+       return customData_duplicate_referenced_layer_index(data, type, layer_index, totelem);
+}
 
-       return layer->data;
+void *CustomData_duplicate_referenced_layer_named(CustomData *data, const int type, const char *name, const int totelem)
+{
+       CustomDataLayer *layer;
+       int layer_index;
+
+       /* get the layer index of the desired layer */
+       layer_index = CustomData_get_named_layer_index(data, type, name);
+
+       return customData_duplicate_referenced_layer_index(data, type, layer_index, totelem);
 }
 
 bool CustomData_is_referenced_layer(struct CustomData *data, int type)