Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dfcc335191cb8fb4d2540da5200482e1e03fa6f7
[simgrid.git] / src / smpi / mpi / smpi_info.cpp
1 /* Copyright (c) 2007-2022. 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 {
11 namespace smpi {
12
13 Info::Info(const Info* orig)
14 {
15   if (orig != nullptr)
16     map_ = orig->map_;
17   this->add_f();
18 }
19
20 void Info::ref()
21 {
22   refcount_++;
23 }
24
25 void Info::unref(Info* info){
26   info->refcount_--;
27   if(info->refcount_==0){
28     F2C::free_f(info->f2c_id());
29     delete info;
30   }
31 }
32
33 int Info::get(const char* key, int valuelen, char* value, int* flag) const
34 {
35   *flag=false;
36   if (auto val = map_.find(key); val != map_.end()) {
37     std::string tmpvalue = val->second;
38
39     memset(value, 0, valuelen);
40     memcpy(value, tmpvalue.c_str(),
41            (tmpvalue.length() + 1 < static_cast<size_t>(valuelen)) ? tmpvalue.length() + 1 : valuelen);
42     *flag=true;
43   }
44   return MPI_SUCCESS;
45 }
46
47 int Info::remove(const char *key){
48   if (map_.erase(key) == 0)
49     return MPI_ERR_INFO_NOKEY;
50   else
51     return MPI_SUCCESS;
52 }
53
54 int Info::get_nkeys(int* nkeys) const
55 {
56   *nkeys = map_.size();
57   return MPI_SUCCESS;
58 }
59
60 int Info::get_nthkey(int n, char* key) const
61 {
62   int num=0;
63   for (auto const& [elm, _] : map_) {
64     if (num == n) {
65       strncpy(key, elm.c_str(), elm.length() + 1);
66       return MPI_SUCCESS;
67     }
68     num++;
69   }
70   return MPI_ERR_ARG;
71 }
72
73 int Info::get_valuelen(const char* key, int* valuelen, int* flag) const
74 {
75   *flag=false;
76   if (auto val = map_.find(key); val != map_.end()) {
77     *valuelen = val->second.length();
78     *flag=true;
79   }
80   return MPI_SUCCESS;
81 }
82
83 Info* Info::f2c(int id){
84   return static_cast<Info*>(F2C::f2c(id));
85 }
86
87 }
88 }