Source: ../../libxipc/xrl_args.hh


 
LOGO
 Annotated List  Files  Globals  Hierarchy  Index  Top
// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-

// Copyright (c) 2001-2008 XORP, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software")
// to deal in the Software without restriction, subject to the conditions
// listed in the XORP LICENSE file. These conditions include: you must
// preserve this copyright notice, and you cannot mention the copyright
// holders in advertising related to the Software without their permission.
// The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
// notice is a summary of the XORP LICENSE file; the license in that file is
// legally binding.

// $XORP: xorp/libxipc/xrl_args.hh,v 1.22 2008/07/23 05:10:44 pavlin Exp $

#ifndef __LIBXIPC_XRL_ARGS_HH__
#define __LIBXIPC_XRL_ARGS_HH__

#include "libxorp/xorp.h"
#include "libxorp/ipv4.hh"
#include "libxorp/ipv6.hh"
#include "libxorp/ipv4net.hh"
#include "libxorp/ipv6net.hh"
#include "libxorp/mac.hh"
#include "libxorp/exceptions.hh"

#include <list>

#include "xrl_atom.hh"


class XrlArgs {
public:
    typedef list<XrlAtom>::const_iterator const_iterator;
    typedef list<XrlAtom>::iterator iterator;

    // Exceptions
    struct BadArgs : public XorpException {
	BadArgs(const char* file, int line, const string& reason) :
	    XorpException("XrlArgs::BadArgs", file, line),
	_reason(reason) {}
	const string why() const { return (_reason); }
    private:
	string _reason;
    };
    class XrlAtomNotFound { };
    class XrlAtomFound { };

public:
    XrlArgs() {}
    explicit XrlArgs(const char* str) throw (InvalidString);

    ~XrlArgs() {}

    /* --- XrlAtom accessors --- */
    XrlArgs& add(const XrlAtom& xa) throw (XrlAtomFound);

    const XrlAtom& get(const XrlAtom& dataless) const throw (XrlAtomNotFound);

    void remove(const XrlAtom& dataless) throw (XrlAtomNotFound);

    /* --- bool accessors --- */

    XrlArgs& add_bool(const char* name, bool val) throw (XrlAtomFound);

    const bool_t& get_bool(const char* name) const throw (BadArgs);

    void remove_bool(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, bool v) throw (XrlAtomFound);

    void get(const char* n, bool& t) const throw (BadArgs);

    /* --- int32 accessors --- */

    XrlArgs& add_int32(const char* name, int32_t val) throw (XrlAtomFound);

    const int32_t& get_int32(const char* name) const throw (BadArgs);

    void remove_int32(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, int32_t v) throw (XrlAtomFound);

    void get(const char* n, int32_t& t) const throw (BadArgs);

    /* --- uint32 accessors --- */

    XrlArgs& add_uint32(const char* name, uint32_t v) throw (XrlAtomFound);

    const uint32_t& get_uint32(const char* name) const throw (BadArgs);

    void remove_uint32(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, uint32_t v) throw (XrlAtomFound);

    void get(const char* n, uint32_t& t) const throw (BadArgs);

    /* --- ipv4 accessors --- */

    XrlArgs& add_ipv4(const char* n, const IPv4& a) throw (XrlAtomFound);

    const IPv4& get_ipv4(const char* name) const throw (BadArgs);

    void remove_ipv4(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, const IPv4& a) throw (XrlAtomFound);

    void get(const char* n, IPv4& a) const throw (BadArgs);

    /* --- ipv4net accessors --- */

    XrlArgs& add_ipv4net(const char* n, const IPv4Net& a) throw (XrlAtomFound);

    const IPv4Net& get_ipv4net(const char* name) const throw (BadArgs);

    void remove_ipv4net(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, const IPv4Net& v) throw (XrlAtomFound);

    void get(const char* n, IPv4Net& t) const throw (BadArgs);

    /* --- ipv6 accessors --- */

    XrlArgs& add_ipv6(const char* name, const IPv6& addr) throw (XrlAtomFound);

    const IPv6& get_ipv6(const char* name) const throw (BadArgs);

    void remove_ipv6(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, const IPv6& a) throw (XrlAtomFound);

    void get(const char* n, IPv6& a) const throw (BadArgs);

    /* --- ipv6net accessors --- */

    XrlArgs& add_ipv6net(const char* name, const IPv6Net& addr)
	throw (XrlAtomFound);

    const IPv6Net& get_ipv6net(const char* name) const throw (BadArgs);

    void remove_ipv6net(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, const IPv6Net& a) throw (XrlAtomFound);

    void get(const char* n, IPv6Net& a) const throw (BadArgs);

    /* --- ipvx accessors --- */

    XrlArgs& add_ipvx(const char* name, const IPvX& ipvx) throw (XrlAtomFound);

    const IPvX get_ipvx(const char* name) const throw (BadArgs);

    void remove_ipvx(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, const IPvX& a) throw (XrlAtomFound);

    void get(const char* n, IPvX& a) const throw (BadArgs);

