Logo AND Algorithmique Numérique Distribuée

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