blob: 805be36c81904c9619c8802d6e2b28fdf159791e [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
16#include <boost/shared_ptr.hpp>
17#include <boost/make_shared.hpp>
18#include <boost/date_time/posix_time/posix_time_types.hpp>
Jeff Thompson0e03ef12013-06-19 14:29:43 -070019#include "../config.h"
Jeff Thompsonfa306642013-06-17 15:06:57 -070020
21namespace ndn
22{
Jeff Thompson0e03ef12013-06-19 14:29:43 -070023// Depending on where ./configure found shared_ptr, define the ptr_lib namespace.
24// We always use ptr_lib::shared_ptr.
25#if HAVE_STD_SHARED_PTR
26namespace ptr_lib = std;
27#elif HAVE_BOOST_SHARED_PTR
Jeff Thompson29310402013-06-19 13:32:26 -070028namespace ptr_lib = boost;
Jeff Thompson0e03ef12013-06-19 14:29:43 -070029#else
30#error "Can't find shared_ptr in std or boost"
31#endif
32
Jeff Thompsonfa306642013-06-17 15:06:57 -070033typedef boost::posix_time::ptime Time;
34typedef boost::posix_time::time_duration TimeInterval;
35
36namespace time
37{
38inline TimeInterval Seconds (int secs) { return boost::posix_time::seconds (secs); }
39inline TimeInterval Milliseconds (int msecs) { return boost::posix_time::milliseconds (msecs); }
40inline TimeInterval Microseconds (int musecs) { return boost::posix_time::microseconds (musecs); }
41
42inline TimeInterval Seconds (double fractionalSeconds)
43{
44 double seconds, microseconds;
45 seconds = std::modf (fractionalSeconds, &microseconds);
46 microseconds *= 1000000;
47
48 return time::Seconds((int)seconds) + time::Microseconds((int)microseconds);
49}
50
51inline Time Now () { return boost::posix_time::microsec_clock::universal_time (); }
52
53const Time UNIX_EPOCH_TIME = Time (boost::gregorian::date (1970, boost::gregorian::Jan, 1));
54inline TimeInterval NowUnixTimestamp ()
55{
56 return TimeInterval (time::Now () - UNIX_EPOCH_TIME);
57}
58} // time
59} // ndn
60
Jeff Thompsonfa306642013-06-17 15:06:57 -070061#include <vector>
62#include <boost/shared_ptr.hpp>
63#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>
74#include <boost/make_shared.hpp>
75
76namespace ndn {
77typedef std::vector<unsigned char> Bytes;
78typedef std::vector<std::string>Comps;
79
Jeff Thompsonfa306642013-06-17 15:06:57 -070080inline
81const unsigned char *
82head(const Bytes &bytes)
83{
84 return &bytes[0];
85}
86
87inline
88unsigned char *
89head (Bytes &bytes)
90{
91 return &bytes[0];
92}
93
94// --- Bytes operations start ---
95inline void
96readRaw(Bytes &bytes, const unsigned char *src, size_t len)
97{
98 if (len > 0)
99 {
100 bytes.resize(len);
101 memcpy (head (bytes), src, len);
102 }
103}
104
Jeff Thompson29310402013-06-19 13:32:26 -0700105inline ptr_lib::shared_ptr<Bytes>
Jeff Thompsonfa306642013-06-17 15:06:57 -0700106readRawPtr (const unsigned char *src, size_t len)
107{
108 if (len > 0)
109 {
Jeff Thompson29310402013-06-19 13:32:26 -0700110 ptr_lib::shared_ptr<Bytes> ret (new Bytes (len));
Jeff Thompsonfa306642013-06-17 15:06:57 -0700111 memcpy (head (*ret), src, len);
112
113 return ret;
114 }
115 else
Jeff Thompson29310402013-06-19 13:32:26 -0700116 return ptr_lib::shared_ptr<Bytes> ();
Jeff Thompsonfa306642013-06-17 15:06:57 -0700117}
Jeff Thompsonfa306642013-06-17 15:06:57 -0700118} // ndn
119#endif // NDN_COMMON_H