NarrowPath.hxx 2.17 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_FS_NARROW_PATH_HXX
#define MPD_FS_NARROW_PATH_HXX

#include "Path.hxx"
24 25

#ifdef _UNICODE
26
#include "AllocatedPath.hxx"
27 28 29
#include "util/AllocatedString.hxx"
#else
#include "util/StringPointer.hxx"
30
#endif
31 32 33 34 35 36 37

/**
 * A path name that uses the regular (narrow) "char".  This is used to
 * pass a #Path (which may be represented by wchar_t) to a library
 * that accepts only "const char *".
 */
class NarrowPath {
38
#ifdef _UNICODE
39
	typedef AllocatedString<> Value;
40
#else
41
	typedef StringPointer<> Value;
42
#endif
43
	typedef typename Value::const_pointer_type const_pointer_type;
44 45

	Value value;
46 47

public:
48
#ifdef _UNICODE
49
	explicit NarrowPath(Path _path) noexcept;
50
#else
51
	explicit NarrowPath(Path _path):value(_path.c_str()) {}
52
#endif
53

54
	operator const_pointer_type() const {
55
		return c_str();
56 57
	}

58
	const_pointer_type c_str() const {
59
		return value.c_str();
60 61 62
	}
};

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/**
 * A path name converted from a "narrow" string.  This is used to
 * import an existing narrow string to a #Path.
 */
class FromNarrowPath {
#ifdef _UNICODE
	using Value = AllocatedPath;
#else
	using Value = Path;
#endif

	Value value{nullptr};

public:
	FromNarrowPath() = default;

#ifdef _UNICODE
	/**
	 * Throws on error.
	 */
	FromNarrowPath(const char *s);
#else
	constexpr FromNarrowPath(const char *s) noexcept
		:value(Value::FromFS(s)) {}
#endif

#ifndef _UNICODE
	constexpr
#endif
	operator Path() const noexcept {
		return value;
	}
};

97
#endif