Welcome to mirror list, hosted at ThFree Co, Russian Federation.

my_test_node.cc « nodes « simulations « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 855eff2afe21509c8eb19100df16c8afb1fa94c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
#include <cstring>
#include <typeinfo>

#include "BKE_node.h"
#include "BKE_virtual_node_tree.h"

#include "SIM_node_tree.h"

#include "BLI_vector.h"
#include "BLI_string_ref.h"
#include "BLI_set.h"
#include "BLI_linear_allocator.h"
#include "BLI_color.h"
#include "BLI_string.h"
#include "BLI_array_cxx.h"
#include "BLI_map.h"
#include "BLI_listbase_wrapper.h"
#include "BLI_string_utils.h"
#include "BLI_linear_allocated_vector.h"
#include "BLI_index_mask.h"

#include "PIL_time.h"

#include "BKE_context.h"

#include "DNA_space_types.h"

#include "UI_interface.h"

#include "../space_node/node_intern.h"

using BLI::Array;
using BLI::ArrayRef;
using BLI::IndexMask;
using BLI::IntrusiveListBaseWrapper;
using BLI::LinearAllocatedVector;
using BLI::LinearAllocator;
using BLI::Map;
using BLI::MutableArrayRef;
using BLI::rgba_f;
using BLI::Set;
using BLI::StringRef;
using BLI::StringRefNull;
using BLI::Vector;

class SocketDataType;
class BaseSocketDataType;
class ListSocketDataType;

enum class SocketTypeCategory {
  Base,
  List,
};

class SocketDataType {
 public:
  std::string m_ui_name;
  bNodeSocketType *m_socket_type;
  SocketTypeCategory m_category;

  SocketDataType(StringRef ui_name, bNodeSocketType *socket_type, SocketTypeCategory category)
      : m_ui_name(ui_name), m_socket_type(socket_type), m_category(category)
  {
  }
};

class BaseSocketDataType : public SocketDataType {
 public:
  ListSocketDataType *m_list_type;

  BaseSocketDataType(StringRef ui_name, bNodeSocketType *socket_type)
      : SocketDataType(ui_name, socket_type, SocketTypeCategory::Base)
  {
  }
};

class ListSocketDataType : public SocketDataType {
 public:
  BaseSocketDataType *m_base_type;

  ListSocketDataType(StringRef ui_name, bNodeSocketType *socket_type)
      : SocketDataType(ui_name, socket_type, SocketTypeCategory::List)
  {
  }
};

class DataTypesInfo {
 private:
  Set<SocketDataType *> m_data_types;

 public:
  void add_data_type(SocketDataType *data_type)
  {
    m_data_types.add_new(data_type);
  }
};

static DataTypesInfo *socket_data_types;

static BaseSocketDataType *data_socket_float;
static BaseSocketDataType *data_socket_int;
static ListSocketDataType *data_socket_float_list;
static ListSocketDataType *data_socket_int_list;

enum class SocketDeclCategory {
  Mockup,
  FixedDataType,
  Operator,
};

class SocketDecl {
 private:
  bNodeSocketType *m_current_type;
  StringRefNull m_identifier;
  StringRefNull m_ui_name;
  SocketDeclCategory m_category;

 protected:
  SocketDecl(bNodeSocketType *current_type,
             StringRefNull identifier,
             StringRefNull ui_name,
             SocketDeclCategory category)
      : m_current_type(current_type),
        m_identifier(identifier),
        m_ui_name(ui_name),
        m_category(category)
  {
  }

 public:
  void build(bNodeTree *ntree, bNode *node, eNodeSocketInOut in_out)
  {
    nodeAddSocket(
        ntree, node, in_out, m_current_type->idname, m_identifier.data(), m_ui_name.data());
  }

  SocketDeclCategory category() const
  {
    return m_category;
  }

  bNodeSocketType *current_type() const
  {
    return m_current_type;
  }

  StringRefNull identifier() const
  {
    return m_identifier;
  }

  StringRefNull ui_name() const
  {
    return m_ui_name;
  }

  bool socket_is_correct(const bNodeSocket *socket)
  {
    BLI_assert(socket != nullptr);
    if (socket->typeinfo != m_current_type) {
      return false;
    }
    if (socket->name != m_ui_name) {
      return false;
    }
    if (socket->identifier != m_identifier) {
      return false;
    }
    return true;
  }
};

using OperatorSocketFn = void (*)(bNodeTree *ntree,
                                  bNode *node,
                                  bNodeSocket *socket,
                                  bNodeSocket *directly_linked_socket,
                                  bNodeSocket *linked_socket);

class OperatorSocketDecl final : public SocketDecl {
 private:
  OperatorSocketFn m_callback;

 public:
  OperatorSocketDecl(StringRefNull identifier, StringRefNull ui_name, OperatorSocketFn callback)
      : SocketDecl(nodeSocketTypeFind("OperatorSocket"),
                   identifier,
                   ui_name,
                   SocketDeclCategory::Operator),
        m_callback(callback)
  {
  }

  const OperatorSocketFn callback() const
  {
    return m_callback;
  }
};

class FixedDataTypeSocketDecl final : public SocketDecl {
 private:
  const SocketDataType *m_data_type;

