00001
00024 #include "base1_friend.h"
00025
00027 #define BASE1_STR_SIZE 128
00028
00033 typedef struct base1_private_st_ {
00035 const base1_vtable_st *vtable;
00036 } base1_private_st;
00037
00042 const char *
00043 base1_get_val1_description (void)
00044 {
00045 return ("Value 1");
00046 }
00047
00058 my_rc_e
00059 base1_get_public_data (base1_handle base1_h, base1_public_data_st *public_data)
00060 {
00061
00062 if ((NULL == base1_h) || (NULL == public_data)) {
00063 LOG_ERR("Invalid input, base1_h(%p) public_data(%p)", base1_h,
00064 public_data);
00065 return (MY_RC_E_EINVAL);
00066 }
00067
00068 memcpy(public_data, &(base1_h->public_data), sizeof(*public_data));
00069
00070 return (MY_RC_E_SUCCESS);
00071 }
00072
00084 my_rc_e
00085 base1_set_public_data (base1_handle base1_h, base1_public_data_st *public_data)
00086 {
00087
00088 if ((NULL == base1_h) || (NULL == public_data)) {
00089 LOG_ERR("Invalid input, base1_h(%p) public_data(%p)", base1_h,
00090 public_data);
00091 return (MY_RC_E_EINVAL);
00092 }
00093
00094 memcpy(&(base1_h->public_data), public_data, sizeof(base1_h->public_data));
00095
00096 return (MY_RC_E_SUCCESS);
00097 }
00098
00108 my_rc_e
00109 base1_string_size (base1_handle base1_h, size_t *buffer_size)
00110 {
00111 my_rc_e rc = MY_RC_E_SUCCESS;
00112
00113 VALIDATE_VTABLE_FN(base1_h, private_h, vtable, string_size_fn, rc);
00114 if (my_rc_e_is_notok(rc)) {
00115 return (rc);
00116 }
00117
00118 return (base1_h->private_h->vtable->string_size_fn(base1_h, buffer_size));
00119 }
00120
00129 static my_rc_e
00130 base1_string_size_internal (base1_handle base1_h, size_t *buffer_size)
00131 {
00132 if ((NULL == base1_h) || (NULL == buffer_size)) {
00133 LOG_ERR("Invalid input, base1_h(%p) buffer_size(%p)",
00134 base1_h, buffer_size);
00135 return (MY_RC_E_EINVAL);
00136 }
00137
00138 *buffer_size = BASE1_STR_SIZE;
00139
00140 return (MY_RC_E_SUCCESS);
00141 }
00142
00150 static const char *
00151 base1_type_string_internal (base1_handle base1_h)
00152 {
00153 return ("base1");
00154 }
00155
00163 const char *
00164 base1_type_string (base1_handle base1_h)
00165 {
00166 my_rc_e rc = MY_RC_E_SUCCESS;
00167
00168 VALIDATE_VTABLE_FN(base1_h, private_h, vtable, type_string_fn, rc);
00169 if (my_rc_e_is_notok(rc)) {
00170 return ("");
00171 }
00172
00173 return (base1_h->private_h->vtable->type_string_fn(base1_h));
00174 }
00175
00185 my_rc_e
00186 base1_string (base1_handle base1_h, char *buffer, size_t buffer_size)
00187 {
00188 my_rc_e rc = MY_RC_E_SUCCESS;
00189
00190 VALIDATE_VTABLE_FN(base1_h, private_h, vtable, string_fn, rc);
00191 if (my_rc_e_is_notok(rc)) {
00192 return (rc);
00193 }
00194
00195 return (base1_h->private_h->vtable->string_fn(base1_h, buffer,
00196 buffer_size));
00197 }
00198
00209 static my_rc_e
00210 base1_string_internal (base1_handle base1_h, char *buffer, size_t buffer_size)
00211 {
00212 size_t min_size;
00213 my_rc_e rc = MY_RC_E_SUCCESS;
00214
00215 if ((NULL == base1_h) || (NULL == buffer)) {
00216 LOG_ERR("Invalid input, base1_h(%p) buffer(%p) buffer_size(%zu)",
00217 base1_h, buffer, buffer_size);
00218 return (MY_RC_E_EINVAL);
00219 }
00220
00221 VALIDATE_VTABLE_FN(base1_h, private_h, vtable, string_size_fn, rc);
00222 if (my_rc_e_is_notok(rc)) {
00223 return (rc);
00224 }
00225
00226 rc = base1_h->private_h->vtable->string_size_fn(base1_h, &min_size);
00227 if (my_rc_e_is_notok(rc)) {
00228 return (rc);
00229 }
00230
00231 if (buffer_size < min_size) {
00232 LOG_ERR("Invalid input, buffer_size(%zu)", buffer_size);
00233 return (MY_RC_E_EINVAL);
00234 }
00235
00236 snprintf(buffer, buffer_size, "val1(%u) val2(%u) val3(%u)",
00237 base1_h->public_data.val1, base1_h->public_data.val2,
00238 base1_h->val3);
00239
00240 return (MY_RC_E_SUCCESS);
00241 }
00242
00253 static void
00254 base1_delete_internal (base1_handle base1_h, bool free_base1_h)
00255 {
00256 if (NULL == base1_h) {
00257 return;
00258 }
00259
00260 if (NULL != base1_h->private_h) {
00261 free(base1_h->private_h);
00262 base1_h->private_h = NULL;
00263 }
00264
00265 if (free_base1_h) {
00266 free(base1_h);
00267 }
00268 }
00269
00280 void
00281 base1_friend_delete (base1_handle base1_h)
00282 {
00283 base1_delete_internal(base1_h, false);
00284 }
00285
00294 static void
00295 base1_private_delete (base1_handle base1_h)
00296 {
00297 base1_delete_internal(base1_h, true);
00298 }
00299
00306 void
00307 base1_delete (base1_handle base1_h)
00308 {
00309 my_rc_e rc = MY_RC_E_SUCCESS;
00310
00311 VALIDATE_VTABLE_FN(base1_h, private_h, vtable, delete_fn, rc);
00312 if (my_rc_e_is_notok(rc)) {
00313 return;
00314 }
00315
00316 return (base1_h->private_h->vtable->delete_fn(base1_h));
00317 }
00318
00327 static my_rc_e
00328 base1_increase_val3_internal (base1_handle base1_h)
00329 {
00330 if (NULL == base1_h) {
00331 LOG_ERR("Invalid input, base1_h(%p)", base1_h);
00332 return (MY_RC_E_EINVAL);
00333 }
00334
00335 base1_h->val3 *= 2;
00336
00337 return (MY_RC_E_SUCCESS);
00338 }
00339
00346 my_rc_e
00347 base1_increase_val3 (base1_handle base1_h)
00348 {
00349 my_rc_e rc = MY_RC_E_SUCCESS;
00350
00351 VALIDATE_VTABLE_FN(base1_h, private_h, vtable, increase_val3_fn, rc);
00352 if (my_rc_e_is_notok(rc)) {
00353 return (rc);
00354 }
00355
00356 return (base1_h->private_h->vtable->increase_val3_fn(base1_h));
00357 }
00358
00364 static const base1_vtable_st base1_vtable = {
00365 base1_private_delete,
00366 base1_type_string_internal,
00367 base1_string_internal,
00368 base1_string_size_internal,
00369 base1_increase_val3_internal
00370 };
00371
00381 my_rc_e
00382 base1_inherit_vtable (const base1_vtable_st *parent_vtable,
00383 base1_vtable_st *child_vtable,
00384 bool do_null_check)
00385 {
00386 my_rc_e rc = MY_RC_E_SUCCESS;
00387
00388
00389 CT_ASSERT(5 == (sizeof(base1_vtable_st)/sizeof(void*)));
00390
00391 if ((NULL == parent_vtable) || (NULL == child_vtable)) {
00392 LOG_ERR("Invalid input, parent_vtable(%p) "
00393 "child_vtable(%p)", parent_vtable, child_vtable);
00394 return (MY_RC_E_EINVAL);
00395 }
00396
00397 INHERIT_VTABLE_FN(parent_vtable, child_vtable, delete_fn, do_null_check,
00398 rc);
00399 INHERIT_VTABLE_FN(parent_vtable, child_vtable, type_string_fn,
00400 do_null_check, rc);
00401 INHERIT_VTABLE_FN(parent_vtable, child_vtable, string_fn, do_null_check,
00402 rc);
00403 INHERIT_VTABLE_FN(parent_vtable, child_vtable, string_size_fn,
00404 do_null_check, rc);
00405 INHERIT_VTABLE_FN(parent_vtable, child_vtable, increase_val3_fn,
00406 do_null_check, rc);
00407
00408 return (MY_RC_E_SUCCESS);
00409
00410 err_exit:
00411
00412 return (rc);
00413 }
00414
00424 my_rc_e
00425 base1_set_vtable (base1_handle base1_h, base1_vtable_st *vtable)
00426 {
00427 my_rc_e rc;
00428
00429 if ((NULL == base1_h) || (NULL == vtable)) {
00430 LOG_ERR("Invalid input, base1_h(%p) vtable(%p)", base1_h, vtable);
00431 return (MY_RC_E_EINVAL);
00432 }
00433
00434 if (NULL == base1_h->private_h) {
00435 LOG_ERR("Invalid input, base1_h(%p)->private_h(%p)", base1_h,
00436 base1_h->private_h);
00437 return (MY_RC_E_EINVAL);
00438 }
00439
00440 rc = base1_inherit_vtable(&base1_vtable, vtable, true);
00441 if (my_rc_e_is_notok(rc)) {
00442 return (rc);
00443 }
00444
00445 base1_h->private_h->vtable = vtable;
00446
00447 return (MY_RC_E_SUCCESS);
00448 }
00449
00460 my_rc_e
00461 base1_init (base1_handle base1_h)
00462 {
00463 my_rc_e rc = MY_RC_E_SUCCESS;
00464
00465 if (NULL == base1_h) {
00466 LOG_ERR("Invalid input, base1_h(%p)", base1_h);
00467 return (MY_RC_E_EINVAL);
00468 }
00469
00470 base1_h->private_h = calloc(1, sizeof(*base1_h->private_h));
00471 if (NULL == base1_h->private_h) {
00472 rc = MY_RC_E_ENOMEM;
00473 goto err_exit;
00474 }
00475
00476 base1_h->private_h->vtable = &base1_vtable;
00477 base1_h->public_data.val1 = 1;
00478 base1_h->public_data.val2 = 2;
00479 base1_h->val3 = 42;
00480
00481 return (MY_RC_E_SUCCESS);
00482
00483 err_exit:
00484
00485 if (NULL != base1_h->private_h) {
00486 free(base1_h->private_h);
00487 base1_h->private_h = NULL;
00488 }
00489
00490 return (rc);
00491 }
00492
00498 base1_handle
00499 base1_new1 (void)
00500 {
00501 base1_st *base1 = NULL;
00502 my_rc_e rc;
00503
00504 base1 = calloc(1, sizeof(*base1));
00505 if (NULL != base1) {
00506 rc = base1_init(base1);
00507 if (my_rc_e_is_notok(rc)) {
00508 LOG_ERR("Init failed, rc(%s)", my_rc_e_get_string(rc));
00509 goto err_exit;
00510 }
00511 }
00512
00513 return (base1);
00514
00515 err_exit:
00516
00517 base1_private_delete(base1);
00518
00519 return (NULL);
00520 }
00521
00528 base1_handle
00529 base1_new2 (base1_public_data_st *public_data)
00530 {
00531 base1_st *base1 = NULL;
00532
00533 if (NULL == public_data) {
00534 LOG_ERR("Invalid input, public_data(%p)", public_data);
00535 return (NULL);
00536 }
00537
00538 base1 = base1_new1();
00539 if (NULL != base1) {
00540 memcpy(&(base1->public_data), public_data, sizeof(base1->public_data));
00541 }
00542
00543 return (base1);
00544 }
00545
00553 base1_handle
00554 base1_new3 (uint8_t val1, uint32_t val3)
00555 {
00556 base1_st *base1 = NULL;
00557
00558 base1 = base1_new1();
00559 if (NULL != base1) {
00560 base1->public_data.val1 = val1;
00561 base1->val3 = val3;
00562 }
00563
00564 return (base1);
00565 }