-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
161 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 4 additions & 7 deletions
11
packages/react-native-nitro-modules/cpp/react-native-nitro.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,9 @@ | ||
#include "react-native-nitro.hpp" | ||
|
||
#include <swift/bridging> | ||
#include "NitroModules-Swift.h" | ||
|
||
namespace nitro { | ||
double multiply(double a, double b) { | ||
auto exampleClass = NitroModules::HybridObject::init("Heyo!"); | ||
|
||
return a * b; | ||
} | ||
std::shared_ptr<margelo::TestHybridObject> createTestHybridObject() { | ||
return std::make_shared<margelo::TestHybridObject>(); | ||
} | ||
|
||
} |
5 changes: 4 additions & 1 deletion
5
packages/react-native-nitro-modules/cpp/react-native-nitro.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
#ifndef NITRO_H | ||
#define NITRO_H | ||
|
||
#include "test-object/TestHybridObject.hpp" | ||
|
||
namespace nitro { | ||
double multiply(double a, double b); | ||
|
||
std::shared_ptr<margelo::TestHybridObject> createTestHybridObject(); | ||
} | ||
|
||
#endif /* NITRO_H */ |
34 changes: 34 additions & 0 deletions
34
packages/react-native-nitro-modules/cpp/test-object/TestHybridObject.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// Created by Marc Rousavy on 20.02.24. | ||
// | ||
|
||
#include "TestHybridObject.hpp" | ||
|
||
namespace margelo { | ||
|
||
void TestHybridObject::loadHybridMethods() { | ||
// this.int get & set | ||
registerHybridGetter("int", &TestHybridObject::getInt, this); | ||
registerHybridSetter("int", &TestHybridObject::setInt, this); | ||
// this.string get & set | ||
registerHybridGetter("string", &TestHybridObject::getString, this); | ||
registerHybridSetter("string", &TestHybridObject::setString, this); | ||
// this.nullableString get & set | ||
registerHybridGetter("nullableString", &TestHybridObject::getNullableString, this); | ||
registerHybridSetter("nullableString", &TestHybridObject::setNullableString, this); | ||
// this.enum | ||
registerHybridGetter("enum", &TestHybridObject::getEnum, this); | ||
registerHybridSetter("enum", &TestHybridObject::setEnum, this); | ||
// methods | ||
registerHybridMethod("multipleArguments", &TestHybridObject::multipleArguments, this); | ||
// callbacks | ||
registerHybridMethod("getIntGetter", &TestHybridObject::getIntGetter, this); | ||
registerHybridMethod("sayHelloCallback", &TestHybridObject::sayHelloCallback, this); | ||
// custom types | ||
registerHybridMethod("createNewHybridObject", &TestHybridObject::createNewHybridObject, this); | ||
// Promises | ||
registerHybridMethod("calculateFibonacci", &TestHybridObject::calculateFibonacci, this); | ||
registerHybridMethod("calculateFibonacciAsync", &TestHybridObject::calculateFibonacciAsync, this); | ||
} | ||
|
||
} // namespace margelo |
92 changes: 92 additions & 0 deletions
92
packages/react-native-nitro-modules/cpp/test-object/TestHybridObject.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// | ||
// Created by Marc Rousavy on 22.02.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "HybridObject.hpp" | ||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace margelo { | ||
|
||
class TestHybridObject : public HybridObject { | ||
public: | ||
explicit TestHybridObject() : HybridObject("TestHybridObject") {} | ||
|
||
public: | ||
int getInt() { | ||
return _int; | ||
} | ||
void setInt(int newValue) { | ||
_int = newValue; | ||
} | ||
std::string getString() { | ||
return _string; | ||
} | ||
void setString(const std::string& newValue) { | ||
_string = newValue; | ||
} | ||
void setEnum(TestEnum testEnum) { | ||
_enum = testEnum; | ||
} | ||
TestEnum getEnum() { | ||
return _enum; | ||
} | ||
std::optional<std::string> getNullableString() { | ||
return _nullableString; | ||
} | ||
void setNullableString(std::optional<std::string> string) { | ||
_nullableString = string; | ||
} | ||
|
||
std::unordered_map<std::string, double> multipleArguments(int first, bool second, std::string third) { | ||
return std::unordered_map<std::string, double>{{"first", 5312}, {"second", 532233}, {"third", 2786}}; | ||
} | ||
|
||
std::function<int()> getIntGetter() { | ||
return [this]() -> int { return this->_int; }; | ||
} | ||
void sayHelloCallback(std::function<void(std::string)> callback) { | ||
callback("Test Hybrid"); | ||
} | ||
std::shared_ptr<TestHybridObject> createNewHybridObject() { | ||
return std::make_shared<TestHybridObject>(); | ||
} | ||
|
||
uint64_t calculateFibonacci(int count) { | ||
if (count < 0) | ||
throw std::invalid_argument("Cannot calculate fibonacci for " + std::to_string(count) + " - it needs to be at least 0!"); | ||
if (count == 0) | ||
return 0; | ||
if (count == 1) | ||
return 1; | ||
if (count >= 94) | ||
throw std::invalid_argument("Cannot calculate fibonacci for " + std::to_string(count) + | ||
" - it needs to be 94 at max, the number will overflow!"); | ||
|
||
uint64_t prev = 0; | ||
uint64_t current = 1; | ||
for (unsigned int i = 2; i <= count; ++i) { | ||
uint64_t next = prev + current; | ||
prev = current; | ||
current = next; | ||
} | ||
return current; | ||
} | ||
|
||
std::future<uint64_t> calculateFibonacciAsync(int count) { | ||
return std::async(std::launch::async, [count, this]() { return this->calculateFibonacci(count); }); | ||
} | ||
|
||
private: | ||
int _int; | ||
std::string _string; | ||
TestEnum _enum; | ||
std::optional<std::string> _nullableString; | ||
|
||
void loadHybridMethods() override; | ||
}; | ||
|
||
} // namespace margelo |