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 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "key-locator.h" |
| 12 | #include "ndn-cpp/error.h" |
| 13 | |
| 14 | namespace ndn { |
| 15 | |
| 16 | KeyLocator::KeyLocator () |
| 17 | : m_type (NOTSET) |
| 18 | , m_data (0) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | KeyLocator::KeyLocator (const KeyLocator &keyLocator) |
| 23 | : m_type (keyLocator.getType ()) |
| 24 | , m_data (0) |
| 25 | { |
| 26 | switch (m_type) |
| 27 | { |
| 28 | case NOTSET: |
| 29 | break; |
| 30 | case KEY: |
| 31 | m_data = new Blob (keyLocator.getKey ()); |
| 32 | break; |
| 33 | case CERTIFICATE: |
| 34 | m_data = new Blob (keyLocator.getCertificate ()); |
| 35 | break; |
| 36 | case KEYNAME: |
| 37 | m_data = new Name (keyLocator.getKeyName ()); |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | KeyLocator::~KeyLocator () |
| 43 | { |
| 44 | deleteData (); |
| 45 | } |
| 46 | |
| 47 | KeyLocator & |
| 48 | KeyLocator::operator = (const KeyLocator &keyLocator) |
| 49 | { |
| 50 | if (this == &keyLocator) |
| 51 | return *this; |
| 52 | |
| 53 | deleteData (); |
| 54 | m_type = keyLocator.getType (); |
| 55 | |
| 56 | switch (m_type) |
| 57 | { |
| 58 | case NOTSET: |
| 59 | break; |
| 60 | case KEY: |
| 61 | m_data = new Blob (keyLocator.getKey ()); |
| 62 | break; |
| 63 | case CERTIFICATE: |
| 64 | m_data = new Blob (keyLocator.getCertificate ()); |
| 65 | break; |
| 66 | case KEYNAME: |
| 67 | m_data = new Name (keyLocator.getKeyName ()); |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | KeyLocator::deleteData () |
| 76 | { |
| 77 | switch (m_type) |
| 78 | { |
| 79 | case NOTSET: // nothing to clean up |
| 80 | break; |
| 81 | case KEY: |
| 82 | delete reinterpret_cast<Blob*> (m_data); |
| 83 | break; |
| 84 | case CERTIFICATE: |
| 85 | delete reinterpret_cast<Blob*> (m_data); |
| 86 | break; |
| 87 | case KEYNAME: |
| 88 | delete reinterpret_cast<Name*> (m_data); |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | KeyLocator::setType (KeyLocator::Type type) |
| 95 | { |
| 96 | if (m_type == type) |
| 97 | return; |
| 98 | |
| 99 | deleteData (); |
| 100 | m_type = type; |
| 101 | |
| 102 | switch (m_type) |
| 103 | { |
| 104 | case NOTSET: |
| 105 | m_data = 0; |
| 106 | case KEY: |
| 107 | m_data = new Blob; |
| 108 | break; |
| 109 | case CERTIFICATE: |
| 110 | m_data = new Blob; |
| 111 | break; |
| 112 | case KEYNAME: |
| 113 | m_data = new Name; |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | const Blob & |
| 119 | KeyLocator::getKey () const |
| 120 | { |
| 121 | if (m_type != KEY) |
| 122 | BOOST_THROW_EXCEPTION (error::KeyLocator () |
| 123 | << error::msg ("getKey called, but KeyLocator is not of type KeyLocator::KEY")); |
| 124 | return *reinterpret_cast<const Blob*> (m_data); |
| 125 | } |
| 126 | |
| 127 | Blob & |
| 128 | KeyLocator::getKey () |
| 129 | { |
| 130 | if (m_type != KEY) |
| 131 | BOOST_THROW_EXCEPTION (error::KeyLocator () |
| 132 | << error::msg ("getKey called, but KeyLocator is not of type KeyLocator::KEY")); |
| 133 | return *reinterpret_cast<Blob*> (m_data); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | KeyLocator::setKey (const Blob &key) |
| 138 | { |
| 139 | if (m_type != KEY) |
| 140 | BOOST_THROW_EXCEPTION (error::KeyLocator () |
| 141 | << error::msg ("setKey called, but KeyLocator is not of type KeyLocator::KEY")); |
| 142 | *reinterpret_cast<Blob*> (m_data) = key; |
| 143 | } |
| 144 | |
| 145 | const Blob & |
| 146 | KeyLocator::getCertificate () const |
| 147 | { |
| 148 | if (m_type != CERTIFICATE) |
| 149 | BOOST_THROW_EXCEPTION (error::KeyLocator () |
| 150 | << error::msg ("getCertificate called, but KeyLocator is not of type KeyLocator::CERTIFICATE")); |
| 151 | return *reinterpret_cast<const Blob*> (m_data); |
| 152 | } |
| 153 | |
| 154 | Blob & |
| 155 | KeyLocator::getCertificate () |
| 156 | { |
| 157 | if (m_type != CERTIFICATE) |
| 158 | BOOST_THROW_EXCEPTION (error::KeyLocator () |
| 159 | << error::msg ("getCertificate called, but KeyLocator is not of type KeyLocator::CERTIFICATE")); |
| 160 | return *reinterpret_cast<Blob*> (m_data); |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | KeyLocator::setCertificate (const Blob &certificate) |
| 165 | { |
| 166 | if (m_type != CERTIFICATE) |
| 167 | BOOST_THROW_EXCEPTION (error::KeyLocator () |
| 168 | << error::msg ("setCertificate called, but KeyLocator is not of type KeyLocator::CERTIFICATE")); |
| 169 | *reinterpret_cast<Blob*> (m_data) = certificate; |
| 170 | } |
| 171 | |
| 172 | const Name & |
| 173 | KeyLocator::getKeyName () const |
| 174 | { |
| 175 | if (m_type != KEYNAME) |
| 176 | BOOST_THROW_EXCEPTION (error::KeyLocator () |
| 177 | << error::msg ("getKeyName called, but KeyLocator is not of type KeyLocator::KEYNAME")); |
| 178 | return *reinterpret_cast<const Name*> (m_data); |
| 179 | } |
| 180 | |
| 181 | Name & |
| 182 | KeyLocator::getKeyName () |
| 183 | { |
| 184 | if (m_type != KEYNAME) |
| 185 | BOOST_THROW_EXCEPTION (error::KeyLocator () |
| 186 | << error::msg ("getKeyName called, but KeyLocator is not of type KeyLocator::KEYNAME")); |
| 187 | return *reinterpret_cast<Name*> (m_data); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | void |
| 192 | KeyLocator::setKeyName (const Name &name) |
| 193 | { |
| 194 | if (m_type != KEYNAME) |
| 195 | BOOST_THROW_EXCEPTION (error::KeyLocator () |
| 196 | << error::msg ("setKeyName called, but KeyLocator is not of type KeyLocator::KEYNAME")); |
| 197 | *reinterpret_cast<Name*> (m_data) = name; |
| 198 | } |
| 199 | |
| 200 | } |