Understand what an object is before you use object programming.
An object is an abstract encapsulation of data and operations.
Advantages:
Cohesion, easy to use
Disadvantages:
The related operation function is stateful (that is, the side effects that are often said in the function programming, the non-reentrant functions in the operating system development),
Executing the function at different points of time may return different results), not convenient for parallel processing
C language to implement an object, the key is to solve the data and operation of encapsulation.
The encapsulation of 1> data is mainly implemented by structure, and the encapsulation of operation is realized by function pointer.
2> data hiding, mainly through the universal void* pointer to achieve.
The general operation of the object consists of the following 3
1> creation, corresponding to the new function
2> destroy, corresponding to delete function
3> copy, corresponding to the clone function
The C language does not support the string conversion function, Rtti needs to add its own type attribute and implement the corresponding factory class.
Take FFmpeg's Urlprotocol class as an example:
typedef struct URLPROTOCOL {
const char *name;
Int (*url_open) (Urlcontext *h, const char *url, int flags);
/**
* Read data from the protocol.
* IF data is immediately available (even less than size), EOF is
* Reached or an error occurs (including EINTR), return immediately.
* Otherwise:
* In non-blocking mode, return Averror (Eagain) immediately.
* In blocking mode, wait for data/eof/error with a short timeout (0.1s),
* and return Averror (Eagain) on timeout.
* Checking Interrupt_callback, looping on eintr and Eagain and until
* Enough data has been read was left to the calling function; See
* Retry_transfer_wrapper in AVIO.C.
*/
Int (*url_read) (Urlcontext *h, unsigned char *buf, int size);
Int (*url_write) (Urlcontext *h, const unsigned char *buf, int size);
int64_t (*url_seek) (Urlcontext *h, int64_t pos, int whence);
Int (*url_close) (Urlcontext *h);
...
int priv_data_size;
Const Avclass *priv_data_class;
int flags;
Int (*url_check) (Urlcontext *h, int mask);
} Urlprotocol;
The above Url_open corresponds to a series of operating interfaces.
Next look at how to build an object instance
Urlprotocol Ff_tcp_protocol = {
. Name = "TCP",
. Url_open = Tcp_open,
. Url_read = Tcp_read,
. Url_write = Tcp_write,
. Url_close = Tcp_close,
. Url_get_file_handle = Tcp_get_file_handle,
. Url_shutdown = Tcp_shutdown,
. priv_data_size = sizeof (Tcpcontext),
. Priv_data_class = &tcp_context_class,
. Flags = Url_protocol_flag_network,
};
Here TCP is a concrete example of abstract urlprotocol.
The operation functions are implemented as follows:
int Tcp_open (Urlcontext *h, const char *uri, int flags)
{
}
It is important to note that because the Urlprotocol pointer is encapsulated in the urlcontext, we do not add urlprotocol* this pointer.
Unlike C + +, Java and other syntax-level support classes, we need to do a little bit of what the compiler does, and manually bind the operation to the data.
If you feel that adding this pointer is cumbersome, you can use the C language macros, or Perl, Python and other scripting languages to do automatic code generation to improve efficiency.
In-depth understanding of C language 11 object-based (based) programming