    /* --- ipvxnet accessors --- */

    XrlArgs& add_ipvxnet(const char* name, const IPvXNet& ipvxnet)
	throw (XrlAtomFound);

    const IPvXNet get_ipvxnet(const char* name) const throw (BadArgs);

    void remove_ipvxnet(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, const IPvXNet& a) throw (XrlAtomFound);

    void get(const char* n, IPvXNet& a) const throw (BadArgs);

    /* --- mac accessors --- */

    XrlArgs& add_mac(const char* name, const Mac& addr) throw (XrlAtomFound);

    const Mac& get_mac(const char* name) const throw (BadArgs);

    void remove_mac(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, const Mac& a) throw (XrlAtomFound);

    void get(const char* n, Mac& a) const throw (BadArgs);

    /* --- string accessors --- */

    XrlArgs& add_string(const char* name, const string& addr)
	throw (XrlAtomFound);

    const string& get_string(const char* name) const throw (BadArgs);

    void remove_string(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, const string& a) throw (XrlAtomFound);

    void get(const char* n, string& a) const throw (BadArgs);

    /* --- list accessors --- */

    XrlArgs& add_list(const char* name, const XrlAtomList& addr)
	throw (XrlAtomFound);

    const XrlAtomList& get_list(const char* name) const throw (BadArgs);

    void remove_list(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, const XrlAtomList& a) throw (XrlAtomFound);

    void get(const char* n, XrlAtomList& a) const throw (BadArgs);

    /* --- binary data accessors --- */
    XrlArgs& add_binary(const char* name, const vector<uint8_t>& addr)
	throw (XrlAtomFound);

    const vector<uint8_t>& get_binary(const char* name) const throw (BadArgs);

    void remove_binary(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, const vector<uint8_t>& a) throw (XrlAtomFound);

    void get(const char* n, vector<uint8_t>& a) const throw (BadArgs);

    /* --- int64 accessors --- */

    XrlArgs& add_int64(const char* name, int64_t v) throw (XrlAtomFound);

    const int64_t& get_int64(const char* name) const throw (BadArgs);

    void remove_int64(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, int64_t v) throw (XrlAtomFound);

    void get(const char* n, int64_t& t) const throw (BadArgs);

    /* --- uint64 accessors --- */

    XrlArgs& add_uint64(const char* name, uint64_t v) throw (XrlAtomFound);

    const uint64_t& get_uint64(const char* name) const throw (BadArgs);

    void remove_uint64(const char* name) throw (XrlAtomNotFound);

    XrlArgs& add(const char* n, uint64_t v) throw (XrlAtomFound);

    void get(const char* n, uint64_t& t) const throw (BadArgs);


    // ... Add your type's add, get, remove functions here ...

    // Append all atoms from an existing XrlArgs structure
    XrlArgs& add(const XrlArgs& args) throw (XrlAtomFound);

    // Equality testing
    bool matches_template(XrlArgs& t) const;
    bool operator==(const XrlArgs& t) const;

    // Accessor helpers
    size_t size() const;

    const XrlAtom& operator[](uint32_t index) const; // throw out_of_range
    const XrlAtom& item(uint32_t index) const;

    const XrlAtom& operator[](const string& name) const
	throw (XrlAtomNotFound);

    const XrlAtom& item(const string& name) const throw (XrlAtomNotFound);

    void push_front(const XrlAtom& xa);
    const XrlAtom& front() throw (XrlAtomNotFound);
    void pop_front() throw (XrlAtomNotFound);

    void push_back(const XrlAtom& xa);
    const XrlAtom& back() throw (XrlAtomNotFound);
    void pop_back() throw (XrlAtomNotFound);

    const_iterator begin() const	{ return _args.begin(); }
    const_iterator end() const		{ return _args.end(); }

    void clear()			{ _args.clear(); }
    bool empty()			{ return _args.empty(); }
    void swap(XrlArgs& xa)		{ _args.swap(xa._args); }

    /**
     * Get number of bytes needed to pack atoms contained within
     * instance.
     */
    size_t packed_bytes() const;

    /**
     * Pack contained atoms into a byte array.  The size of the byte
     * array should be larger or equal to the value returned by
     * packed_bytes().
     *
     * @param buffer buffer to receive data.
     * @param buffer_bytes size of buffer.
     * @return size of packed data on success, 0 on failure.
     */
    size_t pack(uint8_t* buffer, size_t buffer_bytes) const;

    /**
     * Unpack atoms from byte array into instance.  The atoms are
     * appended to the list of atoms associated with instance.
     *
     * @param buffer to read data from.
     * @param buffer_bytes size of buffer.  The size should exactly match
     *        number of bytes of packed atoms, ie packed_bytes() value
     *        used when packing.
     * @return number of bytes turned into atoms on success, 0 on failure.
     */
    size_t unpack(const uint8_t* buffer, size_t buffer_bytes);

    // String serialization methods
    string str() const;

protected:
    void check_not_found(const XrlAtom &xa) throw (XrlAtomFound);

