Skip to content

Commit

Permalink
Make visitor accept functions return results
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Jul 29, 2024
1 parent 5d34689 commit dd7871d
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 49 deletions.
10 changes: 7 additions & 3 deletions source/base/inc/tactile/base/document/component_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include "tactile/base/container/expected.hpp"
#include "tactile/base/container/pair.hpp"
#include "tactile/base/container/string.hpp"
#include "tactile/base/int.hpp"
Expand All @@ -24,8 +25,12 @@ class IComponentView
* Inspects the component.
*
* \param visitor The visitor to use.
*
* \return
* Nothing if successful; an error code otherwise.
*/
virtual void accept(IDocumentVisitor& visitor) const = 0;
[[nodiscard]]
virtual auto accept(IDocumentVisitor& visitor) const -> Result<void> = 0;

/**
* Returns the name of the component.
Expand All @@ -45,8 +50,7 @@ class IComponentView
* An attribute key and value pair.
*/
[[nodiscard]]
virtual auto get_attribute(usize index) const
-> Pair<const String&, const Attribute&> = 0;
virtual auto get_attribute(usize index) const -> Pair<const String&, const Attribute&> = 0;

/**
* Returns the number of attributes in the component.
Expand Down
7 changes: 6 additions & 1 deletion source/base/inc/tactile/base/document/document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include "tactile/base/container/expected.hpp"
#include "tactile/base/container/path.hpp"
#include "tactile/base/numeric/vec.hpp"
#include "tactile/base/prelude.hpp"
Expand All @@ -24,8 +25,12 @@ class IDocument
* Inspects the document.
*
* \param visitor The visitor to use.
*
* \return
* Nothing if successful; an error code otherwise.
*/
virtual void accept(IDocumentVisitor& visitor) const = 0;
[[nodiscard]]
virtual auto accept(IDocumentVisitor& visitor) const -> Result<void> = 0;

/**
* Updates the state of the document.
Expand Down
7 changes: 6 additions & 1 deletion source/base/inc/tactile/base/document/layer_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include "tactile/base/container/expected.hpp"
#include "tactile/base/container/maybe.hpp"
#include "tactile/base/container/string.hpp"
#include "tactile/base/id.hpp"
Expand Down Expand Up @@ -30,8 +31,12 @@ class ILayerView
* Inspects the layer.
*
* \param visitor The visitor to use.
*
* \return
* Nothing if successful; an error code otherwise.
*/
virtual void accept(IDocumentVisitor& visitor) const = 0;
[[nodiscard]]
virtual auto accept(IDocumentVisitor& visitor) const -> Result<void> = 0;

virtual void write_tile_bytes(ByteStream& byte_stream) const = 0;

Expand Down
6 changes: 5 additions & 1 deletion source/base/inc/tactile/base/document/map_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include "tactile/base/container/expected.hpp"
#include "tactile/base/container/maybe.hpp"
#include "tactile/base/id.hpp"
#include "tactile/base/int.hpp"
Expand All @@ -28,8 +29,11 @@ class IMapView
* Inspects the map.
*
* \param visitor The visitor to use.
*
* \return
* Nothing if successful; an error code otherwise.
*/
virtual void accept(IDocumentVisitor& visitor) const = 0;
virtual auto accept(IDocumentVisitor& visitor) const -> Result<void> = 0;

/**
* Returns the logical size of tiles in the map.
Expand Down
7 changes: 6 additions & 1 deletion source/base/inc/tactile/base/document/object_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include "tactile/base/container/expected.hpp"
#include "tactile/base/container/string.hpp"
#include "tactile/base/id.hpp"
#include "tactile/base/layer/object_type.hpp"
Expand All @@ -27,8 +28,12 @@ class IObjectView
* Inspects the object.
*
* \param visitor The visitor to use.
*
* \return
* Nothing if successful; an error code otherwise.
*/
virtual void accept(IDocumentVisitor& visitor) const = 0;
[[nodiscard]]
virtual auto accept(IDocumentVisitor& visitor) const -> Result<void> = 0;

/**
* Returns a view of the parent object layer, if any.
Expand Down
10 changes: 7 additions & 3 deletions source/base/inc/tactile/base/document/tile_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include "tactile/base/container/expected.hpp"
#include "tactile/base/container/pair.hpp"
#include "tactile/base/id.hpp"
#include "tactile/base/prelude.hpp"
Expand All @@ -25,8 +26,12 @@ class ITileView
* Inspects the tile.
*
* \param visitor The visitor to use.
*
* \return
* Nothing if successful; an error code otherwise.
*/
virtual void accept(IDocumentVisitor& visitor) const = 0;
[[nodiscard]]
virtual auto accept(IDocumentVisitor& visitor) const -> Result<void> = 0;

/**
* Returns the tileset that hosts the tile.
Expand Down Expand Up @@ -73,8 +78,7 @@ class ITileView
* A tile index and frame duration pair.
*/
[[nodiscard]]
virtual auto get_animation_frame(usize index) const
-> Pair<TileIndex, Milliseconds> = 0;
virtual auto get_animation_frame(usize index) const -> Pair<TileIndex, Milliseconds> = 0;

