The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

724 lines
23KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. //==============================================================================
  21. struct ClassDatabase
  22. {
  23. //==============================================================================
  24. struct MemberInfo
  25. {
  26. enum CodeLocationType
  27. {
  28. declaration = 0,
  29. addedToParent,
  30. setBoundsParamX,
  31. setBoundsParamY,
  32. setBoundsParamW,
  33. setBoundsParamH,
  34. // WARNING! When you change any of these, also update the copy that lives in the live editing code
  35. numCodeLocationTypes
  36. };
  37. MemberInfo() {}
  38. MemberInfo (const MemberInfo& other)
  39. : name (other.name), type (other.type)
  40. {
  41. for (int i = 0; i < numCodeLocationTypes; ++i)
  42. locations[i] = other.locations[i];
  43. }
  44. MemberInfo (const String& nm, const String& ty)
  45. : name (nm), type (ty)
  46. {
  47. }
  48. MemberInfo (const ValueTree& v)
  49. : name (v [Ids::name].toString()),
  50. type (v [Ids::class_].toString())
  51. {
  52. for (int i = 0; i < numCodeLocationTypes; ++i)
  53. locations[i] = v [getIdentifierForCodeLocationType (i)].toString();
  54. }
  55. const String& getName() const { return name; }
  56. const String& getType() const { return type; }
  57. const SourceCodeRange& getLocation (CodeLocationType t) const
  58. {
  59. return locations[t];
  60. }
  61. void setLocation (CodeLocationType t, const SourceCodeRange& range)
  62. {
  63. locations[t] = range;
  64. }
  65. void mergeWith (const MemberInfo& other)
  66. {
  67. jassert (name == other.name);
  68. if (other.type.isNotEmpty())
  69. type = other.type;
  70. for (int i = 0; i < numCodeLocationTypes; ++i)
  71. if (other.locations[i].isValid())
  72. locations[i] = other.locations[i];
  73. }
  74. void nudgeAllCodeRanges (const String& file, const int insertPoint, const int delta)
  75. {
  76. for (int i = 0; i < numCodeLocationTypes; ++i)
  77. locations[i].nudge (file, insertPoint, delta);
  78. }
  79. void fileContentChanged (const String& file)
  80. {
  81. for (int i = 0; i < numCodeLocationTypes; ++i)
  82. locations[i].fileContentChanged (file);
  83. }
  84. ValueTree toValueTree() const
  85. {
  86. ValueTree m (Ids::MEMBER);
  87. m.setProperty (Ids::name, name, nullptr);
  88. m.setProperty (Ids::class_, type, nullptr);
  89. for (int i = 0; i < numCodeLocationTypes; ++i)
  90. locations[i].writeToValueTree (m, getIdentifierForCodeLocationType (i));
  91. return m;
  92. }
  93. private:
  94. String name, type;
  95. SourceCodeRange locations [numCodeLocationTypes];
  96. static Identifier getIdentifierForCodeLocationType (int typeIndex)
  97. {
  98. // (These need to remain in order)
  99. static_assert (setBoundsParamX + 1 == setBoundsParamY && setBoundsParamY + 1 == setBoundsParamW
  100. && setBoundsParamW + 1 == setBoundsParamH, "");
  101. static const Identifier ids[] =
  102. {
  103. "declaration",
  104. "addedToParent",
  105. "setBoundsParamX",
  106. "setBoundsParamY",
  107. "setBoundsParamW",
  108. "setBoundsParamH"
  109. };
  110. return ids [typeIndex];
  111. }
  112. };
  113. //==============================================================================
  114. struct MethodInfo
  115. {
  116. MethodInfo() {}
  117. MethodInfo (const MethodInfo& other)
  118. : name (other.name), returnType (other.returnType),
  119. declaration (other.declaration), definition (other.definition),
  120. numArgs (other.numArgs), flags (other.flags)
  121. {
  122. }
  123. String name, returnType;
  124. SourceCodeRange declaration, definition;
  125. int numArgs, flags;
  126. enum
  127. {
  128. isConstructor = 1,
  129. isDefaultConstructor = 2,
  130. isTemplated = 4,
  131. isPublic = 8
  132. };
  133. MethodInfo (const ValueTree& v)
  134. : name (v[Ids::name].toString()),
  135. returnType (v[Ids::returnType].toString()),
  136. declaration (v[Ids::declaration].toString()),
  137. definition (v[Ids::definition].toString()),
  138. numArgs (v[Ids::numArgs]),
  139. flags (v[Ids::flags])
  140. {
  141. }
  142. ValueTree toValueTree() const
  143. {
  144. ValueTree m (Ids::METHOD);
  145. m.setProperty (Ids::name, name, nullptr);
  146. m.setProperty (Ids::returnType, returnType, nullptr);
  147. m.setProperty (Ids::numArgs, numArgs, nullptr);
  148. m.setProperty (Ids::flags, flags, nullptr);
  149. declaration.writeToValueTree (m, Ids::declaration);
  150. definition.writeToValueTree (m, Ids::definition);
  151. return m;
  152. }
  153. void nudgeAllCodeRanges (const String& file, const int insertPoint, const int delta)
  154. {
  155. declaration.nudge (file, insertPoint, delta);
  156. definition.nudge (file, insertPoint, delta);
  157. }
  158. void fileContentChanged (const String& file)
  159. {
  160. declaration.fileContentChanged (file);
  161. definition.fileContentChanged (file);
  162. }
  163. };
  164. //==============================================================================
  165. struct InstantiationFlags
  166. {
  167. InstantiationFlags()
  168. : isAbstract (false),
  169. inAnonymousNamespace (false),
  170. noDefaultConstructor (false)
  171. {}
  172. InstantiationFlags (const InstantiationFlags& other)
  173. : isAbstract (other.isAbstract),
  174. inAnonymousNamespace (other.inAnonymousNamespace),
  175. noDefaultConstructor (other.noDefaultConstructor)
  176. {}
  177. bool canBeInstantiated() const noexcept
  178. {
  179. return ! (isAbstract || inAnonymousNamespace || noDefaultConstructor);
  180. }
  181. String getReasonForUnavailability() const
  182. {
  183. if (isAbstract) return "This class is abstract";
  184. if (noDefaultConstructor) return "This class has no default constructor";
  185. if (inAnonymousNamespace) return "This class is declared inside an anonymous namespace";
  186. return String();
  187. }
  188. bool isDisallowed (const InstantiationFlags& disallowedFlags) const
  189. {
  190. return ! ((disallowedFlags.isAbstract && isAbstract)
  191. || (disallowedFlags.inAnonymousNamespace && inAnonymousNamespace)
  192. || (disallowedFlags.noDefaultConstructor && noDefaultConstructor));
  193. }
  194. bool isAbstract;
  195. bool inAnonymousNamespace;
  196. bool noDefaultConstructor;
  197. };
  198. //==============================================================================
  199. struct Class
  200. {
  201. Class() {}
  202. ~Class() {}
  203. Class (const Class& other)
  204. : className (other.className), members (other.members),
  205. methods (other.methods), classDeclaration (other.classDeclaration),
  206. instantiationFlags (other.instantiationFlags)
  207. {
  208. }
  209. Class (const String& name, const InstantiationFlags& flags,
  210. const Array<MemberInfo>& m,
  211. const Array<MethodInfo>& meth,
  212. const SourceCodeRange& classDeclarationRange)
  213. : className (name),
  214. members (m), methods (meth),
  215. classDeclaration (classDeclarationRange),
  216. instantiationFlags (flags)
  217. {
  218. }
  219. Class& operator= (const Class& other)
  220. {
  221. className = other.className;
  222. members = other.members;
  223. methods = other.methods;
  224. classDeclaration = other.classDeclaration;
  225. instantiationFlags = other.instantiationFlags;
  226. return *this;
  227. }
  228. const String& getName() const noexcept { return className; }
  229. const InstantiationFlags& getInstantiationFlags() const
  230. {
  231. return instantiationFlags;
  232. }
  233. void setInstantiationFlags (const InstantiationFlags& newFlags)
  234. {
  235. instantiationFlags = newFlags;
  236. }
  237. const SourceCodeRange& getClassDeclarationRange() const
  238. {
  239. return classDeclaration;
  240. }
  241. const MemberInfo* findMember (const String& memberName) const
  242. {
  243. for (auto& m : members)
  244. if (m.getName() == memberName)
  245. return &m;
  246. return nullptr;
  247. }
  248. MemberInfo* findMember (const String& memberName)
  249. {
  250. return const_cast<MemberInfo*> (static_cast<const Class&>(*this).findMember (memberName));
  251. }
  252. const MethodInfo* getDefaultConstructor() const
  253. {
  254. for (const MethodInfo& m : methods)
  255. if ((m.flags & MethodInfo::isDefaultConstructor) != 0)
  256. return &m;
  257. return nullptr;
  258. }
  259. const MethodInfo* getConstructor() const
  260. {
  261. if (const MethodInfo* m = getDefaultConstructor())
  262. return m;
  263. for (const MethodInfo& m : methods)
  264. if ((m.flags & MethodInfo::isConstructor) != 0)
  265. return &m;
  266. return nullptr;
  267. }
  268. const MethodInfo* getResizedMethod() const
  269. {
  270. for (const MethodInfo& m : methods)
  271. if (m.name == "resized" && m.numArgs == 0)
  272. return &m;
  273. return nullptr;
  274. }
  275. File getMainSourceFile() const
  276. {
  277. if (const MethodInfo* m = getResizedMethod())
  278. if (m->definition.isValid())
  279. return m->definition.file;
  280. if (const MethodInfo* m = getConstructor())
  281. if (m->definition.isValid())
  282. return m->definition.file;
  283. for (auto& m : methods)
  284. if (m.definition.isValid() && File (m.definition.file).hasFileExtension ("cpp;mm"))
  285. return m.definition.file;
  286. for (auto& m : methods)
  287. if ((m.flags & MethodInfo::isConstructor) != 0 && m.definition.isValid())
  288. return m.definition.file;
  289. for (auto& m : methods)
  290. if (m.definition.isValid() && File (m.definition.file).exists())
  291. return m.definition.file;
  292. return {};
  293. }
  294. Array<File> getAllSourceFiles() const
  295. {
  296. Array<File> files;
  297. for (const MethodInfo& m : methods)
  298. {
  299. files.addIfNotAlreadyThere (m.declaration.file);
  300. files.addIfNotAlreadyThere (m.definition.file);
  301. }
  302. return files;
  303. }
  304. bool isDeclaredInFile (const File& file) const
  305. {
  306. return file == classDeclaration.file;
  307. }
  308. void mergeWith (const Class& other)
  309. {
  310. jassert (*this == other);
  311. if (other.classDeclaration.isValid())
  312. classDeclaration = other.classDeclaration;
  313. for (auto& m : other.members)
  314. {
  315. if (auto* existing = findMember (m.getName()))
  316. existing->mergeWith (m);
  317. else
  318. members.add (m);
  319. }
  320. }
  321. void nudgeAllCodeRanges (const String& file, int index, int delta)
  322. {
  323. for (MemberInfo& m : members) m.nudgeAllCodeRanges (file, index, delta);
  324. for (MethodInfo& m : methods) m.nudgeAllCodeRanges (file, index, delta);
  325. classDeclaration.nudge (file, index, delta);
  326. }
  327. void fileContentChanged (const String& file)
  328. {
  329. for (MemberInfo& m : members) m.fileContentChanged (file);
  330. for (MethodInfo& m : methods) m.fileContentChanged (file);
  331. classDeclaration.fileContentChanged (file);
  332. }
  333. Class (const ValueTree& v)
  334. {
  335. className = v[Ids::name];
  336. instantiationFlags.isAbstract = v[Ids::abstract];
  337. instantiationFlags.inAnonymousNamespace = v[Ids::anonymous];
  338. instantiationFlags.noDefaultConstructor = v[Ids::noDefConstructor];
  339. classDeclaration = v [Ids::classDecl].toString();
  340. for (int i = 0; i < v.getNumChildren(); ++i)
  341. members.add (MemberInfo (v.getChild(i)));
  342. }
  343. ValueTree toValueTree() const
  344. {
  345. ValueTree v (Ids::CLASS);
  346. v.setProperty (Ids::name, className, nullptr);
  347. v.setProperty (Ids::abstract, instantiationFlags.isAbstract, nullptr);
  348. v.setProperty (Ids::anonymous, instantiationFlags.inAnonymousNamespace, nullptr);
  349. v.setProperty (Ids::noDefConstructor, instantiationFlags.noDefaultConstructor, nullptr);
  350. classDeclaration.writeToValueTree (v, Ids::classDecl);
  351. for (const MemberInfo& m : members)
  352. v.appendChild (m.toValueTree(), nullptr);
  353. return v;
  354. }
  355. bool operator== (const Class& other) const noexcept { return className == other.className; }
  356. bool operator!= (const Class& other) const noexcept { return ! operator== (other); }
  357. bool operator< (const Class& other) const noexcept { return className < other.className; }
  358. const Array<MemberInfo>& getMembers() const { return members; }
  359. private:
  360. String className;
  361. Array<MemberInfo> members;
  362. Array<MethodInfo> methods;
  363. SourceCodeRange classDeclaration;
  364. InstantiationFlags instantiationFlags;
  365. JUCE_LEAK_DETECTOR (Class)
  366. };
  367. //==============================================================================
  368. struct Namespace
  369. {
  370. Namespace() : name ("Global Namespace") {}
  371. Namespace (const String& n, const String& full) : name (n), fullName (full) {}
  372. bool isEmpty() const noexcept
  373. {
  374. for (const auto& n : namespaces)
  375. if (! n.isEmpty())
  376. return false;
  377. return components.size() == 0;
  378. }
  379. int getTotalClassesAndNamespaces() const
  380. {
  381. int total = components.size();
  382. for (const auto& n : namespaces)
  383. total += n.getTotalClassesAndNamespaces();
  384. return total;
  385. }
  386. void add (const Class& c, const String::CharPointerType& localName)
  387. {
  388. auto nextDoubleColon = CharacterFunctions::find (localName, CharPointer_ASCII ("::"));
  389. if (nextDoubleColon.isEmpty())
  390. merge (c);
  391. else
  392. getOrCreateNamespace (String (localName, nextDoubleColon))->add (c, nextDoubleColon + 2);
  393. }
  394. bool containsRecursively (const Class& c) const
  395. {
  396. if (components.contains (c))
  397. return true;
  398. for (const auto& n : namespaces)
  399. if (n.containsRecursively (c))
  400. return true;
  401. return false;
  402. }
  403. const Class* findClass (const String& className) const
  404. {
  405. for (auto& c : components)
  406. if (c.getName() == className)
  407. return &c;
  408. for (auto& n : namespaces)
  409. if (auto* c = n.findClass (className))
  410. return c;
  411. return nullptr;
  412. }
  413. const MemberInfo* findClassMemberInfo (const String& className, const String& memberName) const
  414. {
  415. if (auto* classInfo = findClass (className))
  416. return classInfo->findMember (memberName);
  417. return nullptr;
  418. }
  419. void findClassesDeclaredInFile (Array<Class*>& results, const File& file)
  420. {
  421. for (auto& c : components)
  422. if (c.isDeclaredInFile (file))
  423. results.add (&c);
  424. for (auto& n : namespaces)
  425. n.findClassesDeclaredInFile (results, file);
  426. }
  427. void merge (const Namespace& other)
  428. {
  429. if (components.size() == 0)
  430. {
  431. components = other.components;
  432. }
  433. else
  434. {
  435. for (const auto& c : other.components)
  436. merge (c);
  437. }
  438. for (const auto& n : other.namespaces)
  439. getOrCreateNamespace (n.name)->merge (n);
  440. }
  441. void merge (const Class& c)
  442. {
  443. const int existing = components.indexOf (c);
  444. if (existing < 0)
  445. components.add (c);
  446. else
  447. components.getReference (existing).mergeWith (c);
  448. }
  449. Namespace* findNamespace (const String& targetName)
  450. {
  451. for (auto& n : namespaces)
  452. if (n.name == targetName)
  453. return &n;
  454. return nullptr;
  455. }
  456. Namespace* createNamespace (const String& newName)
  457. {
  458. namespaces.add (Namespace (newName, fullName + "::" + newName));
  459. return findNamespace (newName);
  460. }
  461. Namespace* getOrCreateNamespace (const String& newName)
  462. {
  463. if (Namespace* existing = findNamespace (newName))
  464. return existing;
  465. return createNamespace (newName);
  466. }
  467. void addInstantiableClasses (SortedSet<Class>& classes) const
  468. {
  469. for (const auto& c : components)
  470. if (c.getInstantiationFlags().canBeInstantiated())
  471. classes.add (c);
  472. for (const auto& n : namespaces)
  473. n.addInstantiableClasses (classes);
  474. }
  475. void swapWith (Namespace& other) noexcept
  476. {
  477. name.swapWith (other.name);
  478. components.swapWith (other.components);
  479. namespaces.swapWith (other.namespaces);
  480. }
  481. void nudgeAllCodeRanges (const String& file, int index, int delta)
  482. {
  483. for (auto& c : components) c.nudgeAllCodeRanges (file, index, delta);
  484. for (auto& n : namespaces) n.nudgeAllCodeRanges (file, index, delta);
  485. }
  486. void fileContentChanged (const String& file)
  487. {
  488. for (auto& c : components) c.fileContentChanged (file);
  489. for (auto& n : namespaces) n.fileContentChanged (file);
  490. }
  491. bool matches (const Namespace& other) const
  492. {
  493. if (name == other.name
  494. && components == other.components
  495. && namespaces.size() == other.namespaces.size())
  496. {
  497. for (int i = namespaces.size(); --i >= 0;)
  498. if (! namespaces.getReference (i).matches (other.namespaces.getReference(i)))
  499. return false;
  500. return true;
  501. }
  502. return false;
  503. }
  504. void getAllClassNames (StringArray& results, const InstantiationFlags& disallowedFlags) const
  505. {
  506. for (const auto& c : components)
  507. if (c.getInstantiationFlags().isDisallowed (disallowedFlags))
  508. results.add (c.getName());
  509. for (const auto& n : namespaces)
  510. n.getAllClassNames (results, disallowedFlags);
  511. }
  512. ValueTree toValueTree() const
  513. {
  514. ValueTree v (Ids::CLASSLIST);
  515. v.setProperty (Ids::name, name, nullptr);
  516. for (const auto& c : components) v.appendChild (c.toValueTree(), nullptr);
  517. for (const auto& n : namespaces) v.appendChild (n.toValueTree(), nullptr);
  518. return v;
  519. }
  520. void loadFromValueTree (const ValueTree& v)
  521. {
  522. name = v[Ids::name];
  523. for (int i = 0; i < v.getNumChildren(); ++i)
  524. {
  525. const ValueTree c (v.getChild(i));
  526. if (c.hasType (Ids::CLASS))
  527. components.add (Class (c));
  528. else if (c.hasType (Ids::CLASSLIST))
  529. createNamespace (c[Ids::name])->loadFromValueTree (c);
  530. }
  531. }
  532. bool operator== (const Namespace& other) const noexcept { return name == other.name; }
  533. bool operator!= (const Namespace& other) const noexcept { return ! operator== (other); }
  534. bool operator< (const Namespace& other) const noexcept { return name < other.name; }
  535. String name, fullName;
  536. SortedSet<Class> components;
  537. SortedSet<Namespace> namespaces;
  538. JUCE_LEAK_DETECTOR (Namespace)
  539. };
  540. struct ClassList
  541. {
  542. ClassList() {}
  543. void clear()
  544. {
  545. Namespace newNamespace;
  546. globalNamespace.swapWith (newNamespace);
  547. }
  548. void registerComp (const Class& comp)
  549. {
  550. globalNamespace.add (comp, comp.getName().getCharPointer());
  551. }
  552. void merge (const ClassList& other)
  553. {
  554. globalNamespace.merge (other.globalNamespace);
  555. }
  556. void swapWith (ClassList& other) noexcept
  557. {
  558. globalNamespace.swapWith (other.globalNamespace);
  559. }
  560. //==============================================================================
  561. ValueTree toValueTree() const
  562. {
  563. return globalNamespace.toValueTree();
  564. }
  565. static ClassList fromValueTree (const ValueTree& v)
  566. {
  567. ClassList l;
  568. l.globalNamespace.loadFromValueTree (v);
  569. return l;
  570. }
  571. Namespace globalNamespace;
  572. bool operator== (const ClassList& other) const noexcept { return globalNamespace.matches (other.globalNamespace); }
  573. bool operator!= (const ClassList& other) const noexcept { return ! operator== (other); }
  574. private:
  575. JUCE_LEAK_DETECTOR (ClassList)
  576. };
  577. };