From 672b4b27d81ad4de17385fde67b3fb3ccd694a2e Mon Sep 17 00:00:00 2001 From: Davis King Date: Sat, 26 Apr 2014 15:35:45 -0400 Subject: [PATCH] Added locally_change_current_dir --- dlib/misc_api.h | 2 ++ dlib/misc_api/misc_api_kernel_abstract.h | 35 ++++++++++++++++++ dlib/misc_api/misc_api_shared.h | 45 ++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 dlib/misc_api/misc_api_shared.h diff --git a/dlib/misc_api.h b/dlib/misc_api.h index df4f73011..acf87be7c 100644 --- a/dlib/misc_api.h +++ b/dlib/misc_api.h @@ -14,5 +14,7 @@ #include "misc_api/posix.h" #endif +#include "misc_api/misc_api_shared.h" + #endif // DLIB_MISC_APi_ diff --git a/dlib/misc_api/misc_api_kernel_abstract.h b/dlib/misc_api/misc_api_kernel_abstract.h index 1e2ff1d47..103aa6612 100644 --- a/dlib/misc_api/misc_api_kernel_abstract.h +++ b/dlib/misc_api/misc_api_kernel_abstract.h @@ -59,6 +59,41 @@ namespace dlib to change the current working directory. !*/ +// ---------------------------------------------------------------------------------------- + + class locally_change_current_dir : noncopyable + { + /*! + WHAT THIS OBJECT REPRESENTS + This object is a RAII tool for safely switching the current directory + to a new directory and then automatically switching back to the original + directory upon this object's destruction. + !*/ + public: + explicit locally_change_current_dir ( + const std::string& new_dir + ); + /*! + ensures + - calls set_current_dir(new_dir) + - #old_dir() == The value of get_current_dir() prior to switching to new_dir. + !*/ + + const std::string& old_dir ( + ) const; + /*! + ensures + - returns the directory we switch back to once this object is destructed. + !*/ + + ~locally_change_current_dir( + ); + /*! + ensures + - calls set_current_dir(old_dir()) + !*/ + }; + // ---------------------------------------------------------------------------------------- class dir_create_error : public error { diff --git a/dlib/misc_api/misc_api_shared.h b/dlib/misc_api/misc_api_shared.h new file mode 100644 index 000000000..8826c2d0f --- /dev/null +++ b/dlib/misc_api/misc_api_shared.h @@ -0,0 +1,45 @@ +// Copyright (C) 2014 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#ifndef DLIB_MISC_API_ShARED_H__ +#define DLIB_MISC_API_ShARED_H__ + +#include +#include "../noncopyable.h" + +namespace dlib +{ + +// ---------------------------------------------------------------------------------------- + + class locally_change_current_dir : noncopyable + { + public: + explicit locally_change_current_dir ( + const std::string& new_dir + ) + { + _old_dir = get_current_dir(); + set_current_dir(new_dir); + } + + ~locally_change_current_dir() + { + set_current_dir(_old_dir); + } + + const std::string& old_dir ( + ) const + { + return _old_dir; + } + + private: + std::string _old_dir; + }; + +// ---------------------------------------------------------------------------------------- + +} + +#endif // DLIB_MISC_API_ShARED_H__ +