Skip to content

Commit

Permalink
adding serialization bindings to the CNZSL API (#43)
Browse files Browse the repository at this point in the history
* adding serialization bindings to the CNZSL API

---------

Co-authored-by: Jérôme Leclercq <[email protected]>
  • Loading branch information
Kbz-8 and SirLynix authored Sep 14, 2024
1 parent 46aad44 commit 509e0fc
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
39 changes: 39 additions & 0 deletions include/CNZSL/Serializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright (C) 2024 kbz_8 ( [email protected] )
This file is part of the "Nazara Shading Language - C Binding" project
For conditions of distribution and use, see copyright notice in Config.hpp
*/

#pragma once

#ifndef CNZSL_SERIALIZER_H
#define CNZSL_SERIALIZER_H

#include <CNZSL/Config.h>
#include <CNZSL/Module.h>

#include <stddef.h>

#ifdef __cplusplus
extern "C"
{
#endif

typedef struct nzslSerializer nzslSerializer;
typedef struct nzslDeserializer nzslDeserializer;

CNZSL_API nzslSerializer* nzslSerializerCreate(void);
CNZSL_API void nzslSerializerDestroy(nzslSerializer* serializerPtr);
CNZSL_API void nzslSerializeShader(nzslSerializer* serializerPtr, const nzslModule* modulePtr);
CNZSL_API const char* nzslSerializerGetLastError(const nzslSerializer* serializerPtr);

CNZSL_API nzslDeserializer* nzslDeserializerCreate(const void* data, size_t dataSize);
CNZSL_API void nzslDeserializerDestroy(nzslDeserializer* deserializerPtr);
CNZSL_API nzslModule* nzslDeserializeShader(nzslDeserializer* deserializerPtr);
CNZSL_API const char* nzslDeserializerGetLastError(const nzslDeserializer* deserializerPtr);

#ifdef __cplusplus
}
#endif

#endif /* CNZSL_SERIALIZER_H */
89 changes: 89 additions & 0 deletions src/CNZSL/Serializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (C) 2024 kbz_8 ( [email protected] )
// This file is part of the "Nazara Shading Language - C Binding" project
// For conditions of distribution and use, see copyright notice in Config.hpp

#include <CNZSL/Serializer.h>
#include <CNZSL/Module.h>
#include <CNZSL/Structs/Serializer.hpp>
#include <CNZSL/Structs/Module.hpp>
#include <NZSL/Ast/AstSerializer.hpp>
#include <fmt/format.h>
#include <cassert>

extern "C"
{
CNZSL_API nzslSerializer* nzslSerializerCreate(void)
{
return new nzslSerializer;
}

CNZSL_API void nzslSerializerDestroy(nzslSerializer* serializerPtr)
{
delete serializerPtr;
}

CNZSL_API void nzslSerializeShader(nzslSerializer* serializerPtr, const nzslModule* modulePtr)
{
assert(serializerPtr);
assert(modulePtr);

try
{
nzsl::Ast::SerializeShader(serializerPtr->serializer, *modulePtr->module);
}
catch(std::exception& e)
{
serializerPtr->lastError = fmt::format("nzslSerializeShader failed: {}", e.what());
}
catch(...)
{
serializerPtr->lastError = "nzslSerializeShader failed with unknown error";
}
}

CNZSL_API const char* nzslSerializerGetLastError(const nzslSerializer* serializerPtr)
{
assert(serializerPtr);
return serializerPtr->lastError.c_str();
}

CNZSL_API nzslDeserializer* nzslDeserializerCreate(const void* data, size_t dataSize)
{
return new nzslDeserializer(data, dataSize);
}

CNZSL_API void nzslDeserializerDestroy(nzslDeserializer* deserializerPtr)
{
delete deserializerPtr;
}

CNZSL_API nzslModule* nzslDeserializeShader(nzslDeserializer* deserializerPtr)
{
assert(deserializerPtr);

try
{
nzsl::Ast::ModulePtr module = nzsl::Ast::DeserializeShader(deserializerPtr->deserializer);

nzslModule* modulePtr = nzslModuleCreate();
modulePtr->module = std::move(module);
return modulePtr;
}
catch(std::exception& e)
{
deserializerPtr->lastError = fmt::format("nzslDeserializeShader failed: {}", e.what());
return nullptr;
}
catch(...)
{
deserializerPtr->lastError = "nzslDeserializeShader failed with unknown error";
return nullptr;
}
}

CNZSL_API const char* nzslDeserializerGetLastError(const nzslDeserializer* deserializerPtr)
{
assert(deserializerPtr);
return deserializerPtr->lastError.c_str();
}
}
29 changes: 29 additions & 0 deletions src/CNZSL/Structs/Serializer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2024 kbz_8 ( [email protected] )
// This file is part of the "Nazara Shading Language - C Binding" project
// For conditions of distribution and use, see copyright notice in Config.hpp

#pragma once

#ifndef CNZSL_STRUCTS_SERIALIZER_HPP
#define CNZSL_STRUCTS_SERIALIZER_HPP

#include <NZSL/Serializer.hpp>

#include <string>
#include <cstddef>

struct nzslSerializer
{
nzsl::Serializer serializer;
std::string lastError;
};

struct nzslDeserializer
{
nzsl::Deserializer deserializer;
std::string lastError;

nzslDeserializer(const void* data, size_t dataSize) : deserializer(data, dataSize) {}
};

#endif // CNZSL_STRUCTS_SERIALIZER_HPP

0 comments on commit 509e0fc

Please sign in to comment.