Note: This article excerpts the second book "C ++ cross-platform and framework development" that is being created by myself. Some of the wording is not adjusted for blog posts. Please note when reading this article.
Template brings the ability of generic programming to C ++, but also makes it difficult to use. In general, the three motivations for using templates are improving reusability, removing forced conversion, and avoiding potential errors. Here, let's take a look at an example of error avoidance.
Suppose we have the simplified timer Management Module Program shown in Figure 1. From the constructor's perspective, the three parameters specify the timer delay time, callback function, and callback function parameters. The callback function is encapsulated by the timer_callback_t class. When the timer expires, its fire () function will be called. Indirectly, the fire () function calls the handle () function of the callback Class Object saved by the timer.
1.
2. class timer_callback_t
3 .{
4. virtual void handle (timer_t & _ timer, timer_callback_arg_t * _ p_arg) = 0;
5 .};
6.
7. class timer_t
8 .{
9. public:
10. timer_t (msecond_t _ duration, timer_callback_t * _ p_callback,
11. timer_callback_arg_t * _ p_callback_arg );
12.
13. private:
14. void fire ()
15 .{
16. p_callback _. handle (this, p_callback_arg _);
17 .}
18.
19. timer_callback_t * p_callback _;
20. timer_callback_arg_t * p_callback_arg _;
21 .};
Figure 1
Figure 2 shows how to use a timer. First, you must use the timer_callback_t class to implement the corresponding callback function class. Then, you need to instantiate the callback function class when creating the timer. The foo () and bar () functions in the figure respectively demonstrate two methods for instantiating callback function classes. The former uses static class variables, the latter uses dynamic allocation through new.
1. class connect_timeout_callback_t: public timer_callback_t
2 .{
3. void handle (timer_t & _ timer, timer_callback_arg_t * _ p_arg)
4 .{
5. // do something here
6 .}
7 .};
8.
9. void foo ()
10 .{
11. static connect_timeout_callback_t callback;
12. timer_t * p_timer = new timer_t (100, & callback, 0 );
13 .}
14.
15. void bar ()
16 .{
17. connect_timeout_callback_t * p_callback = new connect_timeout_callback_t ();
18. timer_t * p_timer = new timer_t (100, p_callback, 0 );
19 .}
Figure 2
The implementation of the timer module makes it easy to make mistakes when instantiating callback function classes in foo () and bar () functions. In the methods used by the foo () function, if you accidentally forget to define the class variables as static, the program will eventually fail because the variables are allocated to the stack; in the bar () function, if you forget to release the memory allocated through new through delete, internal leakage will occur. Can these potential problems be avoided through design?
Figure 3 is the program that uses the template to rewrite the timer management module. The biggest change is that the timer_t class constructor saves the specified callback function class instance, and the callback function class and callback function parameters become two template types. Another change is that the fire () function instantiates the callback function class by defining static variables.
1. template <typename T_CALLBACK, typename T_CALLBACK_ARG>
2. class timer_callback_t
3 .{
4. virtual void handle (timer_t <T_CALLBACK, T_CALLBACK_ARG> & _ timer,
5. T_CALLBACK_ARG _ arg) = 0;
6 .};
7.
8. template <typename T_CALLBACK, typename T_CALLBACK_ARG>
9. class timer_t
10 .{
11. public:
12. timer_t (msecond_t _ duration, T_CALLBACK_ARG _ callback_arg );
13.
14. private:
15. void fire ()
16 .{
17. static T_CALLBACK callback;
18. callback. handle (* this, callback_arg _);
19 .}
20.
21. T_CALLBACK_ARG callback_arg _;
22 .};
Figure 3
Figure 4 shows how to use a timer in the new implementation. Obviously, we use templates to circumvent some potential problems through internalization.
1. class connect_timeout_callback_t:
2. public timer_callback_t <connect_timeout_callback_t, void *>
3 .{
4. void handle (timer_t <connect_timeout_callback_t, void *> & _ timer, void * _ arg)
5 .{
6. // do something here
7 .}
8 .};
9.
10. void foo ()
11 .{
12. timer_t <connect_timeout_callback_t, void *> * p_timer = new timer (100, 0 );
13 .}
Figure 4
From Li Yun