Advanced Programming in Linux -- CLStatus and clstatus
Many libraries provide several classes for external users. How can I tell the caller whether an error occurs when an error occurs in each method and the error code (in Linux, the error occurs. in h, the global errno is used to save the error code executed by our Linux program )? There are many methods. For the sake of simplicity, the function will return an object, which saves the return value and error code of the function.
/** CLStatus. h ** Author: lilin * email: lilin@uestc.edu.cn */# ifndef CLSTATUS_H # define CLSTATUS_H // class CLStatus {public:/* lReturnCode> = 0 indicates success, otherwise it fails */CLStatus (long lReturnCode, long lErrorCode); CLStatus (const CLStatus & s); virtual ~ CLStatus (); public: bool IsSuccess (); public:/* This can be m_lErrorCode and m_lReturnCode as public members to hide the write, however, read */const long & m_clReturnCode; const long & m_clErrorCode; private: // return value long m_lReturnCode; // error code long m_lErrorCode;}; # endif
#include "CLStatus.h"CLStatus::CLStatus(long lReturnCode, long lErrorCode) : m_clReturnCode(m_lReturnCode), m_clErrorCode(m_lErrorCode){m_lReturnCode = lReturnCode;m_lErrorCode = lErrorCode;}CLStatus::~CLStatus(){}CLStatus::CLStatus(const CLStatus& s) : m_clReturnCode(m_lReturnCode), m_clErrorCode(m_lErrorCode){m_lReturnCode = s.m_lReturnCode;m_lErrorCode = s.m_lErrorCode;}bool CLStatus::IsSuccess(){if(m_clReturnCode >= 0)return true;elsereturn false;}
M_lReturnCode; stores the returned value, and m_lErrorCode stores the error code. Const long & m_clReturnCode; const long & m_clErrorCode; in this way, m_lErrorCode and m_lReturnCode can be used as public members to hide the write, but the read can be made public. Of course, we have other options to mention the getMIReturnCode () method to achieve the same effect. Of course, here we feel that the get method is better suited to the Object-oriented Data encapsulation feature, instead of the set method. (For the code, see github.
APUESrc/2/2.7/
Can the above Code be optimized in terms of efficiency?
/* * test.cpp * * Author: lilin * email: lilin@uestc.edu.cn */#include <iostream>using namespace std;class A{public:A(){cout << "In A(): " << hex << (long)this << endl;}A(const A&){cout << "In A(const A&): " << hex << (long)this << endl;}~A(){cout << "In ~A(): " << hex << (long)this << endl;}A& operator=(const A& a){cout << "In operator=: " << hex << (long)this << " = " << hex << (long)(&a) << endl;return *this;}};A f(){A a;return a;}int main(int argc, char* argv[]){A a;a = f();return 0;}The code execution result is as follows:
In A(): 7fff834e277eIn A(): 7fff834e277fIn operator=: 7fff834e277e = 7fff834e277fIn ~A(): 7fff834e277fIn ~A(): 7fff834e277e
Slightly modify the code:
/* * test.cpp * * Author: lilin * email: lilin@uestc.edu.cn */#include <iostream>using namespace std;class A{public:A(){cout << "In A(): " << hex << (long)this << endl;}A(const A&){cout << "In A(const A&): " << hex << (long)this << endl;}~A(){cout << "In ~A(): " << hex << (long)this << endl;}A& operator=(const A& a){cout << "In operator=: " << hex << (long)this << " = " << hex << (long)(&a) << endl;return *this;}};A f(){return A();}int main(int argc, char* argv[]){A a = f();return 0;}Check the running result:
In ~A(): 7ffff682a68fIn ~A(): 7ffff682a68e
Obviously, less than one object is created in the entire process, and less than one = operation is called. Is efficiency significantly improved. But why? Here, let's see what the program has done?
The modified Code actually creates only one object, which is created in the f () function. In A a = f (); in this line of code, the call is not A value assignment operation, but a default copy constructor, in the default copy constructor, the object reference is directly returned. Therefore, only one CLStatus object is created. As to why the default copy constructor is called instead of the overload assignment operation, you can refer to the differences between the copy constructor and the assignment operator.
Therefore, in order to ensure both efficiency and portability, in the future, the return values of our functions are all packaged with CLStatus and then returned. We recommend that you write the code as follows:
CLStatus f(){return CLStatus(…);}CLStatus s = f();
(If you have any questions or suggestions, please contact cfreestar@163.com)
What are the adverse factors for Linux to learn about advanced programming in the UNIX environment ??
If you do not have any basis for this,
Let's look at the linux program design first,
Basic,
Lay a solid foundation for subsequent learning,
Later, let's look at the linux advanced program design,
In-depth Linux kernel architecture,
And so on !!!
Is Unix advanced programming applicable in Linux?
Many operating systems on the market today are early Unix branches. For example, Linux is a Unix branch. What are the sco unix, Solaris series, and FreeBSD are derived from Unix. Their C library is exactly the same, and the system calls functions are basically the same, but there are few differences. So if you are familiar with Linux programming, you can say that you are familiar with Unix programming.