115 lines
2.2 KiB
Plaintext
115 lines
2.2 KiB
Plaintext
// Copyright (C) 2010, Guy Barrand. All rights reserved.
|
|
// See the file tools.license for terms.
|
|
|
|
#ifndef tools_tos
|
|
#define tools_tos
|
|
|
|
// Used in BatchLab/MemoryTuple and Rio_Tuple.
|
|
// We need something different than the to<T>
|
|
// to handle std::vector<> and bool.
|
|
|
|
#include "sprintf"
|
|
#include "charmanip"
|
|
#include "typedefs"
|
|
|
|
#include <vector>
|
|
|
|
namespace tools {
|
|
|
|
inline std::string tos(unsigned char a_value){
|
|
std::string s;
|
|
if(is_printable(a_value))
|
|
sprintf(s,32,"%c",a_value);
|
|
else
|
|
sprintf(s,32,"%d",a_value);
|
|
return s;
|
|
}
|
|
|
|
inline std::string tos(char a_value){
|
|
std::string s;
|
|
if(is_printable(a_value))
|
|
sprintf(s,32,"%c",a_value);
|
|
else
|
|
sprintf(s,32,"%d",a_value);
|
|
return s;
|
|
}
|
|
|
|
inline std::string tos(unsigned short a_value) {
|
|
std::string s;
|
|
sprintf(s,32,"%d",a_value);
|
|
return s;
|
|
}
|
|
|
|
inline std::string tos(short a_value){
|
|
std::string s;
|
|
sprintf(s,32,"%d",a_value);
|
|
return s;
|
|
}
|
|
|
|
inline std::string tos(unsigned int a_value) {
|
|
std::string s;
|
|
sprintf(s,32,"%u",a_value);
|
|
return s;
|
|
}
|
|
|
|
inline std::string tosx(unsigned int a_value) {
|
|
std::string s;
|
|
sprintf(s,32,"%x",a_value);
|
|
return s;
|
|
}
|
|
|
|
inline std::string tos(int a_value){
|
|
std::string s;
|
|
sprintf(s,32,"%d",a_value);
|
|
return s;
|
|
}
|
|
|
|
inline std::string tos(uint64 a_value) {
|
|
std::string s;
|
|
sprintf(s,32,uint64_format(),a_value);
|
|
return s;
|
|
}
|
|
|
|
inline std::string tos(int64 a_value){
|
|
std::string s;
|
|
sprintf(s,32,int64_format(),a_value);
|
|
return s;
|
|
}
|
|
|
|
inline std::string tos(float a_value){
|
|
std::string s;
|
|
sprintf(s,32,"%g",a_value);
|
|
return s;
|
|
}
|
|
|
|
inline std::string tos(double a_value){
|
|
std::string s;
|
|
sprintf(s,32,"%g",a_value);
|
|
return s;
|
|
}
|
|
|
|
inline std::string tos(bool a_value){
|
|
return std::string(a_value?"true":"false");
|
|
}
|
|
|
|
inline std::string tos(const std::string& a_value){return a_value;}
|
|
|
|
template <class T>
|
|
inline std::string tos(const std::vector<T>& a_value,
|
|
const std::string& a_sep = "\n" ) {
|
|
unsigned int number = a_value.size();
|
|
if(number<=0) return "";
|
|
std::string result;
|
|
number--;
|
|
for(unsigned int index=0;index<number;index++) {
|
|
result += tos(a_value[index]);
|
|
result += a_sep;
|
|
}
|
|
result += tos(a_value[number]);
|
|
return result;
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|