Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Jeff Thompson | 71b2f87 | 2013-12-17 12:03:17 -0800 | [diff] [blame] | 8 | #ifndef NDN_KEY_LOCATOR_HPP |
| 9 | #define NDN_KEY_LOCATOR_HPP |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 10 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 11 | #include "encoding/tlv-element.hpp" |
Jeff Thompson | 2e6269c | 2013-08-22 10:36:00 -0700 | [diff] [blame] | 12 | #include "name.hpp" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 13 | |
| 14 | namespace ndn { |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 15 | |
| 16 | namespace error { |
| 17 | struct KeyLocator : public std::runtime_error { KeyLocator(const std::string &what) : std::runtime_error(what) {} }; |
| 18 | } // error |
| 19 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 20 | class KeyLocator { |
| 21 | public: |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 22 | enum { |
| 23 | KeyLocator_None = -1, |
| 24 | KeyLocator_Name = 0 |
| 25 | }; |
| 26 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 27 | KeyLocator() |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 28 | : type_(KeyLocator_None) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 29 | { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 30 | } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 32 | inline bool |
| 33 | empty() const |
| 34 | { |
| 35 | return type_ == KeyLocator_None; |
| 36 | } |
| 37 | |
| 38 | uint32_t |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 39 | getType() const { return type_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 40 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 41 | void |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 42 | setType(uint32_t type) { type_ = type; } |
Jeff Thompson | 2c8dff5 | 2013-11-18 18:17:12 -0800 | [diff] [blame] | 43 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 44 | const Block& |
| 45 | getValue() const { return value_; } |
| 46 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 47 | void |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 48 | setValue(const Block &value) { value_ = value; } |
| 49 | |
| 50 | //////////////////////////////////////////////////////// |
| 51 | // Helper methods for different types of key locators |
| 52 | // |
| 53 | // For now only Name type is actually supported |
| 54 | |
| 55 | Name |
| 56 | getName() const |
| 57 | { |
| 58 | if (type_ != KeyLocator_Name) |
| 59 | throw error::KeyLocator("Requested Name, but KeyLocator is not of the Name type"); |
| 60 | |
| 61 | return Name(getValue()); |
| 62 | } |
Jeff Thompson | 63d0269 | 2013-08-16 12:09:07 -0700 | [diff] [blame] | 63 | |
Jeff Thompson | e5c53aa | 2013-11-19 10:53:25 -0800 | [diff] [blame] | 64 | void |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 65 | setName(const Name &name) |
| 66 | { |
| 67 | type_ = KeyLocator_Name; |
| 68 | value_ = name.wireEncode(); |
| 69 | } |
Jeff Thompson | 35a2bc1 | 2013-09-17 14:23:13 -0700 | [diff] [blame] | 70 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 71 | private: |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 72 | uint32_t type_; |
| 73 | Block value_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 74 | }; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 75 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | #endif |