A few cleanups, and make a few things more modern

This commit is contained in:
Florent Le Coz
2015-12-23 11:05:55 +01:00
parent 9167cdf126
commit 9ac0d3a576
5 changed files with 23 additions and 22 deletions
+6 -12
View File
@@ -7,7 +7,7 @@
* If you want to exit if the file does not exist when it is open for * If you want to exit if the file does not exist when it is open for
* reading, set Config::file_must_exist = true. * reading, set Config::file_must_exist = true.
* *
* Config::get() can the be used to access the values in the conf. * Config::get() can then be used to access the values in the conf.
* *
* Use Config::close() when you're done getting/setting value. This will * Use Config::close() when you're done getting/setting value. This will
* save the config into the file. * save the config into the file.
@@ -33,35 +33,29 @@ public:
/** /**
* returns a value from the config. If it doesn’t exist, use * returns a value from the config. If it doesn’t exist, use
* the second argument as the default. * the second argument as the default.
* @param option The option we want
* @param def The default value in case the option does not exist
*/ */
static std::string get(const std::string&, const std::string&); static std::string get(const std::string&, const std::string&);
/** /**
* returns a value from the config. If it doesn’t exist, use * returns a value from the config. If it doesn’t exist, use
* the second argument as the default. * the second argument as the default.
* @param option The option we want
* @param def The default value in case the option does not exist
*/ */
static int get_int(const std::string&, const int&); static int get_int(const std::string&, const int&);
/** /**
* Set a value for the given option. And write all the config * Set a value for the given option. And write all the config
* in the file from which it was read if boolean is set. * in the file from which it was read if save is true.
* @param option The option to set
* @param value The value to use
* @param save if true, save the config file
*/ */
static void set(const std::string&, const std::string&, bool save = false); static void set(const std::string&, const std::string&, bool save = false);
/** /**
* Adds a function to a list. This function will be called whenever a * Adds a function to a list. This function will be called whenever a
* configuration change occurs. * configuration change occurs (when set() is called, or when the initial
* conf is read)
*/ */
static void connect(t_config_changed_callback); static void connect(t_config_changed_callback);
/** /**
* Close the config file, saving it to the file is save == true. * Destroy the instance, forcing it to be recreated (with potentially
* different parameters) the next time it’s needed.
*/ */
static void close(); static void close();
/** /**
* Set the value of the filename to use, before calling any method. * Set the value of the filename to use, before calling any method.
*/ */
+3 -3
View File
@@ -124,12 +124,12 @@ void XmppParser::end_element(const XML_Char*)
} }
} }
void XmppParser::char_data(const XML_Char* data, int len) void XmppParser::char_data(const XML_Char* data, const size_t len)
{ {
if (this->current_node->has_children()) if (this->current_node->has_children())
this->current_node->get_last_child()->add_to_tail(std::string(data, len)); this->current_node->get_last_child()->add_to_tail({data, len});
else else
this->current_node->add_to_inner(std::string(data, len)); this->current_node->add_to_inner({data, len});
} }
void XmppParser::stanza_event(const Stanza& stanza) const void XmppParser::stanza_event(const Stanza& stanza) const
+1 -1
View File
@@ -82,7 +82,7 @@ public:
/** /**
* Some inner or tail data has been parsed * Some inner or tail data has been parsed
*/ */
void char_data(const XML_Char* data, int len); void char_data(const XML_Char* data, const size_t len);
/** /**
* Calls all the stanza_callbacks one by one. * Calls all the stanza_callbacks one by one.
*/ */
+8 -2
View File
@@ -199,6 +199,11 @@ void XmlNode::set_name(const std::string& name)
this->name = name; this->name = name;
} }
void XmlNode::set_name(std::string&& name)
{
this->name = std::move(name);
}
const std::string XmlNode::get_name() const const std::string XmlNode::get_name() const
{ {
return this->name; return this->name;
@@ -228,7 +233,7 @@ bool XmlNode::has_children() const
return !this->children.empty(); return !this->children.empty();
} }
const std::string XmlNode::get_tag(const std::string& name) const const std::string& XmlNode::get_tag(const std::string& name) const
{ {
try try
{ {
@@ -237,7 +242,8 @@ const std::string XmlNode::get_tag(const std::string& name) const
} }
catch (const std::out_of_range& e) catch (const std::out_of_range& e)
{ {
return ""; static const std::string def{};
return def;
} }
} }
+5 -4
View File
@@ -96,6 +96,7 @@ public:
XmlNode* get_last_child() const; XmlNode* get_last_child() const;
XmlNode* get_parent() const; XmlNode* get_parent() const;
void set_name(const std::string& name); void set_name(const std::string& name);
void set_name(std::string&& name);
const std::string get_name() const; const std::string get_name() const;
/** /**
* Serialize the stanza into a string * Serialize the stanza into a string
@@ -110,7 +111,7 @@ public:
* Gets the value for the given attribute, returns an empty string if the * Gets the value for the given attribute, returns an empty string if the
* node as no such attribute. * node as no such attribute.
*/ */
const std::string get_tag(const std::string& name) const; const std::string& get_tag(const std::string& name) const;
/** /**
* Remove the attribute of the node. Does nothing if that attribute is not * Remove the attribute of the node. Does nothing if that attribute is not
* present. Returns true if the tag was removed, false if it was absent. * present. Returns true if the tag was removed, false if it was absent.
@@ -133,13 +134,13 @@ private:
XmlNode& operator=(XmlNode&&) = delete; XmlNode& operator=(XmlNode&&) = delete;
}; };
std::ostream& operator<<(std::ostream& os, const XmlNode& node);
/** /**
* An XMPP stanza is just an XML node of level 2 in the XMPP document (the * An XMPP stanza is just an XML node of level 2 in the XMPP document (the
* level 1 ones are the <stream::stream/>, and the ones above 2 are just the * level 1 ones are the <stream::stream/>, and the ones above 2 are just the
* content of the stanzas) * content of the stanzas)
*/ */
typedef XmlNode Stanza; using Stanza = XmlNode;
std::ostream& operator<<(std::ostream& os, const XmlNode& node);
#endif // XMPP_STANZA_INCLUDED #endif // XMPP_STANZA_INCLUDED