Logo AND Algorithmique Numérique Distribuée

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