@@ -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,18 @@ namespace internal {
1883
1898
1884
1899
template <typename VectorType>
1885
1900
struct VectorAccess {
1901
+ #if __cplusplus >= 201703L
1902
+ static std::optional<typename VectorType::value_type> get (
1903
+ const VectorType& v,
1904
+ typename VectorType::size_type index
1905
+ ) {
1906
+ if (index < v.size ()) {
1907
+ return v[index];
1908
+ } else {
1909
+ return {};
1910
+ }
1911
+ }
1912
+ #else
1886
1913
static val get (
1887
1914
const VectorType& v,
1888
1915
typename VectorType::size_type index
@@ -1893,6 +1920,7 @@ struct VectorAccess {
1893
1920
return val::undefined ();
1894
1921
}
1895
1922
}
1923
+ #endif
1896
1924
1897
1925
static bool set (
1898
1926
VectorType& v,
@@ -1909,6 +1937,9 @@ struct VectorAccess {
1909
1937
template <typename T>
1910
1938
class_<std::vector<T>> register_vector (const char * name) {
1911
1939
typedef std::vector<T> VecType;
1940
+ #if __cplusplus >= 201703L
1941
+ register_optional<T>();
1942
+ #endif
1912
1943
1913
1944
void (VecType::*push_back)(const T&) = &VecType::push_back;
1914
1945
void (VecType::*resize)(const size_t , const T&) = &VecType::resize;
@@ -1923,13 +1954,6 @@ class_<std::vector<T>> register_vector(const char* name) {
1923
1954
;
1924
1955
}
1925
1956
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
1957
// //////////////////////////////////////////////////////////////////////////////
1934
1958
// MAPS
1935
1959
// //////////////////////////////////////////////////////////////////////////////
@@ -1938,6 +1962,19 @@ namespace internal {
1938
1962
1939
1963
template <typename MapType>
1940
1964
struct MapAccess {
1965
+ #if __cplusplus >= 201703L
1966
+ static std::optional<typename MapType::mapped_type> get (
1967
+ const MapType& m,
1968
+ const typename MapType::key_type& k
1969
+ ) {
1970
+ auto i = m.find (k);
1971
+ if (i == m.end ()) {
1972
+ return {};
1973
+ } else {
1974
+ return i->second ;
1975
+ }
1976
+ }
1977
+ #else
1941
1978
static val get (
1942
1979
const MapType& m,
1943
1980
const typename MapType::key_type& k
@@ -1949,6 +1986,7 @@ struct MapAccess {
1949
1986
return val (i->second );
1950
1987
}
1951
1988
}
1989
+ #endif
1952
1990
1953
1991
static void set (
1954
1992
MapType& m,
@@ -1975,6 +2013,9 @@ struct MapAccess {
1975
2013
template <typename K, typename V>
1976
2014
class_<std::map<K, V>> register_map (const char * name) {
1977
2015
typedef std::map<K,V> MapType;
2016
+ #if __cplusplus >= 201703L
2017
+ register_optional<V>();
2018
+ #endif
1978
2019
1979
2020
size_t (MapType::*size)() const = &MapType::size;
1980
2021
return class_<MapType>(name)
0 commit comments