 public:
  FixedDataTypeSocketDecl(StringRefNull identifier,
                          StringRefNull ui_name,
                          const SocketDataType *data_type)
      : SocketDecl(
            data_type->m_socket_type, identifier, ui_name, SocketDeclCategory::FixedDataType),
        m_data_type(data_type)
  {
  }

  const SocketDataType *data_type() const
  {
    return m_data_type;
  }
};

class MockupSocketDecl final : public SocketDecl {
 public:
  MockupSocketDecl(bNodeSocketType *type, StringRefNull identifier, StringRefNull ui_name)
      : SocketDecl(type, identifier, ui_name, SocketDeclCategory::Mockup)
  {
  }
};

class NodeDecl {
 public:
  bNodeTree *m_ntree;
  bNode *m_node;
  LinearAllocatedVector<SocketDecl *> m_inputs;
  LinearAllocatedVector<SocketDecl *> m_outputs;
  bool m_has_operator_input = false;

  NodeDecl(bNodeTree *ntree, bNode *node) : m_ntree(ntree), m_node(node)
  {
  }

  void reserve_decls(LinearAllocator<> &allocator, uint input_amount, uint output_amount)
  {
    m_inputs.reserve(input_amount, allocator);
    m_outputs.reserve(output_amount, allocator);
  }

  void build() const
  {
    nodeRemoveAllSockets(m_ntree, m_node);
    for (SocketDecl *decl : m_inputs) {
      decl->build(m_ntree, m_node, SOCK_IN);
    }
    for (SocketDecl *decl : m_outputs) {
      decl->build(m_ntree, m_node, SOCK_OUT);
    }
  }

  bool sockets_are_correct() const
  {
    if (!this->sockets_are_correct(m_node->inputs, m_inputs)) {
      return false;
    }
    if (!this->sockets_are_correct(m_node->outputs, m_outputs)) {
      return false;
    }
    return true;
  }

 private:
  bool sockets_are_correct(ListBase &sockets_list, ArrayRef<SocketDecl *> decls) const
  {
    uint i = 0;
    LISTBASE_FOREACH (bNodeSocket *, socket, &sockets_list) {
      if (i == decls.size()) {
        return false;
      }
      SocketDecl *decl = decls[i];
      if (!decl->socket_is_correct(socket)) {
        return false;
      }
      i++;
    }
    if (i != decls.size()) {
      return false;
    }
    return true;
  }
};

template<typename T> static T *get_node_storage(bNode *node);
template<typename T> static const T *get_node_storage(const bNode *node);
template<typename T> static T *get_socket_storage(bNodeSocket *socket);

class NodeBuilder {
 private:
  LinearAllocator<> &m_allocator;
  NodeDecl &m_node_decl;

 public:
  NodeBuilder(LinearAllocator<> &allocator, NodeDecl &node_decl)
      : m_allocator(allocator), m_node_decl(node_decl)
  {
  }

  template<typename T> T *node_storage()
  {
    return get_node_storage<T>(m_node_decl.m_node);
  }

  void fixed_input(StringRef identifier, StringRef ui_name, SocketDataType &type)
  {
    FixedDataTypeSocketDecl *decl = m_allocator.construct<FixedDataTypeSocketDecl>(
        m_allocator.copy_string(identifier), m_allocator.copy_string(ui_name), &type);
    m_node_decl.m_inputs.append(decl, m_allocator);
  }

  void fixed_output(StringRef identifier, StringRef ui_name, SocketDataType &type)
  {
    FixedDataTypeSocketDecl *decl = m_allocator.construct<FixedDataTypeSocketDecl>(
        m_allocator.copy_string(identifier), m_allocator.copy_string(ui_name), &type);
    m_node_decl.m_outputs.append(decl, m_allocator);
  }

  void operator_input(StringRef identifier, StringRef ui_name, OperatorSocketFn callback)
  {
    OperatorSocketDecl *decl = m_allocator.construct<OperatorSocketDecl>(
        m_allocator.copy_string(ui_name), m_allocator.copy_string(identifier), callback);
    m_node_decl.m_inputs.append(decl, m_allocator);
    m_node_decl.m_has_operator_input = true;
  }

  void float_input(StringRef identifier, StringRef ui_name)
  {
    this->fixed_input(identifier, ui_name, *data_socket_float);
  }

  void int_input(StringRef identifier, StringRef ui_name)
  {
    this->fixed_input(identifier, ui_name, *data_socket_int);
  }

  void float_output(StringRef identifier, StringRef ui_name)
  {
    this->fixed_output(identifier, ui_name, *data_socket_float);
  }

  void int_output(StringRef identifier, StringRef ui_name)
  {
    this->fixed_output(identifier, ui_name, *data_socket_int);
  }
};

static void declare_test_node(NodeBuilder &builder)
{
  MyTestNodeStorage *storage = builder.node_storage<MyTestNodeStorage>();

  builder.float_input("a", "ID 1");
  builder.int_input("b", "ID 2");
  builder.int_input("c", "ID 4");
  builder.float_output("c", "ID 3");

  for (int i = 0; i < storage->x; i++) {
    builder.fixed_input(
        "id" + std::to_string(i), "Hello " + std::to_string(i), *data_socket_float_list);
  }
}

