Skip to content

Commit

Permalink
BRAYNS-663 Refactor json (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien4193 authored Sep 12, 2024
1 parent 91b17c9 commit cd93484
Show file tree
Hide file tree
Showing 32 changed files with 698 additions and 438 deletions.
4 changes: 2 additions & 2 deletions src/brayns/core/engine/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ struct PerspectiveCameraSettings
{
float fovy = radians(60.0F);
float aspect = 1.0F;
std::optional<DepthOfField> depthOfField = std::nullopt;
std::optional<DepthOfField> depthOfField = {};
bool architectural = false;
std::optional<Stereo> stereo = std::nullopt;
std::optional<Stereo> stereo = {};
};

class PerspectiveCamera : public Camera
Expand Down
4 changes: 2 additions & 2 deletions src/brayns/core/engine/Framebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ struct FramebufferSettings
Size2 resolution;
FramebufferFormat format = FramebufferFormat::Srgba8;
std::set<FramebufferChannel> channels = {FramebufferChannel::Color};
std::optional<Accumulation> accumulation = std::nullopt;
std::optional<Data<ImageOperation>> operations = std::nullopt;
std::optional<Accumulation> accumulation = {};
std::optional<Data<ImageOperation>> operations = {};
};

class FramebufferData
Expand Down
2 changes: 1 addition & 1 deletion src/brayns/core/engine/GeometricModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct Materials
{
std::variant<IndexInRenderer, Data<IndexInRenderer>> values;
std::variant<std::monostate, Color4, Data<Color4>> colors = {};
std::optional<Data<std::uint8_t>> indices = std::nullopt;
std::optional<Data<std::uint8_t>> indices = {};
};

struct GeometricModelSettings
Expand Down
26 changes: 13 additions & 13 deletions src/brayns/core/engine/Geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class Geometry : public Managed<OSPGeometry>
struct MeshSettings
{
Data<Vector3> positions;
std::optional<Data<Vector3>> normals = std::nullopt;
std::optional<Data<Color4>> colors = std::nullopt;
std::optional<Data<Vector2>> uvs = std::nullopt;
std::optional<Data<Vector3>> normals = {};
std::optional<Data<Color4>> colors = {};
std::optional<Data<Vector2>> uvs = {};
};

class Mesh : public Geometry
Expand All @@ -56,7 +56,7 @@ class Mesh : public Geometry
struct TriangleMeshSettings
{
MeshSettings base;
std::optional<Data<Index3>> indices = std::nullopt;
std::optional<Data<Index3>> indices = {};
};

class TriangleMesh : public Mesh
Expand All @@ -70,7 +70,7 @@ TriangleMesh createTriangleMesh(Device &device, const TriangleMeshSettings &sett
struct QuadMeshSettings
{
MeshSettings base;
std::optional<Data<Index4>> indices = std::nullopt;
std::optional<Data<Index4>> indices = {};
};

class QuadMesh : public Mesh
Expand All @@ -84,7 +84,7 @@ QuadMesh createQuadMesh(Device &device, const QuadMeshSettings &settings);
struct SphereSettings
{
Data<Vector4> spheres;
std::optional<Data<Vector2>> uvs = std::nullopt;
std::optional<Data<Vector2>> uvs = {};
};

class Spheres : public Geometry
Expand All @@ -98,8 +98,8 @@ Spheres createSpheres(Device &device, const SphereSettings &settings);
struct DiscSettings
{
Data<Vector4> spheres;
std::optional<Data<Vector3>> normals = std::nullopt;
std::optional<Data<Vector2>> uvs = std::nullopt;
std::optional<Data<Vector3>> normals = {};
std::optional<Data<Vector2>> uvs = {};
};

class Discs : public Geometry
Expand All @@ -114,8 +114,8 @@ struct CylinderSettings
{
Data<Vector4> spheres;
Data<std::uint32_t> indices;
std::optional<Data<Color4>> colors = std::nullopt;
std::optional<Data<Vector2>> uvs = std::nullopt;
std::optional<Data<Color4>> colors = {};
std::optional<Data<Vector2>> uvs = {};
};

class Cylinders : public Geometry
Expand Down Expand Up @@ -170,8 +170,8 @@ struct CurveSettings
{
Data<Vector4> spheres;
Data<std::uint32_t> indices;
std::optional<Data<Color4>> colors = std::nullopt;
std::optional<Data<Vector2>> uvs = std::nullopt;
std::optional<Data<Color4>> colors = {};
std::optional<Data<Vector2>> uvs = {};
CurveType type = RoundCurve();
CurveBasis basis = LinearCurve();
};
Expand Down Expand Up @@ -200,7 +200,7 @@ Boxes createBoxes(Device &device, const BoxSettings &settings);
struct PlaneSettings
{
Data<Vector4> coefficients;
std::optional<Data<Box3>> bounds = std::nullopt;
std::optional<Data<Box3>> bounds = {};
};

class Planes : public Geometry
Expand Down
2 changes: 1 addition & 1 deletion src/brayns/core/engine/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct RendererSettings
float minContribution = 0.001F;
float varianceThreshold = 0.0F;
Background background = Color4(0.0F, 0.0F, 0.0F, 0.0F);
std::optional<Texture2D> maxDepth = std::nullopt;
std::optional<Texture2D> maxDepth = {};
PixelFilter pixelFilter = PixelFilter::Gauss;
};

