blob: 84fd60c629c112832ebc868c8598f4c3ca7f3a2b [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- 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
14namespace ndn {
15
16KeyLocator::KeyLocator ()
17 : m_type (NOTSET)
18 , m_data (0)
19{
20}
21
22KeyLocator::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
42KeyLocator::~KeyLocator ()
43{
44 deleteData ();
45}
46
47KeyLocator &
48KeyLocator::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
74void
75KeyLocator::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
93void
94KeyLocator::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
118const Blob &
119KeyLocator::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
127Blob &
128KeyLocator::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
136void
137KeyLocator::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
145const Blob &
146KeyLocator::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
154Blob &
155KeyLocator::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
163void
164KeyLocator::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
172const Name &
173KeyLocator::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
181Name &
182KeyLocator::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
191void
192KeyLocator::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}