Skip to content

Commit 0f8da7c

Browse files
committed
#️⃣ Prepare binding function of std::optional.
1 parent 09862a0 commit 0f8da7c

File tree

8 files changed

+51
-50
lines changed

8 files changed

+51
-50
lines changed

source/MRJavaScript/MRExpected.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ using namespace MR;
1111

1212
EMSCRIPTEN_BINDINGS( ExpectedModule )
1313
{
14+
// TODO
15+
class_<Expected<EdgeLoop>>( "ExpectedEdgeLoop" )
16+
.function( "hasValue", &Expected<EdgeLoop>::has_value );
1417
// class_<tl::expected<EdgeLoop, std::string>>( "ExpectedEdgeLoop" )
1518
// .function( "has_value", &tl::expected<EdgeLoop, std::string>::has_value )
1619
// // .function("value", &tl::expected<EdgeLoop, std::string>::value, return_value_policy::reference())
@@ -20,9 +23,7 @@ EMSCRIPTEN_BINDINGS( ExpectedModule )
2023
// return exp.has_value();
2124
// } );
2225

23-
24-
// class_<Expected<Mesh>>( "ExpectedMesh" )
25-
// .function( "has_value", &Expected<Mesh>::has_value )
26-
// // .function( "value", &Expected<Mesh>::value, return_value_policy::reference() )
27-
// ;
26+
27+
class_<Expected<Mesh>>( "ExpectedMesh" )
28+
.function( "hasValue", &Expected<Mesh>::has_value );
2829
}

source/MRJavaScript/MRMovementBuildBody.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@
44
#include <MRMesh/MRMesh.h>
55
#include <MRMesh/MRMeshFwd.h>
66
#include <MRMesh/MRMovementBuildBody.h>
7+
#include <MRMesh/MRAffineXf3.h>
8+
#include <MRMesh/MRVector3.h>
79

810
using namespace emscripten;
911
using namespace MR;
1012

1113
EMSCRIPTEN_BINDINGS( MovementBuildBodyModule )
1214
{
15+
class_<MovementBuildBodyParams>( "MovementBuildBodyParams" )
16+
.constructor<>()
1317

18+
.property( "allowRotation", &MovementBuildBodyParams::allowRotation )
19+
.property( "center", &MovementBuildBodyParams::center ) // OptionalVector3f
20+
.property( "bodyNormal", &MovementBuildBodyParams::bodyNormal ) // OptionalVector3f
21+
.property( "b2tXf", &MovementBuildBodyParams::b2tXf, allow_raw_pointers() ) // AffineXf3f*
22+
;
23+
24+
function( "makeMovementBuildBody", &makeMovementBuildBody, allow_raw_pointers() );
1425
}

source/MRJavaScript/MROffset.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,5 @@ EMSCRIPTEN_BINDINGS( ThickenMeshModule )
7979
function( "thickenMesh", &thickenMeshWrapper );
8080
function( "suggestVoxelSize", &suggestVoxelSize );
8181

82-
// FIXME: export `Expected`
83-
// function( "offsetMesh", &offsetMesh );
82+
function( "offsetMesh", &offsetMesh );
8483
}

source/MRJavaScript/MRSurroundingContour.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ using namespace MR;
1010

1111
EMSCRIPTEN_BINDINGS( SurroundingContourModule )
1212
{
13-
// FIXME: export `Expected`
14-
// function( "surroundingContourEdges",
15-
// select_overload<Expected<EdgeLoop>( const Mesh&, std::vector<EdgeId>, const EdgeMetric&, const Vector3f& )>
16-
// ( &surroundingContour ),
17-
// allow_raw_pointers()
18-
// );
13+
function( "surroundingContourEdges",
14+
select_overload<Expected<EdgeLoop>( const Mesh&, std::vector<EdgeId>, const EdgeMetric&, const Vector3f& )>
15+
( &surroundingContour ),
16+
allow_raw_pointers()
17+
);
1918

20-
// function( "surroundingContourVertices",
21-
// select_overload<Expected<EdgeLoop>( const Mesh&, std::vector<VertId>, const EdgeMetric&, const Vector3f& )>
22-
// ( &surroundingContour ),
23-
// allow_raw_pointers()
24-
// );
19+
function( "surroundingContourVertices",
20+
select_overload<Expected<EdgeLoop>( const Mesh&, std::vector<VertId>, const EdgeMetric&, const Vector3f& )>
21+
( &surroundingContour ),
22+
allow_raw_pointers()
23+
);
2524
}

source/MRJavaScript/MRUtils.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
#include <optional>
12

23
#include <emscripten/bind.h>
4+
#include <emscripten/val.h>
35

