reorganize sources

This commit is contained in:
Eduardo Bart
2011-08-15 11:06:15 -03:00
parent 4e03b15b27
commit d8cc37afdb
113 changed files with 621 additions and 3437 deletions

View File

@@ -1,194 +0,0 @@
#ifndef AUXILIARY_H
#define AUXILIARY_H
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <exception>
#include <cxxabi.h>
/// Namespace that contains auxiliary functions and templates
namespace aux {
/// Fill an ostream by concatenating args
/// Usage:
/// aux::fill_ostream(stream, a1, a2, ..., aN);
inline void fill_ostream(std::ostringstream&) { }
template<class T, class... Args>
void fill_ostream(std::ostringstream& stream, const T& first, const Args&... rest) {
stream << first;
fill_ostream(stream, rest...);
}
/// Makes a std::string by concatenating args
/// Usage:
/// std::string str = aux::make_string(a1, a2, ..., aN);
template<class... T>
std::string make_string(const T&... args) {
std::ostringstream buf;
fill_ostream(buf, args...);
return buf.str();
}
// used by dumper
struct dump_util {
~dump_util() { std::cout << std::endl; }
template<class T>
dump_util& operator<<(const T& v) {
std::cout << v << " ";
return *this;
}
};
/// Utility for dumping variables
/// Usage:
/// aux::dump << v1, v2, ..., vN;
static const struct dumper {
dumper() { }
template<class T>
dump_util operator<<(const T& v) const {
dump_util d;
d << v;
return d;
}
} dump;
/// Utility for printing messages into stdout
/// Usage:
/// aux::print(v1, v2, ..., vN);
template<class... T>
void print(const T&... args) {
std::ostringstream buf;
fill_ostream(buf, args...);
std::cout << buf.str();
}
/// Same as aux::print but adds a new line at the end
template<class... T>
void println(const T&... args) {
print(args...);
std::cout << std::endl;
}
/// Demangle names for GNU g++ compiler
inline std::string demangle_name(const char* name) {
size_t len;
int status;
std::string ret;
char* demangled = abi::__cxa_demangle(name, 0, &len, &status);
if(demangled) {
ret = demangled;
free(demangled);
}
return ret;
}
/// Returns the name of a type
/// e.g. aux::demangle_type<Foo*>() returns a string containing 'Foo*'
template<typename T>
std::string demangle_type() {
return demangle_name(typeid(T).name());
}
/// Cast a type to another type
/// @return whether the conversion was successful or not
template<typename T, typename R>
bool cast(const T& in, R& out) {
std::stringstream ss;
ss << in;
ss >> out;
return !!ss && ss.eof();
}
// cast a type to string
template<typename T>
bool cast(const T& in, std::string& out) {
std::stringstream ss;
ss << in;
out = ss.str();
return true;
}
// cast string to string
template<>
inline bool cast(const std::string& in, std::string& out) {
out = in;
return true;
}
// special cast from string to boolean
template<>
inline bool cast(const std::string& in, bool& b) {
static std::string validNames[2][4] = {{"true","yes","on","1"}, {"false","no","off","0"}};
bool ret = false;
for(int i=0;i<4;++i) {
if(in == validNames[0][i]) {
b = true;
ret = true;
break;
} else if(in == validNames[1][i]) {
b = false;
ret = true;
break;
}
}
return ret;
}
// special cast from boolean to string
template<>
inline bool cast(const bool& in, std::string& out) {
out = (in ? "true" : "false");
return true;
}
// used by safe_cast
class bad_cast : public std::bad_cast {
public:
virtual ~bad_cast() throw() { }
template<class T, class R>
void setWhat() {
m_what = make_string("failed to cast value of type '", demangle_type<T>(),
"' to type '", demangle_type<R>(), "'");
}
virtual const char* what() { return m_what.c_str(); }
private:
std::string m_what;
};
/// Cast a type to another type, any error throws a aux::bad_cast_exception
/// Usage:
/// R r = aux::safe_cast<R>(t);
template<typename R, typename T>
R safe_cast(const T& t) {
R r;
if(!cast(t, r)) {
bad_cast e;
e.setWhat<T,R>();
throw e;
}
return r;
}
/// Cast a type to another type, cast errors are ignored
/// Usage:
/// R r = aux::unsafe_cast<R>(t);
template<typename R, typename T>
R unsafe_cast(const T& t) {
R r;
try {
r = safe_cast<R,T>(t);
} catch(bad_cast& e) {
println(e.what());
}
return r;
}
// an empty string to use anywhere needed
const static std::string empty_string;
}
// shortcut for aux::dump
const static aux::dumper& dump = aux::dump;
#endif

View File

@@ -1,8 +1,9 @@
#ifndef LOGGER_H
#define LOGGER_H
#include "auxiliary.h"
#include "tools.h"
//TODO: a real logger
class Logger
{
public:
@@ -17,16 +18,16 @@ public:
};
// specialized logging
#define logDebug(...) Logger::log(Logger::LogDebug, aux::make_string(__VA_ARGS__))
#define logInfo(...) Logger::log(Logger::LogInfo, aux::make_string(__VA_ARGS__))
#define logWarning(...) Logger::log(Logger::LogWarning, aux::make_string(__VA_ARGS__))
#define logError(...) Logger::log(Logger::LogError, aux::make_string(__VA_ARGS__))
#define logFatal(...) Logger::log(Logger::LogFatal, aux::make_string(__VA_ARGS__))
#define logDebug(...) Logger::log(Logger::LogDebug, fw::mkstr(__VA_ARGS__))
#define logInfo(...) Logger::log(Logger::LogInfo, fw::mkstr(__VA_ARGS__))
#define logWarning(...) Logger::log(Logger::LogWarning, fw::mkstr(__VA_ARGS__))
#define logError(...) Logger::log(Logger::LogError, fw::mkstr(__VA_ARGS__))
#define logFatal(...) Logger::log(Logger::LogFatal, fw::mkstr(__VA_ARGS__))
#define logTrace() Logger::log(Logger::LogDebug, "", __PRETTY_FUNCTION__)
#define logTraceDebug(...) Logger::log(Logger::LogDebug, aux::make_string(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceInfo(...) Logger::log(Logger::LogInfo, aux::make_string(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceWarning(...) log(Logger::LogWarning, aux::make_string(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceError(...) Logger::log(Logger::LogError, aux::make_string(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceDebug(...) Logger::log(Logger::LogDebug, fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceInfo(...) Logger::log(Logger::LogInfo, fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceWarning(...) log(Logger::LogWarning, fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#define logTraceError(...) Logger::log(Logger::LogError, fw::mkstr(__VA_ARGS__), __PRETTY_FUNCTION__)
#endif

View File

@@ -58,8 +58,7 @@ typedef TPoint<float> PointF;
template<class T>
std::ostream& operator<<(std::ostream& out, const TPoint<T>& point)
{
out << "Point(" << point.x << ","
<< point.y << ")";
out << point.x << " " << point.y;
return out;
}

195
src/framework/util/tools.h Normal file
View File

@@ -0,0 +1,195 @@
#ifndef TOOLS_H
#define TOOLS_H
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <exception>
#include <cxxabi.h>
namespace fw {
/// Fill an ostream by concatenating args
/// Usage:
/// fw::fill_ostream(stream, a1, a2, ..., aN);
inline void fill_ostream(std::ostringstream&) { }
template<class T, class... Args>
void fill_ostream(std::ostringstream& stream, const T& first, const Args&... rest) {
stream << first;
fill_ostream(stream, rest...);
}
/// Makes a std::string by concatenating args
/// Usage:
/// std::string str = fw::mkstr(a1, a2, ..., aN);
template<class... T>
std::string mkstr(const T&... args) {
std::ostringstream buf;
fill_ostream(buf, args...);
return buf.str();
}
// used by dumper
struct dump_util {
~dump_util() { std::cout << std::endl; }
template<class T>
dump_util& operator<<(const T& v) {
std::cout << v << " ";
return *this;
}
};
/// Utility for dumping variables
/// Usage:
/// fw::dump << v1, v2, ..., vN;
struct dumper {
dumper() { }
template<class T>
dump_util operator<<(const T& v) const {
dump_util d;
d << v;
return d;
}
};
/// Utility for printing messages into stdout
/// Usage:
/// fw::print(v1, v2, ..., vN);
template<class... T>
void print(const T&... args) {
std::ostringstream buf;
fill_ostream(buf, args...);
std::cout << buf.str();
}
/// Same as fw::print but adds a new line at the end
template<class... T>
void println(const T&... args) {
print(args...);
std::cout << std::endl;
}
/// Demangle names for GNU g++ compiler
inline std::string demangle_name(const char* name) {
size_t len;
int status;
std::string ret;
char* demangled = abi::__cxa_demangle(name, 0, &len, &status);
if(demangled) {
ret = demangled;
free(demangled);
}
return ret;
}
/// Returns the name of a type
/// e.g. fw::demangle_type<Foo*>() returns a string containing 'Foo*'
template<typename T>
std::string demangle_type() {
return demangle_name(typeid(T).name());
}
/// Cast a type to another type
/// @return whether the conversion was successful or not
template<typename T, typename R>
bool cast(const T& in, R& out) {
std::stringstream ss;
ss << in;
ss >> out;
return !!ss && ss.eof();
}
// cast a type to string
template<typename T>
bool cast(const T& in, std::string& out) {
std::stringstream ss;
ss << in;
out = ss.str();
return true;
}
// cast string to string
template<>
inline bool cast(const std::string& in, std::string& out) {
out = in;
return true;
}
// special cast from string to boolean
template<>
inline bool cast(const std::string& in, bool& b) {
static std::string validNames[2][4] = {{"true","yes","on","1"}, {"false","no","off","0"}};
bool ret = false;
for(int i=0;i<4;++i) {
if(in == validNames[0][i]) {
b = true;
ret = true;
break;
} else if(in == validNames[1][i]) {
b = false;
ret = true;
break;
}
}
return ret;
}
// special cast from boolean to string
template<>
inline bool cast(const bool& in, std::string& out) {
out = (in ? "true" : "false");
return true;
}
// used by safe_cast
class bad_cast : public std::bad_cast {
public:
virtual ~bad_cast() throw() { }
template<class T, class R>
void setWhat() {
m_what = mkstr("failed to cast value of type '", demangle_type<T>(),
"' to type '", demangle_type<R>(), "'");
}
virtual const char* what() { return m_what.c_str(); }
private:
std::string m_what;
};
/// Cast a type to another type, any error throws a fw::bad_cast_exception
/// Usage:
/// R r = fw::safe_cast<R>(t);
template<typename R, typename T>
R safe_cast(const T& t) {
R r;
if(!cast(t, r)) {
bad_cast e;
e.setWhat<T,R>();
throw e;
}
return r;
}
/// Cast a type to another type, cast errors are ignored
/// Usage:
/// R r = fw::unsafe_cast<R>(t);
template<typename R, typename T>
R unsafe_cast(const T& t) {
R r;
try {
r = safe_cast<R,T>(t);
} catch(bad_cast& e) {
println(e.what());
}
return r;
}
// an empty string to use anywhere needed
const static std::string empty_string;
}
// shortcut for fw::dump
const static fw::dumper dump;
#endif

View File

@@ -1,7 +1,7 @@
#include "translator.h"
#include <boost/algorithm/string.hpp>
AlignmentFlag parseAlignment(std::string aligment)
AlignmentFlag fw::translateAlignment(std::string aligment)
{
boost::to_lower(aligment);
boost::erase_all(aligment, " ");
@@ -25,7 +25,7 @@ AlignmentFlag parseAlignment(std::string aligment)
return AlignCenter;
}
AnchorPoint parseAnchorPoint(const std::string& anchorPoint)
AnchorPoint fw::translateAnchorPoint(const std::string& anchorPoint)
{
if(anchorPoint == "left")
return AnchorLeft;

View File

@@ -1,9 +1,14 @@
#ifndef TRANSLATOR_H
#define TRANSLATOR_H
#include <const.h>
#include "../const.h"
#include <string>
AlignmentFlag parseAlignment(std::string aligment);
AnchorPoint parseAnchorPoint(const std::string& anchorPoint);
namespace fw {
AlignmentFlag translateAlignment(std::string aligment);
AnchorPoint translateAnchorPoint(const std::string& anchorPoint);
};
#endif