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