Expand Down
2 changes: 1 addition & 1 deletion src/brayns/core/engine/Volume.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct RegularVolumeSettings
Vector3 spacing = {1.0F, 1.0F, 1.0F};
VolumeType type = VolumeType::VertexCentered;
VolumeFilter filter = VolumeFilter::Linear;
std::optional<float> background = std::nullopt;
std::optional<float> background = {};
};

class RegularVolume : public Volume
Expand Down
10 changes: 5 additions & 5 deletions src/brayns/core/engine/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ namespace brayns
{
struct GroupSettings
{
std::optional<Data<GeometricModel>> geometries = std::nullopt;
std::optional<Data<GeometricModel>> clippingGeometries = std::nullopt;
std::optional<Data<VolumetricModel>> volumes = std::nullopt;
std::optional<Data<Light>> lights = std::nullopt;
std::optional<Data<GeometricModel>> geometries = {};
std::optional<Data<GeometricModel>> clippingGeometries = {};
std::optional<Data<VolumetricModel>> volumes = {};
std::optional<Data<Light>> lights = {};
};

class Group : public Managed<OSPGroup>
Expand Down Expand Up @@ -67,7 +67,7 @@ Instance createInstance(Device &device, const InstanceSettings &settings);

struct WorldSettings
{
std::optional<Data<Instance>> instances = std::nullopt;
std::optional<Data<Instance>> instances = {};
};

class World : public Managed<OSPWorld>
Expand Down
4 changes: 3 additions & 1 deletion src/brayns/core/json/Json.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
#include "JsonValue.h"

#include "types/Arrays.h"
#include "types/Constants.h"
#include "types/Buffer.h"
#include "types/Consts.h"
#include "types/Enums.h"
#include "types/Maps.h"
#include "types/Math.h"
#include "types/Objects.h"
#include "types/Primitives.h"
#include "types/Schema.h"
#include "types/Sets.h"
#include "types/Update.h"
#include "types/Variants.h"
#include "types/Vectors.h"
54 changes: 33 additions & 21 deletions src/brayns/core/json/JsonReflector.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,57 +22,69 @@
#pragma once

#include <concepts>
#include <utility>

#include "JsonSchema.h"
#include "JsonValue.h"

namespace brayns
{
template<typename T>
struct JsonReflector;

template<typename T>
concept HasJsonSchema = std::same_as<JsonSchema, decltype(JsonReflector<T>::getSchema())>;

template<typename T>
concept JsonSerializable = std::same_as<JsonValue, decltype(JsonReflector<T>::serialize(std::declval<T>()))>;
struct JsonReflector;

template<typename T>
concept JsonDeserializable = std::same_as<T, decltype(JsonReflector<T>::deserialize(std::declval<JsonValue>()))>;
concept ReflectedJson = requires(T value, JsonValue json) {
{ JsonReflector<T>::getSchema() } -> std::same_as<JsonSchema>;
{ JsonReflector<T>::serialize(std::as_const(value), json) };
{ JsonReflector<T>::deserialize(std::as_const(json), value) };
};

template<typename T>
concept ReflectedJson = HasJsonSchema<T> && JsonSerializable<T> && JsonDeserializable<T>;
template<ReflectedJson T>
JsonSchema getJsonSchema()
{
return JsonReflector<T>::getSchema();
}

template<HasJsonSchema T>
const JsonSchema &getJsonSchema()
template<ReflectedJson T>
void serializeToJson(const T &value, JsonValue &json)
{
static const auto schema = JsonReflector<T>::getSchema();
return schema;
return JsonReflector<T>::serialize(value, json);
}

template<JsonSerializable T>
template<ReflectedJson T>
JsonValue serializeToJson(const T &value)
{
return JsonReflector<T>::serialize(value);
auto json = JsonValue();
serializeToJson(value, json);
return json;
}

template<ReflectedJson T>
void deserializeJson(const JsonValue &json, T &value)
{
return JsonReflector<T>::deserialize(json, value);
}

template<JsonDeserializable T>
T deserializeAs(const JsonValue &json)
template<ReflectedJson T>
T deserializeJsonAs(const JsonValue &json)
{
return JsonReflector<T>::deserialize(json);
auto value = T{};
deserializeJson(json, value);
return value;
}

template<JsonSerializable T>
template<ReflectedJson T>
std::string stringifyToJson(const T &value)
{
auto json = serializeToJson(value);
return stringify(json);
}

template<JsonDeserializable T>
template<ReflectedJson T>
T parseJsonAs(const std::string &data)
{
auto json = parseJson(data);
return deserializeAs<T>(json);
return deserializeJsonAs<T>(json);
}
}
2 changes: 1 addition & 1 deletion src/brayns/core/json/JsonSchema.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ struct JsonSchema
{
std::string description = {};
bool required = true;
JsonValue defaultValue = {};
std::optional<JsonValue> defaultValue = {};
std::vector<JsonSchema> oneOf = {};
JsonType type = JsonType::Undefined;
JsonValue constant = {};
Expand Down
13 changes: 5 additions & 8 deletions src/brayns/core/json/types/Arrays.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct JsonArrayReflector
};
}

