sapling/eden/fs/model/Hash.h
Adam Simpkins 4147c7b937 make Hash objects assignable, and add a default constructor
Summary:
Previously Hash objects could not be assigned to after they were created, since
they contained a const member.  This makes the data non-const, so a Hash
variable can be replaced to contain new contents after it is created.

This also adds a default constructor, which zero-initializes the hash.  The
default constructor makes it possible to declare a Hash with a 0-value at one
location, and then set it to the desired value at some later point.

Reviewed By: bolinfest

Differential Revision: D3406773

fbshipit-source-id: 41e2c7e3ad5bc4d14813be4adaa03866701380f6
2016-06-08 16:16:59 -07:00

69 lines
1.3 KiB
C++

/*
* Copyright (c) 2016, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#pragma once
#include <folly/Range.h>
#include <stdint.h>
#include <array>
#include <boost/operators.hpp>
namespace folly {
class IOBuf;
}
namespace facebook {
namespace eden {
/**
* Immutable 160-bit hash.
*/
class Hash : boost::totally_ordered<Hash> {
public:
enum { RAW_SIZE = 20 };
using Storage = std::array<uint8_t, RAW_SIZE>;
/**
* Create a 0-initialized hash
*/
Hash();
explicit Hash(Storage bytes);
explicit Hash(folly::ByteRange bytes);
/**
* @param hex is a string of 40 hexadecimal characters.
*/
explicit Hash(folly::StringPiece hex);
/**
* Compute the SHA1 hash of an IOBuf chain.
*/
static Hash sha1(const folly::IOBuf* buf);
/**
* Compute the SHA1 hash of a ByteRange
*/
static Hash sha1(folly::ByteRange data);
folly::ByteRange getBytes() const;
/** @return 40-character [lowercase] hex representation of this hash. */
std::string toString() const;
bool operator==(const Hash&) const;
bool operator<(const Hash&) const;
private:
Storage bytes_;
};
}
}