Logo AND Algorithmique Numérique Distribuée

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