Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into mq
[simgrid.git] / src / smpi / include / smpi_keyvals.hpp
index b594f09a136c8544e95ab4a947f1b7b658359caf..768bc515538a83ebc459cd91e2abecbbc5d90fc9 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-2023. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -38,8 +38,7 @@ struct smpi_key_elem {
   bool delete_attr; // if true, xbt_free(attr) on delete: used by Fortran bindings
 };
 
-namespace simgrid{
-namespace smpi{
+namespace simgrid::smpi {
 
 class Keyval{
   private:
@@ -106,8 +105,7 @@ template <typename T> int Keyval::attr_delete(int keyval){
 
   smpi_key_elem& elem = elem_it->second;
   int flag            = 0;
-  int ret             = call_deleter<T>((T*)this, elem, keyval, attr->second, &flag);
-  if (ret != MPI_SUCCESS)
+  if (int ret = call_deleter<T>((T*)this, elem, keyval, attr->second, &flag); ret != MPI_SUCCESS)
     return ret;
 
   elem.refcount--;
@@ -119,12 +117,10 @@ template <typename T> int Keyval::attr_delete(int keyval){
 
 
 template <typename T> int Keyval::attr_get(int keyval, void* attr_value, int* flag){
-  auto elem_it = T::keyvals_.find(keyval);
-  if (elem_it == T::keyvals_.end() || elem_it->second.deleted)
+  if (auto elem_it = T::keyvals_.find(keyval); elem_it == T::keyvals_.end() || elem_it->second.deleted)
     return MPI_ERR_ARG;
 
-  auto attr = attributes().find(keyval);
-  if (attr != attributes().end()) {
+  if (auto attr = attributes().find(keyval); attr != attributes().end()) {
     *static_cast<void**>(attr_value) = attr->second;
     *flag=1;
   } else {
@@ -139,14 +135,13 @@ template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
     return MPI_ERR_ARG;
 
   smpi_key_elem& elem = elem_it->second;
-  int flag=0;
-  auto p  = attributes().emplace(keyval, attr_value);
-  if (p.second) {
+  if (auto [attr, inserted] = attributes().try_emplace(keyval, attr_value); inserted) {
     elem.refcount++;
   } else {
-    int ret = call_deleter<T>((T*)this, elem, keyval,p.first->second,&flag);
+    int flag = 0;
+    int ret  = call_deleter<T>((T*)this, elem, keyval, attr->second, &flag);
     // overwrite previous value
-    p.first->second = attr_value;
+    attr->second = attr_value;
     if(ret!=MPI_SUCCESS)
       return ret;
   }
@@ -154,12 +149,12 @@ template <typename T> int Keyval::attr_put(int keyval, void* attr_value){
 }
 
 template <typename T> void Keyval::cleanup_attr(){
-  for (auto const& it : attributes()) {
-    auto elem_it = T::keyvals_.find(it.first);
+  for (auto const& [key, value] : attributes()) {
+    auto elem_it = T::keyvals_.find(key);
     xbt_assert(elem_it != T::keyvals_.end());
     smpi_key_elem& elem = elem_it->second;
     int flag            = 0;
-    call_deleter<T>((T*)this, elem, it.first, it.second, &flag);
+    call_deleter<T>((T*)this, elem, key, value, &flag);
     elem.refcount--;
     if (elem.deleted && elem.refcount == 0)
       T::keyvals_.erase(elem_it);
@@ -167,7 +162,6 @@ template <typename T> void Keyval::cleanup_attr(){
   attributes().clear();
 }
 
-}
-}
+} // namespace simgrid::smpi
 
 #endif