All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Data Fields | Private Attributes | Related Functions
as_string Struct Reference

Detailed Description

Container for NULL-terminates string values.

Initialization

An as_string should be initialized via one of the provided function.

To initialize a stack allocated as_string, use as_string_init():

as_string_init(&s, "abc", false);

The 3rd argument indicates whether the string value should be free()d when as_string is destroyed.

To create and initialize a heap allocated as_integer, use as_integer_new():

as_string * s = as_string_new("abc", false);

Destruction

When the as_string instance is no longer required, then you should release the resources associated with it via as_string_destroy():

Usage

There are two functions for getting the boxed value contained by as_string:

as_string_get() returns the contained value. If an error occurred, then NULL is returned. Possible errors is the as_integer instance is NULL.

char * sval = as_string_get(i);

as_string_getorelse() allows you to return a default value if an error occurs:

char * sval = as_string_getorelse(i, "oops!");

Conversions

as_string is derived from as_val, so it is generally safe to down cast:

as_val val = (as_val) s;

However, upcasting is more error prone. When doing so, you should use as_string_fromval(). If conversion fails, then the return value is NULL.

Definition at line 109 of file as_string.h.

#include "as_string.h"

+ Inheritance diagram for as_string:
+ Collaboration diagram for as_string:

Data Fields

bool free
 
size_t len
 
char * value
 
- Data Fields inherited from as_val
cf_atomic32 count
 
bool free
 
enum as_val_t type
 

Private Attributes

as_val _
 

Related Functions

(Note that these are not member functions.)

void as_string_destroy (as_string *string)
 
as_stringas_string_fromval (const as_val *v)
 
char * as_string_get (const as_string *string)
 
char * as_string_getorelse (const as_string *string, char *fallback)
 
as_stringas_string_init (as_string *string, char *value, bool free)
 
size_t as_string_len (as_string *string)
 
as_stringas_string_new (char *value, bool free)
 
char * as_string_tostring (const as_string *string)
 
as_valas_string_toval (const as_string *s)
 

Friends And Related Function Documentation

void as_string_destroy ( as_string string)
related

Destroy the as_string and associated resources.

Definition at line 173 of file as_string.h.

as_string * as_string_fromval ( const as_val v)
related

Convert from an as_val.

Definition at line 243 of file as_string.h.

char * as_string_get ( const as_string string)
related

Get the string value.

Definition at line 208 of file as_string.h.

char * as_string_getorelse ( const as_string string,
char *  fallback 
)
related

Get the string value. If string is NULL, then return the fallback value.

Definition at line 198 of file as_string.h.

as_string * as_string_init ( as_string string,
char *  value,
bool  free 
)
related

Initialize a stack allocated as_string.

If free is true, then the string value will be freed when the as_string is destroyed.

Parameters
stringThe stack allocated as_string to initialize
valueThe NULL terminated string of character.
freeIf true, then the value will be freed when as_string is destroyed.
Returns
On success, the initialized string. Otherwise NULL.
size_t as_string_len ( as_string string)
related

The length of the string

Parameters
stringThe string to get the length of.
Returns
the length of the string in bytes.
as_string * as_string_new ( char *  value,
bool  free 
)
related

Create and initialize a new heap allocated as_string.

If free is true, then the string value will be freed when the as_string is destroyed.

Parameters
valueThe NULL terminated string of character.
freeIf true, then the value will be freed when as_string is destroyed.
Returns
On success, the new string. Otherwise NULL.
char * as_string_tostring ( const as_string string)
related

Get the string value.

Definition at line 219 of file as_string.h.

as_val * as_string_toval ( const as_string s)
related

Convert to an as_val.

Definition at line 233 of file as_string.h.

Field Documentation

as_val as_string::_
private

as_boolean is a subtype of as_val. You can cast as_boolean to as_val.

Definition at line 116 of file as_string.h.

bool as_string::free

If true, then as_string.value can be freed.

Definition at line 121 of file as_string.h.

size_t as_string::len

The length of the string.

Definition at line 131 of file as_string.h.

char* as_string::value

The string value.

Definition at line 126 of file as_string.h.


The documentation for this struct was generated from the following file: