blob: b517c1ba38fc83765b7daef8ae05821e43fb49ec [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>
19
20namespace ndn
21{
Jeff Thompson29310402013-06-19 13:32:26 -070022namespace ptr_lib = boost;
Jeff Thompsonfa306642013-06-17 15:06:57 -070023typedef boost::posix_time::ptime Time;
24typedef boost::posix_time::time_duration TimeInterval;
25
26namespace time
27{
28inline TimeInterval Seconds (int secs) { return boost::posix_time::seconds (secs); }
29inline TimeInterval Milliseconds (int msecs) { return boost::posix_time::milliseconds (msecs); }
30inline TimeInterval Microseconds (int musecs) { return boost::posix_time::microseconds (musecs); }
31
32inline TimeInterval Seconds (double fractionalSeconds)
33{
34 double seconds, microseconds;
35 seconds = std::modf (fractionalSeconds, &microseconds);
36 microseconds *= 1000000;
37
38 return time::Seconds((int)seconds) + time::Microseconds((int)microseconds);
39}
40
41inline Time Now () { return boost::posix_time::microsec_clock::universal_time (); }
42
43const Time UNIX_EPOCH_TIME = Time (boost::gregorian::date (1970, boost::gregorian::Jan, 1));
44inline TimeInterval NowUnixTimestamp ()
45{
46 return TimeInterval (time::Now () - UNIX_EPOCH_TIME);
47}
48} // time
49} // ndn
50
Jeff Thompsonfa306642013-06-17 15:06:57 -070051#include <vector>
52#include <boost/shared_ptr.hpp>
53#include <boost/exception/all.hpp>
54#include <boost/function.hpp>
55#include <string>
56#include <sstream>
57#include <map>
58#include <utility>
59#include <string.h>
60#include <boost/iostreams/filter/gzip.hpp>
61#include <boost/iostreams/filtering_stream.hpp>
62#include <boost/iostreams/device/back_inserter.hpp>
63#include <boost/range/iterator_range.hpp>
64#include <boost/make_shared.hpp>
65
66namespace ndn {
67typedef std::vector<unsigned char> Bytes;
68typedef std::vector<std::string>Comps;
69
Jeff Thompsonfa306642013-06-17 15:06:57 -070070inline
71const unsigned char *
72head(const Bytes &bytes)
73{
74 return &bytes[0];
75}
76
77inline
78unsigned char *
79head (Bytes &bytes)
80{
81 return &bytes[0];
82}
83
84// --- Bytes operations start ---
85inline void
86readRaw(Bytes &bytes, const unsigned char *src, size_t len)
87{
88 if (len > 0)
89 {
90 bytes.resize(len);
91 memcpy (head (bytes), src, len);
92 }
93}
94
Jeff Thompson29310402013-06-19 13:32:26 -070095inline ptr_lib::shared_ptr<Bytes>
Jeff Thompsonfa306642013-06-17 15:06:57 -070096readRawPtr (const unsigned char *src, size_t len)
97{
98 if (len > 0)
99 {
Jeff Thompson29310402013-06-19 13:32:26 -0700100 ptr_lib::shared_ptr<Bytes> ret (new Bytes (len));
Jeff Thompsonfa306642013-06-17 15:06:57 -0700101 memcpy (head (*ret), src, len);
102
103 return ret;
104 }
105 else
Jeff Thompson29310402013-06-19 13:32:26 -0700106 return ptr_lib::shared_ptr<Bytes> ();
Jeff Thompsonfa306642013-06-17 15:06:57 -0700107}
Jeff Thompsonfa306642013-06-17 15:06:57 -0700108} // ndn
109#endif // NDN_COMMON_H