blob: 2b75bc8bb782f566459614006a6a629b04d46ff1 [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Alexander Afanasyev
5 * Zhenkai Zhu
6 *
7 * BSD license, See the LICENSE file for more information
8 *
9 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
10 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
11 */
12
13#ifndef NDN_COMMON_H
14#define NDN_COMMON_H
15
Jeff Thompsonfa306642013-06-17 15:06:57 -070016#include <boost/date_time/posix_time/posix_time_types.hpp>
Jeff Thompson0e03ef12013-06-19 14:29:43 -070017#include "../config.h"
Jeff Thompsonfa306642013-06-17 15:06:57 -070018
Jeff Thompson0e03ef12013-06-19 14:29:43 -070019// Depending on where ./configure found shared_ptr, define the ptr_lib namespace.
Jeff Thompson11dfb302013-06-19 14:47:15 -070020// We always use ndn::ptr_lib::shared_ptr.
Jeff Thompson0e03ef12013-06-19 14:29:43 -070021#if HAVE_STD_SHARED_PTR
Jeff Thompson11dfb302013-06-19 14:47:15 -070022#include <memory>
23namespace ndn { namespace ptr_lib = std; }
Jeff Thompson0e03ef12013-06-19 14:29:43 -070024#elif HAVE_BOOST_SHARED_PTR
Jeff Thompson11dfb302013-06-19 14:47:15 -070025#include <boost/shared_ptr.hpp>
Jeff Thompson5deb3222013-06-19 14:59:31 -070026#include <boost/make_shared.hpp>
Jeff Thompson11dfb302013-06-19 14:47:15 -070027namespace ndn { namespace ptr_lib = boost; }
Jeff Thompson0e03ef12013-06-19 14:29:43 -070028#else
29#error "Can't find shared_ptr in std or boost"
30#endif
31
Jeff Thompson11dfb302013-06-19 14:47:15 -070032namespace ndn
33{
Jeff Thompsonfa306642013-06-17 15:06:57 -070034typedef boost::posix_time::ptime Time;
35typedef boost::posix_time::time_duration TimeInterval;
36
37namespace time
38{
39inline TimeInterval Seconds (int secs) { return boost::posix_time::seconds (secs); }
40inline TimeInterval Milliseconds (int msecs) { return boost::posix_time::milliseconds (msecs); }
41inline TimeInterval Microseconds (int musecs) { return boost::posix_time::microseconds (musecs); }
42
43inline TimeInterval Seconds (double fractionalSeconds)
44{
45 double seconds, microseconds;
46 seconds = std::modf (fractionalSeconds, &microseconds);
47 microseconds *= 1000000;
48
49 return time::Seconds((int)seconds) + time::Microseconds((int)microseconds);
50}
51
52inline Time Now () { return boost::posix_time::microsec_clock::universal_time (); }
53
54const Time UNIX_EPOCH_TIME = Time (boost::gregorian::date (1970, boost::gregorian::Jan, 1));
55inline TimeInterval NowUnixTimestamp ()
56{
57 return TimeInterval (time::Now () - UNIX_EPOCH_TIME);
58}
59} // time
60} // ndn
61
Jeff Thompsonfa306642013-06-17 15:06:57 -070062#include <vector>
Jeff Thompsonfa306642013-06-17 15:06:57 -070063#include <boost/exception/all.hpp>
64#include <boost/function.hpp>
65#include <string>
66#include <sstream>
67#include <map>
68#include <utility>
69#include <string.h>
70#include <boost/iostreams/filter/gzip.hpp>
71#include <boost/iostreams/filtering_stream.hpp>
72#include <boost/iostreams/device/back_inserter.hpp>
73#include <boost/range/iterator_range.hpp>
Jeff Thompsonfa306642013-06-17 15:06:57 -070074
75namespace ndn {
76typedef std::vector<unsigned char> Bytes;
77typedef std::vector<std::string>Comps;
78
Jeff Thompsonfa306642013-06-17 15:06:57 -070079inline
80const unsigned char *
81head(const Bytes &bytes)
82{
83 return &bytes[0];
84}
85
86inline
87unsigned char *
88head (Bytes &bytes)
89{
90 return &bytes[0];
91}
92
93// --- Bytes operations start ---
94inline void
95readRaw(Bytes &bytes, const unsigned char *src, size_t len)
96{
97 if (len > 0)
98 {
99 bytes.resize(len);
100 memcpy (head (bytes), src, len);
101 }
102}
103
Jeff Thompson29310402013-06-19 13:32:26 -0700104inline ptr_lib::shared_ptr<Bytes>
Jeff Thompsonfa306642013-06-17 15:06:57 -0700105readRawPtr (const unsigned char *src, size_t len)
106{
107 if (len > 0)
108 {
Jeff Thompson29310402013-06-19 13:32:26 -0700109 ptr_lib::shared_ptr<Bytes> ret (new Bytes (len));
Jeff Thompsonfa306642013-06-17 15:06:57 -0700110 memcpy (head (*ret), src, len);
111
112 return ret;
113 }
114 else
Jeff Thompson29310402013-06-19 13:32:26 -0700115 return ptr_lib::shared_ptr<Bytes> ();
Jeff Thompsonfa306642013-06-17 15:06:57 -0700116}
Jeff Thompsonfa306642013-06-17 15:06:57 -0700117} // ndn
118#endif // NDN_COMMON_H