Skip to content

Commit

Permalink
object: new interface to wrap data types
Browse files Browse the repository at this point in the history
The object interface is a tiny wrapper over cfl_array, cfl_kvlist and
cfl_variant. The main purpose is to allow to link objects into a cfl_list
and also abstract the api usage a bit.

Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Feb 23, 2024
1 parent 62ef009 commit 1c11f40
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/cfl/cfl.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <cfl/cfl_kvlist.h>
#include <cfl/cfl_time.h>
#include <cfl/cfl_variant.h>
#include <cfl/cfl_object.h>

int cfl_init();

Expand Down
41 changes: 41 additions & 0 deletions include/cfl/cfl_object.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* CFL
* ===
* Copyright (C) 2022 The CFL Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef CFL_OBJECT_H
#define CFL_OBJECT_H

enum {
CFL_OBJECT_NONE = 0,
CFL_OBJECT_KVLIST = 1,
CFL_OBJECT_VARIANT,
CFL_OBJECT_ARRAY
};

struct cfl_object {
int type;
struct cfl_variant *variant;
struct cfl_list _head;
};

struct cfl_object *cfl_object_create();
void cfl_object_destroy(struct cfl_object *obj);
int cfl_object_set(struct cfl_object *o, int type, void *ptr);
int cfl_object_print(FILE *stream, struct cfl_object *o);

#endif
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(src
cfl_time.c
cfl_kv.c
cfl_kvlist.c
cfl_object.c
cfl_array.c
cfl_variant.c
cfl_checksum.c
Expand Down
106 changes: 106 additions & 0 deletions src/cfl_object.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* CFL
* ===
* Copyright (C) 2022-2024 The CFL Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "cfl/cfl.h"

/* CFL Object
* ==========
* the CFL Object interface is a generic object that can hold a cfl_kvlist, cfl_array
* or a cfl_variant. It's used as a wrapper to link different objects and/or provide
* a common interface to the user.
*/


/* Create CFL Object context */
struct cfl_object *cfl_object_create()
{
struct cfl_object *o;

o = calloc(1, sizeof(struct cfl_object));
if (!o) {
cfl_errno();
return NULL;
}

/* mark it as not initialized */
o->type = CFL_OBJECT_NONE;
return o;
}

/*
* Associate a CFL data type to the object. We only support kvlist, array and variant. Note
* that everything is held as a variant internally.
*/
int cfl_object_set(struct cfl_object *o, int type, void *ptr)
{
if (!o) {
return -1;
}

if (type == CFL_OBJECT_KVLIST) {
o->type = CFL_OBJECT_KVLIST;
o->variant = cfl_variant_create_from_kvlist(ptr);
}
else if (type == CFL_OBJECT_VARIANT) {
o->type = CFL_OBJECT_VARIANT;
o->variant = ptr;
}
else if (type == CFL_OBJECT_ARRAY) {
o->type = CFL_OBJECT_ARRAY;
o->variant = cfl_variant_create_from_array(ptr);
}
else {
return -1;
}

return 0;
}

int cfl_object_print(FILE *stream, struct cfl_object *o)
{
if (!o) {
return -1;
}

if (!o->variant) {
return -1;
}

cfl_variant_print(stream, o->variant);
printf("\n");

return 0;
}

/*
* Destroy the object, note that if a CFL data type is linked
* it will be destroyed as well
*/
void cfl_object_destroy(struct cfl_object *o)
{
if (!o) {
return;
}

if (o->variant) {
cfl_variant_destroy(o->variant);
}

free(o);
}
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(UNIT_TESTS_FILES
list.c
variant.c
kvlist.c
object.c
)

configure_file(
Expand Down

0 comments on commit 1c11f40

Please sign in to comment.