mastodon-cpp  0.110.0
entity.hpp
1 /* This file is part of mastodon-cpp.
2  * Copyright © 2019 tastytea <tastytea@tastytea.de>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as published by
6  * the Free Software Foundation, version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Affero General Public License for more details.
12  *
13  * You should have received a copy of the GNU Affero General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef MASTODON_CPP_EASY_ENTITY_HPP
18 #define MASTODON_CPP_EASY_ENTITY_HPP
19 
20 #include <string>
21 #include <jsoncpp/json/json.h>
22 
23 #include "types_easy.hpp"
24 
25 using std::string;
26 
27 namespace Mastodon
28 {
29 namespace Easy
30 {
36  class Entity
37  {
38  public:
46  explicit Entity(const string &json);
47 
55  explicit Entity(const Json::Value &object);
56 
62  Entity();
63 
69  virtual ~Entity();
70 
76  operator const Json::Value() const;
77 
85  void from_string(const string &json);
86 
94  const string to_string() const;
95 
103  void from_object(const Json::Value &object);
104 
112  const Json::Value to_object() const;
113 
119  virtual bool valid() const = 0;
120 
126  const string error() const;
127 
156  bool was_set() const;
157 
158  protected:
165  const Json::Value get(const string &key) const;
166 
172  const string get_string(const string &key) const;
173 
179  uint64_t get_uint64(const string &key) const;
180 
186  double get_double(const string &key) const;
187 
193  bool get_bool(const string &key) const;
194 
200  const Easy::time_type get_time(const string &key) const;
201 
208  const std::vector<string> get_vector(const string &key) const;
209 
215  void set(const string &key, const Json::Value &value);
216 
220  std::uint64_t stouint64(const string &str) const;
221 
231  bool check_valid(const std::vector<string> &attributes) const;
232 
233  private:
234  Json::Value _tree;
235  mutable bool _was_set;
236 };
237 }
238 }
239 
240 #endif // MASTODON_CPP_EASY_ENTITY_HPP
const Easy::time_type get_time(const string &key) const
Returns the value of key as Easy::time.
Definition: entity.cpp:230
const Json::Value get(const string &key) const
Returns the value of key as Json::Value.
Definition: entity.cpp:133
uint64_t get_uint64(const string &key) const
Returns the value of key as std::uint64_t.
Definition: entity.cpp:188
Entity()
Constructs an empty Entity object.
Definition: entity.cpp:43
void set(const string &key, const Json::Value &value)
Sets the value of key.
Definition: entity.cpp:263
double get_double(const string &key) const
Returns the value of key as double.
Definition: entity.cpp:202
virtual bool valid() const =0
Returns true if the Entity holds valid data.
Type for time. Converts to time_point and string.
Definition: types_easy.hpp:150
const string to_string() const
Returns the JSON of the Entity as formatted string.
Definition: entity.cpp:87
void from_object(const Json::Value &object)
Replaces the Entity with a new one from a JSON object.
Definition: entity.cpp:92
bool was_set() const
Returns true if the last requested value was set, false if it was unset.
Definition: entity.cpp:128
virtual ~Entity()
Destroys the object.
Definition: entity.cpp:48
const Json::Value to_object() const
Returns the JSON object of the Entity.
Definition: entity.cpp:97
bool check_valid(const std::vector< string > &attributes) const
Checks if an Entity is valid.
Definition: entity.cpp:102
void from_string(const string &json)
Replaces the Entity with a new one from a JSON string.
Definition: entity.cpp:56
bool get_bool(const string &key) const
Returns the value of key as bool.
Definition: entity.cpp:216
Collection of things to interface with server software that implements the Mastodon API.
Definition: mastodon-cpp.hpp:47
Base class for all entities.
Definition: entity.hpp:36
const string error() const
Returns error string sent by the server.
Definition: entity.cpp:116
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:174
const std::vector< string > get_vector(const string &key) const
Returns the value of key as vector.
Definition: entity.cpp:245
std::uint64_t stouint64(const string &str) const
Returns value of str as uint64_t.
Definition: entity.cpp:301