UVW source code (4) and uvw source code
I was a little lazy after my 11 holiday and I haven't thought of writing anything for a long time. In addition, I recently played the LOL S game. It takes a long time to get in touch with LOL. Although I usually play less and enjoy a good level of food, I still won't miss the time for such a large event. It is mainly able to feel the passion of contestants for competition and the response to the ever-changing battles. KeKe ~~.
This article focuses on the source code discussion of UVW. It will introduce some other interesting things in the project and provide a little more knowledge.
1. templates
I have mentioned a lot of things before, but I have ignored them. For C ++ templates, many people have some opinions on this, saying it is hard to understand and difficult to compile.
We all know that templates are also a type of C ++ polymorphism, which belongs to the compilation phase polymorphism. That is to say, C ++ has added a lot of runtime work to the compilation phase, your code remains at the language abstraction level. However, the compiler actually features the use of templates of the same type during compilation. Therefore, compared with the running polymorphism, the running performance of the template is better, but the compilation time may be a little longer.
Although all of my views on templates are mentioned in the past, templates play a significant role in application. The simplest example is as follows:
std::shared_ptr<uvw::TcpHandle> tcp = loop.resource<uvw::TcpHandle>();
This is a statement for creating TcpHandle in our project. If we follow the General C idea, the code we write is probably like this:
TcpHandle* tcp = loop.resource(type);
OK. It looks normal, but a lot of problems are exposed. The prototype of this resource function should be:
Void * loop: resource (int type); or Handle * loop: resource (int type );
It must be a pointer to the base class void * or Handle, but you can only judge it by your understanding of the Code and type value. It returns the TcpHandle * type. If I accidentally write the return type as UdpHandle *, it also inherits from the Handle class, and C ++ does not detect this. At this time, the value of the template is displayed. It can directly restrict the returned type based on the template type. This avoids the above situation.
For example, when we talk about static in the second article, we cannot implement such a function without a template.
Some friends do not need templates when coding, or at most use some containers. Maybe even using a template will have some negative effects mentioned above. Therefore, many people know templates, but they do not often use them. Things that are not often used will often be forgotten very quickly, resulting in such a bad cycle. If the template can solve the practical coding problem, just use it boldly. Do not be afraid to use it incorrectly. If it is wrong, it can be used better!
2. enum
Enum is an enumeration type, but C ++ 11 has extensions for enum. First read this Code:
Fs. hpp 55 ~ 69
enum class UVDirentTypeT: std::underlying_type_t<uv_dirent_type_t> { UNKNOWN = UV_DIRENT_UNKNOWN, FILE = UV_DIRENT_FILE, 。。。。。。 };enum class UVCopyFileFlags: int { EXCL = UV_FS_COPYFILE_EXCL};
The differences between enum and enum class are as follows:
1. If the same header file contains the following code:
1 enum HandleType{TCP,UDP,POLL,FS};2 enum StreamType{TCP,UDP,POLL,FS};
This compilation fails, because enum itself is equivalent to a set of # define, and the name cannot be the same. However, if you use enum class to write, it is OK, for example:
1 enum class HandleType{TCP,UDP,POLL,FS};2 enum class StreamType{TCP,UDP,POLL,FS};
However, the scope needs to be added during use.
2. We all know that each element in enum can be converted to the int type directly (for different compilers, the types may be different ). However, enum class is not allowed. For example:
1 enum HandleType {TCP, UDP, POLL, FS}; 2 enum class StreamType {TCP, UDP, POLL, FS}; 3 4 int handleType = TCP; 5 int streanType = StreamType:: TCP; // Error
The compilation error occurs in the 5th rows. enum is easily converted to int by implicit conversion. In a sense, enum class solves this problem.
In addition, the element of the enum class can specify the type, for example:
1 enum smallenum: int16_t 2 { 3 a, 4 b, 5 c 6 }; 7 8 enum class altitude: char 9 { 10 high='h',11 low='l', 12 };
We can see that the "class" in this enum class can be omitted. What if I want to convert the enum class to the desired type? For example, in the above two examples, we can do this:
1 smallenum s = smallenum::a;2 int16_t i = static_cast<int16_t>(s);3 4 altitude e = altitude::high;5 char c = static_cast<char>(e);
Of course, switch... Case... Statements can be used directly.
Last
In the source code, some technologies and methods used in the syntax class are introduced here. These things are actually not difficult. You may know that you may have some knowledge about them through introduction, mainly to make it easier for you to read the source code and eliminate some obstacles.
Some readers may not be very familiar with the writing and usage of project code, but I want to say this should be a coding mode of modern C ++. You may not see a pointer in a project, but a strong conversion. You may use a large number of templates and many restricted keywords. Class implementations and definitions are stored in the same file, just like java, there are many other things you are not used. But when you get familiar with it, understand it, and get used to it, the efficiency may rise. In fact, in the end, we still need to improve our understanding of C ++.
Later, I will share some projects with you, such as architecture and algorithms. Due to the limited level, please correct me if there is an error in the blog.