23
23
24
24
#pragma once
25
25
26
- #include < limits> // for numeric_limits<>
27
- #include < string> // for string
28
- #include < utility> // for move
29
-
30
- #include < boost/filesystem/path.hpp> // for path
26
+ #include < filesystem> // for path
27
+ #include < limits> // for numeric_limits<>
28
+ #include < string> // for string
29
+ #include < utility> // for move
31
30
31
+ #include < fable/fable_fwd.hpp> // for Environment
32
32
#include < fable/schema/interface.hpp> // for Base<>
33
+ #include < fable/utility/path.hpp> // for adl_serializer<>
33
34
34
35
namespace fable {
36
+ namespace schema {
35
37
36
- // Forward declarations:
37
- class Environment ; // from <fable/environment.hpp>
38
+ /* *
39
+ * Helper type trait class to use with std::enable_if and friends.
40
+ *
41
+ * The value `is_path<T>::value` is true if T is one of:
42
+ * - std::filesystem::path
43
+ * - boost::filesystem::path, if boost_path.hpp is included
44
+ *
45
+ * \see fable/schema/boost_path.hpp
46
+ */
47
+ template <typename T>
48
+ struct is_path : std::false_type {};
38
49
39
- namespace schema {
50
+ template <>
51
+ struct is_path <std::filesystem::path> : std::true_type {};
52
+
53
+ /* *
54
+ * State represents valid states a path can be in relative to the filesystem.
55
+ */
56
+ enum class PathState {
57
+ Any, // / any valid path
58
+ Absent, // / path does not exist
59
+ Exists, // / path exists
60
+ Executable, // / path exists in search path or locally and has executable permission
61
+ FileExists, // / path exists and is a file
62
+ DirExists, // / path exists and is a directory
63
+ NotFile, // / path does not exist or is a directory
64
+ NotDir, // / path does not exist or is a file
65
+ };
40
66
41
67
/* *
42
68
* Path de-/serializes a string that represents a filesystem path.
@@ -52,26 +78,20 @@ namespace schema {
52
78
*
53
79
* The Path schema type allows the user to specify these properties and will
54
80
* validate these during deserialization.
81
+ *
82
+ * Path is a template class that supports both:
83
+ * - std::filesystem::path
84
+ * - boost::filesystem::path, if boost_path.hpp is included
55
85
*/
56
- class Path : public Base <Path> {
86
+ template <typename T>
87
+ class Path : public Base <Path<T>> {
88
+ static_assert (is_path<T>::value);
89
+
57
90
public: // Types and Constructors
58
- using Type = boost::filesystem::path;
91
+ using Type = T;
92
+ using State = PathState;
59
93
60
- /* *
61
- * State represents valid states a path can be in relative to the filesystem.
62
- */
63
- enum class State {
64
- Any, // / any valid path
65
- Absent, // / path does not exist
66
- Exists, // / path exists
67
- Executable, // / path exists in search path or locally and has executable permission
68
- FileExists, // / path exists and is a file
69
- DirExists, // / path exists and is a directory
70
- NotFile, // / path does not exist or is a directory
71
- NotDir, // / path does not exist or is a file
72
- };
73
-
74
- Path (Type* ptr, std::string desc) : Base(JsonType::string, std::move(desc)), ptr_(ptr) {}
94
+ Path (Type* ptr, std::string desc) : Base<Path<T>>(JsonType::string, std::move(desc)), ptr_(ptr) {}
75
95
76
96
public: // Special
77
97
/* *
@@ -213,20 +233,10 @@ class Path : public Base<Path> {
213
233
Type* ptr_{nullptr };
214
234
};
215
235
216
- template <typename S >
217
- inline Path make_schema (boost::filesystem::path * ptr, S&& desc) {
236
+ template <typename T, typename S, std:: enable_if_t <is_path<T>::value, bool > = true >
237
+ inline Path<T> make_schema (T * ptr, S&& desc) {
218
238
return Path (ptr, std::forward<S>(desc));
219
239
}
220
240
221
241
} // namespace schema
222
242
} // namespace fable
223
-
224
- namespace nlohmann {
225
-
226
- template <>
227
- struct adl_serializer <boost::filesystem::path> {
228
- static void to_json (json& j, const boost::filesystem::path& p) { j = p.native (); }
229
- static void from_json (const json& j, boost::filesystem::path& p) { p = j.get <std::string>(); }
230
- };
231
-
232
- } // namespace nlohmann
0 commit comments