class SocketDefinition {
 public:
  using DrawInNodeFn = std::function<void(struct bContext *C,
                                          struct uiLayout *layout,
                                          struct PointerRNA *ptr,
                                          struct PointerRNA *node_ptr,
                                          const char *text)>;
  using NewStorageFn = std::function<void *()>;
  using CopyStorageFn = std::function<void *(const void *)>;
  using FreeStorageFn = std::function<void(void *)>;
  template<typename T> using TypedInitStorageFn = std::function<void(T *)>;

 private:
  bNodeSocketType m_stype;
  DrawInNodeFn m_draw_in_node_fn;
  rgba_f m_color = {0.0f, 0.0f, 0.0f, 1.0f};
  std::string m_storage_struct_name;
  NewStorageFn m_new_storage_fn;
  CopyStorageFn m_copy_storage_fn;
  FreeStorageFn m_free_storage_fn;

 public:
  SocketDefinition(StringRef idname)
  {
    memset(&m_stype, 0, sizeof(bNodeSocketType));
    idname.copy(m_stype.idname);
    m_stype.type = SOCK_CUSTOM;
    m_stype.draw = SocketDefinition::draw_in_node;
    m_draw_in_node_fn = [](struct bContext *UNUSED(C),
                           struct uiLayout *layout,
                           struct PointerRNA *UNUSED(ptr),
                           struct PointerRNA *UNUSED(node_ptr),
                           const char *text) { uiItemL(layout, text, 0); };

    m_stype.draw_color = SocketDefinition::get_draw_color;
    m_stype.free_self = [](bNodeSocketType *UNUSED(stype)) {};

    m_new_storage_fn = []() { return nullptr; };
    m_copy_storage_fn = [](const void *storage) {
      BLI_assert(storage == nullptr);
      UNUSED_VARS_NDEBUG(storage);
      return nullptr;
    };
    m_free_storage_fn = [](void *storage) {
      BLI_assert(storage == nullptr);
      UNUSED_VARS_NDEBUG(storage);
    };

    m_stype.init_fn = SocketDefinition::init_socket;
    m_stype.copy_fn = SocketDefinition::copy_socket;
    m_stype.free_fn = SocketDefinition::free_socket;

    m_stype.userdata = (void *)this;
  }

  void set_color(rgba_f color)
  {
    m_color = color;
  }

  void add_dna_storage(StringRef struct_name,
                       NewStorageFn init_storage_fn,
                       CopyStorageFn copy_storage_fn,
                       FreeStorageFn free_storage_fn)
  {
    m_storage_struct_name = struct_name;
    m_new_storage_fn = init_storage_fn;
    m_copy_storage_fn = copy_storage_fn;
    m_free_storage_fn = free_storage_fn;
  }

  template<typename T>
  void add_dna_storage(StringRef struct_name, TypedInitStorageFn<T> init_storage_fn)
  {
    this->add_dna_storage(
        struct_name,
        [init_storage_fn]() {
          void *buffer = MEM_callocN(sizeof(T), __func__);
          init_storage_fn((T *)buffer);
          return buffer;
        },
        [](const void *buffer) {
          void *new_buffer = MEM_callocN(sizeof(T), __func__);
          memcpy(new_buffer, buffer, sizeof(T));
          return new_buffer;
        },
        [](void *buffer) { MEM_freeN(buffer); });
  }

  void add_draw_fn(DrawInNodeFn draw_in_node_fn)
  {
    m_draw_in_node_fn = draw_in_node_fn;
  }

  StringRefNull storage_struct_name() const
  {
    return m_storage_struct_name;
  }

  void register_type()
  {
    nodeRegisterSocketType(&m_stype);
  }

  static const SocketDefinition *get_from_socket(bNodeSocket *socket)
  {
    const SocketDefinition *def = (const SocketDefinition *)socket->typeinfo->userdata;
    BLI_assert(def != nullptr);
    return def;
  }

  void *get_dna_storage_copy(bNodeSocket *socket) const
  {
    if (socket->default_value == nullptr) {
      return nullptr;
    }
    void *storage_copy = m_copy_storage_fn(socket->default_value);
    return storage_copy;
  }

  void free_dna_storage(void *storage) const
  {
    m_free_storage_fn(storage);
  }

 private:
  static void init_socket(bNodeTree *UNUSED(ntree), bNode *UNUSED(node), bNodeSocket *socket)
  {
    const SocketDefinition *def = get_from_socket(socket);
    socket->default_value = def->m_new_storage_fn();
  }

  static void copy_socket(bNodeTree *UNUSED(dst_ntree),
                          bNode *UNUSED(dst_node),
                          bNodeSocket *dst_socket,
                          const bNodeSocket *src_socket)
  {
    const SocketDefinition *def = get_from_socket(dst_socket);
    dst_socket->default_value = def->m_copy_storage_fn(src_socket->default_value);
  }

  static void free_socket(bNodeTree *UNUSED(ntree), bNode *UNUSED(node), bNodeSocket *socket)
  {
    const SocketDefinition *def = get_from_socket(socket);
    def->m_free_storage_fn(socket->default_value);
    socket->default_value = nullptr;
  }

  static void draw_in_node(struct bContext *C,
                           struct uiLayout *layout,
                           struct PointerRNA *ptr,
                           struct PointerRNA *node_ptr,
                           const char *text)
  {
    bNodeSocket *socket = (bNodeSocket *)ptr->data;
    const SocketDefinition *def = get_from_socket(socket);
    def->m_draw_in_node_fn(C, layout, ptr, node_ptr, text);
  }

