mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
Enhancement - Record accessors
- Implemented a variety of get/set routines for Record.
This commit is contained in:
parent
0eff6fa2b1
commit
2e5e20e3e5
4 changed files with 63 additions and 24 deletions
|
@ -25,6 +25,8 @@
|
|||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <sstream>
|
||||
#include "util.h"
|
||||
#include "Record.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -63,3 +65,56 @@ void Record::parse (const std::string& input)
|
|||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::vector <Att> Record::all ()
|
||||
{
|
||||
std::vector <Att> all;
|
||||
foreach (a, mAtts)
|
||||
all.push_back (a->second);
|
||||
|
||||
return all;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
const std::string Record::get (const std::string& name)
|
||||
{
|
||||
if (mAtts.find (name) != mAtts.end ())
|
||||
return mAtts[name].value ();
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int Record::getInt (const std::string& name)
|
||||
{
|
||||
if (mAtts.find (name) != mAtts.end ())
|
||||
return ::atoi (mAtts[name].value ().c_str ());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void Record::set (const std::string& name, const std::string& value)
|
||||
{
|
||||
mAtts[name] = Att (name, value);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void Record::set (const std::string& name, int value)
|
||||
{
|
||||
std::stringstream svalue;
|
||||
svalue << value;
|
||||
|
||||
mAtts[name] = Att (name, svalue.str ());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void Record::remove (const std::string& name)
|
||||
{
|
||||
std::map <std::string, Att> copy = mAtts;
|
||||
mAtts.clear ();
|
||||
foreach (i, copy)
|
||||
if (i->first != name)
|
||||
mAtts[i->first] = i->second;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
16
src/Record.h
16
src/Record.h
|
@ -28,6 +28,7 @@
|
|||
#define INCLUDED_RECORD
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "Att.h"
|
||||
|
||||
class Record
|
||||
|
@ -42,16 +43,15 @@ public:
|
|||
virtual std::string composeCSV () = 0;
|
||||
void parse (const std::string&);
|
||||
|
||||
/*
|
||||
void getAttributes (std::map<std::string, std::string>&);
|
||||
const std::string getAttribute (const std::string&);
|
||||
void setAttribute (const std::string&, const std::string&);
|
||||
void setAttributes (const std::map <std::string, std::string>&);
|
||||
void removeAttribute (const std::string&);
|
||||
*/
|
||||
std::vector <Att> all ();
|
||||
const std::string get (const std::string&);
|
||||
int getInt (const std::string&);
|
||||
void set (const std::string&, const std::string&);
|
||||
void set (const std::string&, int);
|
||||
void remove (const std::string&);
|
||||
|
||||
private:
|
||||
std::vector <Att> mAtts;
|
||||
std::map <std::string, Att> mAtts;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
15
src/T.cpp
15
src/T.cpp
|
@ -215,21 +215,6 @@ void T::setAttribute (const std::string& name, const std::string& value)
|
|||
mAttributes[name] = value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void T::setAttributes (const std::map <std::string, std::string>& attributes)
|
||||
{
|
||||
foreach (i, attributes)
|
||||
{
|
||||
if (i->first.find (' ') != std::string::npos)
|
||||
throw std::string ("An attribute name may not contain spaces");
|
||||
|
||||
if (i->second.find (' ') != std::string::npos)
|
||||
throw std::string ("An attribute value may not contain spaces");
|
||||
|
||||
mAttributes[i->first] = i->second;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void T::removeAttribute (const std::string& name)
|
||||
{
|
||||
|
|
1
src/T.h
1
src/T.h
|
@ -77,7 +77,6 @@ public:
|
|||
void getAttributes (std::map<std::string, std::string>&);
|
||||
const std::string getAttribute (const std::string&);
|
||||
void setAttribute (const std::string&, const std::string&);
|
||||
void setAttributes (const std::map <std::string, std::string>&);
|
||||
void removeAttribute (const std::string&);
|
||||
|
||||
void getAnnotations (std::map <time_t, std::string>&) const;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue