Home  >  Article  >  Backend Development  >  Can Key-Oriented Access Protection Patterns Be Made More Reusable?

Can Key-Oriented Access Protection Patterns Be Made More Reusable?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-27 08:43:09758browse

Can Key-Oriented Access Protection Patterns Be Made More Reusable?

Can We Enhance the Reusability of the Key-Oriented Access Protection Pattern?

In its current form, the access protection pattern utilizing keys exhibits limitations in its reusability. To address this issue, various enhancements have been proposed.

C 03 Improvements:

Using macros, we can simplify and generalize the pattern. The PASSKEY macros define passkeys for specific classes or functions, providing a centralized location for managing access control. Here's an example:

#define PASSKEY_1(pKeyname, pFriend1)                             \
        class EXPAND(pKeyname)                                    \
        {                                                         \
        private:                                                  \
            friend EXPAND(pFriend1);                              \
            EXPAND(pKeyname)() {}                                 \
                                                                  \
            EXPAND(pKeyname)(const EXPAND(pKeyname)&);            \
            EXPAND(pKeyname)& operator=(const EXPAND(pKeyname)&); \
        }

While this method improves the pattern's ease of use, it has drawbacks. It requires callers to know the specific passkey needed and can lead to macro clutter.

C 0x Enhancements:

In C 0x, the pattern can achieve its greatest potential thanks to variadic templates and template parameter friends. Here's an implementation:

template <typename T>
class passkey
{
private:
    friend T; // Now possible in C++0x
    passkey() {}

    // Noncopyable
    passkey(const passkey&amp;) = delete;
    passkey&amp; operator=(const passkey&amp;) = delete;
};

#define PASSKEY_FUNCTION(pTag, pFunc, ...)               \
        struct EXPAND(pTag);                             \
                                                         \
        template <>                                      \
        class passkey<EXPAND(pTag)>                      \
        {                                                \
        private:                                         \
            friend pFunc __VA_ARGS__;                    \
            passkey() {}                                 \
                                                         \
            passkey(const passkey&amp;) = delete;            \
            passkey&amp; operator=(const passkey&amp;) = delete; \
        }

Now, each class has its unique passkey, and functions can specify their allowed passkeys. Callers simply create the necessary passkey and invoke the desired method. This approach offers increased reusability and flexibility through its generic implementation.

The above is the detailed content of Can Key-Oriented Access Protection Patterns Be Made More Reusable?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn