File: projucer_ClassDatabase.h

package info (click to toggle)
juce 4.3.0~repack-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 62,060 kB
  • ctags: 38,418
  • sloc: cpp: 290,180; java: 3,322; makefile: 338; xml: 277; ansic: 255; sh: 182; python: 135
file content (712 lines) | stat: -rw-r--r-- 23,585 bytes parent folder | download
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
/*
  ==============================================================================

   This file is part of the JUCE library.
   Copyright (c) 2015 - ROLI Ltd.

   Permission is granted to use this software under the terms of either:
   a) the GPL v2 (or any later version)
   b) the Affero GPL v3

   Details of these licenses can be found at: www.gnu.org/licenses

   JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

   ------------------------------------------------------------------------------

   To release a closed-source product which uses JUCE, commercial licenses are
   available: visit www.juce.com for more information.

  ==============================================================================
*/

struct ClassDatabase
{
    //==============================================================================
    struct MemberInfo
    {
        enum CodeLocationType
        {
            declaration = 0,
            addedToParent,
            setBoundsParamX,
            setBoundsParamY,
            setBoundsParamW,
            setBoundsParamH,

            // WARNING! When you change any of these, also update the copy that lives in the live editing code

            numCodeLocationTypes
        };

        MemberInfo() {}

        MemberInfo (const MemberInfo& other)
            : name (other.name), type (other.type)
        {
            for (int i = 0; i < numCodeLocationTypes; ++i)
                locations[i] = other.locations[i];
        }

        MemberInfo (const String& nm, const String& ty)
            : name (nm), type (ty)
        {
        }

        MemberInfo (const ValueTree& v)
            : name (v [Ids::name].toString()),
              type (v [Ids::class_].toString())
        {
            for (int i = 0; i < numCodeLocationTypes; ++i)
                locations[i] = v [getIdentifierForCodeLocationType (i)].toString();
        }

        const String& getName() const   { return name; }
        const String& getType() const   { return type; }

        const SourceCodeRange& getLocation (CodeLocationType t) const
        {
            return locations[t];
        }

        void setLocation (CodeLocationType t, const SourceCodeRange& range)
        {
            locations[t] = range;
        }

        void mergeWith (const MemberInfo& other)
        {
            jassert (name == other.name);
            if (other.type.isNotEmpty())
                type = other.type;

            for (int i = 0; i < numCodeLocationTypes; ++i)
                if (other.locations[i].isValid())
                    locations[i] = other.locations[i];
        }

        void nudgeAllCodeRanges (const String& file, const int insertPoint, const int delta)
        {
            for (int i = 0; i < numCodeLocationTypes; ++i)
                locations[i].nudge (file, insertPoint, delta);
        }

        void fileContentChanged (const String& file)
        {
            for (int i = 0; i < numCodeLocationTypes; ++i)
                locations[i].fileContentChanged (file);
        }

        ValueTree toValueTree() const
        {
            ValueTree m (Ids::MEMBER);
            m.setProperty (Ids::name, name, nullptr);
            m.setProperty (Ids::class_, type, nullptr);

            for (int i = 0; i < numCodeLocationTypes; ++i)
                locations[i].writeToValueTree (m, getIdentifierForCodeLocationType (i));

            return m;
        }

    private:
        String name, type;
        SourceCodeRange locations [numCodeLocationTypes];

        static Identifier getIdentifierForCodeLocationType (int typeIndex)
        {
            // (These need to remain in order)
            static_assert (setBoundsParamX + 1 == setBoundsParamY && setBoundsParamY + 1 == setBoundsParamW
                            && setBoundsParamW + 1 == setBoundsParamH, "");

            static const Identifier ids[] =
            {
                "declaration",
                "addedToParent",
                "setBoundsParamX",
                "setBoundsParamY",
                "setBoundsParamW",
                "setBoundsParamH"
            };

            return ids [typeIndex];
        }
    };

