ctkScopedCurrentDir.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkScopedCurrentDir_h
  15. #define __ctkScopedCurrentDir_h
  16. // Qt includes
  17. #include <QScopedPointer>
  18. // CTK includes
  19. #include "ctkCoreExport.h"
  20. class ctkScopedCurrentDirPrivate;
  21. ///
  22. /// \brief Use this class to change the current application directory in a given scope
  23. /// and automatically restore it.
  24. ///
  25. /// This is particulary useful in case a plugin and its dependent libraries should be loaded from
  26. /// a known directory.
  27. /// Indeed, changing the application PATH, LD_LIBRARY_PATH or DYLD_LIBRARY_PATH within the current
  28. /// process won't have the desired effect. The loader checks for these variables only once when
  29. /// the process starts.
  30. /// \sa http://stackoverflow.com/questions/856116/changing-ld-library-path-at-runtime-for-ctypes
  31. /// \sa http://stackoverflow.com/questions/1178094/change-current-process-environment
  32. ///
  33. /// \ingroup Core
  34. class CTK_CORE_EXPORT ctkScopedCurrentDir
  35. {
  36. public:
  37. explicit ctkScopedCurrentDir(const QString& path);
  38. virtual ~ctkScopedCurrentDir();
  39. /// Return the current application path
  40. /// \sa QDir::currentPath
  41. QString currentPath()const;
  42. /// Return saved current path
  43. QString savedCurrentPath()const;
  44. protected:
  45. QScopedPointer<ctkScopedCurrentDirPrivate> d_ptr;
  46. private:
  47. Q_DECLARE_PRIVATE(ctkScopedCurrentDir);
  48. Q_DISABLE_COPY(ctkScopedCurrentDir);
  49. };
  50. #endif