wibble  0.1.28
core.h
Go to the documentation of this file.
00001 #ifndef WIBBLE_COMMANDLINE_CORE_H
00002 #define WIBBLE_COMMANDLINE_CORE_H
00003 
00004 #include <wibble/exception.h>
00005 #include <string>
00006 #include <list>
00007 #include <set>
00008 
00009 namespace wibble {
00010 
00011 namespace exception {
00012 class BadOption : public Consistency
00013 {
00014     std::string m_error;
00015 
00016 public:
00017     BadOption(const std::string& error, const std::string& context = std::string("parsing commandline options")) throw ()
00018         : Consistency(context), m_error(error) {}
00019     ~BadOption() throw () {}
00020 
00021     virtual const char* type() const throw () { return "BadOption"; }
00022     virtual std::string desc() const throw () { return m_error; }
00023 
00024 };
00025 }
00026 
00027 namespace commandline {
00028 
00029 class ArgList : public std::list<std::string>
00030 {
00031 public:
00032     // Remove the item pointed by the iterator, and advance the iterator to the
00033     // next item.  Returns i itself.
00034     inline iterator& eraseAndAdvance(iterator& i)
00035     {
00036         if (i == end())
00037             return i;
00038         iterator next = i;
00039         ++next;
00040         erase(i);
00041         i = next;
00042         return i;
00043     }
00044 
00045     static bool isSwitch(const char* str);
00046     static bool isSwitch(const std::string& str);
00047     static bool isSwitch(const const_iterator& iter);
00048     static bool isSwitch(const iterator& iter);
00049 };
00050 
00051 class Managed
00052 {
00053 public:
00054     virtual ~Managed() {}
00055 };
00056 
00062 class MemoryManager
00063 {
00064     std::set<Managed*> components;
00065 
00066     Managed* addManaged(Managed* o) { components.insert(o); return o; }
00067 public:
00068     ~MemoryManager()
00069     {
00070         for (std::set<Managed*>::const_iterator i = components.begin();
00071                 i != components.end(); ++i)
00072             delete *i;
00073     }
00074 
00075     template<typename T>
00076     T* add(T* item) { addManaged(item); return item; }
00077 };
00078 
00079 }
00080 
00081 }
00082 
00083 // vim:set ts=4 sw=4:
00084 #endif