    //==============================================================================
    struct MethodInfo
    {
        MethodInfo() {}

        MethodInfo (const MethodInfo& other)
            : name (other.name), returnType (other.returnType),
              declaration (other.declaration), definition (other.definition),
              numArgs (other.numArgs), flags (other.flags)
        {
        }

        String name, returnType;
        SourceCodeRange declaration, definition;
        int numArgs, flags;

        enum
        {
            isConstructor = 1,
            isDefaultConstructor = 2,
            isTemplated = 4,
            isPublic = 8
        };

        MethodInfo (const ValueTree& v)
            : name (v[Ids::name].toString()),
              returnType (v[Ids::returnType].toString()),
              declaration (v[Ids::declaration].toString()),
              definition (v[Ids::definition].toString()),
              numArgs (v[Ids::numArgs]),
              flags (v[Ids::flags])
        {
        }

        ValueTree toValueTree() const
        {
            ValueTree m (Ids::METHOD);
            m.setProperty (Ids::name, name, nullptr);
            m.setProperty (Ids::returnType, returnType, nullptr);
            m.setProperty (Ids::numArgs, numArgs, nullptr);
            m.setProperty (Ids::flags, flags, nullptr);
            declaration.writeToValueTree (m, Ids::declaration);
            definition.writeToValueTree (m, Ids::definition);
            return m;
        }

        void nudgeAllCodeRanges (const String& file, const int insertPoint, const int delta)
        {
            declaration.nudge (file, insertPoint, delta);
            definition.nudge (file, insertPoint, delta);
        }

        void fileContentChanged (const String& file)
        {
            declaration.fileContentChanged (file);
            definition.fileContentChanged (file);
        }
    };

    //==============================================================================
    struct InstantiationFlags
    {
        InstantiationFlags()
            : isAbstract (false),
              inAnonymousNamespace (false),
              noDefaultConstructor (false)
        {}

        InstantiationFlags (const InstantiationFlags& other)
            : isAbstract (other.isAbstract),
              inAnonymousNamespace (other.inAnonymousNamespace),
              noDefaultConstructor (other.noDefaultConstructor)
        {}

        bool canBeInstantiated() const noexcept
        {
            return ! (isAbstract || inAnonymousNamespace || noDefaultConstructor);
        }

        String getReasonForUnavailability() const
        {
            if (isAbstract)             return "This class is abstract";
            if (noDefaultConstructor)   return "This class has no default constructor";
            if (inAnonymousNamespace)   return "This class is declared inside an anonymous namespace";
            return String();
        }

        bool isDisallowed (const InstantiationFlags& disallowedFlags) const
        {
            return ! ((disallowedFlags.isAbstract && isAbstract)
                      || (disallowedFlags.inAnonymousNamespace && inAnonymousNamespace)
                      || (disallowedFlags.noDefaultConstructor && noDefaultConstructor));
        }

        bool isAbstract;
        bool inAnonymousNamespace;
        bool noDefaultConstructor;
    };

    //==============================================================================
    struct Class
    {
        Class() {}
        ~Class() {}

        Class (const Class& other)
            : className (other.className), members (other.members),
              methods (other.methods), classDeclaration (other.classDeclaration),
              instantiationFlags (other.instantiationFlags)
        {
        }

        Class (const String& name, const InstantiationFlags& flags,
               const Array<MemberInfo>& m,
               const Array<MethodInfo>& meth,
               const SourceCodeRange& classDeclarationRange)
            : className (name),
              members (m), methods (meth),
              classDeclaration (classDeclarationRange),
              instantiationFlags (flags)
        {
        }

        Class& operator= (const Class& other)
        {
            className = other.className;
            members = other.members;
            methods = other.methods;
            classDeclaration = other.classDeclaration;
            instantiationFlags = other.instantiationFlags;
            return *this;
        }

        const String& getName() const noexcept          { return className; }

