blob: f6c6f9d6abadbe8e1e6fffcb3549679aa2cd10c1 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson5cae5e52013-07-10 19:41:20 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson5cae5e52013-07-10 19:41:20 -07005 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompson71b2f872013-12-17 12:03:17 -08008#ifndef NDN_KEY_LOCATOR_HPP
9#define NDN_KEY_LOCATOR_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -070010
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080011#include "encoding/tlv-element.hpp"
Jeff Thompson2e6269c2013-08-22 10:36:00 -070012#include "name.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070013
14namespace ndn {
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080015
16namespace error {
17struct KeyLocator : public std::runtime_error { KeyLocator(const std::string &what) : std::runtime_error(what) {} };
18} // error
19
Jeff Thompson5cae5e52013-07-10 19:41:20 -070020class KeyLocator {
21public:
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080022 enum {
23 KeyLocator_None = -1,
24 KeyLocator_Name = 0
25 };
26
Jeff Thompson5cae5e52013-07-10 19:41:20 -070027 KeyLocator()
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080028 : type_(KeyLocator_None)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070029 {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070030 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070031
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080032 inline bool
33 empty() const
34 {
35 return type_ == KeyLocator_None;
36 }
37
38 uint32_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070039 getType() const { return type_; }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070040
Jeff Thompson0050abe2013-09-17 12:50:25 -070041 void
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080042 setType(uint32_t type) { type_ = type; }
Jeff Thompson2c8dff52013-11-18 18:17:12 -080043
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080044 const Block&
45 getValue() const { return value_; }
46
Jeff Thompson0050abe2013-09-17 12:50:25 -070047 void
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080048 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 Thompson63d02692013-08-16 12:09:07 -070063
Jeff Thompsone5c53aa2013-11-19 10:53:25 -080064 void
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080065 setName(const Name &name)
66 {
67 type_ = KeyLocator_Name;
68 value_ = name.wireEncode();
69 }
Jeff Thompson35a2bc12013-09-17 14:23:13 -070070
Jeff Thompson5cae5e52013-07-10 19:41:20 -070071private:
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080072 uint32_t type_;
73 Block value_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070074};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080075
Jeff Thompson5cae5e52013-07-10 19:41:20 -070076}
77
78#endif