BOOST serialization supplementary instructions

xiaoxiao2021-03-06  41

Boost serialization supplementation This studies have studied the serialized application of Boost :: XML_Serialization yesterday, especially in the XML_Serialization partialized part of the sequence of objects, it is worth studying, I spent some time looking at this part How is the code is implemented, and you will be recorded, do a note and friends. (1) Review Yesterday research to do the object sequenced part of the object to do the sequence Test * a = NULL; OA >> Boost_Serialization_nvp ( nvalue); OA >> Boost_Serialization_nvp (INTS); OA >> Boost_Serialization_nvp (a); At this time, it will generate the implementation of this part of the TEST object is to track the code is this (2) object creation. Process a. First, start from Operator >> (Test * & A) This operator starts B. Then call Load_Override (Test * & A, BPPST_PFTO INT) => Basic_Xml_iarchive :: load_override (test * & a, 0) c.basic_xml_iarchive: : Load_override (Test * & a, 0) This function is a few things, the node starts load_start ("A" / * Name, the default is the name of the variable * /), the node ends load_end ("a") The other is the core LOAD function. D.archive :: load function Do the type matching invoke (xml_iarchive, a / * is referenced * /) E.INVOKE function Do two main things, Register_Type (XML_iarchive, Test * & a) and xml_iarchive.load_pointer

The register_type function implements a brief description of the code. This function does not have any special features. It is mainly to generate a cobject_id structure that adds it to an array. COBJECT_ID needs a Basic_ISerializer structure, this structure saves two static Type pointer XML_iarchive's pointer, one is a pointer of Test *. Consider the following code to clear template const basic_pointer_iserializer * register_type (T * t = NULL) {const basic_pointer_iserializer & bpis = archive :: detail :: instantiate_pointer_iserializer (static_cast (NULL), static_cast (Null); this-> this () -> register_basic_serializer (bpis.get_basic_serializer ()); return bpis;} These two pointers are saved here

A brief description of the ar.load_pointer function, this function is a functional function of dynamically generating class instance. There is no code to process the version object ID. It is worth recording bpis_ptr-> load_object_ptr (Ar, Object_ID_Vector [ui] .address, co.file_version) this function is the more critical function, to achieve the following specific code template BOOST_DLLEXPORT void pointer_iserializer :: load_object_ptr (basic_iarchive & ar, void * & x, const Unsigned int file_version) const {auto_ptr_with_deleter : :: invoke ()); if (null == ap.get ()) Boost :: throw_exception (std :: bad_alloc ()); t * t = ap.get (); x = t; // this addresses an obscure situtation that occurs when load_constructor // de-serializes something through and a pointer.ar.next_object_pointer (t); Archive & ar_impl = boost :: smart_cast_reference (ar); Boost :: Serialization :: Load_construct_data_adl (ar_impl, t, file_version); ar_impl >> boost :: serialization :: make_nvp (null, * t); ap.release (); The red part of the code is allocated by the memory, in which the AUTO_PTR_WITH_DELETER This smart pointer is used, and the distributor of the Heap_allocator , the implementation code of these two tool classes is simple, see their code clear, Calling this sentence will assign a pile of memory for the TEST object as a pointer pointing, pay attention to this There is no call to Test's constructor, just assigning a space for this object because memory usage memory is used in HEAP_ALLOCATOR is Operator New (SIZEOF (T)), this operation is only assigned a space and does not call Test constructor. The code of the purple part is a true call constructor part. After debugging, it will find that finally by calling Template Static Void Construction (Archive & / * Ar * /, T * T) {/ / default is inplace invocation of default constructor // Note the: before the placement new. Required if the placement new defined.::new(T )T;} This function is called The constructor of the Test object, and this object is really created.

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

New Post(0)