Luna/libluna/include/luna/Badge.h

38 lines
934 B
C
Raw Normal View History

/**
* @file Badge.h
* @author apio (cloudapio.eu)
* @brief A simple way to control who can call functions.
*
* @copyright Copyright (c) 2022-2023, the Luna authors.
*
*/
#pragma once
/**
* @brief A struct that can only be constructed by one type, used to control access to functions/methods.
*
* Example: There is a private method FooClass::foo() that you want to be callable by BarClass without making it a
* friend.
*
* So, make FooClass::foo() public and make it take a Badge<BarClass> which will only be constructible by BarClass, thus
* limiting the method even though it is public.
*
* @tparam T The type that can construct this badge.
*/
template <class T> struct Badge
{
private:
/**
* @brief Construct a new Badge.
*
* This can only be done by the type T.
*/
constexpr Badge() = default;
Badge(const Badge<T>&) = delete;
Badge(Badge<T>&&) = delete;
friend T;
2023-01-02 12:07:29 +00:00
};