mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-09-04 23:18:44 +02:00
docs: Move the refstring SECTION
Move the contents to the data-structures.md file. Helps: #3037
This commit is contained in:
committed by
Philip Withnall
parent
8164220213
commit
97cb9fa220
@@ -18,74 +18,6 @@
|
||||
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* SECTION:refstring
|
||||
* @Title: Reference counted strings
|
||||
* @Short_description: Strings with reference counted memory management
|
||||
*
|
||||
* Reference counted strings are normal C strings that have been augmented
|
||||
* with a reference counter to manage their resources. You allocate a new
|
||||
* reference counted string and acquire and release references as needed,
|
||||
* instead of copying the string among callers; when the last reference on
|
||||
* the string is released, the resources allocated for it are freed.
|
||||
*
|
||||
* Typically, reference counted strings can be used when parsing data from
|
||||
* files and storing them into data structures that are passed to various
|
||||
* callers:
|
||||
*
|
||||
* |[<!-- language="C" -->
|
||||
* PersonDetails *
|
||||
* person_details_from_data (const char *data)
|
||||
* {
|
||||
* // Use g_autoptr() to simplify error cases
|
||||
* g_autoptr(GRefString) full_name = NULL;
|
||||
* g_autoptr(GRefString) address = NULL;
|
||||
* g_autoptr(GRefString) city = NULL;
|
||||
* g_autoptr(GRefString) state = NULL;
|
||||
* g_autoptr(GRefString) zip_code = NULL;
|
||||
*
|
||||
* // parse_person_details() is defined elsewhere; returns refcounted strings
|
||||
* if (!parse_person_details (data, &full_name, &address, &city, &state, &zip_code))
|
||||
* return NULL;
|
||||
*
|
||||
* if (!validate_zip_code (zip_code))
|
||||
* return NULL;
|
||||
*
|
||||
* // add_address_to_cache() and add_full_name_to_cache() are defined
|
||||
* // elsewhere; they add strings to various caches, using refcounted
|
||||
* // strings to avoid copying data over and over again
|
||||
* add_address_to_cache (address, city, state, zip_code);
|
||||
* add_full_name_to_cache (full_name);
|
||||
*
|
||||
* // person_details_new() is defined elsewhere; it takes a reference
|
||||
* // on each string
|
||||
* PersonDetails *res = person_details_new (full_name,
|
||||
* address,
|
||||
* city,
|
||||
* state,
|
||||
* zip_code);
|
||||
*
|
||||
* return res;
|
||||
* }
|
||||
* ]|
|
||||
*
|
||||
* In the example above, we have multiple functions taking the same strings
|
||||
* for different uses; with typical C strings, we'd have to copy the strings
|
||||
* every time the life time rules of the data differ from the life time of
|
||||
* the string parsed from the original buffer. With reference counted strings,
|
||||
* each caller can take a reference on the data, and keep it as long as it
|
||||
* needs to own the string.
|
||||
*
|
||||
* Reference counted strings can also be "interned" inside a global table
|
||||
* owned by GLib; while an interned string has at least a reference, creating
|
||||
* a new interned reference counted string with the same contents will return
|
||||
* a reference to the existing string instead of creating a new reference
|
||||
* counted string instance. Once the string loses its last reference, it will
|
||||
* be automatically removed from the global interned strings table.
|
||||
*
|
||||
* Since: 2.58
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "grefstring.h"
|
||||
|
Reference in New Issue
Block a user