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