  static void get_draw_color(struct bContext *UNUSED(C),
                             struct PointerRNA *ptr,
                             struct PointerRNA *UNUSED(node_ptr),
                             float *r_color)
  {
    bNodeSocket *socket = (bNodeSocket *)ptr->data;
    const SocketDefinition *def = get_from_socket(socket);
    memcpy(r_color, def->m_color, sizeof(rgba_f));
  }
};

class NodeDefinition {
 public:
  using DeclareNodeFn = std::function<void(NodeBuilder &node_builder)>;
  using NewStorageFn = std::function<void *()>;
  using CopyStorageFn = std::function<void *(const void *)>;
  using FreeStorageFn = std::function<void(void *)>;
  using DrawInNodeFn =
      std::function<void(struct uiLayout *layout, struct bContext *C, struct PointerRNA *ptr)>;
  template<typename T> using TypedNewStorageFn = std::function<T *()>;
  template<typename T> using TypedCopyStorageFn = std::function<T *(const T *)>;
  template<typename T> using TypedFreeStorageFn = std::function<void(T *)>;
  template<typename T> using TypedInitStorageFn = std::function<void(T *)>;
  using CopyBehaviorFn = std::function<void(bNode *dst_node, const bNode *src_node)>;
  using LabelFn = std::function<void(bNodeTree *ntree, bNode *node, char *r_label, int maxlen)>;

 private:
  bNodeType m_ntype;
  DeclareNodeFn m_declare_node_fn;
  NewStorageFn m_new_storage_fn;
  CopyStorageFn m_copy_storage_fn;
  FreeStorageFn m_free_storage_fn;
  CopyBehaviorFn m_copy_node_fn;
  DrawInNodeFn m_draw_in_node_fn;
  LabelFn m_label_fn;

 public:
  NodeDefinition(StringRef idname, StringRef ui_name, StringRef ui_description)
  {
    bNodeType *ntype = &m_ntype;

    memset(ntype, 0, sizeof(bNodeType));
    ntype->minwidth = 20;
    ntype->minheight = 20;
    ntype->maxwidth = 1000;
    ntype->maxheight = 1000;
    ntype->height = 100;
    ntype->width = 140;
    ntype->type = NODE_CUSTOM;

    idname.copy(ntype->idname);
    ui_name.copy(ntype->ui_name);
    ui_description.copy(ntype->ui_description);

    ntype->userdata = (void *)this;

    m_declare_node_fn = [](NodeBuilder &UNUSED(builder)) {};
    m_new_storage_fn = []() { return nullptr; };
    m_copy_storage_fn = [](const void *storage) {
      BLI_assert(storage == nullptr);
      UNUSED_VARS_NDEBUG(storage);
      return nullptr;
    };
    m_free_storage_fn = [](void *storage) {
      BLI_assert(storage == nullptr);
      UNUSED_VARS_NDEBUG(storage);
    };
    m_draw_in_node_fn = [](struct uiLayout *UNUSED(layout),
                           struct bContext *UNUSED(C),
                           struct PointerRNA *UNUSED(ptr)) {};
    m_copy_node_fn = [](bNode *UNUSED(dst_node), const bNode *UNUSED(src_node)) {};

    ntype->poll = [](bNodeType *UNUSED(ntype), bNodeTree *UNUSED(ntree)) { return true; };
    ntype->initfunc = init_node;
    ntype->copyfunc = copy_node;
    ntype->freefunc = free_node;

    ntype->draw_buttons = [](struct uiLayout *layout, struct bContext *C, struct PointerRNA *ptr) {
      bNode *node = (bNode *)ptr->data;
      NodeDefinition *def = type_from_node(node);
      def->m_draw_in_node_fn(layout, C, ptr);
    };

    ntype->draw_nodetype = node_draw_default;
    ntype->draw_nodetype_prepare = node_update_default;
    ntype->select_area_func = node_select_area_default;
    ntype->tweak_area_func = node_tweak_area_default;
    ntype->resize_area_func = node_resize_area_default;
    ntype->draw_buttons_ex = nullptr;
  }

  void add_declaration(DeclareNodeFn declare_fn)
  {
    m_declare_node_fn = declare_fn;
  }

  void add_dna_storage(StringRef struct_name,
                       NewStorageFn new_storage_fn,
                       CopyStorageFn copy_storage_fn,
                       FreeStorageFn free_storage_fn)
  {
    struct_name.copy(m_ntype.storagename);
    m_new_storage_fn = new_storage_fn;
    m_copy_storage_fn = copy_storage_fn;
    m_free_storage_fn = free_storage_fn;
  }

  template<typename T>
  void add_dna_storage(StringRef struct_name,
                       TypedNewStorageFn<T> new_storage_fn,
                       TypedCopyStorageFn<T> copy_storage_fn,
                       TypedFreeStorageFn<T> free_storage_fn)
  {
    this->add_dna_storage(
        struct_name,
        [new_storage_fn]() { return (void *)new_storage_fn(); },
        [copy_storage_fn](const void *storage_) {
          const T *storage = (const T *)storage_;
          T *new_storage = copy_storage_fn(storage);
          return (void *)new_storage;
        },
        [free_storage_fn](const void *storage_) {
          T *storage = (T *)storage_;
          free_storage_fn(storage);
        });
  }

