Logo AND Algorithmique Numérique Distribuée

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