Fawkes API  Fawkes Development Version
eclipseclp_config.cpp
1 
2 /***************************************************************************
3  * eclipseclp_config.cpp - config wrapper for Eclipse-CLP
4  *
5  * Created: Fri Jan 30 17:17:16 2015 +0100
6  * Copyright 2015 Gesche Gierse
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #include "eclipseclp_config.h"
23 
24 #include <cstring>
25 #include <eclipseclass.h>
26 #include <typeinfo>
27 
28 namespace fawkes {
29 
30 /** @class fawkes::EclExternalConfig
31  * Wrapper class for using the config in the implementation of the external
32  * predicates.
33  * @author Gesche Gierse
34  */
35 
36 Configuration * EclExternalConfig::m_config = NULL;
37 EclExternalConfig *EclExternalConfig::m_instance = NULL;
38 
39 /** Constructor. */
40 EclExternalConfig::EclExternalConfig()
41 {
42  if (m_instance == NULL) {
43  m_instance = this;
44  } else {
45  //throw Exception("There is already an instance of type EclExternalConfig instantiated");
46  }
47 }
48 
49 /** Constructor. */
50 EclExternalConfig::EclExternalConfig(Configuration *config)
51 {
52  if (m_instance == NULL) {
53  ;
54  m_instance = this;
55  m_config = config;
56  } else {
57  m_config = config;
58  //throw Exception("There is already an instance of type EclExternalConfig instantiated");
59  }
60 }
61 
62 /** Destructor. */
64 {
65  delete m_instance;
66  //delete m_config;
67 }
68 
69 /** Creates the initial EclExternalConfig object
70  * @param bb pointer to the Configuration to be used
71  */
72 void
74 {
75  m_instance = new EclExternalConfig(bb);
76 }
77 
78 /** Get the EclExternalConfig instance.
79  * @return the instance
80  */
83 {
84  if (!m_instance) {
85  throw Exception("No instance of type EclExternalConfig instantiated");
86  }
87 
88  return m_instance;
89 }
90 
91 /** Access the Configuration instance.
92  * @return the config instance
93  */
96 {
97  if (!m_config) {
98  throw Exception("No instance of type Configuration instantiated");
99  }
100 
101  return m_config;
102 }
103 
104 } // namespace fawkes
105 
106 using namespace fawkes;
107 
108 /* implements get_config_value(Path, Value)
109  */
110 int
111 p_get_config_value()
112 {
114  char * path;
115 
116  if (EC_succeed != EC_arg(1).is_string(&path)) {
117  fprintf(stderr, "p_get_config_value(): no path given\n");
118  return EC_fail;
119  }
120 
121  if (config->is_bool(path)) {
122  if (config->is_list(path)) {
123  std::vector<bool> vec = config->get_bools(path);
124  EC_word res = nil();
125  for (std::vector<bool>::reverse_iterator it = vec.rbegin(); it != vec.rend(); ++it)
126  res = list(EC_word(*it), res);
127  if (EC_succeed != EC_arg(2).unify(res)) {
128  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
129  return EC_fail;
130  }
131  return EC_succeed;
132  } else if (config->get_bool(path)) {
133  if (EC_succeed != EC_arg(2).unify(EC_atom((char *)"true"))) {
134  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
135  return EC_fail;
136  }
137  return EC_succeed;
138  } else {
139  if (EC_succeed != EC_arg(2).unify(EC_atom((char *)"false"))) {
140  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
141  return EC_fail;
142  }
143  return EC_succeed;
144  }
145  } else if (config->is_int(path)) {
146  if (config->is_list(path)) {
147  std::vector<int> vec = config->get_ints(path);
148  EC_word res = nil();
149  for (std::vector<int>::reverse_iterator it = vec.rbegin(); it != vec.rend(); ++it)
150  res = list(EC_word((long)*it), res);
151  if (EC_succeed != EC_arg(2).unify(res)) {
152  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
153  return EC_fail;
154  }
155  return EC_succeed;
156  } else if (EC_succeed != EC_arg(2).unify(EC_word((long)config->get_int(path)))) {
157  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
158  return EC_fail;
159  }
160  return EC_succeed;
161  } else if (config->is_uint(path)) {
162  if (config->is_list(path)) {
163  std::vector<unsigned int> vec = config->get_uints(path);
164  EC_word res = nil();
165  for (std::vector<unsigned int>::reverse_iterator it = vec.rend(); it != vec.rbegin(); --it)
166  res = list(EC_word((long)*it), res);
167  if (EC_succeed != EC_arg(2).unify(res)) {
168  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
169  return EC_fail;
170  }
171  return EC_succeed;
172  } else if (EC_succeed != EC_arg(2).unify(EC_word((long)config->get_uint(path)))) {
173  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
174  return EC_fail;
175  }
176  return EC_succeed;
177  }
178 
179  else if (config->is_float(path)) {
180  if (config->is_list(path)) {
181  std::vector<float> vec = config->get_floats(path);
182  EC_word res = nil();
183  for (std::vector<float>::reverse_iterator it = vec.rbegin(); it != vec.rend(); ++it)
184  res = list(EC_word((double)*it), res);
185  if (EC_succeed != EC_arg(2).unify(res)) {
186  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
187  return EC_fail;
188  }
189  return EC_succeed;
190  }
191  if (EC_succeed != EC_arg(2).unify(EC_word((double)config->get_float(path)))) {
192  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
193  return EC_fail;
194  }
195  return EC_succeed;
196  } else if (config->is_string(path)) {
197  if (config->is_list(path)) {
198  std::vector<std::string> vec = config->get_strings(path);
199  EC_word res = nil();
200  for (std::vector<std::string>::reverse_iterator it = vec.rbegin(); it != vec.rend(); ++it)
201  res = list(EC_word((*it).c_str()), res);
202  if (EC_succeed != EC_arg(2).unify(res)) {
203  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
204  return EC_fail;
205  }
206  return EC_succeed;
207  } else if (EC_succeed != EC_arg(2).unify(EC_word(config->get_string(path).c_str()))) {
208  fprintf(stderr, "p_get_config_value(): could not bind return value\n");
209  return EC_fail;
210  }
211  return EC_succeed;
212  } else {
213  fprintf(stderr,
214  "p_get_config_value(): could not find type of config value! Type: %s\n",
215  config->get_type(path).c_str());
216  return EC_fail;
217  }
218  return EC_fail;
219 }
fawkes::Configuration::get_ints
virtual std::vector< int > get_ints(const char *path)=0
fawkes::Configuration::get_type
virtual std::string get_type(const char *path)=0
fawkes::EclExternalConfig::~EclExternalConfig
~EclExternalConfig()
Destructor.
Definition: eclipseclp_config.cpp:68
fawkes::Configuration::get_bools
virtual std::vector< bool > get_bools(const char *path)=0
fawkes::EclExternalConfig::config_instance
static Configuration * config_instance()
Access the Configuration instance.
Definition: eclipseclp_config.cpp:100
fawkes::Configuration::get_bool
virtual bool get_bool(const char *path)=0
fawkes::Configuration::is_bool
virtual bool is_bool(const char *path)=0
fawkes::Configuration::is_list
virtual bool is_list(const char *path)=0
fawkes::Configuration::get_uints
virtual std::vector< unsigned int > get_uints(const char *path)=0
fawkes::Configuration::get_int
virtual int get_int(const char *path)=0
fawkes::Configuration
Definition: config.h:70
fawkes::Configuration::get_floats
virtual std::vector< float > get_floats(const char *path)=0
fawkes
fawkes::Configuration::is_uint
virtual bool is_uint(const char *path)=0
fawkes::Configuration::get_strings
virtual std::vector< std::string > get_strings(const char *path)=0
fawkes::Configuration::is_float
virtual bool is_float(const char *path)=0
fawkes::Configuration::get_float
virtual float get_float(const char *path)=0
fawkes::Configuration::get_uint
virtual unsigned int get_uint(const char *path)=0
fawkes::Configuration::get_string
virtual std::string get_string(const char *path)=0
fawkes::Configuration::is_int
virtual bool is_int(const char *path)=0
fawkes::EclExternalConfig
Definition: eclipseclp_config.h:30
fawkes::EclExternalConfig::instance
static EclExternalConfig * instance()
Get the EclExternalConfig instance.
Definition: eclipseclp_config.cpp:87
fawkes::EclExternalConfig::create_initial_object
static void create_initial_object(Configuration *config)
Creates the initial EclExternalConfig object.
Definition: eclipseclp_config.cpp:78
fawkes::Configuration::is_string
virtual bool is_string(const char *path)=0
fawkes::Exception
Definition: exception.h:41