Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / smpi / mpi / smpi_info.cpp
1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "smpi_info.hpp"
7 #include "smpi_comm.hpp"
8 #include "simgrid/Exception.hpp"
9
10 namespace simgrid::smpi {
11
12 Info::Info(const Info* orig)
13 {
14   if (orig != nullptr)
15     map_ = orig->map_;
16   this->add_f();
17 }
18
19 void Info::ref()
20 {
21   refcount_++;
22 }
23
24 void Info::unref(Info* info){
25   info->refcount_--;
26   if(info->refcount_==0){
27     F2C::free_f(info->f2c_id());
28     delete info;
29   }
30 }
31
32 int Info::get(const char* key, int valuelen, char* value, int* flag) const
33 {
34   *flag=false;
35   if (auto val = map_.find(key); val != map_.end()) {
36     std::string tmpvalue = val->second;
37
38     memset(value, 0, valuelen);
39     memcpy(value, tmpvalue.c_str(),
40            (tmpvalue.length() + 1 < static_cast<size_t>(valuelen)) ? tmpvalue.length() + 1 : valuelen);
41     *flag=true;
42   }
43   return MPI_SUCCESS;
44 }
45
46 int Info::remove(const char *key){
47   if (map_.erase(key) == 0)
48     return MPI_ERR_INFO_NOKEY;
49   else
50     return MPI_SUCCESS;
51 }
52
53 int Info::get_nkeys(int* nkeys) const
54 {
55   *nkeys = map_.size();
56   return MPI_SUCCESS;
57 }
58
59 int Info::get_nthkey(int n, char* key) const
60 {
61   int num=0;
62   for (auto const& [elm, _] : map_) {
63     if (num == n) {
64       strncpy(key, elm.c_str(), elm.length() + 1);
65       return MPI_SUCCESS;
66     }
67     num++;
68   }
69   return MPI_ERR_ARG;
70 }
71
72 int Info::get_valuelen(const char* key, int* valuelen, int* flag) const
73 {
74   *flag=false;
75   if (auto val = map_.find(key); val != map_.end()) {
76     *valuelen = val->second.length();
77     *flag=true;
78   }
79   return MPI_SUCCESS;
80 }
81
82 Info* Info::f2c(int id){
83   return static_cast<Info*>(F2C::f2c(id));
84 }
85
86 } // namespace simgrid::smpi