Skip to content

Commit 73579fe

Browse files
author
Ilija Puaca
authored
Exception-free API consolidated (#158)
1 parent 5a8462a commit 73579fe

File tree

9 files changed

+102
-50
lines changed

9 files changed

+102
-50
lines changed

cpp/examples/deck.gl/texture-render.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ auto createDeck(const char* argv[], const wgpu::Device& device, const lumagl::Si
8888
deckProps->drawingOptions = std::make_shared<DrawingOptions>(device, device.CreateQueue());
8989

9090
probegl::Error error;
91-
auto deck = Deck::make(error, deckProps);
91+
auto deck = Deck::make(deckProps, error);
9292
noErrorOrTerminate(error);
9393

9494
return deck;
@@ -111,7 +111,7 @@ int main(int argc, const char* argv[]) {
111111
lumagl::Size windowSize{640, 480};
112112
auto options = std::make_shared<GLFWAnimationLoop::Options>(windowSize, "Texture Render");
113113
probegl::Error error;
114-
auto animationLoop = AnimationLoopFactory::createAnimationLoop(error, options);
114+
auto animationLoop = AnimationLoopFactory::createAnimationLoop(options, error);
115115
auto device = animationLoop->device();
116116

117117
auto framebufferSize = windowSize * animationLoop->devicePixelRatio();

cpp/modules/deck.gl/core/src/lib/deck.cc

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,35 @@ auto Deck::Props::getProperties() const -> const std::shared_ptr<Properties> {
5757
return properties;
5858
}
5959

60-
auto Deck::make(probegl::Error& error, std::shared_ptr<Deck::Props> props) noexcept -> std::shared_ptr<Deck> {
60+
auto Deck::make(std::shared_ptr<Deck::Props> props, probegl::Error& error) noexcept -> std::shared_ptr<Deck> {
6161
return probegl::catchError<Deck>([&]() { return Deck{props}; }, error);
6262
}
6363

64+
auto Deck::make(probegl::Error& error) noexcept -> std::shared_ptr<Deck> {
65+
return probegl::catchError<Deck>([&]() { return Deck{}; }, error);
66+
}
67+
6468
void Deck::setProps(std::shared_ptr<Deck::Props> props, probegl::Error& error) noexcept {
6569
probegl::catchError([&]() { this->setProps(props); }, error);
6670
}
6771

68-
void Deck::run(probegl::Error& error, std::function<void(Deck*)> onAfterRender) noexcept {
72+
void Deck::run(std::function<void(Deck*)> onAfterRender, probegl::Error& error) noexcept {
6973
probegl::catchError([&]() { this->run(onAfterRender); }, error);
7074
}
7175

72-
void Deck::draw(wgpu::TextureView textureView, probegl::Error& error,
73-
std::function<void(Deck*)> onAfterRender) noexcept {
76+
void Deck::run(probegl::Error& error) noexcept {
77+
probegl::catchError([&]() { this->run(); }, error);
78+
}
79+
80+
void Deck::draw(wgpu::TextureView textureView, std::function<void(Deck*)> onAfterRender,
81+
probegl::Error& error) noexcept {
7482
probegl::catchError([&]() { this->draw(textureView, onAfterRender); }, error);
7583
}
7684

85+
void Deck::draw(wgpu::TextureView textureView, probegl::Error& error) noexcept {
86+
probegl::catchError([&]() { this->draw(textureView); }, error);
87+
}
88+
7789
Deck::Deck(std::shared_ptr<Deck::Props> props) : Component(props), _needsRedraw{"Initial render"} {
7890
this->animationLoop = lumagl::AnimationLoopFactory::createAnimationLoop(props->drawingOptions);
7991
this->context = std::make_shared<LayerContext>(this, this->animationLoop->device());

cpp/modules/deck.gl/core/src/lib/deck.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@ class Deck : public Component {
4141

4242
#pragma mark - Exception-free API
4343

44-
static auto make(probegl::Error& error, std::shared_ptr<Deck::Props> props = std::make_shared<Deck::Props>()) noexcept
45-
-> std::shared_ptr<Deck>;
44+
static auto make(std::shared_ptr<Deck::Props> props, probegl::Error& error) noexcept -> std::shared_ptr<Deck>;
45+
static auto make(probegl::Error& error) noexcept -> std::shared_ptr<Deck>;
4646
void setProps(std::shared_ptr<Deck::Props> props, probegl::Error& error) noexcept;
4747

48-
void run(
49-
probegl::Error& error, std::function<void(Deck*)> onAfterRender = [](Deck*) {}) noexcept;
50-
void draw(
51-
wgpu::TextureView textureView, probegl::Error& error,
52-
std::function<void(Deck*)> onAfterRender = [](Deck*) {}) noexcept;
48+
void run(std::function<void(Deck*)> onAfterRender, probegl::Error& error) noexcept;
49+
void run(probegl::Error& error) noexcept;
50+
void draw(wgpu::TextureView textureView, std::function<void(Deck*)> onAfterRender, probegl::Error& error) noexcept;
51+
void draw(wgpu::TextureView textureView, probegl::Error& error) noexcept;
5352

5453
#pragma mark -
5554

cpp/modules/luma.gl/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ set(CORE_SOURCE_FILE_LIST
7878
core/src/model.cc
7979
core/src/blit-model.cc
8080
core/src/size.cc
81+
core/src/animation-loop-factory.cc
8182
)
8283
set(CORE_TESTS_SOURCE_FILE_LIST
8384
)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2020 Unfolded Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
#include "./animation-loop-factory.h" // NOLINT(build/include)
22+
23+
using namespace lumagl;
24+
25+
auto AnimationLoopFactory::createAnimationLoop(const std::shared_ptr<AnimationLoop::Options>& options,
26+
probegl::Error& error) noexcept -> std::shared_ptr<AnimationLoop> {
27+
return probegl::catchError<AnimationLoop>([options]() { return createAnimationLoop(options); }, error);
28+
}
29+
30+
auto AnimationLoopFactory::createAnimationLoop(probegl::Error& error) noexcept -> std::shared_ptr<AnimationLoop> {
31+
return probegl::catchError<AnimationLoop>([]() { return createAnimationLoop(); }, error);
32+
}
33+
34+
auto AnimationLoopFactory::createAnimationLoop(const std::shared_ptr<AnimationLoop::Options>& options)
35+
-> std::shared_ptr<AnimationLoop> {
36+
// If no options are passed, try and initialize the only setup which requires no arguments, which is GLFW
37+
if (!options) {
38+
#if defined(LUMAGL_USES_GLFW)
39+
GLFWAnimationLoop::Options opts;
40+
return std::make_shared<GLFWAnimationLoop>(opts);
41+
#else
42+
return nullptr;
43+
#endif
44+
}
45+
46+
#if defined(LUMAGL_USES_GLFW)
47+
if (auto glfwOptions = std::dynamic_pointer_cast<GLFWAnimationLoop::Options>(options)) {
48+
return std::make_shared<GLFWAnimationLoop>(*glfwOptions.get());
49+
}
50+
#endif
51+
52+
#if defined(LUMAGL_ENABLE_BACKEND_METAL)
53+
if (auto metalOptions = std::dynamic_pointer_cast<MetalAnimationLoop::Options>(options)) {
54+
return std::make_shared<MetalAnimationLoop>(*metalOptions.get());
55+
}
56+
#endif
57+
58+
return std::make_shared<AnimationLoop>(*options.get());
59+
}

cpp/modules/luma.gl/core/src/animation-loop-factory.h

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,19 @@
2929

3030
namespace lumagl {
3131

32+
// TODO(ilija@unfolded.ai): Revisit this API
3233
struct AnimationLoopFactory {
3334
public:
3435
#pragma mark - Exception-free API
3536

36-
static auto createAnimationLoop(probegl::Error& error,
37-
const std::shared_ptr<AnimationLoop::Options>& options = nullptr) noexcept
38-
-> std::shared_ptr<AnimationLoop> {
39-
return probegl::catchError<AnimationLoop>([options]() { return createAnimationLoop(options); }, error);
40-
}
37+
static auto createAnimationLoop(const std::shared_ptr<AnimationLoop::Options>& options,
38+
probegl::Error& error) noexcept -> std::shared_ptr<AnimationLoop>;
39+
static auto createAnimationLoop(probegl::Error& error) noexcept -> std::shared_ptr<AnimationLoop>;
4140

4241
#pragma mark -
4342

44-
// TODO(ilija@unfolded.ai): Revisit
4543
static auto createAnimationLoop(const std::shared_ptr<AnimationLoop::Options>& options = nullptr)
46-
-> std::shared_ptr<AnimationLoop> {
47-
// If no options are passed, try and initialize the only setup which requires no arguments, which is GLFW
48-
if (!options) {
49-
#if defined(LUMAGL_USES_GLFW)
50-
GLFWAnimationLoop::Options opts;
51-
return std::make_shared<GLFWAnimationLoop>(opts);
52-
#else
53-
return nullptr;
54-
#endif
55-
}
56-
57-
#if defined(LUMAGL_USES_GLFW)
58-
if (auto glfwOptions = std::dynamic_pointer_cast<GLFWAnimationLoop::Options>(options)) {
59-
return std::make_shared<GLFWAnimationLoop>(*glfwOptions.get());
60-
}
61-
#endif
62-
63-
#if defined(LUMAGL_ENABLE_BACKEND_METAL)
64-
if (auto metalOptions = std::dynamic_pointer_cast<MetalAnimationLoop::Options>(options)) {
65-
return std::make_shared<MetalAnimationLoop>(*metalOptions.get());
66-
}
67-
#endif
68-
69-
return std::make_shared<AnimationLoop>(*options.get());
70-
}
44+
-> std::shared_ptr<AnimationLoop>;
7145
};
7246

7347
} // namespace lumagl

cpp/modules/luma.gl/garrow/src/field.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222

2323
using namespace lumagl::garrow;
2424

25+
Field::Field(const std::string& name, wgpu::VertexFormat type, bool nullable,
26+
const std::shared_ptr<const KeyValueMetadata>& metadata)
27+
: _name{name}, _type{type}, _nullable{nullable}, _metadata{metadata} {
28+
if (nullable) {
29+
throw std::runtime_error("Nullable fields currently not supported");
30+
}
31+
}
32+
2533
auto Field::Equals(const Field& other, bool check_metadata) const -> bool {
2634
if (this == &other) {
2735
return true;

cpp/modules/luma.gl/garrow/src/field.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@ class Field {
3838
// NOTE: type is currently a simple wgpu::VertexFormat value. Arrow has a complex DataType implementation that
3939
// deals with buffer type complexity, which is something that should be put in place once we implement reading
4040
Field(const std::string& name, wgpu::VertexFormat type, bool nullable = false,
41-
const std::shared_ptr<const KeyValueMetadata>& metadata = nullptr)
42-
: _name{name}, _type{type}, _nullable{nullable}, _metadata{metadata} {
43-
if (nullable) {
44-
throw std::runtime_error("Nullable fields currently not supported");
45-
}
46-
}
41+
const std::shared_ptr<const KeyValueMetadata>& metadata = nullptr);
4742

4843
/// \brief Returns the field name.
4944
auto name() const -> const std::string& { return _name; }

deck.gl-native.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
CDAD574324724CD7002736A3 /* metal-binding.h in Headers */ = {isa = PBXBuildFile; fileRef = CDAD574124724CD7002736A3 /* metal-binding.h */; };
8888
CDAD5745247258BA002736A3 /* animation-loop-factory.h in Headers */ = {isa = PBXBuildFile; fileRef = CDAD5744247258BA002736A3 /* animation-loop-factory.h */; };
8989
CDAE9BE0248E00E6002B6253 /* size.cc in Sources */ = {isa = PBXBuildFile; fileRef = CDAE9BDF248E00E6002B6253 /* size.cc */; };
90+
CDAE9BEC248E2FAC002B6253 /* animation-loop-factory.cc in Sources */ = {isa = PBXBuildFile; fileRef = CDAE9BEB248E2FAC002B6253 /* animation-loop-factory.cc */; };
9091
CDC5AB592487A06F008D76B9 /* libarrow.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CDC5AB5724879D2E008D76B9 /* libarrow.a */; };
9192
CDC5AB5B2487AD8E008D76B9 /* libdawn_native.dylib in Embed Libraries */ = {isa = PBXBuildFile; fileRef = CDE57860243C70F5000D8EC6 /* libdawn_native.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
9293
CDC5AB5C2487AD8E008D76B9 /* libdawn_proc.dylib in Embed Libraries */ = {isa = PBXBuildFile; fileRef = CDE5786A243C70F6000D8EC6 /* libdawn_proc.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
@@ -405,6 +406,7 @@
405406
CDAD5744247258BA002736A3 /* animation-loop-factory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "animation-loop-factory.h"; sourceTree = "<group>"; };
406407
CDAD574924726745002736A3 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
407408
CDAE9BDF248E00E6002B6253 /* size.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = size.cc; sourceTree = "<group>"; };
409+
CDAE9BEB248E2FAC002B6253 /* animation-loop-factory.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = "animation-loop-factory.cc"; sourceTree = "<group>"; };
408410
CDC5AB5724879D2E008D76B9 /* libarrow.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libarrow.a; path = "cpp/deps/x64-osx/lib/libarrow.a"; sourceTree = "<group>"; };
409411
CDC5AB5824879FE5008D76B9 /* CMakeLists.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = "<group>"; };
410412
CDC5AB5F2487B403008D76B9 /* error.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = error.h; sourceTree = "<group>"; };
@@ -1035,6 +1037,7 @@
10351037
CD64D44F24718F8C009A2A9C /* metal-animation-loop.h */,
10361038
CD64D44E24718F8C009A2A9C /* metal-animation-loop.mm */,
10371039
CDAD5744247258BA002736A3 /* animation-loop-factory.h */,
1040+
CDAE9BEB248E2FAC002B6253 /* animation-loop-factory.cc */,
10381041
);
10391042
path = src;
10401043
sourceTree = "<group>";
@@ -1960,6 +1963,7 @@
19601963
CDE57852243C6A1F000D8EC6 /* json-loader.cc in Sources */,
19611964
CDE57810243C6A1F000D8EC6 /* view.cc in Sources */,
19621965
CDE577E5243C6A1F000D8EC6 /* arrow-mapper.cc in Sources */,
1966+
CDAE9BEC248E2FAC002B6253 /* animation-loop-factory.cc in Sources */,
19631967
CDE577F5243C6A1F000D8EC6 /* attribute-manager.cc in Sources */,
19641968
CDE5782F243C6A1F000D8EC6 /* layers.cc in Sources */,
19651969
CDE577AC243C6A1F000D8EC6 /* web-mercator-utils.cc in Sources */,

0 commit comments

Comments
 (0)