Added support for forwarding/implementation includes to enhance compile time and hide system headers.

This commit is contained in:
gulrak 2019-03-24 01:53:44 -07:00
parent 72e8d2e950
commit 03f849d23f
9 changed files with 670 additions and 449 deletions

View File

@ -11,7 +11,7 @@ if(CMAKE_CXX_STANDARD LESS 11)
endif() endif()
add_library(ghc_filesystem INTERFACE) add_library(ghc_filesystem INTERFACE)
target_sources(ghc_filesystem INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include/ghc/filesystem.hpp) target_sources(ghc_filesystem INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include/ghc/filesystem.hpp ${CMAKE_CURRENT_SOURCE_DIR}/include/ghc/fs_fwd.hpp ${CMAKE_CURRENT_SOURCE_DIR}/include/ghc/fs_impl.hpp)
target_include_directories(ghc_filesystem INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include) target_include_directories(ghc_filesystem INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_options(ghc_filesystem INTERFACE "$<$<C_COMPILER_ID:MSVC>:/utf-8>") target_compile_options(ghc_filesystem INTERFACE "$<$<C_COMPILER_ID:MSVC>:/utf-8>")
target_compile_options(ghc_filesystem INTERFACE "$<$<CXX_COMPILER_ID:MSVC>:/utf-8>") target_compile_options(ghc_filesystem INTERFACE "$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

View File

@ -85,7 +85,7 @@ in the standard, and there might be issues in these implementations too.
## Usage ## Usage
As it is a header-only library, it should be enough to copy the header As it is at first a header-only library, it should be enough to copy the header
into your project folder oder point your include path to this directory and into your project folder oder point your include path to this directory and
simply include the `filesystem.hpp` header. simply include the `filesystem.hpp` header.
@ -135,6 +135,13 @@ without that switch ([see](https://blogs.msdn.microsoft.com/vcblog/2018/04/09/ms
Be aware too, as a header-only library, it is not hiding the fact, that it Be aware too, as a header-only library, it is not hiding the fact, that it
uses system includes, so they "pollute" your global namespace. uses system includes, so they "pollute" your global namespace.
Alternatively, starting from v1.1.0 `ghc::filesystem` can also be used by
including one of two additional wrapper headers. These allow to include
a forwarded version in most places (`ghc/fs_fwd.hpp`) while hiding the
implementation details in a single cpp that includes `ghc/fs_impl.hpp` to
implement the needed code. That way system includes are only visible from
inside the cpp, all other places are clean.
Additionally, starting from v1.1.0, it is possible to add `ghc::filesystem` Additionally, starting from v1.1.0, it is possible to add `ghc::filesystem`
as a git submodule, add the directory to your `CMakeLists.txt` with as a git submodule, add the directory to your `CMakeLists.txt` with
`add_subdirectory()` and then simply use `target_link_libraries(your-target ghc_filesystem)` `add_subdirectory()` and then simply use `target_link_libraries(your-target ghc_filesystem)`
@ -375,6 +382,12 @@ to the expected behavior.
will be a valid directive. will be a valid directive.
Still you can simply only add the header file to you project and include it Still you can simply only add the header file to you project and include it
from there. from there.
* Enhancement ([#10](https://github.com/gulrak/filesystem/issues/10)),
support for separation of implementation and forwarded api: Two
additional simple includes are added, that can be used to forward
`ghc::filesystem` declarations (`fs_fwd.hpp`) and to wrap the
implementation into a single cpp (`fs_impl.hpp`)
### [v1.0.10](https://github.com/gulrak/filesystem/releases/tag/v1.0.10) ### [v1.0.10](https://github.com/gulrak/filesystem/releases/tag/v1.0.10)

File diff suppressed because it is too large Load Diff

46
include/ghc/fs_fwd.hpp Normal file
View File

@ -0,0 +1,46 @@
//---------------------------------------------------------------------------------------
//
// ghc::filesystem - A C++17-like filesystem implementation for C++11/C++14
//
//---------------------------------------------------------------------------------------
//
// Copyright (c) 2018, Steffen Schümann <s.schuemann@pobox.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//---------------------------------------------------------------------------------------
// fs_fwd.hpp - The forwarding header for the header/implementation seperated usage of
// ghc::filesystem.
// This file can be include at any place, where ghc::filesystem api is needed while
// not bleeding implementation details (e.g. system includes) into the global namespace,
// as long as one cpp includes fs_impl.hpp to deliver the matching implementations.
//---------------------------------------------------------------------------------------
#ifndef GHC_FILESYSTEM_FWD_H
#define GHC_FILESYSTEM_FWD_H
#define GHC_FILESYSTEM_FWD
#include <ghc/filesystem.hpp>
#endif // GHC_FILESYSTEM_FWD_H

43
include/ghc/fs_impl.hpp Normal file
View File

@ -0,0 +1,43 @@
//---------------------------------------------------------------------------------------
//
// ghc::filesystem - A C++17-like filesystem implementation for C++11/C++14
//
//---------------------------------------------------------------------------------------
//
// Copyright (c) 2018, Steffen Schümann <s.schuemann@pobox.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//---------------------------------------------------------------------------------------
// fs_impl.hpp - The implementation header for the header/implementation seperated usage of
// ghc::filesystem.
// This file can be used to hide the implementation of ghc::filesystem into a single cpp.
// The cpp has to include this before including fs_fwd.hpp directly or via a different
// header to work.
//---------------------------------------------------------------------------------------
#define GHC_FILESYSTEM_IMPLEMENTATION
#include <ghc/filesystem.hpp>

View File

@ -40,3 +40,6 @@ endif()
add_executable(multifile_test multi1.cpp multi2.cpp catch.hpp) add_executable(multifile_test multi1.cpp multi2.cpp catch.hpp)
target_link_libraries(multifile_test ghc_filesystem) target_link_libraries(multifile_test ghc_filesystem)
add_executable(fwd_impl_test fwd_test.cpp impl_test.cpp)
target_link_libraries(fwd_impl_test ghc_filesystem)

View File

@ -64,8 +64,12 @@ using fstream = std::fstream;
#define GHC_OS_WINDOWS #define GHC_OS_WINDOWS
#endif #endif
#else #else
#ifdef GHC_FILESYSTEM_FWD_TEST
#include <ghc/fs_fwd.hpp>
#else
#define NOMINMAX #define NOMINMAX
#include <ghc/filesystem.hpp> #include <ghc/filesystem.hpp>
#endif
namespace fs { namespace fs {
using namespace ghc::filesystem; using namespace ghc::filesystem;
using ifstream = ghc::filesystem::ifstream; using ifstream = ghc::filesystem::ifstream;
@ -74,7 +78,9 @@ using fstream = ghc::filesystem::fstream;
} }
#endif #endif
#ifndef GHC_FILESYSTEM_FWD_TEST
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_MAIN
#endif
#include "catch.hpp" #include "catch.hpp"
//#define TEST_LWG_2935_BEHAVIOUR //#define TEST_LWG_2935_BEHAVIOUR

7
test/fwd_test.cpp Normal file
View File

@ -0,0 +1,7 @@
// This test file is part of the fwd_test.cpp/impl_test.cpp pair
// and used to test the new optional two-part usage of ghc::filesystem
// where exactly one cpp includes fs_impl.hpp and all others use
// fs_fwd.hpp (to test this with maximum functionality, the unit tests
// are included here, signaling they should only include the fs_fwd.hpp)
#define GHC_FILESYSTEM_FWD_TEST
#include "filesystem_test.cpp"

8
test/impl_test.cpp Normal file
View File

@ -0,0 +1,8 @@
// This test file is part of the fwd_test.cpp/impl_test.cpp pair
// and used to test the new optional two-part usage of ghc::filesystem
// where exactly one cpp includes fs_impl.hpp and all others use
// fs_fwd.hpp (to test this with maximum functionality, the unit tests
// are included here, signaling they should only include the fs_fwd.hpp)
#include <ghc/fs_impl.hpp>
#define CATCH_CONFIG_MAIN
#include "catch.hpp"