Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 1 | /* -*- 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 Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 16 | #include <boost/make_shared.hpp> |
| 17 | #include <boost/date_time/posix_time/posix_time_types.hpp> |
Jeff Thompson | 0e03ef1 | 2013-06-19 14:29:43 -0700 | [diff] [blame] | 18 | #include "../config.h" |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 19 | |
Jeff Thompson | 0e03ef1 | 2013-06-19 14:29:43 -0700 | [diff] [blame] | 20 | // Depending on where ./configure found shared_ptr, define the ptr_lib namespace. |
Jeff Thompson | 11dfb30 | 2013-06-19 14:47:15 -0700 | [diff] [blame^] | 21 | // We always use ndn::ptr_lib::shared_ptr. |
Jeff Thompson | 0e03ef1 | 2013-06-19 14:29:43 -0700 | [diff] [blame] | 22 | #if HAVE_STD_SHARED_PTR |
Jeff Thompson | 11dfb30 | 2013-06-19 14:47:15 -0700 | [diff] [blame^] | 23 | #include <memory> |
| 24 | namespace ndn { namespace ptr_lib = std; } |
Jeff Thompson | 0e03ef1 | 2013-06-19 14:29:43 -0700 | [diff] [blame] | 25 | #elif HAVE_BOOST_SHARED_PTR |
Jeff Thompson | 11dfb30 | 2013-06-19 14:47:15 -0700 | [diff] [blame^] | 26 | #include <boost/shared_ptr.hpp> |
| 27 | namespace ndn { namespace ptr_lib = boost; } |
Jeff Thompson | 0e03ef1 | 2013-06-19 14:29:43 -0700 | [diff] [blame] | 28 | #else |
| 29 | #error "Can't find shared_ptr in std or boost" |
| 30 | #endif |
| 31 | |
Jeff Thompson | 11dfb30 | 2013-06-19 14:47:15 -0700 | [diff] [blame^] | 32 | namespace ndn |
| 33 | { |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 34 | typedef boost::posix_time::ptime Time; |
| 35 | typedef boost::posix_time::time_duration TimeInterval; |
| 36 | |
| 37 | namespace time |
| 38 | { |
| 39 | inline TimeInterval Seconds (int secs) { return boost::posix_time::seconds (secs); } |
| 40 | inline TimeInterval Milliseconds (int msecs) { return boost::posix_time::milliseconds (msecs); } |
| 41 | inline TimeInterval Microseconds (int musecs) { return boost::posix_time::microseconds (musecs); } |
| 42 | |
| 43 | inline TimeInterval Seconds (double fractionalSeconds) |
| 44 | { |
| 45 | double seconds, microseconds; |
| 46 | seconds = std::modf (fractionalSeconds, µseconds); |
| 47 | microseconds *= 1000000; |
| 48 | |
| 49 | return time::Seconds((int)seconds) + time::Microseconds((int)microseconds); |
| 50 | } |
| 51 | |
| 52 | inline Time Now () { return boost::posix_time::microsec_clock::universal_time (); } |
| 53 | |
| 54 | const Time UNIX_EPOCH_TIME = Time (boost::gregorian::date (1970, boost::gregorian::Jan, 1)); |
| 55 | inline TimeInterval NowUnixTimestamp () |
| 56 | { |
| 57 | return TimeInterval (time::Now () - UNIX_EPOCH_TIME); |
| 58 | } |
| 59 | } // time |
| 60 | } // ndn |
| 61 | |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 62 | #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 | |
| 77 | namespace ndn { |
| 78 | typedef std::vector<unsigned char> Bytes; |
| 79 | typedef std::vector<std::string>Comps; |
| 80 | |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 81 | inline |
| 82 | const unsigned char * |
| 83 | head(const Bytes &bytes) |
| 84 | { |
| 85 | return &bytes[0]; |
| 86 | } |
| 87 | |
| 88 | inline |
| 89 | unsigned char * |
| 90 | head (Bytes &bytes) |
| 91 | { |
| 92 | return &bytes[0]; |
| 93 | } |
| 94 | |
| 95 | // --- Bytes operations start --- |
| 96 | inline void |
| 97 | readRaw(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 Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 106 | inline ptr_lib::shared_ptr<Bytes> |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 107 | readRawPtr (const unsigned char *src, size_t len) |
| 108 | { |
| 109 | if (len > 0) |
| 110 | { |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 111 | ptr_lib::shared_ptr<Bytes> ret (new Bytes (len)); |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 112 | memcpy (head (*ret), src, len); |
| 113 | |
| 114 | return ret; |
| 115 | } |
| 116 | else |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 117 | return ptr_lib::shared_ptr<Bytes> (); |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 118 | } |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 119 | } // ndn |
| 120 | #endif // NDN_COMMON_H |