  template<typename T>
  void add_dna_storage(StringRef struct_name, TypedInitStorageFn<T> init_storage_fn)
  {
    this->add_dna_storage(
        struct_name,
        [init_storage_fn]() {
          void *buffer = MEM_callocN(sizeof(T), __func__);
          init_storage_fn((T *)buffer);
          return buffer;
        },
        [](const void *buffer) {
          void *new_buffer = MEM_callocN(sizeof(T), __func__);
          memcpy(new_buffer, buffer, sizeof(T));
          return new_buffer;
        },
        [](void *buffer) { MEM_freeN(buffer); });
  }

  void add_copy_behavior(CopyBehaviorFn copy_fn)
  {
    m_copy_node_fn = copy_fn;
  }

  template<typename T>
  void add_copy_behavior(std::function<void(T *dst_storage, const T *src_storage)> copy_fn)
  {
    this->add_copy_behavior([copy_fn](bNode *dst_node, const bNode *src_node) {
      T *dst_storage = get_node_storage<T>(dst_node);
      const T *src_storage = get_node_storage<T>(src_node);
      copy_fn(dst_storage, src_storage);
    });
  }

  void add_draw_fn(DrawInNodeFn draw_fn)
  {
    m_draw_in_node_fn = draw_fn;
  }

  void add_label_fn(LabelFn label_fn)
  {
    m_ntype.labelfunc = node_label;
    m_label_fn = label_fn;
  }

  void register_type()
  {
    nodeRegisterType(&m_ntype);
  }

  static void declare_node(bNode *node, NodeBuilder &builder)
  {
    NodeDefinition *def = type_from_node(node);
    def->m_declare_node_fn(builder);
  }

 private:
  static NodeDefinition *type_from_node(bNode *node)
  {
    return (NodeDefinition *)node->typeinfo->userdata;
  }

  static void init_node(bNodeTree *ntree, bNode *node)
  {
    NodeDefinition *def = type_from_node(node);

    LinearAllocator<> allocator;
    NodeDecl node_decl{ntree, node};
    NodeBuilder node_builder{allocator, node_decl};
    node->storage = def->m_new_storage_fn();
    def->m_declare_node_fn(node_builder);
    node_decl.build();
  }

  static void copy_node(bNodeTree *UNUSED(dst_ntree), bNode *dst_node, const bNode *src_node)
  {
    BLI_assert(dst_node->typeinfo == src_node->typeinfo);
    NodeDefinition *def = type_from_node(dst_node);

    dst_node->storage = def->m_copy_storage_fn(src_node->storage);
    def->m_copy_node_fn(dst_node, src_node);
  }

  static void free_node(bNode *node)
  {
    NodeDefinition *def = type_from_node(node);
    def->m_free_storage_fn(node->storage);
    node->storage = nullptr;
  }

  static void node_label(bNodeTree *ntree, bNode *node, char *r_label, int maxlen)
  {
    NodeDefinition *def = type_from_node(node);
    def->m_label_fn(ntree, node, r_label, maxlen);
  }
};

template<typename T> static T *get_node_storage(bNode *node)
{
#ifdef DEBUG
  const char *type_name = typeid(T).name();
  const char *expected_name = node->typeinfo->storagename;
  BLI_assert(strstr(type_name, expected_name));
#endif
  return (T *)node->storage;
}

template<typename T> static const T *get_node_storage(const bNode *node)
{
#ifdef DEBUG
  const char *type_name = typeid(T).name();
  const char *expected_name = node->typeinfo->storagename;
  BLI_assert(strstr(type_name, expected_name));
#endif
  return (const T *)node->storage;
}

template<typename T> static T *get_socket_storage(bNodeSocket *socket)
{
#ifdef DEBUG
  const char *type_name = typeid(T).name();
  const char *expected_name =
      SocketDefinition::get_from_socket(socket)->storage_struct_name().data();
  BLI_assert(strstr(type_name, expected_name));
#endif
  return (T *)socket->default_value;
}

static void update_tree(bContext *C)
{
  bNodeTree *ntree = CTX_wm_space_node(C)->edittree;
  ntree->update = NTREE_UPDATE;
  ntreeUpdateTree(CTX_data_main(C), ntree);
}

