00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef COMMAND_LINE_H
00021 #define COMMAND_LINE_H
00022
00023 #include <string>
00024 #include <sstream>
00025 #include <list>
00026
00027 #include "ns3/callback.h"
00028
00029 namespace ns3 {
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 class CommandLine
00045 {
00046 public:
00047 ~CommandLine ();
00048
00049
00050
00051
00052
00053
00054
00055
00056 template <typename T>
00057 void AddValue (const std::string &name,
00058 const std::string &help,
00059 T &value);
00060
00061
00062
00063
00064
00065
00066
00067
00068 void AddValue (const std::string &name,
00069 const std::string &help,
00070 Callback<bool, std::string> callback);
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 void Parse (int argc, char *argv[]) const;
00082 private:
00083 class Item
00084 {
00085 public:
00086 std::string m_name;
00087 std::string m_help;
00088 virtual ~Item ();
00089 virtual bool Parse (std::string value) = 0;
00090 };
00091 template <typename T>
00092 class UserItem : public Item
00093 {
00094 public:
00095 virtual bool Parse (std::string value);
00096 T *m_valuePtr;
00097 };
00098 class CallbackItem : public Item
00099 {
00100 public:
00101 virtual bool Parse (std::string value);
00102 Callback<bool, std::string> m_callback;
00103 };
00104
00105 void HandleArgument (std::string name, std::string value) const;
00106 void PrintHelp (void) const;
00107 void PrintGlobals (void) const;
00108 void PrintAttributes (std::string type) const;
00109 void PrintGroup (std::string group) const;
00110 void PrintTypeIds (void) const;
00111 void PrintGroups (void) const;
00112
00113 typedef std::list<Item *> Items;
00114 Items m_items;
00115 };
00116
00117 }
00118
00119 namespace ns3 {
00120
00121 template <typename T>
00122 void
00123 CommandLine::AddValue (const std::string &name,
00124 const std::string &help,
00125 T &value)
00126 {
00127 UserItem<T> *item = new UserItem<T> ();
00128 item->m_name = name;
00129 item->m_help = help;
00130 item->m_valuePtr = &value;
00131 m_items.push_back (item);
00132 }
00133
00134 template <typename T>
00135 bool
00136 CommandLine::UserItem<T>::Parse (std::string value)
00137 {
00138 std::istringstream iss;
00139 iss.str (value);
00140 iss >> (*m_valuePtr);
00141 return !iss.bad () && !iss.fail ();
00142 }
00143
00144 }
00145
00146 #endif