    list<XrlAtom> _args;
};


// ----------------------------------------------------------------------------
// Inline methods

inline XrlArgs&
XrlArgs::add(const char* n, bool v) throw (XrlAtomFound)
{
    return add_bool(n, v);
}

inline void
XrlArgs::get(const char* n, bool& t) const throw (BadArgs)
{
    t = get_bool(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, int32_t v) throw (XrlAtomFound)
{
    return add_int32(n, v);
}

inline void
XrlArgs::get(const char* n, int32_t& t) const throw (BadArgs)
{
    t = get_int32(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, uint32_t v) throw (XrlAtomFound)
{
    return add_uint32(n, v);
}

inline void
XrlArgs::get(const char* n, uint32_t& t) const throw (BadArgs)
{
    t = get_uint32(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, const IPv4& a) throw (XrlAtomFound)
{
    return add_ipv4(n, a);
}

inline void
XrlArgs::get(const char* n, IPv4& a) const throw (BadArgs)
{
    a = get_ipv4(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, const IPv4Net& v) throw (XrlAtomFound)
{
    return add_ipv4net(n, v);
}

inline void
XrlArgs::get(const char* n, IPv4Net& t) const throw (BadArgs)
{
    t = get_ipv4net(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, const IPv6& a) throw (XrlAtomFound)
{
    return add_ipv6(n, a);
}

inline void
XrlArgs::get(const char* n, IPv6& a) const throw (BadArgs)
{
    a = get_ipv6(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, const IPv6Net& a) throw (XrlAtomFound)
{
    return add_ipv6net(n, a);
}

inline void
XrlArgs::get(const char* n, IPv6Net& a) const throw (BadArgs)
{
    a = get_ipv6net(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, const IPvX& a) throw (XrlAtomFound)
{
    return add_ipvx(n, a);
}

inline void
XrlArgs::get(const char* n, IPvX& a) const throw (BadArgs)
{
    a = get_ipvx(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, const IPvXNet& a) throw (XrlAtomFound)
{
    return add_ipvxnet(n, a);
}

inline void
XrlArgs::get(const char* n, IPvXNet& a) const throw (BadArgs)
{
    a = get_ipvxnet(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, const Mac& a) throw (XrlAtomFound)
{
    return add_mac(n, a);
}

inline void
XrlArgs::get(const char* n, Mac& a) const throw (BadArgs)
{
    a = get_mac(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, const string& a) throw (XrlAtomFound)
{
    return add_string(n, a);
}

inline void
XrlArgs::get(const char* n, string& a) const throw (BadArgs)
{
    a = get_string(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, const XrlAtomList& a) throw (XrlAtomFound)
{
    return add_list(n, a);
}

inline void
XrlArgs::get(const char* n, XrlAtomList& a) const throw (BadArgs)
{
    a = get_list(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, const vector<uint8_t>& a) throw (XrlAtomFound)
{
    return add_binary(n, a);
}

inline void
XrlArgs::get(const char* n, vector<uint8_t>& a) const throw (BadArgs)
{
    a = get_binary(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, int64_t v) throw (XrlAtomFound)
{
    return add_int64(n, v);
}

inline void
XrlArgs::get(const char* n, int64_t& t) const throw (BadArgs)
{
    t = get_int64(n);
}

inline XrlArgs&
XrlArgs::add(const char* n, uint64_t v) throw (XrlAtomFound)
{
    return add_uint64(n, v);
}

inline void
XrlArgs::get(const char* n, uint64_t& t) const throw (BadArgs)
{
    t = get_uint64(n);
}

inline const XrlAtom&
XrlArgs::item(uint32_t index) const
{
    return operator[](index);
}

inline const XrlAtom&
XrlArgs::item(const string& name) const throw (XrlAtomNotFound)
{
    return operator[](name);
}

inline void
XrlArgs::push_front(const XrlAtom& xa)
{
    _args.push_front(xa);
}

inline const XrlAtom&
XrlArgs::front() throw (XrlAtomNotFound)
{
    if (_args.empty())
	throw XrlAtomNotFound();
    return _args.front();
}

inline void
XrlArgs::pop_front() throw (XrlAtomNotFound)
{
    if (_args.empty())
	throw XrlAtomNotFound();
    _args.pop_front();
}

inline void
XrlArgs::push_back(const XrlAtom& xa)
{
    _args.push_back(xa);
}

inline const XrlAtom&
XrlArgs::back() throw (XrlAtomNotFound)
{
    if (_args.empty())
	throw XrlAtomNotFound();
    return _args.back();
}

inline void
XrlArgs::pop_back() throw (XrlAtomNotFound)
{
    if (_args.empty())
	throw XrlAtomNotFound();
    _args.pop_back();
}

#endif // __LIBXIPC_XRL_ARGS_HH__

Generated by: bms on anglepoise.lon.incunabulum.net on Wed Jul 23 10:05:36 2008, using kdoc 2.0a54+XORP.