Skip to content

Commit

Permalink
conduit/c: use a type safe alias for conduit_{datatype,node} (#795)
Browse files Browse the repository at this point in the history
Using `typedef void` meant that the APIs were all taking `void*` which
in turn means that any pointer is valid to pass to these types. Instead,
use a unique opaque type so that callers must either do the unsafe
casting themselves or use the `cpp_to_c` APIs to do the casting the
intended way.

The actual definition of the structs themselves are empty; they are just
used it as a way to smuggle the C++ pointer around under an opaque name.

Fixes: #782
  • Loading branch information
mathstuf committed Jul 20, 2021
1 parent 2e38e20 commit 570b4af
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project aspires to adhere to [Semantic Versioning](https://semver.org/s

#### General
- Avoid compile issue with using `_Pragma()` with Python 3.8 on Windows
- `conduit_node` and `conduit_datatype` in the C API are no longer aliases to `void` so that callers cannot pass just any pointer to the APIs.

#### Blueprint
- Added the `blueprint::mesh::examples::polychain` example. It is an example of a polyhedral mesh. See Mesh Blueprint Examples docs (https://llnl-conduit.readthedocs.io/en/latest/blueprint_mesh.html#polychain) for more details.
Expand Down
27 changes: 15 additions & 12 deletions src/libs/conduit/c/conduit_cpp_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,91 +18,94 @@
namespace conduit
{

struct conduit_node_impl {};

//---------------------------------------------------------------------------//
Node *
cpp_node(conduit_node *cnode)
{
return static_cast<Node*>(cnode);
return reinterpret_cast<Node*>(cnode);
}

//---------------------------------------------------------------------------//
conduit_node *
c_node(Node *node)
{
return (void*)node;
return reinterpret_cast<conduit_node*>(node);
}


//---------------------------------------------------------------------------//
const Node *
cpp_node(const conduit_node *cnode)
{
return static_cast<const Node*>(cnode);
return reinterpret_cast<const Node*>(cnode);
}

//---------------------------------------------------------------------------//
const conduit_node *
c_node(const Node *node)
{
return (void*)node;
return reinterpret_cast<const conduit_node*>(node);
}

//---------------------------------------------------------------------------//
Node &
cpp_node_ref(conduit_node *cnode)
{
return *static_cast<Node*>(cnode);
return *reinterpret_cast<Node*>(cnode);
}

//---------------------------------------------------------------------------//
const Node &
cpp_node_ref(const conduit_node *cnode)
{
return *static_cast<const Node*>(cnode);
return *reinterpret_cast<const Node*>(cnode);
}

struct conduit_datatype_impl {};

//---------------------------------------------------------------------------//
DataType *
cpp_datatype(conduit_datatype *cdatatype)
{
return static_cast<DataType*>(cdatatype);
return reinterpret_cast<DataType*>(cdatatype);
}

//---------------------------------------------------------------------------//
conduit_datatype *
c_datatype(DataType *datatype)
{
return (void*)datatype;
return reinterpret_cast<conduit_datatype*>(datatype);
}


//---------------------------------------------------------------------------//
const DataType *
cpp_datatype(const conduit_datatype *cdatatype)
{
return static_cast<const DataType*>(cdatatype);
return reinterpret_cast<const DataType*>(cdatatype);
}

//---------------------------------------------------------------------------//
const conduit_datatype *
c_datatype(const DataType *datatype)
{
return (void*)datatype;
return reinterpret_cast<const conduit_datatype*>(datatype);
}

//---------------------------------------------------------------------------//
DataType &
cpp_datatype_ref(conduit_datatype *cdatatype)
{
return *static_cast<DataType*>(cdatatype);
return *reinterpret_cast<DataType*>(cdatatype);
}

//---------------------------------------------------------------------------//
const DataType &
cpp_datatype_ref(const conduit_datatype *cdatatype)
{
return *static_cast<const DataType*>(cdatatype);
return *reinterpret_cast<const DataType*>(cdatatype);
}

}
Expand Down
3 changes: 2 additions & 1 deletion src/libs/conduit/c/conduit_datatype.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ extern "C" {
// -- typedef for conduit_datatype --
//-----------------------------------------------------------------------------

typedef void conduit_datatype;
struct conduit_datatype_impl;
typedef struct conduit_datatype_impl conduit_datatype;

CONDUIT_API conduit_index_t conduit_datatype_id(const conduit_datatype *cdatatype);
CONDUIT_API char* conduit_datatype_name(const conduit_datatype *cdatatype);
Expand Down
3 changes: 2 additions & 1 deletion src/libs/conduit/c/conduit_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ extern "C" {
// -- typedef for conduit_node --
//-----------------------------------------------------------------------------

typedef void conduit_node;
struct conduit_node_impl;
typedef struct conduit_node_impl conduit_node;

//-----------------------------------------------------------------------------
// -- conduit_node creation and destruction --
Expand Down

0 comments on commit 570b4af

Please sign in to comment.