@@ -1875,6 +1875,21 @@ class class_ {
1875
1875
}
1876
1876
};
1877
1877
1878
+ #if __cplusplus >= 201703L
1879
+ template <typename T>
1880
+ void register_optional () {
1881
+ // Optional types are automatically registered for some internal types so
1882
+ // only run the register method once so we don't conflict with a user's
1883
+ // bindings if they also register the optional type.
1884
+ thread_local bool hasRun;
1885
+ if (hasRun) {
1886
+ return ;
1887
+ }
1888
+ hasRun = true ;
1889
+ internal::_embind_register_optional (internal::TypeID<std::optional<T>>::get (), internal::TypeID<T>::get ());
1890
+ }
1891
+ #endif
1892
+
1878
1893
// //////////////////////////////////////////////////////////////////////////////
1879
1894
// VECTORS
1880
1895
// //////////////////////////////////////////////////////////////////////////////
@@ -1883,6 +1898,20 @@ namespace internal {
1883
1898
1884
1899
template <typename VectorType>
1885
1900
struct VectorAccess {
1901
+ // This nearly duplicated code is used for generating more specific TypeScript
1902
+ // types when using more modern C++ versions.
1903
+ #if __cplusplus >= 201703L
1904
+ static std::optional<typename VectorType::value_type> get (
1905
+ const VectorType& v,
1906
+ typename VectorType::size_type index
1907
+ ) {
1908
+ if (index < v.size ()) {
1909
+ return v[index];
1910
+ } else {
1911
+ return {};
1912
+ }
1913
+ }
1914
+ #else
1886
1915
static val get (
1887
1916
const VectorType& v,
1888
1917
typename VectorType::size_type index
@@ -1893,6 +1922,7 @@ struct VectorAccess {
1893
1922
return val::undefined ();
1894
1923
}
1895
1924
}
1925
+ #endif
1896
1926
1897
1927
static bool set (
1898
1928
VectorType& v,
@@ -1909,6 +1939,9 @@ struct VectorAccess {
1909
1939
template <typename T>
1910
1940
class_<std::vector<T>> register_vector (const char * name) {
1911
1941
typedef std::vector<T> VecType;
1942
+ #if __cplusplus >= 201703L
1943
+ register_optional<T>();
1944
+ #endif
1912
1945
1913
1946
void (VecType::*push_back)(const T&) = &VecType::push_back;
1914
1947
void (VecType::*resize)(const size_t , const T&) = &VecType::resize;
@@ -1923,13 +1956,6 @@ class_<std::vector<T>> register_vector(const char* name) {
1923
1956
;
1924
1957
}
1925
1958
1926
- #if __cplusplus >= 201703L
1927
- template <typename T>
1928
- void register_optional () {
1929
- internal::_embind_register_optional (internal::TypeID<std::optional<T>>::get (), internal::TypeID<T>::get ());
1930
- }
1931
- #endif
1932
-
1933
1959
// //////////////////////////////////////////////////////////////////////////////
1934
1960
// MAPS
1935
1961
// //////////////////////////////////////////////////////////////////////////////
@@ -1938,6 +1964,21 @@ namespace internal {
1938
1964
1939
1965
template <typename MapType>
1940
1966
struct MapAccess {
1967
+ // This nearly duplicated code is used for generating more specific TypeScript
1968
+ // types when using more modern C++ versions.
1969
+ #if __cplusplus >= 201703L
1970
+ static std::optional<typename MapType::mapped_type> get (
1971
+ const MapType& m,
1972
+ const typename MapType::key_type& k
1973
+ ) {
1974
+ auto i = m.find (k);
1975
+ if (i == m.end ()) {
1976
+ return {};
1977
+ } else {
1978
+ return i->second ;
1979
+ }
1980
+ }
1981
+ #else
1941
1982
static val get (
1942
1983
const MapType& m,
1943
1984
const typename MapType::key_type& k
@@ -1949,6 +1990,7 @@ struct MapAccess {
1949
1990
return val (i->second );
1950
1991
}
1951
1992
}
1993
+ #endif
1952
1994
1953
1995
static void set (
1954
1996
MapType& m,
@@ -1975,6 +2017,9 @@ struct MapAccess {
1975
2017
template <typename K, typename V>
1976
2018
class_<std::map<K, V>> register_map (const char * name) {
1977
2019
typedef std::map<K,V> MapType;
2020
+ #if __cplusplus >= 201703L
2021
+ register_optional<V>();
2022
+ #endif
1978
2023
1979
2024
size_t (MapType::*size)() const = &MapType::size;
1980
2025
return class_<MapType>(name)
0 commit comments