base2.c
Go to the documentation of this file.
00001 
00026 #include "base2_friend.h"
00027 
00029 #define BASE2_STR_SIZE 64
00030 
00035 typedef struct base2_private_st_ {
00037     const base2_vtable_st *vtable;
00038 } base2_private_st;
00039 
00049 my_rc_e
00050 base2_string_size (base2_handle base2_h, size_t *buffer_size)
00051 {
00052     my_rc_e rc = MY_RC_E_SUCCESS;
00053 
00054     VALIDATE_VTABLE_FN(base2_h, private_h, vtable, string_size_fn, rc);
00055     if (my_rc_e_is_notok(rc)) {
00056         return (rc);
00057     }
00058 
00059     return (base2_h->private_h->vtable->string_size_fn(base2_h, buffer_size));
00060 }
00061 
00070 static my_rc_e
00071 base2_string_size_internal (base2_handle base2_h, size_t *buffer_size)
00072 {
00073     if ((NULL == base2_h) || (NULL == buffer_size)) {
00074         LOG_ERR("Invalid input, base2_h(%p) buffer_size(%p)",
00075                 base2_h, buffer_size);
00076         return (MY_RC_E_EINVAL);
00077     }
00078 
00079     *buffer_size = BASE2_STR_SIZE;
00080 
00081     return (MY_RC_E_SUCCESS);
00082 }
00083 
00091 static const char *
00092 base2_type_string_internal (base2_handle base2_h)
00093 {
00094     return ("base2");
00095 }
00096 
00104 const char *
00105 base2_type_string (base2_handle base2_h)
00106 {
00107     my_rc_e rc = MY_RC_E_SUCCESS;
00108 
00109     VALIDATE_VTABLE_FN(base2_h, private_h, vtable, type_string_fn, rc);
00110     if (my_rc_e_is_notok(rc)) {
00111         return ("");
00112     }
00113 
00114     return (base2_h->private_h->vtable->type_string_fn(base2_h));
00115 }
00116 
00126 my_rc_e
00127 base2_string (base2_handle base2_h, char *buffer, size_t buffer_size)
00128 {
00129     my_rc_e rc = MY_RC_E_SUCCESS;
00130 
00131     VALIDATE_VTABLE_FN(base2_h, private_h, vtable, string_fn, rc);
00132     if (my_rc_e_is_notok(rc)) {
00133         return (rc);
00134     }
00135 
00136     return (base2_h->private_h->vtable->string_fn(base2_h, buffer,
00137                                                   buffer_size));
00138 }
00139 
00150 static my_rc_e
00151 base2_string_internal (base2_handle base2_h, char *buffer, size_t buffer_size)
00152 {
00153     size_t min_size;
00154     my_rc_e rc = MY_RC_E_SUCCESS;
00155 
00156     if ((NULL == base2_h) || (NULL == buffer)) {
00157         LOG_ERR("Invalid input, base2_h(%p) buffer(%p) buffer_size(%zu)",
00158                 base2_h, buffer, buffer_size);
00159         return (MY_RC_E_EINVAL);
00160     }
00161 
00162     VALIDATE_VTABLE_FN(base2_h, private_h, vtable, string_size_fn, rc);
00163     if (my_rc_e_is_notok(rc)) {
00164         return (rc);
00165     }
00166 
00167     rc = base2_h->private_h->vtable->string_size_fn(base2_h, &min_size);
00168     if (my_rc_e_is_notok(rc)) {
00169         return (rc);
00170     }
00171 
00172     if (buffer_size < min_size) {
00173         LOG_ERR("Invalid input, buffer_size(%zu)", buffer_size);
00174         return (MY_RC_E_EINVAL);
00175     }
00176 
00177     snprintf(buffer, buffer_size, "val1(%u)", base2_h->val1);
00178 
00179     return (MY_RC_E_SUCCESS);
00180 }
00181 
00192 static void
00193 base2_delete_internal (base2_handle base2_h, bool free_base2_h)
00194 {
00195     if (NULL == base2_h) {
00196         return;
00197     }
00198 
00199     if (NULL != base2_h->private_h) {
00200         free(base2_h->private_h);
00201         base2_h->private_h = NULL;
00202     }
00203 
00204     if (free_base2_h) {
00205         free(base2_h);
00206     }
00207 }
00208 
00219 void
00220 base2_friend_delete (base2_handle base2_h)
00221 {
00222     base2_delete_internal(base2_h, false);
00223 }
00224 
00233 static void
00234 base2_private_delete (base2_handle base2_h)
00235 {
00236     base2_delete_internal(base2_h, true);
00237 }
00238 
00245 void
00246 base2_delete (base2_handle base2_h)
00247 {
00248     my_rc_e rc = MY_RC_E_SUCCESS;
00249 
00250     VALIDATE_VTABLE_FN(base2_h, private_h, vtable, delete_fn, rc);
00251     if (my_rc_e_is_notok(rc)) {
00252         return;
00253     }
00254 
00255     return (base2_h->private_h->vtable->delete_fn(base2_h));
00256 }
00257 
00264 my_rc_e
00265 base2_increase_val1 (base2_handle base2_h)
00266 {
00267     my_rc_e rc = MY_RC_E_SUCCESS;
00268 
00269     VALIDATE_VTABLE_FN(base2_h, private_h, vtable, increase_val1_fn, rc);
00270     if (my_rc_e_is_notok(rc)) {
00271         return (rc);
00272     }
00273 
00274     return (base2_h->private_h->vtable->increase_val1_fn(base2_h));
00275 }
00276 
00284 my_rc_e
00285 base2_get_val1 (base2_handle base2_h, uint32_t *val1)
00286 {
00287     if ((NULL == base2_h) || (NULL == val1)) {
00288         LOG_ERR("Invalid input, base2_h(%p) val1(%p)", base2_h, val1);
00289         return (MY_RC_E_EINVAL);
00290     }
00291 
00292     *val1 = base2_h->val1;
00293 
00294     return (MY_RC_E_SUCCESS);
00295 }
00296 
00302 static const base2_vtable_st base2_vtable = {
00303     base2_private_delete,
00304     base2_type_string_internal,
00305     base2_string_internal,
00306     base2_string_size_internal,
00307     NULL
00308 };
00309 
00319 my_rc_e
00320 base2_inherit_vtable (const base2_vtable_st *parent_vtable,
00321                       base2_vtable_st *child_vtable,
00322                       bool do_null_check)
00323 {
00324     my_rc_e rc = MY_RC_E_SUCCESS;
00325 
00326     /* Always add a new check here if functions are added. */
00327     CT_ASSERT(5 == (sizeof(base2_vtable_st)/sizeof(void*)));
00328 
00329     if ((NULL == parent_vtable) || (NULL == child_vtable)) {
00330         LOG_ERR("Invalid input, parent_vtable(%p) "
00331                 "child_vtable(%p)", parent_vtable, child_vtable);
00332         return (MY_RC_E_EINVAL);
00333     }
00334 
00335     INHERIT_VTABLE_FN(parent_vtable, child_vtable, delete_fn, do_null_check,
00336                       rc);
00337     INHERIT_VTABLE_FN(parent_vtable, child_vtable, type_string_fn,
00338                       do_null_check, rc);
00339     INHERIT_VTABLE_FN(parent_vtable, child_vtable, string_fn, do_null_check,
00340                       rc);
00341     INHERIT_VTABLE_FN(parent_vtable, child_vtable, string_size_fn,
00342                       do_null_check, rc);
00343     INHERIT_VTABLE_FN(parent_vtable, child_vtable, increase_val1_fn,
00344                       do_null_check, rc);
00345 
00346     return (MY_RC_E_SUCCESS);
00347 
00348 err_exit:
00349 
00350     return (rc);
00351 }
00352 
00362 my_rc_e
00363 base2_set_vtable (base2_handle base2_h, base2_vtable_st *vtable)
00364 {
00365     my_rc_e rc;
00366 
00367     if ((NULL == base2_h) || (NULL == vtable)) {
00368         LOG_ERR("Invalid input, base2_h(%p) vtable(%p)", base2_h, vtable);
00369         return (MY_RC_E_EINVAL);
00370     }
00371 
00372     if (NULL == base2_h->private_h) {
00373         LOG_ERR("Invalid input, base2_h(%p)->private_h(%p)", base2_h,
00374                 base2_h->private_h);
00375         return (MY_RC_E_EINVAL);
00376     }
00377 
00378     rc = base2_inherit_vtable(&base2_vtable, vtable, true);
00379     if (my_rc_e_is_notok(rc)) {
00380         return (rc);
00381     }
00382 
00383     base2_h->private_h->vtable = vtable;
00384 
00385     return (MY_RC_E_SUCCESS);
00386 }
00387 
00398 my_rc_e
00399 base2_init (base2_handle base2_h)
00400 {
00401     my_rc_e rc = MY_RC_E_SUCCESS;
00402 
00403     if (NULL == base2_h) {
00404         LOG_ERR("Invalid input, base2_h(%p)", base2_h);
00405         return (MY_RC_E_EINVAL);
00406     }
00407 
00408     base2_h->private_h = calloc(1, sizeof(*base2_h->private_h));
00409     if (NULL == base2_h->private_h) {
00410         rc = MY_RC_E_ENOMEM;
00411         goto err_exit;
00412     }
00413 
00414     base2_h->private_h->vtable = &base2_vtable;
00415     base2_h->val1 = 7;
00416 
00417     return (MY_RC_E_SUCCESS);
00418 
00419 err_exit:
00420 
00421     if (NULL != base2_h->private_h) {
00422         free(base2_h->private_h);
00423         base2_h->private_h = NULL;
00424     }
00425 
00426     return (rc);
00427 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines