AggregPacking
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
utils.hpp
Go to the documentation of this file.
1 /*
2 *
3 * AggregPacking : Generator of random aggregate packings in concrete
4 *
5 * version 0.1 (2014-08-22)
6 *
7 * Copyright (C) 2014 Jan Stransky
8 *
9 * Czech Technical University, Faculty of Civil Engineering,
10 * Department of Structural Mechanics, 166 29 Prague, Czech Republic
11 *
12 * AggregPacking is free software: you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by the
14 * Free Software Foundation, either version 3 of the License, or (at your
15 * option) any later version.
16 *
17 * AggregPacking is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 */
25 
31 #ifndef UTILS_HPP
32 #define UTILS_HPP
33 
34 #include <stdexcept>
35 
36 #include <iostream>
37 #ifdef AGGREGPACKING_SERIALIZATION
38 #include <fstream>
39 #include <string>
40 using std::istream;
41 using std::ostream;
42 using std::ifstream;
43 using std::ofstream;
44 using std::string;
45 #endif
46 
47 namespace aggregpacking {
48 
49 /**********************************************************************
50 * LOG
51 **********************************************************************/
52 #ifdef AGGREGPACKING_DEBUG
53 #define LOG_DEBUG1(msg1) std::cout<<__FILE__<<": "<<__LINE__<<": "<<msg1<<"\n";
55 
57 #define LOG_DEBUG2(msg1,msg2) std::cout<<__FILE__<<": "<<__LINE__<<": "<<msg1<<" "<<msg2<<"\n";
58 
60 #define LOG_DEBUG3(msg1,msg2,msg3) std::cout<<__FILE__<<": "<<__LINE__<<": "<<msg1<<" "<<msg2<<" "<<msg3<<"\n";
61 
63 #define LOG_DEBUG4(msg1,msg2,msg3,msg4) std::cout<<__FILE__<<": "<<__LINE__<<": "<<msg1<<" "<<msg2<<" "<<msg3<<" "<<msg4<<"\n";
64 
66 #define LOG_DEBUG5(msg1,msg2,msg3,msg4,msg5) std::cout<<__FILE__<<": "<<__LINE__<<": "<<msg1<<" "<<msg2<<" "<<msg3<<" "<<msg4<<" "<<msg5<<"\n";
67 
69 #define LOG_DEBUG(msg) LOG_DEBUG1(msg)
70 
71 #else
72 #define LOG_DEBUG1(msg1)
74 #define LOG_DEBUG2(msg1,msg2)
75 #define LOG_DEBUG3(msg1,msg2,msg3)
76 #define LOG_DEBUG4(msg1,msg2,msg3,msg4)
77 #define LOG_DEBUG5(msg1,msg2,msg3,msg4,msg5)
78 #define LOG_DEBUG(msg)
79 #endif
81 
83 #define __S1(x) #x
84 #define __S2(x) __S1(x)
85 #define __AGGREGPACKING_CERR std::cerr<<__FILE__<<": "<<__LINE__<<": "
86 #define __AGGREGPACKING_THROW throw std::runtime_error(std::string(__FILE__)+": "+__S2(__LINE__))
87 
90 #define LOG_ERROR1(msg1) __AGGREGPACKING_CERR<<msg1<<"\n"; __AGGREGPACKING_THROW;
91 
93 #define LOG_ERROR2(msg1,msg2) __AGGREGPACKING_CERR<<msg1<<" "<<msg2<<"\n"; __AGGREGPACKING_THROW;
94 
96 #define LOG_ERROR3(msg1,msg2,msg3) __AGGREGPACKING_CERR<<msg1<<" "<<msg2<<" "<<msg3<<"\n"; __AGGREGPACKING_THROW;
97 
99 #define LOG_ERROR4(msg1,msg2,msg3,msg4) __AGGREGPACKING_CERR<<msg1<<" "<<msg2<<" "<<msg3<<" "<<msg4<<"\n"; __AGGREGPACKING_THROW;
100 
102 #define LOG_ERROR5(msg1,msg2,msg3,msg4,msg5) __AGGREGPACKING_CERR<<msg1<<" "<<msg2<<" "<<msg3<<" "<<msg4<<" "<<msg5<<"\n"; __AGGREGPACKING_THROW;
103 
105 #define LOG_ERROR(msg) LOG_ERROR1(msg)
106 
108 #define LOG_WARN1(msg1) __AGGREGPACKING_CERR<<msg1<<"\n";
109 
111 #define LOG_WARN2(msg1,msg2) __AGGREGPACKING_CERR<<msg1<<" "<<msg2<<"\n";
112 
114 #define LOG_WARN3(msg1,msg2,msg3) __AGGREGPACKING_CERR<<msg1<<" "<<msg2<<" "<<msg3<<"\n";
115 
117 #define LOG_WARN4(msg1,msg2,msg3,msg4) __AGGREGPACKING_CERR<<msg1<<" "<<msg2<<" "<<msg3<<" "<<msg4<<"\n";
118 
120 #define LOG_WARN5(msg1,msg2,msg3,msg4,msg5) __AGGREGPACKING_CERR<<msg1<<" "<<msg2<<" "<<msg3<<" "<<msg4<<" "<<msg5<<"\n";
121 
123 #define LOG_WARN(msg) LOG_WARN1(msg)
124 
125 
126 /**********************************************************************
127 * FILES
128 **********************************************************************/
129 #ifdef AGGREGPACKING_SERIALIZATION
130 
133 bool initFile(ofstream& f, const string& fName, const string& memoizeDb);
134 
136 
138 bool initFile(ifstream& f, const string& fName, const string& memoizeDb);
139 
141 template <typename T>
142 void saveObject(const T& t, const string& fName, const string& memoizeDb) {
143  ofstream f;
144  if (!initFile(f,fName,memoizeDb)) return;
145  f << t;
146  f.close();
147 }
148 
150 template <typename T>
151 bool loadObject(T& t, const string& fName, const string& memoizeDb) {
152  ifstream f;
153  if (!initFile(f,fName,memoizeDb)) return false;
154  string s;
155  f >> s;
156  if (s != t.getClassName()) { LOG_ERROR2(t.getClassName(),"wrong indentification class name"); }
157  f >> t;
158  f.close();
159  return true;
160 }
161 #endif
162 
163 } // namespace
164 #endif
#define LOG_ERROR2(msg1, msg2)
Error log taking 2 argument.
Definition: utils.hpp:93
void saveObject(const T &t, const string &fName, const string &memoizeDb)
Auxiliary template function for saving objects to files.
Definition: utils.hpp:142
bool loadObject(T &t, const string &fName, const string &memoizeDb)
Auxiliary template function for lading objects from files.
Definition: utils.hpp:151
bool initFile(ofstream &f, const string &fName, const string &fileName)
Init file for ostream.
Definition: utils.cpp:39