Skip to content

Commit

Permalink
Implemented move semantics for Config and Setting
Browse files Browse the repository at this point in the history
Signed-off-by: David C. Manuelda <[email protected]>
  • Loading branch information
StormBytePP committed Oct 31, 2024
1 parent e4c5d2c commit 4d00bed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/libconfig.h++
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ class LIBCONFIGXX_API Setting
typedef SettingConstIterator const_iterator;

public:

Setting(Setting&&) noexcept;
Setting& operator=(Setting&&) noexcept;
virtual ~Setting();

inline Type getType() const { return(_type); }
Expand Down Expand Up @@ -474,6 +475,8 @@ class LIBCONFIGXX_API Config
};

Config();
Config(Config&&) noexcept;
Config& operator=(Config&&) noexcept;
virtual ~Config();

void clear();
Expand Down Expand Up @@ -516,8 +519,8 @@ class LIBCONFIGXX_API Config
inline void readFile(const std::string &filename)
{ readFile(filename.c_str()); }

void writeFile(const char *filename) const;
inline void writeFile(const std::string &filename) const
void writeFile(const char *filename);
inline void writeFile(const std::string &filename)
{ writeFile(filename.c_str()); }

Setting & lookup(const char *path) const;
Expand Down
38 changes: 37 additions & 1 deletion lib/libconfigcpp.c++
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,22 @@ Config::Config()
config_set_fatal_error_func(__fatal_error_func);
}

Config::Config(Config&& cfg) noexcept
{
_config = cfg._config;
cfg._config = nullptr;
}

Config& Config::operator=(Config&& cfg) noexcept
{
if (this != &cfg)
{
_config = cfg._config;
cfg._config = nullptr;
}
return *this;
}

// ---------------------------------------------------------------------------

Config::~Config()
Expand Down Expand Up @@ -521,7 +537,7 @@ void Config::readFile(const char *filename)

// ---------------------------------------------------------------------------

void Config::writeFile(const char *filename) const
void Config::writeFile(const char *filename)
{
if(! config_write_file(_config, filename))
handleError();
Expand Down Expand Up @@ -689,6 +705,26 @@ Setting::Setting(config_setting_t *setting)
}
}

Setting::Setting(Setting&& setting) noexcept
{
_setting = setting._setting;
setting._setting = nullptr;
_type = std::move(setting._type);
_format = std::move(setting._format);
}

Setting& Setting::operator=(Setting&& setting) noexcept
{
if (this != &setting)
{
_setting = setting._setting;
setting._setting = nullptr;
_type = std::move(setting._type);
_format = std::move(setting._format);
}
return *this;
}

// ---------------------------------------------------------------------------

Setting::~Setting()
Expand Down

0 comments on commit 4d00bed

Please sign in to comment.