static JsonValue serialize(const T &value)
static void serialize(const T &value, JsonValue &json)
{
auto array = createJsonArray();

Expand All @@ -52,22 +52,19 @@ struct JsonArrayReflector
array->add(jsonItem);
}

return array;
json = array;
}

static T deserialize(const JsonValue &json)
static void deserialize(const JsonValue &json, T &value)
{
const auto &array = getArray(json);

auto value = T();
value.clear();

for (const auto &jsonItem : array)
{
auto item = deserializeAs<ValueType>(jsonItem);
auto item = deserializeJsonAs<ValueType>(jsonItem);
value.push_back(std::move(item));
}

return value;
}
};

Expand Down
69 changes: 69 additions & 0 deletions src/brayns/core/json/types/Buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
*
* Responsible Author: adrien.fleury@epfl.ch
*
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include <brayns/core/json/JsonReflector.h>

namespace brayns
{
template<ReflectedJson T>
class JsonBuffer
{
public:
const JsonValue &get() const
{
return _json;
}

void store(const JsonValue &json)
{
_json = json;
}

void extract(T &value) const
{
deserializeJson(_json, value);
}

private:
JsonValue _json;
};

template<ReflectedJson T>
struct JsonReflector<JsonBuffer<T>>
{
static JsonSchema getSchema()
{
return getJsonSchema<T>();
}

static void serialize(const JsonBuffer<T> &value, JsonValue &json)
{
json = value.get();
}

static void deserialize(const JsonValue &json, JsonBuffer<T> &value)
{
value.store(json);
}
};
}
Loading

0 comments on commit cd93484

Please sign in to comment.