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 | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 22 | namespace ptr_lib = boost; |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 23 | typedef boost::posix_time::ptime Time; |
| 24 | typedef boost::posix_time::time_duration TimeInterval; |
| 25 | |
| 26 | namespace time |
| 27 | { |
| 28 | inline TimeInterval Seconds (int secs) { return boost::posix_time::seconds (secs); } |
| 29 | inline TimeInterval Milliseconds (int msecs) { return boost::posix_time::milliseconds (msecs); } |
| 30 | inline TimeInterval Microseconds (int musecs) { return boost::posix_time::microseconds (musecs); } |
| 31 | |
| 32 | inline TimeInterval Seconds (double fractionalSeconds) |
| 33 | { |
| 34 | double seconds, microseconds; |
| 35 | seconds = std::modf (fractionalSeconds, µseconds); |
| 36 | microseconds *= 1000000; |
| 37 | |
| 38 | return time::Seconds((int)seconds) + time::Microseconds((int)microseconds); |
| 39 | } |
| 40 | |
| 41 | inline Time Now () { return boost::posix_time::microsec_clock::universal_time (); } |
| 42 | |
| 43 | const Time UNIX_EPOCH_TIME = Time (boost::gregorian::date (1970, boost::gregorian::Jan, 1)); |
| 44 | inline TimeInterval NowUnixTimestamp () |
| 45 | { |
| 46 | return TimeInterval (time::Now () - UNIX_EPOCH_TIME); |
| 47 | } |
| 48 | } // time |
| 49 | } // ndn |
| 50 | |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 51 | #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 | |
| 66 | namespace ndn { |
| 67 | typedef std::vector<unsigned char> Bytes; |
| 68 | typedef std::vector<std::string>Comps; |
| 69 | |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 70 | inline |
| 71 | const unsigned char * |
| 72 | head(const Bytes &bytes) |
| 73 | { |
| 74 | return &bytes[0]; |
| 75 | } |
| 76 | |
| 77 | inline |
| 78 | unsigned char * |
| 79 | head (Bytes &bytes) |
| 80 | { |
| 81 | return &bytes[0]; |
| 82 | } |
| 83 | |
| 84 | // --- Bytes operations start --- |
| 85 | inline void |
| 86 | readRaw(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 Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 95 | inline ptr_lib::shared_ptr<Bytes> |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 96 | readRawPtr (const unsigned char *src, size_t len) |
| 97 | { |
| 98 | if (len > 0) |
| 99 | { |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 100 | ptr_lib::shared_ptr<Bytes> ret (new Bytes (len)); |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 101 | memcpy (head (*ret), src, len); |
| 102 | |
| 103 | return ret; |
| 104 | } |
| 105 | else |
Jeff Thompson | 2931040 | 2013-06-19 13:32:26 -0700 | [diff] [blame] | 106 | return ptr_lib::shared_ptr<Bytes> (); |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 107 | } |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 108 | } // ndn |
| 109 | #endif // NDN_COMMON_H |