aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/include/CStringView.h
diff options
context:
space:
mode:
authorAlexander Karatarakis <alkarata@microsoft.com>2017-04-03 14:21:51 -0700
committerAlexander Karatarakis <alkarata@microsoft.com>2017-04-04 16:44:41 -0700
commit604d0e58dab76f8ab31eb4f7807f7b561c4d571d (patch)
tree56c5f18eaa5cc9e3fa7129a7479e243c4db44988 /toolsrc/include/CStringView.h
parent80e48c2756cc2c453ba221fe38c32f969d5139ea (diff)
downloadvcpkg-604d0e58dab76f8ab31eb4f7807f7b561c4d571d.tar.gz
vcpkg-604d0e58dab76f8ab31eb4f7807f7b561c4d571d.zip
cstring_view -> CStringView
Diffstat (limited to 'toolsrc/include/CStringView.h')
-rw-r--r--toolsrc/include/CStringView.h30
1 files changed, 30 insertions, 0 deletions
diff --git a/toolsrc/include/CStringView.h b/toolsrc/include/CStringView.h
new file mode 100644
index 000000000..3da76e63f
--- /dev/null
+++ b/toolsrc/include/CStringView.h
@@ -0,0 +1,30 @@
+#pragma once
+#include <string>
+
+namespace vcpkg
+{
+ template<class CharType>
+ struct BasicCStringView
+ {
+ constexpr BasicCStringView() : cstr(nullptr) {}
+ constexpr BasicCStringView(const CharType* cstr) : cstr(cstr) {}
+ BasicCStringView(const std::basic_string<CharType>& str) : cstr(str.c_str()) {}
+
+ constexpr operator const CharType*() const { return cstr; }
+
+ constexpr const CharType* c_str() const { return cstr; }
+
+ private:
+ const CharType* cstr;
+ };
+
+ using CStringView = BasicCStringView<char>;
+ using CWStringView = BasicCStringView<wchar_t>;
+
+ inline const char* to_printf_arg(const CStringView spec) { return spec.c_str(); }
+
+ inline const wchar_t* to_wprintf_arg(const CWStringView spec) { return spec.c_str(); }
+
+ static_assert(sizeof(CStringView) == sizeof(void*), "CStringView must be a simple wrapper around char*");
+ static_assert(sizeof(CWStringView) == sizeof(void*), "CWStringView must be a simple wrapper around wchar_t*");
+}