        const InstantiationFlags& getInstantiationFlags() const
        {
            return instantiationFlags;
        }

        void setInstantiationFlags (const InstantiationFlags& newFlags)
        {
            instantiationFlags = newFlags;
        }

        const SourceCodeRange& getClassDeclarationRange() const
        {
            return classDeclaration;
        }

        MemberInfo* findMember (const String& memberName) const
        {
            for (MemberInfo& m : members)
                if (m.getName() == memberName)
                    return &m;

            return nullptr;
        }

        const MethodInfo* getDefaultConstructor() const
        {
            for (const MethodInfo& m : methods)
                if ((m.flags & MethodInfo::isDefaultConstructor) != 0)
                    return &m;

            return nullptr;
        }

        const MethodInfo* getConstructor() const
        {
            if (const MethodInfo* m = getDefaultConstructor())
                return m;

            for (const MethodInfo& m : methods)
                if ((m.flags & MethodInfo::isConstructor) != 0)
                    return &m;

            return nullptr;
        }

        const MethodInfo* getResizedMethod() const
        {
            for (const MethodInfo& m : methods)
                if (m.name == "resized" && m.numArgs == 0)
                    return &m;

            return nullptr;
        }

        File getMainSourceFile() const
        {
            if (const MethodInfo* m = getResizedMethod())
                if (m->definition.isValid())
                    return m->definition.file;

            if (const MethodInfo* m = getConstructor())
                if (m->definition.isValid())
                    return m->definition.file;

            for (MethodInfo& m : methods)
                if (m.definition.isValid() && File (m.definition.file).hasFileExtension ("cpp;mm"))
                    return m.definition.file;

            for (MethodInfo& m : methods)
                if ((m.flags & MethodInfo::isConstructor) != 0 && m.definition.isValid())
                    return m.definition.file;

            for (MethodInfo& m : methods)
                if (m.definition.isValid() && File (m.definition.file).exists())
                    return m.definition.file;

            return File();
        }

        Array<File> getAllSourceFiles() const
        {
            Array<File> files;

            for (const MethodInfo& m : methods)
            {
                files.addIfNotAlreadyThere (m.declaration.file);
                files.addIfNotAlreadyThere (m.definition.file);
            }

            return files;
        }

        bool isDeclaredInFile (const File& file) const
        {
            return file == classDeclaration.file;
        }

        void mergeWith (const Class& other)
        {
            jassert (*this == other);

            if (other.classDeclaration.isValid())
                classDeclaration = other.classDeclaration;

            for (const MemberInfo& m : other.members)
            {
                if (MemberInfo* existing = findMember (m.getName()))
                    existing->mergeWith (m);
                else
                    members.add (m);
            }
        }

        void nudgeAllCodeRanges (const String& file, int index, int delta)
        {
            for (MemberInfo& m : members)   m.nudgeAllCodeRanges (file, index, delta);
            for (MethodInfo& m : methods)   m.nudgeAllCodeRanges (file, index, delta);

            classDeclaration.nudge (file, index, delta);
        }

        void fileContentChanged (const String& file)
        {
            for (MemberInfo& m : members)   m.fileContentChanged (file);
            for (MethodInfo& m : methods)   m.fileContentChanged (file);

            classDeclaration.fileContentChanged (file);
        }

        Class (const ValueTree& v)
        {
            className = v[Ids::name];
            instantiationFlags.isAbstract = v[Ids::abstract];
            instantiationFlags.inAnonymousNamespace = v[Ids::anonymous];
            instantiationFlags.noDefaultConstructor = v[Ids::noDefConstructor];

            classDeclaration = v [Ids::classDecl].toString();

            for (int i = 0; i < v.getNumChildren(); ++i)
                members.add (MemberInfo (v.getChild(i)));
        }

