How to implement a DepRecated API in C ++

zhaozj2021-02-16  89

How to implement a DepRecated API in C

How to Deprecate AN API IN C

Perhaps a first-defined interface in the development process is like this:

1 |

2 | #pragma overce

3 | INT SomeApiv1 (Char * Byptr);

4 |

The test procedure is as follows:

5 | int main ()

6 | {

3 | INT VALUE;

8 | int RET = SomeApiv1 ((char *) & value);

9 | Printf ("Value Return IS% D, PARAM RETURN IS% # 06X", RET, VALUE);

10 | RETURN 0;

11 |}

12 |

After a while, the second version of the function defined in this way, the first edition is no longer available.

13 | #pragma overce

14 |

15 | INT SomeApiv1 (Char * Byptr); // DepRecated

16 | INT & BYAPIV2 (INT & BYREF);

17 |

In fact, SomeAPIV1 has become a package of SomeApiv2.

18 | INT SomeApiv1 (Char * Byptr)

19 | {

Int value;

21 | int RET = SomeApiv2 (Value);

22 | Memcpy (Byptr, & Value, SizeOf (int));

23 | RETURN RET;

24 |}

25 |

At this time, the test program can be compiled naturally.

If the author of the test program does not check the comments in the header file, I don't know at all, the interface has been abandoned. It may always use the old interface.

If the interface is changed:

26 | #pragma overce

27 | Int SomeApiv2 (INT & BYREF);

28 |

29 | Template

30 | Int SomeApiv1 (Char * Byptr);

31 |

32 | //

33 | // Deprecated API Wrap Here

34 | //

35 | Template <>

36 | INT SomeApiv1 (char * byptr)

37 | {

38 | INT VALUE;

39 | int RET = SomeApiv2 (Value);

40 | Memcpy (Byptr, & Value, SizeOf (int));

41 | RETURN RET;

42 |}

43 |

At this time, compile the test program, the compiler will prompt:

Error C2783: "Int SomeApiv1 (Char *)": Template parameters that fail to derive "IS_DEPRECATED"

DepRecatedApi.cpp (22): See "SomeApiv1" declaration

"See" SomeApiv1 "declaration" !! OK, this is what we want: Tips author interface has been given up.

Of course, if you want to use an old version temporarily. It doesn't matter. In the code, it is clear that it is the abandoned interface.

You can:

44 | Int main ()

45 | {

46 | INT VALUE;

47 | int RET = SomeApiv1 ((char *) & value);

48 | Printf ("Value Return IS% D, PARAM RETURN IS% # 06X", RET, VALUE); 49 | RETURN 0;

50 |}

51 |

Attached to someAPIV1 implementation:

52 | Int SomeApiv1 (Char * Byptr)

53 | {

54 | BYPTR [0] = 0x00;

55 | BYPTR [1] = 0x01;

56 | BYPTR [2] = 0x00;

57 | BYPTR [3] = 0x00;

58 |

59 | Printf ("SomeApiv1 Called! / N");

60 | Return 1;

61 |}

62 |

SomeApiv2 implementation:

63 | Int SomeApiv2 (INT & BYREF)

64 | {

65 | BYREF = 0x0200;

66 | Printf ("SomeApiv2 Called! / N");

67 | RETURN 2;

68 |}

69 |

转载请注明原文地址:https://www.9cbs.com/read-15604.html

New Post(0)