aegis.cpp
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
field.hpp
1 //
2 // field.hpp
3 // *********
4 //
5 // Copyright (c) 2019 Sharon W (sharon at aegis dot gg)
6 //
7 // Distributed under the MIT License. (See accompanying file LICENSE)
8 //
9 
10 #pragma once
11 
12 #include "aegis/config.hpp"
13 #include "aegis/snowflake.hpp"
14 #include <nlohmann/json.hpp>
15 
16 namespace aegis
17 {
18 
19 namespace gateway
20 {
21 
22 namespace objects
23 {
24 
27 struct field
28 {
29  field(std::string n, std::string v) : _name(n), _value(v) {}
30  field(std::string n, std::string v, bool il) : _name(n), _value(v), _is_inline(il) {}
31  field() = default;
32 
34 
37  field & name(const std::string & str)
38  {
39  _name = str;
40  return *this;
41  }
42 
44 
47  field & value(const std::string & str)
48  {
49  _value = str;
50  return *this;
51  }
52 
54 
57  field & is_inline(const bool b)
58  {
59  _is_inline = b;
60  return *this;
61  }
62 
64  std::string & name() noexcept
65  {
66  return _name;
67  }
68 
70  std::string & value() noexcept
71  {
72  return _value;
73  }
74 
76  bool & is_inline() noexcept
77  {
78  return _is_inline;
79  }
80 
81 private:
82  std::string _name;
83  std::string _value;
84  bool _is_inline = false;
85  friend void from_json(const nlohmann::json& j, field& m);
86  friend void to_json(nlohmann::json& j, const field& m);
87 };
88 
90 inline void from_json(const nlohmann::json& j, field& m)
91 {
92  if (j.count("name"))
93  m._name = j["name"].get<std::string>();
94  if (j.count("value"))
95  m._value = j["value"].get<std::string>();
96  if (j.count("inline"))
97  m._is_inline = j["inline"];
98 }
99 
100 inline void to_json(nlohmann::json& j, const field& m)
101 {
102  j["name"] = m._name;
103  j["value"] = m._value;
104  j["inline"] = m._is_inline;
105 }
107 
108 }
109 
110 }
111 
112 }
std::string & name() noexcept
Get the name of the field.
Definition: field.hpp:64
field & value(const std::string &str)
Sets the value of the field.
Definition: field.hpp:47
Definition: field.hpp:27
bool & is_inline() noexcept
Get whether the field is inline.
Definition: field.hpp:76
field & name(const std::string &str)
Sets the name of the field.
Definition: field.hpp:37
field & is_inline(const bool b)
Sets whether the field is inline.
Definition: field.hpp:57
std::string & value() noexcept
Get the value of the field.
Definition: field.hpp:70