Logo AND Algorithmique Numérique Distribuée

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