/**
* Returns a view of the associated metadata.
Expand Down
7 changes: 6 additions & 1 deletion source/base/inc/tactile/base/document/tileset_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include "tactile/base/container/expected.hpp"
#include "tactile/base/container/path.hpp"
#include "tactile/base/container/string.hpp"
#include "tactile/base/id.hpp"
Expand All @@ -26,8 +27,12 @@ class ITilesetView
* Inspects the tileset.
*
* \param visitor The visitor to use.
*
* \return
* Nothing if successful; an error code otherwise.
*/
virtual void accept(IDocumentVisitor& visitor) const = 0;
[[nodiscard]]
virtual auto accept(IDocumentVisitor& visitor) const -> Result<void> = 0;

/**
* Returns the first global tile identifier associated with the tileset.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ class ObjectViewMock : public IObjectView
{
ON_CALL(*this, accept).WillByDefault([this](IDocumentVisitor& visitor) {
visitor.visit(*this).value();
return kOK;
});

ON_CALL(*this, get_meta).WillByDefault(testing::ReturnRef(mMeta));
}

MOCK_METHOD(void, accept, (IDocumentVisitor&), (const, override));
MOCK_METHOD(Result<void>, accept, (IDocumentVisitor&), (const, override));

MOCK_METHOD(const ILayerView*, get_parent_layer, (), (const, override));

Expand Down Expand Up @@ -100,7 +101,7 @@ class TileViewMock : public ITileView
ON_CALL(*this, get_meta).WillByDefault(testing::ReturnRef(mMeta));
}

MOCK_METHOD(void, accept, (IDocumentVisitor&), (const, override));
MOCK_METHOD(Result<void>, accept, (IDocumentVisitor&), (const, override));

MOCK_METHOD(const ITilesetView&, get_parent_tileset, (), (const, override));

Expand Down Expand Up @@ -135,7 +136,7 @@ class TilesetViewMock : public ITilesetView
ON_CALL(*this, get_meta).WillByDefault(testing::ReturnRef(mMeta));
}

MOCK_METHOD(void, accept, (IDocumentVisitor&), (const, override));
MOCK_METHOD(Result<void>, accept, (IDocumentVisitor&), (const, override));

MOCK_METHOD(TileID, get_first_tile_id, (), (const, override));

Expand Down Expand Up @@ -173,7 +174,7 @@ class LayerViewMock : public ILayerView
ON_CALL(*this, get_meta).WillByDefault(testing::ReturnRef(mMeta));
}

MOCK_METHOD(void, accept, (IDocumentVisitor&), (const, override));
MOCK_METHOD(Result<void>, accept, (IDocumentVisitor&), (const, override));

MOCK_METHOD(void, write_tile_bytes, (ByteStream&), (const, override));

Expand Down Expand Up @@ -242,7 +243,7 @@ class MapViewMock : public IMapView
ON_CALL(*this, get_meta).WillByDefault(testing::ReturnRef(mMeta));
}

MOCK_METHOD(void, accept, (IDocumentVisitor&), (const, override));
MOCK_METHOD(Result<void>, accept, (IDocumentVisitor&), (const, override));

MOCK_METHOD(Int2, get_tile_size, (), (const, override));

Expand Down
3 changes: 2 additions & 1 deletion source/core/inc/tactile/core/document/layer_view_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class LayerViewImpl final : public ILayerView
const ILayerView* parent_layer,
EntityID layer_id);

void accept(IDocumentVisitor& visitor) const override;
[[nodiscard]]
auto accept(IDocumentVisitor& visitor) const -> Result<void> override;

void write_tile_bytes(ByteStream& byte_stream) const override;

Expand Down
3 changes: 2 additions & 1 deletion source/core/inc/tactile/core/document/map_document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class MapDocument final : public IDocument

~MapDocument() noexcept override;

void accept(IDocumentVisitor& visitor) const override;
[[nodiscard]]
auto accept(IDocumentVisitor& visitor) const -> Result<void> override;

void update() override;

Expand Down
3 changes: 2 additions & 1 deletion source/core/inc/tactile/core/document/map_view_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class MapViewImpl final : public IMapView
*/
MapViewImpl(const MapDocument* document);

void accept(IDocumentVisitor& visitor) const override;
[[nodiscard]]
auto accept(IDocumentVisitor& visitor) const -> Result<void> override;

[[nodiscard]]
auto get_tile_size() const -> Int2 override;
Expand Down
3 changes: 2 additions & 1 deletion source/core/inc/tactile/core/document/object_view_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class ObjectViewImpl final : public IObjectView
const ITileView* parent_tile,
EntityID object_id);

void accept(IDocumentVisitor& visitor) const override;
[[nodiscard]]
auto accept(IDocumentVisitor& visitor) const -> Result<void> override;

[[nodiscard]]
auto get_parent_layer() const -> const ILayerView* override;
Expand Down
3 changes: 2 additions & 1 deletion source/core/inc/tactile/core/document/tile_view_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class TileViewImpl final : public ITileView
const ITilesetView* tileset_view,
EntityID tile_id);

