blob: 33c78a8cc953f08feb1d481fc5ac0dc6b67cc888 [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>
63#include <boost/shared_ptr.hpp>
64#include <boost/exception/all.hpp>
65#include <boost/function.hpp>
66#include <string>
67#include <sstream>
68#include <map>
69#include <utility>
70#include <string.h>
71#include <boost/iostreams/filter/gzip.hpp>
72#include <boost/iostreams/filtering_stream.hpp>
73#include <boost/iostreams/device/back_inserter.hpp>
74#include <boost/range/iterator_range.hpp>
75#include <boost/make_shared.hpp>
76
77namespace ndn {
78typedef std::vector<unsigned char> Bytes;
79typedef std::vector<std::string>Comps;
80
Jeff Thompsonfa306642013-06-17 15:06:57 -070081inline
82const unsigned char *
83head(const Bytes &bytes)
84{
85 return &bytes[0];
86}
87
88inline
89unsigned char *
90head (Bytes &bytes)
91{
92 return &bytes[0];
93}
94
95// --- Bytes operations start ---
96inline void
97readRaw(Bytes &bytes, const unsigned char *src, size_t len)
98{
99 if (len > 0)
100 {
101 bytes.resize(len);
102 memcpy (head (bytes), src, len);
103 }
104}
105
Jeff Thompson29310402013-06-19 13:32:26 -0700106inline ptr_lib::shared_ptr<Bytes>
Jeff Thompsonfa306642013-06-17 15:06:57 -0700107readRawPtr (const unsigned char *src, size_t len)
108{
109 if (len > 0)
110 {
Jeff Thompson29310402013-06-19 13:32:26 -0700111 ptr_lib::shared_ptr<Bytes> ret (new Bytes (len));
Jeff Thompsonfa306642013-06-17 15:06:57 -0700112 memcpy (head (*ret), src, len);
113
114 return ret;
115 }
116 else
Jeff Thompson29310402013-06-19 13:32:26 -0700117 return ptr_lib::shared_ptr<Bytes> ();
Jeff Thompsonfa306642013-06-17 15:06:57 -0700118}
Jeff Thompsonfa306642013-06-17 15:06:57 -0700119} // ndn
120#endif // NDN_COMMON_H