-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#5] updates to config::Config, definition of cemuhook::Config
- Loading branch information
Showing
5 changed files
with
249 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include "config.h" | ||
#include <stdexcept> | ||
|
||
namespace kmicki::config | ||
{ | ||
template<class T> | ||
ConfigItem<T>::ConfigItem(std::string const& name, T const& val) | ||
{ | ||
Name = name; | ||
Val = val; | ||
} | ||
|
||
template<class T> | ||
void Config::SetValue<T>(std::string const& name, T const& value) | ||
{ | ||
auto item = std::find_if(configData.begin(),configData.end(), | ||
[&](std::unique_ptr<ConfigItemBase> const& x { x->Name == name }); | ||
|
||
if(item == configData.end()) | ||
{ | ||
configData.emplace_back(new ConfigItem(name,value)); | ||
return; | ||
} | ||
|
||
item->Val = value; | ||
} | ||
|
||
template<class T> | ||
bool Config::GetItem<T>(std::string const& name, ConfigItem<T> * value) const | ||
{ | ||
auto item = std::find_if(configData.begin(),configData.end(), | ||
[&](std::unique_ptr<ConfigItemBase> const& x { x->Name == name }); | ||
|
||
if(item == configData.end()) | ||
{ | ||
value = nullptr; | ||
return false; | ||
} | ||
|
||
value = &*item; | ||
return true; | ||
} | ||
|
||
template<class T> | ||
bool Config::GetValue<T>(std::string const& name, T * value) const | ||
{ | ||
ConfigItem<T> * item; | ||
if(!GetItem(name,item)) | ||
{ | ||
value = nullptr; | ||
return false; | ||
} | ||
|
||
value = item->Val; | ||
return true; | ||
} | ||
|
||
template<class T> | ||
bool Config::GetValue<T>(std::string const& name, T * value, bool & susbscription) const | ||
{ | ||
T * item; | ||
if(!GetItem(name,item)) | ||
return false; | ||
|
||
if(*value != *item) | ||
susbscription = true; | ||
|
||
value = item; | ||
return true; | ||
} | ||
|
||
template<class T> | ||
T const& Config::GetValue<T>(std::string const& name) const | ||
{ | ||
T* value; | ||
if(!GetValue(name,value)) | ||
throw std::invalid_argument("name"); | ||
|
||
return *value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include "cemuhook/config.h" | ||
#include <sstream> | ||
#include <algorithm> | ||
|
||
namespace kmicki::config | ||
{ | ||
using ConfigInterface = cemuhook::ConfigInterface; | ||
static const int cConfigInterfaceEnumLen = 2; | ||
static std::string cConfigInterfaceStrVals[cEnumLen] = { "local" , "all" }; | ||
|
||
template<> | ||
bool ConfigItem<ConfigInterface>::Update(std::string const& value,std::string & message) | ||
{ | ||
|
||
std::string strVal(value.length(),' '); | ||
std::transform(value.begin(),value.end(),strVal.begin(),[](char c){ return std::tolower(c); }); | ||
for (int i = 0; i < cConfigInterfaceEnumLen; ++i) | ||
if (strVal == cConfigInterfaceStrVals[i]) | ||
{ | ||
Val = (ConfigInterface)i; | ||
std::ostringstream str; | ||
str << "existing item value updated (type: string), Name=" << Name << " Value=" << strVal; | ||
message = str.str(); | ||
return true; | ||
} | ||
|
||
std::ostringstream str; | ||
str << "value invalid (type: interface), Name=" << Name << " Value=" << value; | ||
message = str.str(); | ||
return false; | ||
} | ||
|
||
template<> | ||
std::string ConfigItem<ConfigInterface>::ValToString() const | ||
{ | ||
if(Val >= 0 && Val <= cConfigInterfaceEnumLen) | ||
return cConfigInterfaceStrVals[Val]; | ||
return ""; | ||
} | ||
|
||
template struct ConfigItem<ConfigInterface>; | ||
} | ||
|
||
namespace kmicki::cemuhook | ||
{ | ||
static const std::string cPrefix = "server."; | ||
|
||
Config::Config(std::vector<std::unique_ptr<ConfigItemBase>> & _configData) | ||
: config::Config(_configData) | ||
{ | ||
ToDefault(); | ||
Load(); | ||
} | ||
|
||
ConfigInterface const& Config::Interface() const | ||
{ | ||
return interface; | ||
} | ||
|
||
int const& Config::Port() const | ||
{ | ||
return port; | ||
} | ||
|
||
static const std::string cInterfaceStr = cPrefix + "interface"; | ||
static const std::string cPortStr = cPrefix + "port"; | ||
|
||
void Config::Load() | ||
{ | ||
bool subscription = false; | ||
GetValue(cInterfaceStr,&interface,subscription); | ||
GetValue(cPortStr,&port,subscription); | ||
if(subscription) | ||
FireSubscriptions(); | ||
} | ||
|
||
void Config::Save() | ||
{ | ||
SetValue(cInterfaceStr,interface); | ||
SetValue(cPortStr,port); | ||
} | ||
|
||
void Config::ToDefault() | ||
{ | ||
static const ConfigInterface cInterfaceDefault = CfgIfAll; | ||
static const int cPortDefault = 26760; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters