sapling/eden/fs/win/utils/Guid.h
Xavier Deguillard 0273817488 win: simplify path management
Summary:
The StringConv.h header contains many functions to convert from Windows paths
to Eden's path (and vice versa) to workaround the fact that Eden's path don't
support wide strings that Windows uses. Let's simply add support for these wide
strings in PathFuncs so we can greatly simplify all the call sites. Instead of
calling "edenToWinName(winstr)", "PathComponent(winstr)" is both more
descriptive and more idiomatic.

To be fair, I'm not entirely a fan of the approach taken in this diff, as this
adds Windows specific code to PathFuncs.h, but I feel that the benefit is too
big to not do that.

Reviewed By: chadaustin

Differential Revision: D23004523

fbshipit-source-id: 3a1507e398a66909773251907db01e06603b91dd
2020-08-10 08:53:13 -07:00

71 lines
1.4 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#pragma once
#include "folly/portability/Windows.h"
#include <combaseapi.h>
#include "eden/fs/win/utils/WinError.h"
namespace facebook {
namespace eden {
class Guid {
public:
static Guid generate() {
GUID id;
HRESULT result = CoCreateGuid(&id);
if (FAILED(result)) {
throw std::system_error(
result, HResultErrorCategory::get(), "Failed to create a GUID");
}
return Guid{id};
}
Guid() noexcept : guid_{0} {}
Guid(const GUID& guid) noexcept : guid_{guid} {}
Guid(const Guid& other) noexcept : guid_{other.guid_} {}
Guid& operator=(const Guid& other) noexcept {
guid_ = other.guid_;
return *this;
}
const GUID& getGuid() const noexcept {
return guid_;
}
operator const GUID&() const noexcept {
return guid_;
}
operator const GUID*() const noexcept {
return &guid_;
}
bool operator==(const Guid& other) const noexcept {
return guid_ == other.guid_;
}
bool operator!=(const Guid& other) const noexcept {
return !(*this == other);
}
private:
GUID guid_;
};
struct CompareGuid {
bool operator()(const GUID& left, const GUID& right) const noexcept {
return memcmp(&left, &right, sizeof(right)) < 0;
}
};
} // namespace eden
} // namespace facebook