void register_node_type_my_test_node()
{
  {
    static NodeDefinition ntype("MyTestNode", "My Test Node", "My Description");
    ntype.add_declaration(declare_test_node);
    ntype.add_dna_storage<MyTestNodeStorage>("MyTestNodeStorage",
                                             [](MyTestNodeStorage *storage) { storage->x = 3; });
    ntype.add_copy_behavior<MyTestNodeStorage>(
        [](MyTestNodeStorage *dst_storage, const MyTestNodeStorage *UNUSED(src_storage)) {
          dst_storage->x += 1;
        });
    ntype.add_draw_fn([](uiLayout *layout, struct bContext *UNUSED(C), struct PointerRNA *ptr) {
      bNode *node = (bNode *)ptr->data;
      MyTestNodeStorage *storage = (MyTestNodeStorage *)node->storage;
      uiBut *but = uiDefButI(uiLayoutGetBlock(layout),
                             UI_BTYPE_NUM,
                             0,
                             "X value",
                             0,
                             0,
                             50,
                             50,
                             &storage->x,
                             -1000,
                             1000,
                             3,
                             20,
                             "my x value");
      uiItemL(layout, "Hello World", 0);
      UI_but_func_set(
          but,
          [](bContext *C, void *UNUSED(arg1), void *UNUSED(arg2)) { update_tree(C); },
          nullptr,
          nullptr);
    });

    ntype.register_type();
  }
  {
    static NodeDefinition ntype("MyTestNode2", "Node 2", "Description");
    ntype.add_declaration([](NodeBuilder &node_builder) {
      node_builder.float_input("a", "A");
      node_builder.float_input("b", "B");
      node_builder.float_output("result", "Result");
    });
    ntype.add_label_fn([](bNodeTree *UNUSED(ntree), bNode *node, char *r_label, int maxlen) {
      if (node->flag & NODE_HIDDEN) {
        BLI_strncpy(r_label, "Custom Label", maxlen);
      }
    });
    ntype.register_type();
  }
  {
    static NodeDefinition ntype("FloatAddNode", "Float Add Node", "");
    ntype.add_dna_storage<FloatAddNodeStorage>(
        "FloatAddNodeStorage",
        []() { return (FloatAddNodeStorage *)MEM_callocN(sizeof(FloatAddNodeStorage), __func__); },
        [](const FloatAddNodeStorage *storage) {
          FloatAddNodeStorage *new_storage = (FloatAddNodeStorage *)MEM_callocN(
              sizeof(FloatAddNodeStorage), __func__);
          LISTBASE_FOREACH (VariadicNodeSocketIdentifier *, value, &storage->inputs_info) {
            void *new_value = MEM_dupallocN(value);
            BLI_addtail(&new_storage->inputs_info, new_value);
          }
          return new_storage;
        },
        [](FloatAddNodeStorage *storage) {
          BLI_freelistN(&storage->inputs_info);
          MEM_freeN(storage);
        });
    ntype.add_declaration([](NodeBuilder &node_builder) {
      FloatAddNodeStorage *storage = node_builder.node_storage<FloatAddNodeStorage>();
      LISTBASE_FOREACH (VariadicNodeSocketIdentifier *, value, &storage->inputs_info) {
        node_builder.float_input(value->identifier, "Value");
      }
      node_builder.operator_input(
          "New Input",
          "New",
          [](bNodeTree *UNUSED(ntree),
             bNode *node,
             bNodeSocket *UNUSED(socket),
             bNodeSocket *UNUSED(directly_linked_socket),
             bNodeSocket *UNUSED(linked_socket)) {
            /* TODO: refresh node and make link */
            FloatAddNodeStorage *storage = get_node_storage<FloatAddNodeStorage>(node);
            VariadicNodeSocketIdentifier *value = (VariadicNodeSocketIdentifier *)MEM_callocN(
                sizeof(VariadicNodeSocketIdentifier), __func__);
            BLI_uniquename(&storage->inputs_info,
                           value,
                           "ID",
                           '.',
                           offsetof(VariadicNodeSocketIdentifier, identifier),
                           sizeof(value->identifier));
            BLI_addtail(&storage->inputs_info, value);
          });
      node_builder.float_output("result", "Result");
    });
    ntype.add_draw_fn([](uiLayout *layout, struct bContext *UNUSED(C), struct PointerRNA *ptr) {
      bNode *node = (bNode *)ptr->data;
      uiBut *but = uiDefBut(uiLayoutGetBlock(layout),
                            UI_BTYPE_BUT,
                            0,
                            "Add Input",
                            0,
                            0,
                            100,
                            40,
                            nullptr,
                            0,
                            0,
                            0,
                            0,
                            "Add new input");
      UI_but_func_set(
          but,
          [](bContext *C, void *arg1, void *UNUSED(arg2)) {
            bNode *node = (bNode *)arg1;
            FloatAddNodeStorage *storage = get_node_storage<FloatAddNodeStorage>(node);
            VariadicNodeSocketIdentifier *value = (VariadicNodeSocketIdentifier *)MEM_callocN(
                sizeof(VariadicNodeSocketIdentifier), __func__);
            BLI_uniquename(&storage->inputs_info,
                           value,
                           "ID",
                           '.',
                           offsetof(VariadicNodeSocketIdentifier, identifier),
                           sizeof(value->identifier));
            BLI_addtail(&storage->inputs_info, value);
            update_tree(C);
          },
          node,
          nullptr);
    });
    ntype.register_type();
  }
}