        ValueTree toValueTree() const
        {
            ValueTree v (Ids::CLASS);
            v.setProperty (Ids::name, className, nullptr);
            v.setProperty (Ids::abstract, instantiationFlags.isAbstract, nullptr);
            v.setProperty (Ids::anonymous, instantiationFlags.inAnonymousNamespace, nullptr);
            v.setProperty (Ids::noDefConstructor, instantiationFlags.noDefaultConstructor, nullptr);
            classDeclaration.writeToValueTree (v, Ids::classDecl);

            for (const MemberInfo& m : members)
                v.addChild (m.toValueTree(), -1, nullptr);

            return v;
        }

        bool operator== (const Class& other) const noexcept  { return className == other.className; }
        bool operator!= (const Class& other) const noexcept  { return ! operator== (other); }
        bool operator<  (const Class& other) const noexcept  { return className < other.className; }

        const Array<MemberInfo>& getMembers() const     { return members; }

    private:
        String className;
        Array<MemberInfo> members;
        Array<MethodInfo> methods;
        SourceCodeRange classDeclaration;
        InstantiationFlags instantiationFlags;

        JUCE_LEAK_DETECTOR (Class)
    };

    //==============================================================================
    struct Namespace
    {
        Namespace()  : name ("Global Namespace") {}
        Namespace (const String& n, const String& full)  : name (n), fullName (full) {}

        bool isEmpty() const noexcept
        {
            for (const auto& n : namespaces)
                if (! n.isEmpty())
                    return false;

            return components.size() == 0;
        }

        int getTotalClassesAndNamespaces() const
        {
            int total = components.size();

            for (const auto& n : namespaces)
                total += n.getTotalClassesAndNamespaces();

            return total;
        }

        void add (const Class& c, const String::CharPointerType& localName)
        {
            const String::CharPointerType nextDoubleColon (CharacterFunctions::find (localName, CharPointer_ASCII ("::")));

            if (nextDoubleColon.isEmpty())
                merge (c);
            else
                getOrCreateNamespace (String (localName, nextDoubleColon))->add (c, nextDoubleColon + 2);
        }

        bool containsRecursively (const Class& c) const
        {
            if (components.contains (c))
                return true;

            for (const auto& n : namespaces)
                if (n.containsRecursively (c))
                    return true;

            return false;
        }

        Class* findClass (const String& className) const
        {
            for (Class& c : components)
                if (c.getName() == className)
                    return &c;

            for (const auto& n : namespaces)
                if (Class* c = n.findClass (className))
                    return c;

            return nullptr;
        }

        const MemberInfo* findClassMemberInfo (const String& className, const String& memberName) const
        {
            if (const Class* classInfo = findClass (className))
                return classInfo->findMember (memberName);

            return nullptr;
        }

        void findClassesDeclaredInFile (Array<Class*>& results, const File& file) const
        {
            for (Class& c : components)
                if (c.isDeclaredInFile (file))
                    results.add (&c);

            for (const auto& n : namespaces)
                n.findClassesDeclaredInFile (results, file);
        }

        void merge (const Namespace& other)
        {
            if (components.size() == 0)
            {
                components = other.components;
            }
            else
            {
                for (const auto& c : other.components)
                    merge (c);
            }

            for (const auto& n : other.namespaces)
                getOrCreateNamespace (n.name)->merge (n);
        }

        void merge (const Class& c)
        {
            const int existing = components.indexOf (c);

            if (existing < 0)
                components.add (c);
            else
                components.getReference (existing).mergeWith (c);
        }

        Namespace* findNamespace (const String& targetName) const
        {
            for (auto& n : namespaces)
                if (n.name == targetName)
                    return &n;

            return nullptr;
        }

        Namespace* createNamespace (const String& newName)
        {
            namespaces.add (Namespace (newName, fullName + "::" + newName));
            return findNamespace (newName);
        }

        Namespace* getOrCreateNamespace (const String& newName)
        {
            if (Namespace* existing = findNamespace (newName))
                return existing;

            return createNamespace (newName);
        }

