Warning, /include/Geant4/tools/num2s is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_num2s
0005 #define tools_num2s
0006
0007 // write numerics in a string as if done with a std::ostringstream::operator<<(<num>).
0008
0009 #include "sprintf"
0010 #include "typedefs"
0011
0012 #include <cstddef> //ptrdiff_t
0013
0014 namespace tools {
0015
0016 inline bool num2s(unsigned short a_value,std::string& a_s) { //used in value
0017 return print2s(a_s,32,"%u",a_value);
0018 }
0019
0020 inline bool num2s(short a_value,std::string& a_s){ //used in value
0021 return print2s(a_s,32,"%d",a_value);
0022 }
0023
0024 inline bool num2s(unsigned int a_value,std::string& a_s) {
0025 return print2s(a_s,32,"%u",a_value);
0026 }
0027
0028 inline bool num2s(int a_value,std::string& a_s){
0029 return print2s(a_s,32,"%d",a_value);
0030 }
0031
0032 inline bool num2s(uint64 a_value,std::string& a_s) {
0033 return print2s(a_s,32,uint64_format(),a_value);
0034 }
0035
0036 inline bool num2s(int64 a_value,std::string& a_s){
0037 return print2s(a_s,32,int64_format(),a_value);
0038 }
0039
0040 inline bool num2s(float a_value,std::string& a_s){
0041 return print2s(a_s,32,"%g",a_value);
0042 }
0043
0044 inline bool num2s(double a_value,std::string& a_s){
0045 return print2s(a_s,32,"%g",a_value);
0046 }
0047
0048 template <class T>
0049 inline bool numas(const T& a_value,std::string& a_s){
0050 std::string stmp;
0051 if(!num2s(a_value,stmp)) return false;
0052 a_s += stmp;
0053 return true;
0054 }
0055
0056 // for the below std::vector num2s in case T=std::string :
0057 inline bool num2s(const std::string& a_value,std::string& a_s){a_s = a_value;return true;}
0058
0059 template <class T>
0060 class num_out : public std::string {
0061 typedef std::string parent;
0062 public:
0063 num_out(const T& a_value) {
0064 parent::operator+=("\"");
0065 if(!numas(a_value,*this)) {} //throw
0066 parent::operator+=("\"");
0067 }
0068 public:
0069 num_out(const num_out& a_from):parent(a_from){}
0070 num_out& operator=(const num_out& a_from){parent::operator=(a_from);return *this;}
0071 };
0072
0073 }
0074
0075 #include <vector>
0076
0077 namespace tools {
0078
0079 template <class VEC>
0080 inline bool nums2s(const VEC& a_vals,std::string& a_s,const std::string& a_sep = "\n",bool a_sep_at_end = false) {
0081 a_s.clear();
0082 typename VEC::size_type number = a_vals.size();
0083 if(number<=0) return true; //it is ok.
0084 number--;
0085 std::string stmp;
0086 bool status = true;
0087 for(typename VEC::size_type index=0;index<number;index++) {
0088 if(!num2s(a_vals[index],stmp)) status = false; //continue.
0089 a_s += stmp;
0090 a_s += a_sep;
0091 }
0092 if(!num2s(a_vals[number],stmp)) status = false;
0093 a_s += stmp;
0094 if(a_sep_at_end) a_s += a_sep;
0095 return status;
0096 }
0097
0098 template <class T>
0099 inline bool nums2s(const std::vector<T>& a_vals,std::string& a_s,const std::string& a_sep = "\n",bool a_sep_at_end = false) {
0100 return nums2s< std::vector<T> >(a_vals,a_s,a_sep,a_sep_at_end);
0101 }
0102
0103 }
0104
0105 #endif