cmFileAPI.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmFileAPI_h
  4. #define cmFileAPI_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include "cm_jsoncpp_value.h"
  7. #include "cm_jsoncpp_writer.h"
  8. #include <map>
  9. #include <memory> // IWYU pragma: keep
  10. #include <string>
  11. #include <unordered_set>
  12. #include <vector>
  13. class cmake;
  14. class cmFileAPI
  15. {
  16. public:
  17. cmFileAPI(cmake* cm);
  18. /** Read fileapi queries from disk. */
  19. void ReadQueries();
  20. /** Write fileapi replies to disk. */
  21. void WriteReplies();
  22. /** Get the "cmake" instance with which this was constructed. */
  23. cmake* GetCMakeInstance() const { return this->CMakeInstance; }
  24. private:
  25. cmake* CMakeInstance;
  26. /** The api/v1 directory location. */
  27. std::string APIv1;
  28. /** The set of files we have just written to the reply directory. */
  29. std::unordered_set<std::string> ReplyFiles;
  30. static std::vector<std::string> LoadDir(std::string const& dir);
  31. void RemoveOldReplyFiles();
  32. // Keep in sync with ObjectKindName.
  33. enum class ObjectKind
  34. {
  35. InternalTest
  36. };
  37. /** Identify one object kind and major version. */
  38. struct Object
  39. {
  40. ObjectKind Kind;
  41. unsigned long Version = 0;
  42. friend bool operator<(Object const& l, Object const& r)
  43. {
  44. if (l.Kind != r.Kind) {
  45. return l.Kind < r.Kind;
  46. }
  47. return l.Version < r.Version;
  48. }
  49. };
  50. /** Represent content of a query directory. */
  51. struct Query
  52. {
  53. /** Known object kind-version pairs. */
  54. std::vector<Object> Known;
  55. /** Unknown object kind names. */
  56. std::vector<std::string> Unknown;
  57. };
  58. /** Whether the top-level query directory exists at all. */
  59. bool QueryExists = false;
  60. /** The content of the top-level query directory. */
  61. Query TopQuery;
  62. /** Reply index object generated for object kind/version.
  63. This populates the "objects" field of the reply index. */
  64. std::map<Object, Json::Value> ReplyIndexObjects;
  65. std::unique_ptr<Json::StreamWriter> JsonWriter;
  66. std::string WriteJsonFile(
  67. Json::Value const& value, std::string const& prefix,
  68. std::string (*computeSuffix)(std::string const&) = ComputeSuffixHash);
  69. static std::string ComputeSuffixHash(std::string const&);
  70. static std::string ComputeSuffixTime(std::string const&);
  71. static bool ReadQuery(std::string const& query,
  72. std::vector<Object>& objects);
  73. Json::Value BuildReplyIndex();
  74. Json::Value BuildCMake();
  75. Json::Value BuildReply(Query const& q);
  76. static Json::Value BuildReplyError(std::string const& error);
  77. Json::Value const& AddReplyIndexObject(Object const& o);
  78. static const char* ObjectKindName(ObjectKind kind);
  79. static std::string ObjectName(Object const& o);
  80. Json::Value BuildObject(Object const& object);
  81. Json::Value BuildInternalTest(Object const& object);
  82. };
  83. #endif