void init_socket_data_types()
{
  {
    static SocketDefinition stype("NodeSocketFloatList");
    stype.set_color({0.63, 0.63, 0.63, 0.5});
    stype.register_type();
  }
  {
    static SocketDefinition stype("NodeSocketIntList");
    stype.set_color({0.06, 0.52, 0.15, 0.5});
    stype.register_type();
  }
  {
    static SocketDefinition stype("MyIntSocket");
    stype.set_color({0.06, 0.52, 0.15, 1.0});
    stype.register_type();
  }
  {
    static SocketDefinition stype("OperatorSocket");
    stype.set_color({0.0, 0.0, 0.0, 0.0});
    stype.register_type();
  }
  {
    static SocketDefinition stype("MyFloatSocket");
    stype.set_color({1, 1, 1, 1});
    stype.add_dna_storage<bNodeSocketValueFloat>(
        "bNodeSocketValueFloat", [](bNodeSocketValueFloat *storage) { storage->value = 11.5f; });
    stype.add_draw_fn([](bContext *UNUSED(C),
                         uiLayout *layout,
                         PointerRNA *ptr,
                         PointerRNA *UNUSED(node_ptr),
                         const char *UNUSED(text)) {
      bNodeSocket *socket = (bNodeSocket *)ptr->data;
      bNodeSocketValueFloat *storage = get_socket_storage<bNodeSocketValueFloat>(socket);
      uiDefButF(uiLayoutGetBlock(layout),
                UI_BTYPE_NUM,
                0,
                "My Value",
                0,
                0,
                150,
                30,
                &storage->value,
                -1000,
                1000,
                3,
                20,
                "my x value");
    });
    stype.register_type();
  }

  data_socket_float = new BaseSocketDataType("Float", nodeSocketTypeFind("MyFloatSocket"));
  data_socket_int = new BaseSocketDataType("Integer", nodeSocketTypeFind("MyIntSocket"));
  data_socket_float_list = new ListSocketDataType("Float List",
                                                  nodeSocketTypeFind("NodeSocketFloatList"));
  data_socket_int_list = new ListSocketDataType("Integer List",
                                                nodeSocketTypeFind("NodeSocketIntList"));

  data_socket_float->m_list_type = data_socket_float_list;
  data_socket_float_list->m_base_type = data_socket_float;
  data_socket_int->m_list_type = data_socket_int_list;
  data_socket_int_list->m_base_type = data_socket_int;

  socket_data_types = new DataTypesInfo();
  socket_data_types->add_data_type(data_socket_float);
  socket_data_types->add_data_type(data_socket_int);
  socket_data_types->add_data_type(data_socket_float_list);
  socket_data_types->add_data_type(data_socket_int_list);
}

void free_socket_data_types()
{
  delete socket_data_types;
  delete data_socket_float;
  delete data_socket_int;
  delete data_socket_float_list;
  delete data_socket_int_list;
}

using BKE::VInputSocket;
using BKE::VirtualNodeTree;
using BKE::VNode;
using BKE::VOutputSocket;

struct SocketID {
  bNode *bnode;
  eNodeSocketInOut inout;
  std::string identifier;

  friend bool operator==(const SocketID &a, const SocketID &b)
  {
    return a.bnode == b.bnode && a.inout == b.inout && a.identifier == b.identifier;
  }
};

namespace BLI {
template<> class DefaultHash<SocketID> {
 public:
  uint32_t operator()(const SocketID &value) const
  {
    uint32_t h1 = DefaultHash<bNode *>{}(value.bnode);
    uint32_t h2 = DefaultHash<std::string>{}(value.identifier);
    return h1 * 42523 + h2;
  }
};
};  // namespace BLI

static void get_node_declarations(bNodeTree *ntree,
                                  ArrayRef<const VNode *> vnodes,
                                  LinearAllocator<> &allocator,
                                  MutableArrayRef<const NodeDecl *> r_node_decls)
{
  BLI::assert_same_size(vnodes, r_node_decls);

  /* TODO: handle reroute and frames */
  for (uint i : vnodes.index_range()) {
    const VNode &vnode = *vnodes[i];
    bNode *node = vnode.bnode();
    NodeDecl *node_decl = allocator.construct<NodeDecl>(ntree, node);
    node_decl->reserve_decls(allocator, vnode.inputs().size(), vnode.outputs().size());

    NodeBuilder builder{allocator, *node_decl};
    NodeDefinition::declare_node(node, builder);
    r_node_decls[i] = node_decl;
  }
}