        void addInstantiableClasses (SortedSet<Class>& classes) const
        {
            for (const auto& c : components)
                if (c.getInstantiationFlags().canBeInstantiated())
                    classes.add (c);

            for (const auto& n : namespaces)
                n.addInstantiableClasses (classes);
        }

        void swapWith (Namespace& other) noexcept
        {
            name.swapWith (other.name);
            components.swapWith (other.components);
            namespaces.swapWith (other.namespaces);
        }

        void nudgeAllCodeRanges (const String& file, int index, int delta) const
        {
            for (auto& c : components)  c.nudgeAllCodeRanges (file, index, delta);
            for (auto& n : namespaces)  n.nudgeAllCodeRanges (file, index, delta);
        }

        void fileContentChanged (const String& file) const
        {
            for (auto& c : components)  c.fileContentChanged (file);
            for (auto& n : namespaces)  n.fileContentChanged (file);
        }

        bool matches (const Namespace& other) const
        {
            if (name == other.name
                 && components == other.components
                 && namespaces.size() == other.namespaces.size())
            {
                for (int i = namespaces.size(); --i >= 0;)
                    if (! namespaces.getReference (i).matches (other.namespaces.getReference(i)))
                        return false;

                return true;
            }

            return false;
        }

        void getAllClassNames (StringArray& results, const InstantiationFlags& disallowedFlags) const
        {
            for (const auto& c : components)
                if (c.getInstantiationFlags().isDisallowed (disallowedFlags))
                    results.add (c.getName());

            for (const auto& n : namespaces)
                n.getAllClassNames (results, disallowedFlags);
        }

        ValueTree toValueTree() const
        {
            ValueTree v (Ids::CLASSLIST);

            v.setProperty (Ids::name, name, nullptr);

            for (const auto& c : components)    v.addChild (c.toValueTree(), -1, nullptr);
            for (const auto& n : namespaces)    v.addChild (n.toValueTree(), -1, nullptr);

            return v;
        }

        void loadFromValueTree (const ValueTree& v)
        {
            name = v[Ids::name];

            for (int i = 0; i < v.getNumChildren(); ++i)
            {
                const ValueTree c (v.getChild(i));

                if (c.hasType (Ids::CLASS))
                    components.add (Class (c));
                else if (c.hasType (Ids::CLASSLIST))
                    createNamespace (c[Ids::name])->loadFromValueTree (c);
            }
        }

        bool operator== (const Namespace& other) const noexcept  { return name == other.name; }
        bool operator!= (const Namespace& other) const noexcept  { return ! operator== (other); }
        bool operator<  (const Namespace& other) const noexcept  { return name < other.name; }

        String name, fullName;
        SortedSet<Class> components;
        SortedSet<Namespace> namespaces;

        JUCE_LEAK_DETECTOR (Namespace)
    };

    struct ClassList
    {
        ClassList() {}

        void clear()
        {
            Namespace newNamespace;
            globalNamespace.swapWith (newNamespace);
        }

        void registerComp (const Class& comp)
        {
            globalNamespace.add (comp, comp.getName().getCharPointer());
        }

        void merge (const ClassList& other)
        {
            globalNamespace.merge (other.globalNamespace);
        }

        void swapWith (ClassList& other) noexcept
        {
            globalNamespace.swapWith (other.globalNamespace);
        }

        //==============================================================================
        ValueTree toValueTree() const
        {
            return globalNamespace.toValueTree();
        }

        static ClassList fromValueTree (const ValueTree& v)
        {
            ClassList l;
            l.globalNamespace.loadFromValueTree (v);
            return l;
        }

        Namespace globalNamespace;

        bool operator== (const ClassList& other) const noexcept  { return globalNamespace.matches (other.globalNamespace); }
        bool operator!= (const ClassList& other) const noexcept  { return ! operator== (other); }

    private:
        JUCE_LEAK_DETECTOR (ClassList)
    };
};