Introduction
In this framework contains a serialized basic framework, a basic type identification system that identifies the basic type, complex type, custom type, and STD container type, and can be recursively expanded on this basis.
Complex data structures can be sequenced to the file and recover from the file.
Contains a complete automatic unit test, and test cases, click here to download.
text
Writing this serialization framework initially wanted to use a large project, there are some quite complicated tree data structures built at runtime. If you can sequence this memory tree, you can save the next time. Created time. In addition to some of your own tools, some data want to be saved in the file, and later read from the file, it is also very convenient to use serialization. And at that time, the system has learned a C template technology. It feels difficult to use some advanced template technology in a general programming activity, so I want to write this sequence frame with C template technology. In the last project, there is no use of this serialization framework, but I have reached the second goal, write this sequence framework, let me have a deeper understanding of C template technology.
In this framework contains a serialized basic framework, a basic type identification system that identifies the basic type, complex type, custom type, and STD container type, and can be recursively expanded on this basis.
While writing this frame, I also wrote a complete test case. If there is no test case, you should debug such a frame, it is really difficult to get to the sky, because the template error, the information reported by the compiler is ugly, and some are useless.
The code is written in VC7.1, and can only be used in VC7.1, VC6 supports very limited for C templates, while other compilers are supported in this regard. If you want to use other compilers, you may want to modify the code of the partial type identification. Test framework I use CPPUnit (1.9.14), which is an open source test framework, which can be downloaded at www.xprogramming.com. The code in which Identification, I mainly refer to the "C Template" book, and some code in Boost.
Since it is much more efficient than the operational time-based framework in the MFC written by template. It is also quite simple to use. If you want to learn the advanced technology of the C template, study this framework can benefit much. Because it is a frame code, I write quite specification, annotated, and have a complete test case, which can be automatically registered.
The method used is relatively simple, please refer to the test case in the filewtest.cpp) file.
Ordinary data type:
(Unsigned char, unsigned short, unsigned int, unsigned long, signed char, signed short, signed int, signed long, bool, char, wchar_t, unsigned long long, signed long long, float, double, long double) can be directly serialize And reverse sequence.
For pointer types:
The sequence of sequential pointers specifically points to the object, if the type of object points to the pointer is the type of the serialization frame that cannot be identified will report compilation errors. Note When it is reversed, only one empty pointer is required, and the serialization frame will reach the value of the serialized object to the heap and pay the address to the pointer. If you pass a value of a pointer, an assertion error is triggered in the DEBUG mode. In Release, it will cause the object that the original pointer points to be leaked.
Array for normal data types:
The entire array will be serialized in memory to memory, even if there is no element that is really assigned. An array of deserials can be transmitted. It should be noted that the capacity of the inward array must be greater than or equal to the capacity of the serialized array, otherwise the memory error of the array croucher will cause an assertion error in DEBUG mode. Array of non-deployed data types:
The type of array element can be a type supported by all serialized frames other than a normal data type. When serialization, the serialization framework for each element is called the specific sequence of sequence, and it is also true. Since the array of type types in Release mode is declaring, the compiler generates code that calls the default constructor of the corresponding class. But for the original type, if the pointer array type If the non-explicit manual initialization, the value in the array is a unintentional random value. This sequence frame cannot be identified, which will agree with a serious memory error. Another element of certain elements of the pointer array is NULL, and the serialization framework cannot be processed, and an assertion error is thrown in Debug mode.
Because of the pointer array unless the elements in the array are all meaningful pointers, it should not be used as an array to serialize, and the corresponding traversal logic should be added, and the interesting element is sequenced one by one.
For a general array, if interest is only a small part of the elements, sequencing should be performed in the above manner to improve performance.
Custom data class type:
No copy constructor is required, do not need to copy the value, you do not need the class of the destructor. Such as old-fashioned struct structural types. This type can be highly serialized and serialized by direct copying memory. Just give a class from _data_class_tag to derive, the serialization framework will treat it as a normal data class type.
Custom complex type:
For non-data class types, you must derive from CSerializable, and add the serializable (name, x) macro in the definition of the class. Name is the name of the class, X is the corresponding version number. The introduction of the version number is mainly to avoid using the previously generated serialization file after a class is modified to avoid memory errors. The Virtual Bool Serialize (CMEDIA *) const; function must be implemented in the class, and the specific serialization code is written in this function. The content of this function is very simple. According to serialization and reverse sequencing, it is simple to call each member function that requires serialization and reverse sequence, as follows:
IF (pmedia-> isstoring ()) {* pmedia << m_1 << m_2 << m_3 << m_4 << m_5; return true;}} (pmedia-> isloading ()) {* pmedia >> m_1 >> m_2 >> m_3 >> m_4 >> m_5; return true;
Pay attention to serialization and reverse sequencing order.
Std :: string and std :: wstring Type:
It is relatively simple to use. It is worth noting that it is the same as the string array to make a character pointer. If there is a large String that has a large capacity (generally to avoid the memory weight distribution overhead at the time), it only uses a small portion. Serialized and reverse sequencing, the capacity of String objects is just that part of the memory.
Std :: PaIR Type:
As long as the first and second of Pair must be supported by the serialization framework, it can be normalized and reverse sequence.
STD container type:
(Vector, List, Deque, Stack, Queue, SET, MULTISET, MAP, MULTIMAP) support the above container type, where the element type in the container must be the type supported by the serialization framework.