Logo AND Algorithmique Numérique Distribuée

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