Commit 51638df2 authored by Georgios Bitzes's avatar Georgios Bitzes Committed by Andreas Joachim Peters

NS: Add union type that combines file and container identifiers

parent 7aebc087
......@@ -146,6 +146,82 @@ private:
uint64_t val;
};
//------------------------------------------------------------------------------
//! FileOrContainerIdentifier class - holds either FileIdentifer, or
//! ContainerIdentifier, but not both.
//!
//! It can also be empty.
//------------------------------------------------------------------------------
class FileOrContainerIdentifier {
public:
//----------------------------------------------------------------------------
//! Empty.
//----------------------------------------------------------------------------
FileOrContainerIdentifier() : val(0), isEmpty(true), file(false) {}
//----------------------------------------------------------------------------
//! Has a file
//----------------------------------------------------------------------------
FileOrContainerIdentifier(FileIdentifier file) :
val(file.getUnderlyingUInt64()), isEmpty(false), file(true) {}
//----------------------------------------------------------------------------
//! Has a container
//----------------------------------------------------------------------------
FileOrContainerIdentifier(ContainerIdentifier cont) :
val(cont.getUnderlyingUInt64()), isEmpty(false), file(false) {}
//----------------------------------------------------------------------------
//! Is it empty?
//----------------------------------------------------------------------------
bool empty() const {
return isEmpty;
}
//----------------------------------------------------------------------------
//! Is it a file?
//----------------------------------------------------------------------------
bool isFile() const {
return !isEmpty && file;
}
//----------------------------------------------------------------------------
//! Is it a container?
//----------------------------------------------------------------------------
bool isContainer() const {
return !isEmpty && !file;
}
//----------------------------------------------------------------------------
//! Get FileIdentifier - if empty, or this actually points to a container,
//! FileIdentifier(0) is returned
//----------------------------------------------------------------------------
FileIdentifier toFileIdentifier() const {
if(isEmpty || !file) {
return FileIdentifier(0);
}
return FileIdentifier(val);
}
//----------------------------------------------------------------------------
//! Get ContainerIdentifier - if empty, or this actually points to a file,
//! ContainerIdentifier(0) is returned
//----------------------------------------------------------------------------
ContainerIdentifier toContainerIdentifier() const {
if(isEmpty || file) {
return ContainerIdentifier(0);
}
return ContainerIdentifier(val);
}
private:
uint64_t val;
bool isEmpty;
bool file;
};
EOSNSNAMESPACE_END
namespace Murmur3 {
......
......@@ -1023,3 +1023,29 @@ TEST(Resolver, FidParsing) {
str = "ino:80000000000003e7"; // fid: 999, new encoding
ASSERT_EQ(FileIdentifier(999), Resolver::retrieveFileIdentifier(str));
}
TEST(FileOrContainerIdentifier, BasicSanity) {
FileOrContainerIdentifier empty;
ASSERT_TRUE(empty.empty());
ASSERT_FALSE(empty.isFile());
ASSERT_FALSE(empty.isContainer());
ASSERT_EQ(empty.toFileIdentifier(), FileIdentifier(0));
ASSERT_EQ(empty.toContainerIdentifier(), ContainerIdentifier(0));
FileOrContainerIdentifier file(FileIdentifier(111));
ASSERT_FALSE(file.empty());
ASSERT_TRUE(file.isFile());
ASSERT_FALSE(file.isContainer());
ASSERT_EQ(file.toFileIdentifier(), FileIdentifier(111));
ASSERT_EQ(file.toContainerIdentifier(), ContainerIdentifier(0));
FileOrContainerIdentifier container(ContainerIdentifier(222));
ASSERT_FALSE(container.empty());
ASSERT_FALSE(container.isFile());
ASSERT_TRUE(container.isContainer());
ASSERT_EQ(container.toFileIdentifier(), FileIdentifier(0));
ASSERT_EQ(container.toContainerIdentifier(), ContainerIdentifier(222));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment