RepastHPC  2.3.1
Properties.h
1 /*
2  * Repast for High Performance Computing (Repast HPC)
3  *
4  * Copyright (c) 2010 Argonne National Laboratory
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the Argonne National Laboratory nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * Properties.h
36  *
37  * Created on: Sep 25, 2009
38  * Author: nick
39  */
40 
41 #ifndef PROPERTIES_H_
42 #define PROPERTIES_H_
43 
44 #include <fstream>
45 
46 
47 #include <iostream>
48 #include <map>
49 #include <string>
50 
51 #include <boost/iterator/transform_iterator.hpp>
52 #include <boost/mpi/communicator.hpp>
53 #include <boost/filesystem.hpp>
54 
55 #include "logger.h"
56 
57 #define MAX_PROP_FILE_SIZE 16384
58 
59 namespace repast {
60 
65 struct KeyGetter: public std::unary_function<std::map<std::string, std::string>::value_type, std::string> {
66  std::string operator()(const std::map<std::string, std::string>::value_type& value) const;
67 };
68 
77 class Properties {
78 
79 private:
80  std::map<std::string, std::string> map;
81 
82 public:
83 
84  typedef boost::transform_iterator<KeyGetter, std::map<std::string, std::string>::const_iterator> key_iterator;
85 
89  Properties();
90 
107  Properties(const std::string& file, boost::mpi::communicator* comm = 0, int maxPropFileSize = MAX_PROP_FILE_SIZE);
108 
134  Properties(const std::string& file, int argc, char** argv, boost::mpi::communicator* comm = 0, int maxPropFileSize = MAX_PROP_FILE_SIZE);
135 
144  Properties(int argc, char** argv);
145 
146  virtual ~Properties() {
147  }
148 
156  void putProperty(const std::string& key, std::string value);
157 
167  void putProperty(const std::string& key, long double value);
168 
169 
178  std::string getProperty(const std::string& key) const;
179 
185  bool contains(const std::string& key) const;
186 
192  key_iterator keys_begin() const {
193  return key_iterator(map.begin());
194  }
195 
201  key_iterator keys_end() const {
202  return key_iterator(map.end());
203  }
204 
218  void readFile(const std::string& file, boost::mpi::communicator* comm = 0, int maxPropFileSize = MAX_PROP_FILE_SIZE);
219 
229  void processCommandLineArguments(int argc, char **argv);
230 
236  int size() const {
237  return map.size();
238  }
239 
252  void log(std::string logName, std::vector<std::string> *keysToWrite = 0){
253  if(keysToWrite != 0){
254  std::vector<std::string>::iterator iter = keysToWrite->begin();
255  std::vector<std::string>::iterator iterEnd = keysToWrite->end();
256  while(iter != iterEnd){
257  Log4CL::instance()->get_logger(logName).log(INFO, (*iter) + " = " + (getProperty(*iter)));
258  iter++;
259  }
260  }
261  else{
262  std::map<std::string, std::string>::iterator iter = map.begin();
263  std::map<std::string, std::string>::iterator iterEnd = map.end();
264  while(iter != iterEnd){
265  Log4CL::instance()->get_logger(logName).log(INFO, iter->first + " = " + iter->second);
266  iter++;
267  }
268  }
269  }
270 
271 
279  void writeToSVFile(std::string fileName, std::string separator = ",");
280 
288  void writeToSVFile(std::string fileName, std::vector<std::string> &keysToWrite, std::string separator = ",");
289 
296  bool writeToPropsFile(std::string filename, std::string header = "");
297 
305  bool writeToPropsFile(std::string filename, std::vector<std::string> &keysToWrite, std::string header="");
306 
312  std::string propsFileString(std::vector<std::string> &keysToWrite, std::string header="");
313 
314 
315 };
316 
317 }
318 
319 #endif /* PROPERTIES_H_ */
repast::Properties::log
void log(std::string logName, std::vector< std::string > *keysToWrite=0)
Writes the contents of the properties file to the specified repast log (at 'INFO' log level)
Definition: Properties.h:252
repast::Properties::readFile
void readFile(const std::string &file, boost::mpi::communicator *comm=0, int maxPropFileSize=MAX_PROP_FILE_SIZE)
Adds any properties defined in the specified file.
Definition: Properties.cpp:98
repast::Properties::writeToPropsFile
bool writeToPropsFile(std::string filename, std::string header="")
Writes the contents of this properties object to a file that could be read as another properties file...
Definition: Properties.cpp:200
repast::Properties::size
int size() const
Gets the number of properties in this Properties.
Definition: Properties.h:236
repast::Properties::putProperty
void putProperty(const std::string &key, std::string value)
Puts a property into this Properties with the specified key and value.
Definition: Properties.cpp:78
repast::Properties::keys_end
key_iterator keys_end() const
Gets the end of an iterator over this Properties' keys.
Definition: Properties.h:201
repast::Properties::writeToSVFile
void writeToSVFile(std::string fileName, std::string separator=",")
Writes the contents of the properties file to the specified separated-value file.
Definition: Properties.cpp:158
repast::Properties::keys_begin
key_iterator keys_begin() const
Gets the start of an iterator over this Properties' keys.
Definition: Properties.h:192
repast::Properties::Properties
Properties()
Creates an empty Properties.
Definition: Properties.cpp:62
repast::Properties::contains
bool contains(const std::string &key) const
Gets whether or not this Properties contains the specified key.
Definition: Properties.cpp:86
repast::Properties::getProperty
std::string getProperty(const std::string &key) const
Gets the property with the specified key.
Definition: Properties.cpp:90
repast::Properties::propsFileString
std::string propsFileString(std::vector< std::string > &keysToWrite, std::string header="")
Gets the string that will be written to a properties file using the writeToPropsFile methods.
Definition: Properties.cpp:220
repast::Properties
Map type object that contains key, value(string) properties.
Definition: Properties.h:77
repast::KeyGetter
Unary function used in a transform_iterator that allows the map iterator to return the keys.
Definition: Properties.h:65
repast::Properties::processCommandLineArguments
void processCommandLineArguments(int argc, char **argv)
Processes a char** array of the given size; any component that has an equals sign is entered as a pro...
Definition: Properties.cpp:149