cmWorkingDirectory.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 cmWorkingDirectory_h
  4. #define cmWorkingDirectory_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <string>
  7. /** \class cmWorkingDirectory
  8. * \brief An RAII class to manipulate the working directory.
  9. *
  10. * The current working directory is set to the location given to the
  11. * constructor. The working directory can be changed again as needed
  12. * by calling SetDirectory(). When the object is destroyed, the destructor
  13. * will restore the working directory to what it was when the object was
  14. * created, regardless of any calls to SetDirectory() in the meantime.
  15. */
  16. class cmWorkingDirectory
  17. {
  18. public:
  19. cmWorkingDirectory(std::string const& newdir);
  20. ~cmWorkingDirectory();
  21. bool SetDirectory(std::string const& newdir);
  22. void Pop();
  23. bool Failed() const { return ResultCode != 0; }
  24. /** \return 0 if the last attempt to set the working directory was
  25. * successful. If it failed, the value returned will be the
  26. * \c errno value associated with the failure. A description
  27. * of the error code can be obtained by passing the result
  28. * to \c std::strerror().
  29. */
  30. int GetLastResult() const { return ResultCode; }
  31. private:
  32. std::string OldDir;
  33. int ResultCode;
  34. };
  35. #endif