blob: e29a5b3450b719dc4f9c80e92aff3f8e66690f61 [file] [log] [blame]
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2012-2013 University of California, Los Angeles
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
* Alexander Afanasyev <alexander.afanasyev@ucla.edu>
*/
#ifndef CCNX_NAME_H
#define CCNX_NAME_H
#include <boost/shared_ptr.hpp>
#include "ccnx-common.h"
namespace Ccnx {
class CcnxCharbuf;
typedef boost::shared_ptr<CcnxCharbuf> CcnxCharbufPtr;
// This class is mostly used in CcnxWrapper; users should not be directly using this class
// The main purpose of this class to is avoid manually create and destroy charbuf everytime
class CcnxCharbuf
{
public:
CcnxCharbuf();
CcnxCharbuf(ccn_charbuf *buf);
CcnxCharbuf(const CcnxCharbuf &other);
CcnxCharbuf(const void *buf, size_t length);
~CcnxCharbuf();
// expose internal data structure, use with caution!!
ccn_charbuf *
getBuf() { return m_buf; }
const ccn_charbuf *
getBuf() const { return m_buf; }
const unsigned char *
buf () const
{ return m_buf->buf; }
size_t
length () const
{ return m_buf->length; }
private:
void init(ccn_charbuf *buf);
protected:
ccn_charbuf *m_buf;
};
struct NameException:
virtual boost::exception, virtual std::exception {};
class Name
{
public:
Name();
Name(const std::string &name);
Name(const std::vector<Bytes> &comps);
Name(const Name &other);
Name(const unsigned char *data, const ccn_indexbuf *comps);
Name (const void *buf, const size_t length);
Name (const CcnxCharbuf &buf);
Name (const ccn_charbuf *buf);
virtual ~Name() {}
CcnxCharbufPtr
toCcnxCharbuf() const;
operator CcnxCharbufPtr () const { return toCcnxCharbuf (); }
Name &
appendComp(const Name &comp);
Name &
appendComp(const Bytes &comp);
Name &
appendComp(const std::string &compStr);
Name &
appendComp(const void *buf, size_t size);
/**
* Append int component
*
* Difference between this and appendComp call is that data is appended in network order
*
* Also, this function honors naming convention (%00 prefix is added)
*/
Name &
appendComp(uint64_t number);
template<class T>
Name &
operator ()(const T &comp) { return appendComp (comp); }
Name &
operator ()(const void *buf, size_t size) { return appendComp (buf, size); }
int
size() const {return m_comps.size();}
const Bytes &
getComp (int index) const;
inline const Bytes &
getCompFromBack (int index) const;
// return std::string format of the comp
// if all characters are printable, simply returns the string
// if not, print the bytes in hex string format
std::string
getCompAsString(int index) const;
uint64_t
getCompAsInt (int index) const;
inline std::string
getCompFromBackAsString(int index) const;
inline uint64_t
getCompFromBackAsInt (int index) const;
Name
getPartialName(int start, int n = -1) const;
inline Name
getPartialNameFromBack(int start, int n = -1) const;
std::string
toString() const;
Name &
operator=(const Name &other);
bool
operator==(const std::string &str) const;
bool
operator!=(const std::string &str) const;
friend Name
operator+(const Name &n1, const Name &n2);
private:
std::vector<Bytes> m_comps;
};
std::ostream&
operator <<(std::ostream &os, const Name &name);
bool
operator ==(const Name &n1, const Name &n2);
bool
operator !=(const Name &n1, const Name &n2);
bool
operator <(const Name &n1, const Name &n2);
std::string
Name::getCompFromBackAsString(int index) const
{
return getCompAsString (m_comps.size () - 1 - index);
}
uint64_t
Name::getCompFromBackAsInt (int index) const
{
return getCompAsInt (m_comps.size () - 1 - index);
}
Name
Name::getPartialNameFromBack(int start, int n/* = -1*/) const
{
return getPartialName (m_comps.size () - 1 - start, n);
}
const Bytes &
Name::getCompFromBack (int index) const
{
return getComp (m_comps.size () - 1 - index);
}
} // Ccnx
#endif