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.

719 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. MemberInfo* findMember (const String& memberName) const
  242. {
  243. for (MemberInfo& m : members)
  244. if (m.getName() == memberName)
  245. return &m;
  246. return nullptr;
  247. }
  248. const MethodInfo* getDefaultConstructor() const
  249. {
  250. for (const MethodInfo& m : methods)
  251. if ((m.flags & MethodInfo::isDefaultConstructor) != 0)
  252. return &m;
  253. return nullptr;
  254. }
  255. const MethodInfo* getConstructor() const
  256. {
  257. if (const MethodInfo* m = getDefaultConstructor())
  258. return m;
  259. for (const MethodInfo& m : methods)
  260. if ((m.flags & MethodInfo::isConstructor) != 0)
  261. return &m;
  262. return nullptr;
  263. }
  264. const MethodInfo* getResizedMethod() const
  265. {
  266. for (const MethodInfo& m : methods)
  267. if (m.name == "resized" && m.numArgs == 0)
  268. return &m;
  269. return nullptr;
  270. }
  271. File getMainSourceFile() const
  272. {
  273. if (const MethodInfo* m = getResizedMethod())
  274. if (m->definition.isValid())
  275. return m->definition.file;
  276. if (const MethodInfo* m = getConstructor())
  277. if (m->definition.isValid())
  278. return m->definition.file;
  279. for (MethodInfo& m : methods)
  280. if (m.definition.isValid() && File (m.definition.file).hasFileExtension ("cpp;mm"))
  281. return m.definition.file;
  282. for (MethodInfo& m : methods)
  283. if ((m.flags & MethodInfo::isConstructor) != 0 && m.definition.isValid())
  284. return m.definition.file;
  285. for (MethodInfo& m : methods)
  286. if (m.definition.isValid() && File (m.definition.file).exists())
  287. return m.definition.file;
  288. return {};
  289. }
  290. Array<File> getAllSourceFiles() const
  291. {
  292. Array<File> files;
  293. for (const MethodInfo& m : methods)
  294. {
  295. files.addIfNotAlreadyThere (m.declaration.file);
  296. files.addIfNotAlreadyThere (m.definition.file);
  297. }
  298. return files;
  299. }
  300. bool isDeclaredInFile (const File& file) const
  301. {
  302. return file == classDeclaration.file;
  303. }
  304. void mergeWith (const Class& other)
  305. {
  306. jassert (*this == other);
  307. if (other.classDeclaration.isValid())
  308. classDeclaration = other.classDeclaration;
  309. for (const MemberInfo& m : other.members)
  310. {
  311. if (MemberInfo* existing = findMember (m.getName()))
  312. existing->mergeWith (m);
  313. else
  314. members.add (m);
  315. }
  316. }
  317. void nudgeAllCodeRanges (const String& file, int index, int delta)
  318. {
  319. for (MemberInfo& m : members) m.nudgeAllCodeRanges (file, index, delta);
  320. for (MethodInfo& m : methods) m.nudgeAllCodeRanges (file, index, delta);
  321. classDeclaration.nudge (file, index, delta);
  322. }
  323. void fileContentChanged (const String& file)
  324. {
  325. for (MemberInfo& m : members) m.fileContentChanged (file);
  326. for (MethodInfo& m : methods) m.fileContentChanged (file);
  327. classDeclaration.fileContentChanged (file);
  328. }
  329. Class (const ValueTree& v)
  330. {
  331. className = v[Ids::name];
  332. instantiationFlags.isAbstract = v[Ids::abstract];
  333. instantiationFlags.inAnonymousNamespace = v[Ids::anonymous];
  334. instantiationFlags.noDefaultConstructor = v[Ids::noDefConstructor];
  335. classDeclaration = v [Ids::classDecl].toString();
  336. for (int i = 0; i < v.getNumChildren(); ++i)
  337. members.add (MemberInfo (v.getChild(i)));
  338. }
  339. ValueTree toValueTree() const
  340. {
  341. ValueTree v (Ids::CLASS);
  342. v.setProperty (Ids::name, className, nullptr);
  343. v.setProperty (Ids::abstract, instantiationFlags.isAbstract, nullptr);
  344. v.setProperty (Ids::anonymous, instantiationFlags.inAnonymousNamespace, nullptr);
  345. v.setProperty (Ids::noDefConstructor, instantiationFlags.noDefaultConstructor, nullptr);
  346. classDeclaration.writeToValueTree (v, Ids::classDecl);
  347. for (const MemberInfo& m : members)
  348. v.addChild (m.toValueTree(), -1, nullptr);
  349. return v;
  350. }
  351. bool operator== (const Class& other) const noexcept { return className == other.className; }
  352. bool operator!= (const Class& other) const noexcept { return ! operator== (other); }
  353. bool operator< (const Class& other) const noexcept { return className < other.className; }
  354. const Array<MemberInfo>& getMembers() const { return members; }
  355. private:
  356. String className;
  357. Array<MemberInfo> members;
  358. Array<MethodInfo> methods;
  359. SourceCodeRange classDeclaration;
  360. InstantiationFlags instantiationFlags;
  361. JUCE_LEAK_DETECTOR (Class)
  362. };
  363. //==============================================================================
  364. struct Namespace
  365. {
  366. Namespace() : name ("Global Namespace") {}
  367. Namespace (const String& n, const String& full) : name (n), fullName (full) {}
  368. bool isEmpty() const noexcept
  369. {
  370. for (const auto& n : namespaces)
  371. if (! n.isEmpty())
  372. return false;
  373. return components.size() == 0;
  374. }
  375. int getTotalClassesAndNamespaces() const
  376. {
  377. int total = components.size();
  378. for (const auto& n : namespaces)
  379. total += n.getTotalClassesAndNamespaces();
  380. return total;
  381. }
  382. void add (const Class& c, const String::CharPointerType& localName)
  383. {
  384. const String::CharPointerType nextDoubleColon (CharacterFunctions::find (localName, CharPointer_ASCII ("::")));
  385. if (nextDoubleColon.isEmpty())
  386. merge (c);
  387. else
  388. getOrCreateNamespace (String (localName, nextDoubleColon))->add (c, nextDoubleColon + 2);
  389. }
  390. bool containsRecursively (const Class& c) const
  391. {
  392. if (components.contains (c))
  393. return true;
  394. for (const auto& n : namespaces)
  395. if (n.containsRecursively (c))
  396. return true;
  397. return false;
  398. }
  399. Class* findClass (const String& className) const
  400. {
  401. for (Class& c : components)
  402. if (c.getName() == className)
  403. return &c;
  404. for (const auto& n : namespaces)
  405. if (Class* c = n.findClass (className))
  406. return c;
  407. return nullptr;
  408. }
  409. const MemberInfo* findClassMemberInfo (const String& className, const String& memberName) const
  410. {
  411. if (const Class* classInfo = findClass (className))
  412. return classInfo->findMember (memberName);
  413. return nullptr;
  414. }
  415. void findClassesDeclaredInFile (Array<Class*>& results, const File& file) const
  416. {
  417. for (Class& c : components)
  418. if (c.isDeclaredInFile (file))
  419. results.add (&c);
  420. for (const auto& n : namespaces)
  421. n.findClassesDeclaredInFile (results, file);
  422. }
  423. void merge (const Namespace& other)
  424. {
  425. if (components.size() == 0)
  426. {
  427. components = other.components;
  428. }
  429. else
  430. {
  431. for (const auto& c : other.components)
  432. merge (c);
  433. }
  434. for (const auto& n : other.namespaces)
  435. getOrCreateNamespace (n.name)->merge (n);
  436. }
  437. void merge (const Class& c)
  438. {
  439. const int existing = components.indexOf (c);
  440. if (existing < 0)
  441. components.add (c);
  442. else
  443. components.getReference (existing).mergeWith (c);
  444. }
  445. Namespace* findNamespace (const String& targetName) const
  446. {
  447. for (auto& n : namespaces)
  448. if (n.name == targetName)
  449. return &n;
  450. return nullptr;
  451. }
  452. Namespace* createNamespace (const String& newName)
  453. {
  454. namespaces.add (Namespace (newName, fullName + "::" + newName));
  455. return findNamespace (newName);
  456. }
  457. Namespace* getOrCreateNamespace (const String& newName)
  458. {
  459. if (Namespace* existing = findNamespace (newName))
  460. return existing;
  461. return createNamespace (newName);
  462. }
  463. void addInstantiableClasses (SortedSet<Class>& classes) const
  464. {
  465. for (const auto& c : components)
  466. if (c.getInstantiationFlags().canBeInstantiated())
  467. classes.add (c);
  468. for (const auto& n : namespaces)
  469. n.addInstantiableClasses (classes);
  470. }
  471. void swapWith (Namespace& other) noexcept
  472. {
  473. name.swapWith (other.name);
  474. components.swapWith (other.components);
  475. namespaces.swapWith (other.namespaces);
  476. }
  477. void nudgeAllCodeRanges (const String& file, int index, int delta) const
  478. {
  479. for (auto& c : components) c.nudgeAllCodeRanges (file, index, delta);
  480. for (auto& n : namespaces) n.nudgeAllCodeRanges (file, index, delta);
  481. }
  482. void fileContentChanged (const String& file) const
  483. {
  484. for (auto& c : components) c.fileContentChanged (file);
  485. for (auto& n : namespaces) n.fileContentChanged (file);
  486. }
  487. bool matches (const Namespace& other) const
  488. {
  489. if (name == other.name
  490. && components == other.components
  491. && namespaces.size() == other.namespaces.size())
  492. {
  493. for (int i = namespaces.size(); --i >= 0;)
  494. if (! namespaces.getReference (i).matches (other.namespaces.getReference(i)))
  495. return false;
  496. return true;
  497. }
  498. return false;
  499. }
  500. void getAllClassNames (StringArray& results, const InstantiationFlags& disallowedFlags) const
  501. {
  502. for (const auto& c : components)
  503. if (c.getInstantiationFlags().isDisallowed (disallowedFlags))
  504. results.add (c.getName());
  505. for (const auto& n : namespaces)
  506. n.getAllClassNames (results, disallowedFlags);
  507. }
  508. ValueTree toValueTree() const
  509. {
  510. ValueTree v (Ids::CLASSLIST);
  511. v.setProperty (Ids::name, name, nullptr);
  512. for (const auto& c : components) v.addChild (c.toValueTree(), -1, nullptr);
  513. for (const auto& n : namespaces) v.addChild (n.toValueTree(), -1, nullptr);
  514. return v;
  515. }
  516. void loadFromValueTree (const ValueTree& v)
  517. {
  518. name = v[Ids::name];
  519. for (int i = 0; i < v.getNumChildren(); ++i)
  520. {
  521. const ValueTree c (v.getChild(i));
  522. if (c.hasType (Ids::CLASS))
  523. components.add (Class (c));
  524. else if (c.hasType (Ids::CLASSLIST))
  525. createNamespace (c[Ids::name])->loadFromValueTree (c);
  526. }
  527. }
  528. bool operator== (const Namespace& other) const noexcept { return name == other.name; }
  529. bool operator!= (const Namespace& other) const noexcept { return ! operator== (other); }
  530. bool operator< (const Namespace& other) const noexcept { return name < other.name; }
  531. String name, fullName;
  532. SortedSet<Class> components;
  533. SortedSet<Namespace> namespaces;
  534. JUCE_LEAK_DETECTOR (Namespace)
  535. };
  536. struct ClassList
  537. {
  538. ClassList() {}
  539. void clear()
  540. {
  541. Namespace newNamespace;
  542. globalNamespace.swapWith (newNamespace);
  543. }
  544. void registerComp (const Class& comp)
  545. {
  546. globalNamespace.add (comp, comp.getName().getCharPointer());
  547. }
  548. void merge (const ClassList& other)
  549. {
  550. globalNamespace.merge (other.globalNamespace);
  551. }
  552. void swapWith (ClassList& other) noexcept
  553. {
  554. globalNamespace.swapWith (other.globalNamespace);
  555. }
  556. //==============================================================================
  557. ValueTree toValueTree() const
  558. {
  559. return globalNamespace.toValueTree();
  560. }
  561. static ClassList fromValueTree (const ValueTree& v)
  562. {
  563. ClassList l;
  564. l.globalNamespace.loadFromValueTree (v);
  565. return l;
  566. }
  567. Namespace globalNamespace;
  568. bool operator== (const ClassList& other) const noexcept { return globalNamespace.matches (other.globalNamespace); }
  569. bool operator!= (const ClassList& other) const noexcept { return ! operator== (other); }
  570. private:
  571. JUCE_LEAK_DETECTOR (ClassList)
  572. };
  573. };