DoorKeeper/include/doorkeeper/implem/saltedid.hpp

141 lines
5.1 KiB
C++

/* Copyright 2015, Michele Santullo
* This file is part of DoorKeeper.
*
* DoorKeeper 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 3 of the License, or
* (at your option) any later version.
*
* DoorKeeper 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 DoorKeeper. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id440C878EF94C4468948A57610C7C9B5A
#define id440C878EF94C4468948A57610C7C9B5A
#include "doorkeeper/primitivetypes.hpp"
#include "doorkeeper/implem/dktypes.hpp"
#include <ciso646>
namespace dk {
namespace implem {
template <unsigned int S>
struct uint_t {
typedef typename int_t<S / CHAR_BIT, false>::type type;
};
template <unsigned int N, unsigned int M=8*CHAR_BIT, bool=(N>=M) or (N%CHAR_BIT==0)>
struct RoundToInt {
private:
enum {
v = (N < M ? (N < CHAR_BIT ? CHAR_BIT : N) : M)
};
static_assert(v > 0, "v must be greater than 0");
public:
enum {
value = ((((((v - 1) | ((v - 1) >> 1)) | (((v - 1) | ((v - 1) >> 1)) >> 2)) | ((((v - 1) | ((v - 1) >> 1)) | (((v - 1) | ((v - 1) >> 1)) >> 2)) >> 4)) | (((((v - 1) | ((v - 1) >> 1)) | (((v - 1) | ((v - 1) >> 1)) >> 2)) | ((((v - 1) | ((v - 1) >> 1)) | (((v - 1) | ((v - 1) >> 1)) >> 2)) >> 4)) >> 8)) | ((((((v - 1) | ((v - 1) >> 1)) | (((v - 1) | ((v - 1) >> 1)) >> 2)) | ((((v - 1) | ((v - 1) >> 1)) | (((v - 1) | ((v - 1) >> 1)) >> 2)) >> 4)) | (((((v - 1) | ((v - 1) >> 1)) | (((v - 1) | ((v - 1) >> 1)) >> 2)) | ((((v - 1) | ((v - 1) >> 1)) | (((v - 1) | ((v - 1) >> 1)) >> 2)) >> 4)) >> 8)) >> 16)) + 1
};
static_assert(value <= M, "value must be smaller than M");
static_assert(value and not (value bitand (value - 1)), "value must be a power of two");
};
template <unsigned int B>
struct FillBits;
template <unsigned int B>
struct FillBits {
enum { value = 1 bitor (FillBits<B - 1>::value << 1) };
};
template <>
struct FillBits<0> {
enum { value = 0 };
};
} //namespace implem
template <unsigned int Salt, unsigned int Indx>
class SaltedID {
static_assert(Salt + Indx <= 64, "Only indices up to 64 bits are currently supported");
public:
enum {
bit_size = implem::RoundToInt<Salt + Indx>::value,
max_index = implem::FillBits<Indx>::value,
max_salt = implem::FillBits<Salt>::value
};
private:
typedef typename implem::uint_t<bit_size>::type compound_type;
public:
typedef typename implem::uint_t<implem::RoundToInt<Salt>::value>::type salt_type;
typedef typename implem::uint_t<implem::RoundToInt<Indx>::value>::type index_type;
SaltedID ( void ) { }
SaltedID ( index_type parIndex, salt_type parSalt ) :
m_data(pack_index_salt(parIndex, parSalt))
{
DK_ASSERT(index() == parIndex);
DK_ASSERT(salt() == parSalt);
}
salt_type salt ( void ) const;
index_type index ( void ) const;
SaltedID& operator+= ( index_type parOther ) { m_data = pack_index_salt(index() + parOther, salt()); return *this; }
SaltedID& operator-= ( index_type parOther ) { m_data = pack_index_salt(index() - parOther, salt()); return *this; }
private:
static compound_type pack_index_salt ( index_type parIndex, salt_type parSalt );
compound_type m_data;
};
template <unsigned int Salt, unsigned int Indx>
auto SaltedID<Salt, Indx>::salt() const -> salt_type {
return static_cast<salt_type>(m_data bitand implem::FillBits<Salt>::value);
}
template <unsigned int Salt, unsigned int Indx>
auto SaltedID<Salt, Indx>::index() const -> index_type {
return static_cast<index_type>(m_data >> Salt);
}
template <unsigned int Salt, unsigned int Indx>
auto SaltedID<Salt, Indx>::pack_index_salt (index_type parIndex, salt_type parSalt) -> compound_type {
DK_ASSERT(parIndex <= max_index);
DK_ASSERT(parSalt <= max_salt);
return (static_cast<compound_type>(parIndex) << Salt) bitor parSalt;
}
template <unsigned int Salt, unsigned int Indx>
inline bool operator< (const SaltedID<Salt, Indx>& parLeft, const SaltedID<Salt, Indx>& parRight) {
return parLeft.index() < parRight.index();
}
template <unsigned int Salt, unsigned int Indx>
inline SaltedID<Salt, Indx> operator+ (SaltedID<Salt, Indx> parLeft, typename SaltedID<Salt, Indx>::index_type parRight) {
return (parLeft += parRight);
}
template <unsigned int Salt, unsigned int Indx>
inline SaltedID<Salt, Indx> operator+ (typename SaltedID<Salt, Indx>::index_type parLeft, SaltedID<Salt, Indx> parRight) {
return (parRight += parLeft);
}
template <unsigned int Salt, unsigned int Indx>
inline SaltedID<Salt, Indx> operator- (SaltedID<Salt, Indx> parLeft, typename SaltedID<Salt, Indx>::index_type parRight) {
return (parLeft -= parRight);
}
template <unsigned int Salt, unsigned int Indx>
inline SaltedID<Salt, Indx> operator- (typename SaltedID<Salt, Indx>::index_type parLeft, SaltedID<Salt, Indx> parRight) {
return (parRight -= parLeft);
}
} //namespace dk
#endif