Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5a9697bd78e8f86d17ebbe0ce50151bbab5c654b
[simgrid.git] / src / smpi / 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 "src/smpi/smpi_info.hpp"
8
9 namespace simgrid{
10 namespace smpi{
11
12 Info::Info():refcount_(1){
13   dict_= xbt_dict_new_homogeneous(xbt_free_f);
14 }
15
16 Info::Info(Info* info):refcount_(1){
17   dict_= xbt_dict_new_homogeneous(xbt_free_f);
18   xbt_dict_cursor_t cursor = nullptr;
19   char* key;
20   void* data;
21   xbt_dict_foreach(info->dict_,cursor,key,data){
22     xbt_dict_set(dict_, key, xbt_strdup(static_cast<char*>(data)), nullptr);
23   }
24 }
25
26 Info::~Info(){
27   xbt_dict_free(&dict_);
28 }
29
30 void Info::ref(){
31   refcount_++;
32 }
33
34 void Info::unref(Info* info){
35   info->refcount_--;
36   if(info->refcount_==0){
37     delete info;
38   }
39 }
40
41 void Info::set(char *key, char *value){
42   xbt_dict_set(dict_, key, xbt_strdup(value), nullptr);
43 }
44
45
46
47 int Info::get(char *key, int valuelen, char *value, int *flag){
48   *flag=false;
49   char* tmpvalue=static_cast<char*>(xbt_dict_get_or_null(dict_, key));
50   if(tmpvalue){
51     memset(value, 0, valuelen);
52     memcpy(value,tmpvalue, (strlen(tmpvalue) + 1 < static_cast<size_t>(valuelen)) ? strlen(tmpvalue) + 1 : valuelen);
53     *flag=true;
54   }
55   return MPI_SUCCESS;
56 }
57
58
59 int Info::remove(char *key){
60   try {
61     xbt_dict_remove(dict_, key);
62   }
63   catch(xbt_ex& e){
64     return MPI_ERR_INFO_NOKEY;
65   }
66   return MPI_SUCCESS;
67 }
68
69 int Info::get_nkeys(int *nkeys){
70   *nkeys=xbt_dict_size(dict_);
71   return MPI_SUCCESS;
72 }
73
74 int Info::get_nthkey(int n, char *key){
75   xbt_dict_cursor_t cursor = nullptr;
76   char *keyn;
77   void* data;
78   int num=0;
79   xbt_dict_foreach(dict_,cursor,keyn,data){
80     if(num==n){
81       strncpy(key,keyn,strlen(keyn)+1);
82       xbt_dict_cursor_free(&cursor);
83       return MPI_SUCCESS;
84     }
85     num++;
86   }
87   return MPI_ERR_ARG;
88 }
89
90 int Info::get_valuelen(char *key, int *valuelen, int *flag){
91   *flag=false;
92   char* tmpvalue=static_cast<char*>(xbt_dict_get_or_null(dict_, key));
93   if(tmpvalue){
94     *valuelen=strlen(tmpvalue);
95     *flag=true;
96   }
97   return MPI_SUCCESS;
98 }
99
100 Info* Info::f2c(int id){
101   return static_cast<Info*>(F2C::f2c(id));
102 }
103
104 }
105 }
106