void accept(IDocumentVisitor& visitor) const override;
[[nodiscard]]
auto accept(IDocumentVisitor& visitor) const -> Result<void> override;

[[nodiscard]]
auto get_parent_tileset() const -> const ITilesetView& override;
Expand Down
3 changes: 2 additions & 1 deletion source/core/inc/tactile/core/document/tileset_view_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class TilesetViewImpl final : public ITilesetView
*/
TilesetViewImpl(const MapDocument* document, EntityID tileset_id);

void accept(IDocumentVisitor& visitor) const override;
[[nodiscard]]
auto accept(IDocumentVisitor& visitor) const -> Result<void> override;

[[nodiscard]]
auto get_first_tile_id() const -> TileID override;
Expand Down
17 changes: 13 additions & 4 deletions source/core/src/tactile/core/document/layer_view_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,37 @@ LayerViewImpl::LayerViewImpl(const MapDocument* document,
mMeta {document, mLayerId}
{}

void LayerViewImpl::accept(IDocumentVisitor& visitor) const
auto LayerViewImpl::accept(IDocumentVisitor& visitor) const -> Result<void>
{
const auto& registry = mDocument->get_registry();

visitor.visit(*this);
if (const auto layer_result = visitor.visit(*this); !layer_result.has_value()) {
return propagate_unexpected(layer_result);
}

if (is_group_layer(registry, mLayerId)) {
const auto& group_layer = registry.get<CGroupLayer>(mLayerId);

for (const auto sublayer_id : group_layer.layers) {
const LayerViewImpl sublayer_view {mDocument, this, sublayer_id};
sublayer_view.accept(visitor);
if (const auto sublayer_result = sublayer_view.accept(visitor);
!sublayer_result.has_value()) {
return propagate_unexpected(sublayer_result);
}
}
}
else if (is_object_layer(registry, mLayerId)) {
const auto& object_layer = registry.get<CObjectLayer>(mLayerId);

for (const auto object_id : object_layer.objects) {
const ObjectViewImpl object_view {mDocument, this, object_id};
object_view.accept(visitor);
if (const auto object_result = object_view.accept(visitor); !object_result.has_value()) {
return propagate_unexpected(object_result);
}
}
}

return kOK;
}

void LayerViewImpl::write_tile_bytes(ByteStream& byte_stream) const
Expand Down
4 changes: 2 additions & 2 deletions source/core/src/tactile/core/document/map_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ TACTILE_DEFINE_MOVE(MapDocument);

MapDocument::~MapDocument() noexcept = default;

void MapDocument::accept(IDocumentVisitor& visitor) const
auto MapDocument::accept(IDocumentVisitor& visitor) const -> Result<void>
{
const MapViewImpl map_view {this};
map_view.accept(visitor);
return map_view.accept(visitor);
}

void MapDocument::update()
Expand Down
17 changes: 13 additions & 4 deletions source/core/src/tactile/core/document/map_view_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,35 @@ MapViewImpl::MapViewImpl(const MapDocument* document)
mMeta {mDocument, mDocument->get_registry().get<CDocumentInfo>().root}
{}

void MapViewImpl::accept(IDocumentVisitor& visitor) const
auto MapViewImpl::accept(IDocumentVisitor& visitor) const -> Result<void>
{
const auto& registry = mDocument->get_registry();

const auto& map = _get_map();
const auto& root_layer = registry.get<CGroupLayer>(map.root_layer);

visitor.visit(*this);
if (const auto map_result = visitor.visit(*this); !map_result.has_value()) {
return propagate_unexpected(map_result);
}

for (const auto tileset_id : map.attached_tilesets) {
const TilesetViewImpl tileset_view {mDocument, tileset_id};
tileset_view.accept(visitor);
if (const auto tileset_result = tileset_view.accept(visitor);
!tileset_result.has_value()) {
return propagate_unexpected(tileset_result);
}
}

for (const auto layer_id : root_layer.layers) {
const LayerViewImpl layer_view {mDocument, nullptr, layer_id};
layer_view.accept(visitor);
if (const auto layer_result = layer_view.accept(visitor); !layer_result.has_value()) {
return propagate_unexpected(layer_result);
}
}

// TODO iterate component definitions

return kOK;
}

auto MapViewImpl::get_tile_size() const -> Int2
Expand Down
4 changes: 2 additions & 2 deletions source/core/src/tactile/core/document/object_view_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ ObjectViewImpl::ObjectViewImpl(const IDocument* document,
mMeta {document, object_id}
{}

void ObjectViewImpl::accept(IDocumentVisitor& visitor) const
auto ObjectViewImpl::accept(IDocumentVisitor& visitor) const -> Result<void>
{
visitor.visit(*this);
return visitor.visit(*this);
}

auto ObjectViewImpl::get_parent_layer() const -> const ILayerView*
Expand Down
Loading

0 comments on commit dd7871d

Please sign in to comment.