46
#include <MRMesh/MRMeshFwd.h>
57
#include <MRMesh/MRVector.h>
@@ -195,8 +197,16 @@ EMSCRIPTEN_BINDINGS( UtilsModule )
195197
// ------------------------------------------------------------------------
196198
// Bind the Embind interface for `Optional*`
197199
// ------------------------------------------------------------------------
198-
// FIXME: `std:optional`
199-
// MRJS::register_optional<MeshOrPoints>( "OptionalMeshOrPoints" );
200-
// MRJS::register_optional<Vector3f>( "OptionalVector3f" );
201-
// MRJS::register_optional<VertBitSet>( "OptionalVertBitSet" );
200+
register_optional<MeshOrPoints>();
201+
register_optional<Vector3f>();
202+
register_optional<VertBitSet>();
203+
204+
205+
// ------------------------------------------------------------------------
206+
// Bind the Embind interface for `*Functor*`
207+
// ------------------------------------------------------------------------
208+
class_<std::function<std::string( std::string )>>( "StringFunctorString" )
209+
.constructor<>()
210+
.function( "opcall", &std::function<std::string( std::string )>::operator() )
211+
;
202212
}

source/MRJavaScript/MRUtils.h

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
using namespace emscripten;
1919
using namespace MR;
2020

21-
namespace MRJS {
21+
22+
namespace MRJS
23+
{
2224

2325
// Helper function to convert Expected<T> to JavaScript-friendly result
2426
template<typename T>
@@ -185,27 +187,4 @@ void bindStdArray( const char* jsName )
185187
bindStdArrayImpl<T>( jsName, std::make_index_sequence<N>{} );
186188
}
187189

188-
189-
template<typename OptionalType>
190-
struct OptionalAccess
191-
{
192-
static val value( const OptionalType& v )
193-
{
194-
return val( v.value() );
195-
}
196-
static bool hasValue( const OptionalType& v )
197-
{
198-
return v.has_value();
199-
}
200-
};
201-
template<typename V>
202-
class_<std::optional<V>> register_optional( const char* name )
203-
{
204-
using OptionalV = std::optional<V>;
205-
return class_<OptionalV>( name )
206-
.constructor<>()
207-
.function( "value", &OptionalAccess<OptionalV>::value )
208-
.function( "hasValue", &OptionalAccess<OptionalV>::hasValue );
209-
}
210-
211190
} // namespace MRUtil

source/MRJavaScript/README-zh.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
应该遵守的代码规范。
44

5-
- 标准库`std::function`: ``
6-
- 标准库`std::optional`: ``
5+
- 标准库`tl::expected<T, E>`: ``
6+
- 标准库`std::function`: `class_<std::function<std::string( std::string )>>( "StringFunctorString" ).constructor<>().function( "opcall", &std::function<std::string( std::string )>::operator() );`
7+
- 标准库`std::optional`: `register_optional<SmallClass>();`
78
- 标准库`std::array`: `value_array<std::array<float, 3>>( "Array3f" ).element( emscripten::index<0>() ).element( emscripten::index<1>() ).element( emscripten::index<2>() );`, `value_array<std::array<EdgeId, 2>>( "Array2EdgeId" ).element( emscripten::index<0>() ).element( emscripten::index<1>() );`
89
- 标准库std::vector: `register_vector<Vector3f>( "VectorVector3f" );`
910
- 标准库std::pair: `value_array<std::pair<Vector3f, Vector3f>>( "Vector3fPair" ).element( &std::pair<Vector3f, Vector3f>::first ).element( &std::pair<Vector3f, Vector3f>::second )`

source/MRJavaScript/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
Rules that should obey.
44

5-
- Standard library `std::function`: ``
6-
- Standard library `std::optional`: ``
5+
- Standard library `tl::expected<T, E>`: ``
6+
- Standard library `std::function`: `class_<std::function<std::string( std::string )>>( "StringFunctorString" ).constructor<>().function( "opcall", &std::function<std::string( std::string )>::operator() );`
7+
- Standard library `std::optional`: `register_optional<SmallClass>();`
78
- Standard library `std::array`: `value_array<std::array<float, 3>>( "Array3f" ).element( emscripten::index<0>() ).element( emscripten::index<1>() ).element( emscripten::index<2>() );`, `value_array<std::array<EdgeId, 2>>( "Array2EdgeId" ).element( emscripten::index<0>() ).element( emscripten::index<1>() );`
89
- Standard library `std::vector`: `register_vector<Vector3f>( "VectorVector3f" );`
910
- Standard library `std::pair`: `value_array<std::pair<Vector3f, Vector3f>>( "Vector3fPair" ).element( &std::pair<Vector3f, Vector3f>::first ).element( &std::pair<Vector3f, Vector3f>::second )`

0 commit comments

Comments
 (0)