static void rebuild_nodes_and_keep_state(ArrayRef<const VNode *> vnodes)
{
  if (vnodes.size() == 0) {
    return;
  }

  const VirtualNodeTree &vtree = vnodes[0]->tree();
  bNodeTree *ntree = vtree.btree();

  Set<std::pair<SocketID, SocketID>> links_to_restore;
  Map<SocketID, std::pair<const SocketDefinition *, void *>> value_per_socket;

  /* Remember socket states. */
  for (const VNode *vnode : vnodes) {
    for (const VInputSocket *vinput : vnode->inputs()) {
      SocketID id_to = {vinput->node().bnode(), SOCK_IN, vinput->identifier()};
      const SocketDefinition *def = SocketDefinition::get_from_socket(vinput->bsocket());
      void *storage_copy = def->get_dna_storage_copy(vinput->bsocket());
      if (storage_copy != nullptr) {
        value_per_socket.add_new(id_to, {def, storage_copy});
      }

      for (const VOutputSocket *voutput : vinput->directly_linked_sockets()) {
        SocketID id_from = {voutput->node().bnode(), SOCK_OUT, voutput->identifier()};
        links_to_restore.add({std::move(id_from), id_to});
      }
    }
    for (const VOutputSocket *voutput : vnode->outputs()) {
      SocketID id_from = {voutput->node().bnode(), SOCK_OUT, voutput->identifier()};
      const SocketDefinition *def = SocketDefinition::get_from_socket(voutput->bsocket());
      void *storage_copy = def->get_dna_storage_copy(voutput->bsocket());
      if (storage_copy != nullptr) {
        value_per_socket.add_new(id_from, {def, storage_copy});
      }

      for (const VInputSocket *vinput : voutput->directly_linked_sockets()) {
        SocketID id_to = {vinput->node().bnode(), SOCK_IN, vinput->identifier()};
        links_to_restore.add({id_from, std::move(id_to)});
      }
    }
  }

  /* Rebuild nodes. */
  LinearAllocator<> allocator;
  Array<const NodeDecl *> node_decls(vnodes.size(), nullptr);
  get_node_declarations(ntree, vnodes, allocator, node_decls);
  for (uint i : vnodes.index_range()) {
    node_decls[i]->build();
  }

  /* Restore links. */
  for (const std::pair<SocketID, SocketID> &link_info : links_to_restore) {
    const SocketID &from_id = link_info.first;
    const SocketID &to_id = link_info.second;
    BLI_assert(from_id.inout == SOCK_OUT);
    BLI_assert(to_id.inout == SOCK_IN);

    bNodeSocket *from_socket = nodeFindSocket(from_id.bnode, SOCK_OUT, from_id.identifier.c_str());
    bNodeSocket *to_socket = nodeFindSocket(to_id.bnode, SOCK_IN, to_id.identifier.c_str());

    if (from_socket && to_socket) {
      nodeAddLink(ntree, from_id.bnode, from_socket, to_id.bnode, to_socket);
    }
  }

  /* Restore socket values. */
  value_per_socket.foreach_item(
      [&](const SocketID &socket_id, const std::pair<const SocketDefinition *, void *> &value) {
        bNodeSocket *socket = nodeFindSocket(
            socket_id.bnode, socket_id.inout, socket_id.identifier.c_str());
        if (socket != nullptr) {
          value.first->free_dna_storage(socket->default_value);
          socket->default_value = value.second;
        }
        else {
          value.first->free_dna_storage(value.second);
        }
      });
}

static bool rebuild_currently_outdated_nodes(VirtualNodeTree &vtree,
                                             ArrayRef<const NodeDecl *> node_decls)
{
  Vector<const VNode *> vnodes_to_update;

  for (uint i : node_decls.index_range()) {
    if (!node_decls[i]->sockets_are_correct()) {
      vnodes_to_update.append(vtree.nodes()[i]);
    }
  }

  rebuild_nodes_and_keep_state(vnodes_to_update);
  return vnodes_to_update.size() > 0;
}

static bool remove_invalid_links(VirtualNodeTree &vtree)
{
  Vector<bNodeLink *> links_to_remove;
  for (const VInputSocket *vinput : vtree.all_input_sockets()) {
    for (bNodeLink *link : vinput->incident_links()) {
      if (link->fromsock->typeinfo != vinput->bsocket()->typeinfo) {
        links_to_remove.append(link);
      }
    }
  }

  for (bNodeLink *link : links_to_remove) {
    nodeRemLink(vtree.btree(), link);
  }

  return links_to_remove.size() > 0;
}

static bool run_operator_sockets(const VirtualNodeTree &vtree,
                                 ArrayRef<const NodeDecl *> node_decls)
{
  bNodeTree *ntree = vtree.btree();
  bool tree_changed = false;

  /* TODO: correctly handle multiple operator sockets per node */
  for (uint node_index : node_decls.index_range()) {
    const NodeDecl *node_decl = node_decls[node_index];
    if (node_decl->m_has_operator_input) {
      const VNode *vnode = vtree.nodes()[node_index];
      for (uint input_index : vnode->inputs().index_range()) {
        SocketDecl *socket_decl = node_decl->m_inputs[input_index];
        if (socket_decl->category() == SocketDeclCategory::Operator) {
          const VInputSocket &vinput = vnode->input(input_index);

          if (vinput.directly_linked_sockets().size() == 1 &&
              vinput.linked_sockets().size() == 1) {
            bNodeLink *link = vinput.incident_links()[0];
            nodeRemLink(ntree, link);
            tree_changed = true;

            bNodeSocket *directly_linked_socket = vinput.directly_linked_sockets()[0]->bsocket();
            bNodeSocket *linked_socket = vinput.linked_sockets()[0]->bsocket();

            OperatorSocketDecl *operator_decl = (OperatorSocketDecl *)socket_decl;
            OperatorSocketFn callback = operator_decl->callback();
            if (callback) {
              callback(
                  ntree, vnode->bnode(), vinput.bsocket(), directly_linked_socket, linked_socket);
            }
          }
          else {
            for (bNodeLink *link : vinput.incident_links()) {
              nodeRemLink(ntree, link);
              tree_changed = true;
            }
          }
        }
      }
    }
  }

  return tree_changed;
}

void update_sim_node_tree(bNodeTree *ntree)
{
  VirtualNodeTree vtree(ntree);
  LinearAllocator<> allocator;

  Array<const NodeDecl *> node_decls(vtree.nodes().size());
  get_node_declarations(ntree, vtree.nodes(), allocator, node_decls);

  if (rebuild_currently_outdated_nodes(vtree, node_decls)) {
    vtree.~VirtualNodeTree();
    new (&vtree) VirtualNodeTree(ntree);
  }
  if (run_operator_sockets(vtree, node_decls)) {
    vtree.~VirtualNodeTree();
    new (&vtree) VirtualNodeTree(ntree);
  }
  remove_invalid_links(vtree);
}