blob: 18240252c5bc6aae89f68ad06993ddc114b49628 [file] [log] [blame]
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080022#ifndef CCNX_COMMON_H
23#define CCNX_COMMON_H
24
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080025extern "C" {
26#include <ccn/ccn.h>
27#include <ccn/charbuf.h>
28#include <ccn/keystore.h>
29#include <ccn/uri.h>
30#include <ccn/bloom.h>
31#include <ccn/signing.h>
32}
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080033#include <vector>
34#include <boost/shared_ptr.hpp>
35#include <boost/exception/all.hpp>
36#include <boost/function.hpp>
37#include <string>
38#include <sstream>
39#include <map>
40#include <utility>
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080041#include <string.h>
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080042
43using namespace std;
44namespace Ccnx {
45typedef vector<unsigned char> Bytes;
Zhenkai Zhu9bad2bf2012-12-28 15:31:46 -080046typedef vector<string>Comps;
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080047
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080048typedef boost::shared_ptr<Bytes> BytesPtr;
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080049
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080050inline
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080051const unsigned char *
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080052head(const Bytes &bytes)
53{
54 return &bytes[0];
55}
56
57inline
58unsigned char *
59head (Bytes &bytes)
60{
61 return &bytes[0];
62}
63
64// --- Bytes operations start ---
65inline void
66readRaw(Bytes &bytes, const unsigned char *src, size_t len)
67{
68 if (len > 0)
69 {
70 bytes.resize(len);
71 memcpy (head (bytes), src, len);
72 }
73}
74
75inline BytesPtr
76readRawPtr (const unsigned char *src, size_t len)
77{
78 if (len > 0)
79 {
80 BytesPtr ret (new Bytes (len));
81 memcpy (head (*ret), src, len);
82
83 return ret;
84 }
85 else
86 return BytesPtr ();
87}
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080088
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080089template<class Msg>
90inline BytesPtr
91serializeMsg(const Msg &msg)
92{
93 int size = msg->ByteSize ();
94 BytesPtr bytes (new Bytes (size));
95 msg->SerializeToArray (head(*bytes), size);
96 return bytes;
97}
98
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080099template<class Msg>
100inline boost::shared_ptr<Msg>
101deserializeMsg (const Bytes &bytes)
102{
103 boost::shared_ptr<Msg> retval (new Msg ());
104 retval->ParseFromArray (head (bytes), bytes.size ());
105 return retval;
106}
107
Zhenkai Zhu9bad2bf2012-12-28 15:31:46 -0800108// --- Bytes operations end ---
109
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800110// Exceptions
111typedef boost::error_info<struct tag_errmsg, std::string> error_info_str;
Zhenkai Zhu9bad2bf2012-12-28 15:31:46 -0800112
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800113} // Ccnx
114#endif // CCNX_COMMON_H