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

View File

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

View File

@ -9,34 +9,40 @@
#include <map>
#include <iostream>
#include <sstream>
namespace CPPCOMMON
{
using namespace std;
inline string jsonMPSS(const map<string, string>& mpss)
{
if(mpss.empty())
template<typename T1, typename T2>
string mapToString(const map<T1, T2>& mp)
{
return "{}";
}
string res("{");
map<string, string>::const_iterator it = mpss.begin();
res += it->first;
res += ": ";
res += it->second;
it++;
while(it != mpss.end())
{
res += ", ";
res += it->first;
res += ": ";
res += it->second;
if(mp.empty())
{
return "{}";
}
stringstream ss;
ss<<'{';
typename map<T1, T2>::const_iterator it = mp.begin();
ss<<it->first<<": "<<it->second;
it++;
while(it != mp.end())
{
ss<<", "<<it->first<<": "<<it->second;
it++;
}
ss<<"}";
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();
}
res += "}";
return res;
}
template<class kT, class vT>
void printMap(const map<kT, vT>& mp)