Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill another dict
[simgrid.git] / src / smpi / mpi / smpi_info.cpp
1 /* Copyright (c) 2007-2017. 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   try {
36     std::string tmpvalue = map_.at(key);
37
38     memset(value, 0, valuelen);
39     memcpy(value, tmpvalue.c_str(),
40            (tmpvalue.length() + 1 < static_cast<size_t>(valuelen)) ? tmpvalue.length() + 1 : valuelen);
41     *flag=true;
42     return MPI_SUCCESS;
43   } catch (std::out_of_range& unfound) {
44     return MPI_ERR_INFO_KEY;
45   }
46 }
47
48 int Info::remove(char *key){
49   if (map_.erase(key) == 0)
50     return MPI_ERR_INFO_NOKEY;
51   else
52     return MPI_SUCCESS;
53 }
54
55 int Info::get_nkeys(int *nkeys){
56   *nkeys = map_.size();
57   return MPI_SUCCESS;
58 }
59
60 int Info::get_nthkey(int n, char *key){
61   int num=0;
62   for (auto elm : map_) {
63     if (num == n) {
64       strncpy(key, elm.first.c_str(), elm.first.length() + 1);
65       return MPI_SUCCESS;
66     }
67     num++;
68   }
69   return MPI_ERR_ARG;
70 }
71
72 int Info::get_valuelen(char *key, int *valuelen, int *flag){
73   *flag=false;
74   try {
75     *valuelen = map_.at(key).length();
76     *flag=true;
77     return MPI_SUCCESS;
78   } catch (std::out_of_range& unfound) {
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 }