|
xorp
|
00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*- 00002 // vim:set sts=4 ts=8: 00003 00004 // Copyright (c) 2001-2011 XORP, Inc and Others 00005 // 00006 // This program is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU Lesser General Public License, Version 00008 // 2.1, June 1999 as published by the Free Software Foundation. 00009 // Redistribution and/or modification of this program under the terms of 00010 // any other version of the GNU Lesser General Public License is not 00011 // permitted. 00012 // 00013 // This program is distributed in the hope that it will be useful, but 00014 // WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details, 00016 // see the GNU Lesser General Public License, Version 2.1, a copy of 00017 // which can be found in the XORP LICENSE.lgpl file. 00018 // 00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA; 00020 // http://xorp.net 00021 00022 00023 #ifndef __LIBXIPC_XRL_ARGS_HH__ 00024 #define __LIBXIPC_XRL_ARGS_HH__ 00025 00026 #include "libxorp/xorp.h" 00027 #include "libxorp/ipv4.hh" 00028 #include "libxorp/ipv6.hh" 00029 #include "libxorp/ipv4net.hh" 00030 #include "libxorp/ipv6net.hh" 00031 #include "libxorp/mac.hh" 00032 #include "libxorp/exceptions.hh" 00033 00034 #include "xrl_atom.hh" 00035 00036 00037 class XrlArgs { 00038 public: 00039 typedef vector<XrlAtom> ATOMS; 00040 typedef ATOMS::const_iterator const_iterator; 00041 typedef ATOMS::iterator iterator; 00042 00043 // Exceptions 00044 struct BadArgs : public XorpException { 00045 BadArgs(const char* file, int line, const string& reason) : 00046 XorpException("XrlArgs::BadArgs", file, line), 00047 _reason(reason) {} 00048 const string why() const { return (_reason); } 00049 private: 00050 string _reason; 00051 }; 00052 class XrlAtomNotFound { }; 00053 class XrlAtomFound { }; 00054 00055 public: 00056 XrlArgs() : _have_name(false) {} 00057 explicit XrlArgs(const char* str) throw (InvalidString); 00058 00059 ~XrlArgs() {} 00060 00061 /* --- XrlAtom accessors --- */ 00062 XrlArgs& add(const XrlAtom& xa) throw (XrlAtomFound); 00063 00064 const XrlAtom& get(const XrlAtom& dataless) const throw (XrlAtomNotFound); 00065 const XrlAtom& get(unsigned idx, const char* name) const 00066 throw (XrlAtomNotFound); 00067 00068 void remove(const XrlAtom& dataless) throw (XrlAtomNotFound); 00069 00070 /* --- bool accessors --- */ 00071 00072 XrlArgs& add_bool(const char* name, bool val) throw (XrlAtomFound); 00073 00074 const bool_t& get_bool(const char* name) const throw (BadArgs); 00075 00076 void remove_bool(const char* name) throw (XrlAtomNotFound); 00077 00078 XrlArgs& add(const char* n, bool v) throw (XrlAtomFound); 00079 00080 void get(const char* n, bool& t) const throw (BadArgs); 00081 00082 /* --- int32 accessors --- */ 00083 00084 XrlArgs& add_int32(const char* name, int32_t val) throw (XrlAtomFound); 00085 00086 const int32_t& get_int32(const char* name) const throw (BadArgs); 00087 00088 void remove_int32(const char* name) throw (XrlAtomNotFound); 00089 00090 XrlArgs& add(const char* n, int32_t v) throw (XrlAtomFound); 00091 00092 void get(const char* n, int32_t& t) const throw (BadArgs); 00093 00094 /* --- uint32 accessors --- */ 00095 00096 XrlArgs& add_uint32(const char* name, uint32_t v) throw (XrlAtomFound); 00097 00098 const uint32_t& get_uint32(const char* name) const throw (BadArgs); 00099 00100 void remove_uint32(const char* name) throw (XrlAtomNotFound); 00101 00102 XrlArgs& add(const char* n, uint32_t v) throw (XrlAtomFound); 00103 00104 void get(const char* n, uint32_t& t) const throw (BadArgs); 00105 00106 /* --- ipv4 accessors --- */ 00107 00108 XrlArgs& add_ipv4(const char* n, const IPv4& a) throw (XrlAtomFound); 00109 00110 const IPv4& get_ipv4(const char* name) const throw (BadArgs); 00111 00112 void remove_ipv4(const char* name) throw (XrlAtomNotFound); 00113 00114 XrlArgs& add(const char* n, const IPv4& a) throw (XrlAtomFound); 00115 00116 void get(const char* n, IPv4& a) const throw (BadArgs); 00117 00118 /* --- ipv4net accessors --- */ 00119 00120 XrlArgs& add_ipv4net(const char* n, const IPv4Net& a) throw (XrlAtomFound); 00121 00122 const IPv4Net& get_ipv4net(const char* name) const throw (BadArgs); 00123 00124 void remove_ipv4net(const char* name) throw (XrlAtomNotFound); 00125 00126 XrlArgs& add(const char* n, const IPv4Net& v) throw (XrlAtomFound); 00127 00128 void get(const char* n, IPv4Net& t) const throw (BadArgs); 00129 00130 /* --- ipv6 accessors --- */ 00131 00132 XrlArgs& add_ipv6(const char* name, const IPv6& addr) throw (XrlAtomFound); 00133 00134 const IPv6& get_ipv6(const char* name) const throw (BadArgs); 00135 00136 void remove_ipv6(const char* name) throw (XrlAtomNotFound); 00137 00138 XrlArgs& add(const char* n, const IPv6& a) throw (XrlAtomFound); 00139 00140 void get(const char* n, IPv6& a) const throw (BadArgs); 00141 00142 /* --- ipv6net accessors --- */ 00143 00144 XrlArgs& add_ipv6net(const char* name, const IPv6Net& addr) 00145 throw (XrlAtomFound); 00146 00147 const IPv6Net& get_ipv6net(const char* name) const throw (BadArgs); 00148 00149 void remove_ipv6net(const char* name) throw (XrlAtomNotFound); 00150 00151 XrlArgs& add(const char* n, const IPv6Net& a) throw (XrlAtomFound); 00152 00153 void get(const char* n, IPv6Net& a) const throw (BadArgs); 00154 00155 /* --- ipvx accessors --- */ 00156 00157 XrlArgs& add_ipvx(const char* name, const IPvX& ipvx) throw (XrlAtomFound); 00158 00159 const IPvX get_ipvx(const char* name) const throw (BadArgs); 00160 00161 void remove_ipvx(const char* name) throw (XrlAtomNotFound); 00162 00163 XrlArgs& add(const char* n, const IPvX& a) throw (XrlAtomFound); 00164 00165 void get(const char* n, IPvX& a) const throw (BadArgs); 00166 00167 /* --- ipvxnet accessors --- */ 00168 00169 XrlArgs& add_ipvxnet(const char* name, const IPvXNet& ipvxnet) 00170 throw (XrlAtomFound); 00171 00172 const IPvXNet get_ipvxnet(const char* name) const throw (BadArgs); 00173 00174 void remove_ipvxnet(const char* name) throw (XrlAtomNotFound); 00175 00176 XrlArgs& add(const char* n, const IPvXNet& a) throw (XrlAtomFound); 00177 00178 void get(const char* n, IPvXNet& a) const throw (BadArgs); 00179 00180 /* --- mac accessors --- */ 00181 00182 XrlArgs& add_mac(const char* name, const Mac& addr) throw (XrlAtomFound); 00183 00184 const Mac& get_mac(const char* name) const throw (BadArgs); 00185 00186 void remove_mac(const char* name) throw (XrlAtomNotFound); 00187 00188 XrlArgs& add(const char* n, const Mac& a) throw (XrlAtomFound); 00189 00190 void get(const char* n, Mac& a) const throw (BadArgs); 00191 00192 /* --- string accessors --- */ 00193 00194 XrlArgs& add_string(const char* name, const string& addr) 00195 throw (XrlAtomFound); 00196 00197 const string& get_string(const char* name) const throw (BadArgs); 00198 00199 void remove_string(const char* name) throw (XrlAtomNotFound); 00200 00201 XrlArgs& add(const char* n, const string& a) throw (XrlAtomFound); 00202 00203 void get(const char* n, string& a) const throw (BadArgs); 00204 00205 /* --- list accessors --- */ 00206 00207 XrlArgs& add_list(const char* name, const XrlAtomList& addr) 00208 throw (XrlAtomFound); 00209 00210 const XrlAtomList& get_list(const char* name) const throw (BadArgs); 00211 00212 void remove_list(const char* name) throw (XrlAtomNotFound); 00213 00214 XrlArgs& add(const char* n, const XrlAtomList& a) throw (XrlAtomFound); 00215 00216 void get(const char* n, XrlAtomList& a) const throw (BadArgs); 00217 00218 /* --- binary data accessors --- */ 00219 XrlArgs& add_binary(const char* name, const vector<uint8_t>& addr) 00220 throw (XrlAtomFound); 00221 00222 const vector<uint8_t>& get_binary(const char* name) const throw (BadArgs); 00223 00224 void remove_binary(const char* name) throw (XrlAtomNotFound); 00225 00226 XrlArgs& add(const char* n, const vector<uint8_t>& a) throw (XrlAtomFound); 00227 00228 void get(const char* n, vector<uint8_t>& a) const throw (BadArgs); 00229 00230 /* --- int64 accessors --- */ 00231 00232 XrlArgs& add_int64(const char* name, int64_t v) throw (XrlAtomFound); 00233 00234 const int64_t& get_int64(const char* name) const throw (BadArgs); 00235 00236 void remove_int64(const char* name) throw (XrlAtomNotFound); 00237 00238 XrlArgs& add(const char* n, int64_t v) throw (XrlAtomFound); 00239 00240 void get(const char* n, int64_t& t) const throw (BadArgs); 00241 00242 /* --- uint64 accessors --- */ 00243 00244 XrlArgs& add_uint64(const char* name, uint64_t v) throw (XrlAtomFound); 00245 00246 const uint64_t& get_uint64(const char* name) const throw (BadArgs); 00247 00248 void remove_uint64(const char* name) throw (XrlAtomNotFound); 00249 00250 XrlArgs& add(const char* n, uint64_t v) throw (XrlAtomFound); 00251 00252 void get(const char* n, uint64_t& t) const throw (BadArgs); 00253 00254 /* --- fp64 accessors --- */ 00255 00256 XrlArgs& add_fp64(const char* name, fp64_t v) throw (XrlAtomFound); 00257 00258 const fp64_t& get_fp64(const char* name) const throw (BadArgs); 00259 00260 void remove_fp64(const char* name) throw (XrlAtomNotFound); 00261 00262 XrlArgs& add(const char* n, fp64_t v) throw (XrlAtomFound); 00263 00264 void get(const char* n, fp64_t& t) const throw (BadArgs); 00265 00266 00267 // ... Add your type's add, get, remove functions here ... 00268 00269 // Append all atoms from an existing XrlArgs structure 00270 XrlArgs& add(const XrlArgs& args) throw (XrlAtomFound); 00271 00272 // Equality testing 00273 bool matches_template(XrlArgs& t) const; 00274 bool operator==(const XrlArgs& t) const; 00275 00276 // Accessor helpers 00277 size_t size() const; 00278 00279 const XrlAtom& operator[](uint32_t index) const; // throw out_of_range 00280 const XrlAtom& item(uint32_t index) const; 00281 00282 const XrlAtom& operator[](const string& name) const 00283 throw (XrlAtomNotFound); 00284 00285 const XrlAtom& item(const string& name) const throw (XrlAtomNotFound); 00286 00287 void push_back(const XrlAtom& xa); 00288 const XrlAtom& back() throw (XrlAtomNotFound); 00289 void pop_back() throw (XrlAtomNotFound); 00290 00291 const_iterator begin() const { return _args.begin(); } 00292 const_iterator end() const { return _args.end(); } 00293 00294 void clear() { _args.clear(); } 00295 bool empty() { return _args.empty(); } 00296 void swap(XrlArgs& xa) { _args.swap(xa._args); } 00297 00302 size_t packed_bytes(XrlAtom* head = NULL) const; 00303 00313 size_t pack(uint8_t* buffer, size_t buffer_bytes, 00314 XrlAtom* head = NULL) const; 00315 00326 size_t unpack(const uint8_t* buffer, size_t buffer_bytes, 00327 XrlAtom* head = NULL); 00328 00329 size_t fill(const uint8_t* buffer, size_t buffer_bytes); 00330 00331 static size_t unpack_header(uint32_t& cnt, const uint8_t* in, size_t len); 00332 00333 // String serialization methods 00334 string str() const; 00335 00336 template <class T> 00337 void set_arg(int idx, const T& arg); 00338 00339 protected: 00340 void check_not_found(const XrlAtom &xa) throw (XrlAtomFound); 00341 00342 ATOMS _args; 00343 bool _have_name; 00344 }; 00345 00346 00347 // ---------------------------------------------------------------------------- 00348 // Inline methods 00349 00350 inline XrlArgs& 00351 XrlArgs::add(const char* n, bool v) throw (XrlAtomFound) 00352 { 00353 return add_bool(n, v); 00354 } 00355 00356 inline void 00357 XrlArgs::get(const char* n, bool& t) const throw (BadArgs) 00358 { 00359 t = get_bool(n); 00360 } 00361 00362 inline XrlArgs& 00363 XrlArgs::add(const char* n, int32_t v) throw (XrlAtomFound) 00364 { 00365 return add_int32(n, v); 00366 } 00367 00368 inline void 00369 XrlArgs::get(const char* n, int32_t& t) const throw (BadArgs) 00370 { 00371 t = get_int32(n); 00372 } 00373 00374 inline XrlArgs& 00375 XrlArgs::add(const char* n, uint32_t v) throw (XrlAtomFound) 00376 { 00377 return add_uint32(n, v); 00378 } 00379 00380 inline void 00381 XrlArgs::get(const char* n, uint32_t& t) const throw (BadArgs) 00382 { 00383 t = get_uint32(n); 00384 } 00385 00386 inline XrlArgs& 00387 XrlArgs::add(const char* n, const IPv4& a) throw (XrlAtomFound) 00388 { 00389 return add_ipv4(n, a); 00390 } 00391 00392 inline void 00393 XrlArgs::get(const char* n, IPv4& a) const throw (BadArgs) 00394 { 00395 a = get_ipv4(n); 00396 } 00397 00398 inline XrlArgs& 00399 XrlArgs::add(const char* n, const IPv4Net& v) throw (XrlAtomFound) 00400 { 00401 return add_ipv4net(n, v); 00402 } 00403 00404 inline void 00405 XrlArgs::get(const char* n, IPv4Net& t) const throw (BadArgs) 00406 { 00407 t = get_ipv4net(n); 00408 } 00409 00410 inline XrlArgs& 00411 XrlArgs::add(const char* n, const IPv6& a) throw (XrlAtomFound) 00412 { 00413 return add_ipv6(n, a); 00414 } 00415 00416 inline void 00417 XrlArgs::get(const char* n, IPv6& a) const throw (BadArgs) 00418 { 00419 a = get_ipv6(n); 00420 } 00421 00422 inline XrlArgs& 00423 XrlArgs::add(const char* n, const IPv6Net& a) throw (XrlAtomFound) 00424 { 00425 return add_ipv6net(n, a); 00426 } 00427 00428 inline void 00429 XrlArgs::get(const char* n, IPv6Net& a) const throw (BadArgs) 00430 { 00431 a = get_ipv6net(n); 00432 } 00433 00434 inline XrlArgs& 00435 XrlArgs::add(const char* n, const IPvX& a) throw (XrlAtomFound) 00436 { 00437 return add_ipvx(n, a); 00438 } 00439 00440 inline void 00441 XrlArgs::get(const char* n, IPvX& a) const throw (BadArgs) 00442 { 00443 a = get_ipvx(n); 00444 } 00445 00446 inline XrlArgs& 00447 XrlArgs::add(const char* n, const IPvXNet& a) throw (XrlAtomFound) 00448 { 00449 return add_ipvxnet(n, a); 00450 } 00451 00452 inline void 00453 XrlArgs::get(const char* n, IPvXNet& a) const throw (BadArgs) 00454 { 00455 a = get_ipvxnet(n); 00456 } 00457 00458 inline XrlArgs& 00459 XrlArgs::add(const char* n, const Mac& a) throw (XrlAtomFound) 00460 { 00461 return add_mac(n, a); 00462 } 00463 00464 inline void 00465 XrlArgs::get(const char* n, Mac& a) const throw (BadArgs) 00466 { 00467 a = get_mac(n); 00468 } 00469 00470 inline XrlArgs& 00471 XrlArgs::add(const char* n, const string& a) throw (XrlAtomFound) 00472 { 00473 return add_string(n, a); 00474 } 00475 00476 inline void 00477 XrlArgs::get(const char* n, string& a) const throw (BadArgs) 00478 { 00479 a = get_string(n); 00480 } 00481 00482 inline XrlArgs& 00483 XrlArgs::add(const char* n, const XrlAtomList& a) throw (XrlAtomFound) 00484 { 00485 return add_list(n, a); 00486 } 00487 00488 inline void 00489 XrlArgs::get(const char* n, XrlAtomList& a) const throw (BadArgs) 00490 { 00491 a = get_list(n); 00492 } 00493 00494 inline XrlArgs& 00495 XrlArgs::add(const char* n, const vector<uint8_t>& a) throw (XrlAtomFound) 00496 { 00497 return add_binary(n, a); 00498 } 00499 00500 inline void 00501 XrlArgs::get(const char* n, vector<uint8_t>& a) const throw (BadArgs) 00502 { 00503 a = get_binary(n); 00504 } 00505 00506 inline XrlArgs& 00507 XrlArgs::add(const char* n, int64_t v) throw (XrlAtomFound) 00508 { 00509 return add_int64(n, v); 00510 } 00511 00512 inline void 00513 XrlArgs::get(const char* n, int64_t& t) const throw (BadArgs) 00514 { 00515 t = get_int64(n); 00516 } 00517 00518 inline XrlArgs& 00519 XrlArgs::add(const char* n, uint64_t v) throw (XrlAtomFound) 00520 { 00521 return add_uint64(n, v); 00522 } 00523 00524 inline void 00525 XrlArgs::get(const char* n, uint64_t& t) const throw (BadArgs) 00526 { 00527 t = get_uint64(n); 00528 } 00529 00530 inline XrlArgs& 00531 XrlArgs::add(const char* n, fp64_t v) throw (XrlAtomFound) 00532 { 00533 return add_fp64(n, v); 00534 } 00535 00536 inline void 00537 XrlArgs::get(const char* n, fp64_t& t) const throw (BadArgs) 00538 { 00539 t = get_fp64(n); 00540 } 00541 00542 inline const XrlAtom& 00543 XrlArgs::item(uint32_t index) const 00544 { 00545 return operator[](index); 00546 } 00547 00548 inline const XrlAtom& 00549 XrlArgs::item(const string& name) const throw (XrlAtomNotFound) 00550 { 00551 return operator[](name); 00552 } 00553 00554 inline void 00555 XrlArgs::push_back(const XrlAtom& xa) 00556 { 00557 _args.push_back(xa); 00558 } 00559 00560 inline const XrlAtom& 00561 XrlArgs::back() throw (XrlAtomNotFound) 00562 { 00563 if (_args.empty()) 00564 throw XrlAtomNotFound(); 00565 return _args.back(); 00566 } 00567 00568 inline void 00569 XrlArgs::pop_back() throw (XrlAtomNotFound) 00570 { 00571 if (_args.empty()) 00572 throw XrlAtomNotFound(); 00573 _args.pop_back(); 00574 } 00575 00576 template <class T> 00577 inline void 00578 XrlArgs::set_arg(int idx, const T& arg) 00579 { 00580 XrlAtom& a = _args[idx]; 00581 00582 a.set(arg); 00583 } 00584 00585 #endif // __LIBXIPC_XRL_ARGS_HH__