Kea  1.5.0
user_context.cc
Go to the documentation of this file.
1 // Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 #include <cc/user_context.h>
9 
10 
11 namespace isc {
12 namespace data {
13 
14 void
16  // Set user-context extracting comment
17  ConstElementPtr context = getContext();
18  if (!isNull(context)) {
19  if ((context->getType() == Element::map) &&
20  context->contains("comment")) {
21  ElementPtr copied = isc::data::copy(context);
22  map->set("comment", copied->get("comment"));
23  copied->remove("comment");
24  if (copied->size() > 0) {
25  map->set("user-context", copied);
26  }
27  } else {
28  map->set("user-context", context);
29  }
30  }
31 }
32 
35  ElementPtr result = isc::data::copy(map);
36  // Protect against argument not map
37  if (result->getType() != Element::map) {
38  return (result);
39  }
40  ConstElementPtr ctx = result->get("user-context");
41  // Protect against user context not map
42  if (!ctx || (ctx->getType() != Element::map)) {
43  return (result);
44  }
45  // Extract comment
46  if (ctx->contains("comment")) {
47  ElementPtr ctx_copy = isc::data::copy(ctx);
48  result->set("comment", ctx_copy->get("comment"));
49  ctx_copy->remove("comment");
50  result->remove("user-context");
51  if (ctx_copy->size() > 0) {
52  result->set("user-context", ctx_copy);
53  }
54  }
55  return (result);
56 }
57 
58 }; // end of isc::dhcp namespace
59 }; // end of isc namespace
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
Definition: user_context.h:24
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
bool isNull(ConstElementPtr p)
Checks whether the given ElementPtr is a NULL pointer.
Definition: data.cc:1043
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1114
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
static data::ElementPtr toElement(data::ConstElementPtr map)
Copy extracting comments an Element map.
Definition: user_context.cc:34
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15
Defines the logger used by the top-level component of kea-dhcp-ddns.