首页 > 后端开发 > C++ > 我们如何提高 C 语言中面向密钥的访问保护模式的可重用性?

我们如何提高 C 语言中面向密钥的访问保护模式的可重用性?

Mary-Kate Olsen
发布: 2024-12-09 10:33:08
原创
743 人浏览过

How Can We Improve the Reusability of the Key-Oriented Access-Protection Pattern in C  ?

面向密钥的访问保护模式的可重用性

面向密钥的访问保护模式提供了一种简洁而富有表现力的访问控制方式基于关键对象的方法和函数。然而,该模式目前在可重用性方面受到限制。

C 03 中的改进

提高 C 03 中可重用性的一种方法是使用宏来定义密钥组:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#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)&); \

        }

 

#define PASSKEY_2(pKeyname, pFriend1, pFriend2)                   \

        class EXPAND(pKeyname)                                    \

        {                                                         \

        private:                                                  \

            friend EXPAND(pFriend1);                              \

            friend EXPAND(pFriend2);                              \

            EXPAND(pKeyname)() {}                                 \

                                                                  \

            EXPAND(pKeyname)(const EXPAND(pKeyname)&);            \

            EXPAND(pKeyname)& operator=(const EXPAND(pKeyname)&); \

        }

登录后复制

但是这个方法仍然需要调用者知道具体的passkey它需要创建并且为函数创建密钥可能很麻烦。

C 0x 的改进

C 0x 引入了对密钥模式的重大改进,支持完整的模板生成。通过可变参数模板和模板参数的友谊,以下代码展示了改进的实现:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

// Each class has its own unique key only it can create

template <typename T>

class passkey

{

private:

    friend T; // C++0x, MSVC allows as extension

    passkey() {}

 

    // Noncopyable

    passkey(const passkey&amp;) = delete;

    passkey&amp; operator=(const passkey&amp;) = delete;

};

 

// Functions still require a macro. This is because a friend function requires

// the entire declaration, which is not just a type, but a name as well. We do

// this by creating a tag and specializing the passkey for it, friending the function

#define EXPAND(pX) pX

 

// We use variadic macro parameters to allow

// functions with commas, it all gets pasted

// back together again when we friend it

#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; \

        }

登录后复制

这种增强的实现消除了早期 C 03 解决方案的两个缺点。调用者不再需要猜测要使用哪个密钥,样板代码一般会处理密钥创建和函数交友,只需要最少的额外定义。

以上是我们如何提高 C 语言中面向密钥的访问保护模式的可重用性?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板