Skip to content

Commit 58845cf

Browse files
integration in ofConstants.h
1 parent 86335ad commit 58845cf

File tree

3 files changed

+47
-134
lines changed

3 files changed

+47
-134
lines changed

libs/openFrameworks/utils/ofConstants.h

Lines changed: 8 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -361,100 +361,15 @@ typedef TESSindex ofIndexType;
361361
#endif
362362
#endif
363363

364+
#include <filesystem> // TODO: move to ofMain.h?
365+
#define OF_USING_STD_FS 1 // TODO: remove usage of this #define which is now (C++17) always true
364366

365-
366-
// If you are building with c++17 or newer std filesystem will be enabled by default
367-
#if __cplusplus >= 201500
368-
// #pragma message ( "__cplusplus >= 201500 " )
369-
#define OF_HAS_CPP17
370-
#if __cplusplus < 201703L
371-
// #pragma message ( "__cplusplus < 201703L" )
372-
#define OF_USE_EXPERIMENTAL_FS
373-
#endif
374-
#else
375-
#undef OF_HAS_CPP17
376-
#endif
377-
378-
379-
#ifndef OF_USING_STD_FS
380-
#if defined(OF_HAS_CPP17)
381-
#define OF_USING_STD_FS
382-
#else
383-
#undef OF_USING_STD_FS
384-
#endif
367+
#if defined(TARGET_WIN32) // or 1 // for testing on mac/linux
368+
#define OF_ENABLE_TOLERANT_NARROW_PATH_CONVERSION 1 // to enable the feature
385369
#endif
386370

387-
// Some projects will specify OF_USING_STD_FS even if the compiler isn't newer than 201703L
388-
// This may be okay but we need to test for the way C++17 is including the filesystem
389-
390-
#if defined(OF_USING_STD_FS) && !defined(OF_USE_EXPERIMENTAL_FS)
391-
#if defined(__cpp_lib_filesystem)
392-
// #pragma message ( "ok __cpp_lib_filesystem" )
393-
#undef OF_USE_EXPERIMENTAL_FS
394-
#elif defined(__cpp_lib_experimental_filesystem)
395-
// #pragma message ( "ok __cpp_lib_experimental_filesystem" )
396-
#define OF_USE_EXPERIMENTAL_FS
397-
#elif !defined(__has_include)
398-
// #pragma message ( "not __has_include so we add OF_USE_EXPERIMENTAL_FS? seems wrong" )
399-
#define OF_USE_EXPERIMENTAL_FS
400-
#elif __has_include(<filesystem>)
401-
// If we're compiling on Visual Studio and are not compiling with C++17, we need to use experimental
402-
#ifdef _MSC_VER
403-
404-
// Check and include header that defines "_HAS_CXX17"
405-
#if __has_include(<yvals_core.h>)
406-
#include <yvals_core.h>
407-
408-
// Check for enabled C++17 support
409-
#if defined(_HAS_CXX17) && _HAS_CXX17
410-
// We're using C++17, so let's use the normal version
411-
#undef OF_USE_EXPERIMENTAL_FS
412-
#endif
413-
#endif
414-
415-
// If the macro isn't defined yet, that means any of the other VS specific checks failed, so we need to use experimental
416-
#ifndef INCLUDE_STD_FILESYSTEM_EXPERIMENTAL
417-
#define OF_USE_EXPERIMENTAL_FS
418-
#endif
419-
420-
// Not on Visual Studio. Let's use the normal version
421-
#else // #ifdef _MSC_VER
422-
#undef OF_USE_EXPERIMENTAL_FS
423-
#endif
424-
#else
425-
#undef OF_USE_EXPERIMENTAL_FS
426-
#endif
427-
#endif
428-
429-
430-
#if defined(OF_USING_STD_FS)
431-
#if defined(OF_USE_EXPERIMENTAL_FS)
432-
// C++17 experimental fs support
433-
#include <experimental/filesystem>
434-
namespace std {
435-
namespace experimental{
436-
namespace filesystem {
437-
using path = v1::path;
438-
}
439-
}
440-
}
441-
442-
namespace of {
443-
namespace filesystem = std::experimental::filesystem;
444-
}
445-
#else
446-
#include "ofFilesystem.h"
447-
#endif
448-
#else //not OF_USING_STD_FS
449-
// No experimental or c++17 filesytem support use boost
450-
#if !_MSC_VER
451-
#define BOOST_NO_CXX11_SCOPED_ENUMS
452-
#define BOOST_NO_SCOPED_ENUMS
453-
#endif
454-
455-
#include <boost/filesystem.hpp>
456-
namespace of {
457-
namespace filesystem = boost::filesystem;
458-
}
459-
371+
#if defined(OF_ENABLE_TOLERANT_NARROW_PATH_CONVERSION)
372+
#include "ofFilesystem.h"
373+
#else
374+
namespace of::filesystem { using path = std::filesystem::path; }
460375
#endif

libs/openFrameworks/utils/ofFileUtils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,10 +771,11 @@ class ofFile : public std::fstream {
771771
return myFile;
772772
}
773773

774+
#if defined(OF_ENABLE_TOLERANT_NARROW_PATH_CONVERSION)
774775
operator of::filesystem::path() {
775776
return myFile;
776777
}
777-
//
778+
#endif
778779
operator of::filesystem::path() const {
779780
return myFile;
780781
}
@@ -1153,9 +1154,11 @@ class ofDirectory {
11531154
return myDir;
11541155
}
11551156

1157+
#if defined(OF_ENABLE_TOLERANT_NARROW_PATH_CONVERSION)
11561158
operator of::filesystem::path() {
11571159
return myDir;
11581160
}
1161+
#endif
11591162

11601163
operator of::filesystem::path() const {
11611164
return myDir;

libs/openFrameworks/utils/ofFilesystem.h renamed to libs/openFrameworks/utils/ofFilesystemPath.h

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,17 @@
66
#include <iostream>
77
#include <utility>
88

9-
namespace of {
10-
namespace filesystem {
11-
129
// general approach to return type:
1310
// - use `path` and path &` to wrap std:: return values
1411
// - use bool when bool (for self-documenting API)
1512
// - use (const) auto (inheriting from std:: implementation) for others
1613

14+
namespace of::filesystem {
15+
1716
class path {
1817
private:
1918
std::filesystem::path path_; // simple composition
2019

21-
#if defined(TARGET_WIN32)
22-
// TODO better (ideal) impl this just copy-pasted for proof of concept
23-
mutable std::string cached_narrow_str_;
24-
const char* to_narrow_cstr() const {
25-
std::mbstate_t state = std::mbstate_t();
26-
size_t size_needed = std::wcstombs(nullptr, wstring().c_str(), 0) + 1;
27-
if (size_needed == static_cast<size_t>(-1)) {
28-
throw std::runtime_error("Conversion error from wstring to string");
29-
}
30-
cached_narrow_str_.resize(size_needed);
31-
std::wcstombs(&cached_narrow_str_[0], wstring().c_str(), size_needed);
32-
return cached_narrow_str_.c_str();
33-
}
34-
#endif
35-
3620
public:
3721
// MARK: construction
3822
path() = default;
@@ -53,15 +37,29 @@ class path {
5337
explicit operator const std::filesystem::path::value_type*() const { return path_.c_str(); }
5438

5539
// MARK: string conversions
56-
57-
#if defined(TARGET_WIN32)
40+
41+
#if defined(TARGET_WIN32) // superfluous but usefull to facilitate testing on mac/linux
42+
43+
// TODO better (ideal) impl this just copy-pasted for proof of concept
44+
mutable std::string cached_narrow_str_;
45+
const char* to_narrow_cstr() const {
46+
std::mbstate_t state = std::mbstate_t();
47+
size_t size_needed = std::wcstombs(nullptr, wstring().c_str(), 0) + 1;
48+
if (size_needed == static_cast<size_t>(-1)) {
49+
throw std::runtime_error("Conversion error from wstring to string");
50+
}
51+
cached_narrow_str_.resize(size_needed);
52+
std::wcstombs(&cached_narrow_str_[0], wstring().c_str(), size_needed);
53+
return cached_narrow_str_.c_str();
54+
}
55+
5856
operator std::wstring() const { return path_.wstring(); }
5957
operator const char*() const { return to_narrow_cstr(); } // should try catch on win
6058
operator std::string() const { return path_.string(); } // should try catch on win
6159
explicit operator const std::string() const { return path_.string(); } // should try catch on win
6260
#else
6361
operator std::filesystem::path::string_type() const { return path_.string(); }
64-
explicit operator const std::filesystem::path::string_type() const { return path_.string(); }
62+
explicit operator const std::filesystem::path::string_type() const { return path_.string(); }
6563
#endif
6664

6765
auto wstring() const { return path_.wstring(); }
@@ -76,7 +74,7 @@ class path {
7674
auto u16string() const { return path_.u16string(); }
7775
auto u32string() const { return path_.u32string(); }
7876
const auto c_str() const noexcept { return path_.c_str(); }
79-
77+
8078
const std::filesystem::path& native_path() const { return path_; }
8179

8280
static constexpr auto preferred_separator = std::filesystem::path::preferred_separator;
@@ -96,12 +94,12 @@ class path {
9694
auto file_size() const { return std::filesystem::file_size(path_); }
9795
auto last_write_time() const { return std::filesystem::last_write_time(path_); }
9896
auto get_permissions() const { return std::filesystem::status(path_).permissions(); }
99-
97+
10098
// MARK: path type
10199
path lexically_normal() const {
102100
return path(path_.lexically_normal());
103101
}
104-
102+
105103
template <typename... Args>
106104
path lexically_relative(Args&&... args) const {
107105
return path(path_.lexically_relative(std::forward<Args>(args)...));
@@ -111,7 +109,7 @@ class path {
111109
path lexically_proximate(Args&&... args) const {
112110
return path(path_.lexically_proximate(std::forward<Args>(args)...));
113111
}
114-
112+
115113
// MARK: comparison
116114
// TODO: C++20: spaceship simplification
117115
template <typename T> bool operator==(T&& other) const noexcept { return path_ == std::forward<T>(other); }
@@ -122,35 +120,35 @@ class path {
122120
template <typename T> bool operator>=(T&& other) const noexcept { return path_ >= std::forward<T>(other); }
123121

124122
bool operator!() const noexcept { return empty(); }
125-
123+
126124
template <typename... Args>
127125
auto compare(Args&&... args) const { return path_.compare(std::forward<Args>(args)...); }
128126

129127
void swap(path & other) noexcept { path_.swap(other.path_); }
130-
128+
131129

132130
// MARK: path transformation (return *this)
133-
path& replace_extension(const path & ext = std::filesystem::path()) {
131+
path& replace_extension(const path & ext = std::filesystem::path()) {
134132
path_.replace_extension(ext);
135133
return *this;
136134
}
137-
138-
path& replace_filename(const path & ext = std::filesystem::path()) {
135+
136+
path& replace_filename(const path & ext = std::filesystem::path()) {
139137
path_.replace_filename(ext);
140138
return *this;
141139
}
142-
140+
143141
template <typename T>
144142
path& assign(T&& p) noexcept {
145143
path_.assign(std::move(std::forward<T>(p)));
146144
return *this;
147145
}
148-
146+
149147
path& append(path&& p) noexcept {
150148
path_ /= std::move(p.path_);
151149
return *this;
152150
}
153-
151+
154152
path& operator/=(path&& p) noexcept {
155153
path_ /= std::move(p.path_);
156154
return *this;
@@ -202,7 +200,7 @@ class path {
202200
const friend path operator+(const path& lhs, const RHS& rhs) {
203201
return path(lhs.path_.string() + rhs);
204202
}
205-
203+
206204
// MARK: other sub paths
207205
path root_name() const { return path(path_.root_name()); }
208206
path root_directory() const { return path(path_.root_directory()); }
@@ -212,7 +210,7 @@ class path {
212210
path filename() const { return path(path_.filename()); }
213211
path stem() const { return path(path_.stem()); }
214212
path extension() const { return path(path_.extension()); }
215-
213+
216214
// MARK: file info
217215
bool has_extension() const { return path_.has_extension(); }
218216
bool has_filename() const { return path_.has_filename(); }
@@ -221,13 +219,10 @@ class path {
221219
bool has_root_directory() const { return path_.has_root_directory(); }
222220
bool has_relative_path() const { return path_.has_relative_path(); }
223221
bool has_stem() const { return path_.has_stem(); }
224-
222+
225223
friend std::ostream& operator<<(std::ostream& os, const path& p) {
226224
return os << p.string();
227225
}
228226
};
229-
230-
} // namespace filesystem
231-
} // namespace of
232-
227+
}
233228
#endif // OF_FILESYSTEM_PATH_H

0 commit comments

Comments
 (0)