update cppcommon

This commit is contained in:
gwdwyy 2013-08-18 23:06:18 +08:00
parent ff34095252
commit 8f06d1340a
3 changed files with 31 additions and 24 deletions

View File

@ -54,11 +54,11 @@ namespace CPPCOMMON
{ {
string res; string res;
res += string_format("[%s]\n", joinStr(_args, ", ").c_str()); res += string_format("[%s]\n", joinStr(_args, ", ").c_str());
res += jsonMPSS(_mpss); res += mapToString<string, string>(_mpss);
return res; return res;
} }
string ArgvContext::operator [](int i) string ArgvContext::operator [](uint i)
{ {
if(i < _args.size()) if(i < _args.size())
{ {
@ -93,6 +93,7 @@ int main(int argc, char** argv)
cout<<arg.toString()<<endl; cout<<arg.toString()<<endl;
cout<<arg[1]<<endl; cout<<arg[1]<<endl;
cout<<arg["--hehe"]<<endl; cout<<arg["--hehe"]<<endl;
cout<<pairToString<int,double>(pair<int, double>(1,1.2))<<endl;
return 0; return 0;
} }

View File

@ -20,7 +20,7 @@ namespace CPPCOMMON
~ArgvContext(); ~ArgvContext();
public: public:
string toString(); string toString();
string operator [](int i); string operator [](uint i);
string operator [](const string& key); string operator [](const string& key);
private: private:
vector<string> _args; vector<string> _args;

View File

@ -9,33 +9,39 @@
#include <map> #include <map>
#include <iostream> #include <iostream>
#include <sstream>
namespace CPPCOMMON namespace CPPCOMMON
{ {
using namespace std; using namespace std;
inline string jsonMPSS(const map<string, string>& mpss) template<typename T1, typename T2>
string mapToString(const map<T1, T2>& mp)
{ {
if(mpss.empty()) if(mp.empty())
{ {
return "{}"; return "{}";
} }
string res("{"); stringstream ss;
map<string, string>::const_iterator it = mpss.begin(); ss<<'{';
res += it->first; typename map<T1, T2>::const_iterator it = mp.begin();
res += ": "; ss<<it->first<<": "<<it->second;
res += it->second;
it++; it++;
while(it != mpss.end()) while(it != mp.end())
{ {
res += ", "; ss<<", "<<it->first<<": "<<it->second;
res += it->first;
res += ": ";
res += it->second;
it++; it++;
} }
res += "}"; ss<<"}";
return res; return ss.str();
}
template<typename T1, typename T2>
string pairToString(const pair<T1, T2>& p)
{
stringstream ss;
ss<<p.first<<":"<<p.second;
return ss.str();
} }
template<class kT, class vT> template<class kT, class vT>