Browse Source

Various minor refactors around strings

task/remove_strings
Jessica James 3 years ago
parent
commit
b0e7aad5e3
  1. 2
      src/common/Config.cpp
  2. 51
      src/common/Plugin.cpp
  3. 2
      src/include/Jupiter/Config.h
  4. 3
      src/include/Jupiter/Functions.h
  5. 23
      src/include/Jupiter/Plugin.h
  6. 1
      src/include/Jupiter/Reference_String.h
  7. 1
      src/include/Jupiter/String.hpp
  8. 1
      src/include/Jupiter/String_Type.h

2
src/common/Config.cpp

@ -98,7 +98,7 @@ bool Jupiter::Config::read(const char *in_filename)
return this->read_internal(in_filename); return this->read_internal(in_filename);
} }
bool Jupiter::Config::read(const Jupiter::ReadableString &in_filename) bool Jupiter::Config::read(const std::string_view& in_filename)
{ {
m_name = static_cast<std::string>(in_filename); m_name = static_cast<std::string>(in_filename);
return this->read_internal(m_name.c_str()); return this->read_internal(m_name.c_str());

51
src/common/Plugin.cpp

@ -75,8 +75,8 @@ const char module_file_extension[]{ ".so" };
const Jupiter::ReferenceString config_file_extension = ".ini"_jrs; const Jupiter::ReferenceString config_file_extension = ".ini"_jrs;
Jupiter::StringS plugins_directory = "Plugins"_jrs + directory_character; std::string plugins_directory = "Plugins"s + directory_character;
Jupiter::StringS plugin_configs_directory = "Configs"_jrs + directory_character; std::string plugin_configs_directory = "Configs"s + directory_character;
std::vector<Jupiter::Plugin*> g_plugins; // Array of weak pointers to plugin instances generally stored in static memory std::vector<Jupiter::Plugin*> g_plugins; // Array of weak pointers to plugin instances generally stored in static memory
std::vector<Jupiter::Plugin*>& Jupiter::plugins = g_plugins; std::vector<Jupiter::Plugin*>& Jupiter::plugins = g_plugins;
@ -120,27 +120,31 @@ void Jupiter::Plugin::OnPostInitialize() {
// Static Functions // Static Functions
void Jupiter::Plugin::setDirectory(const Jupiter::ReadableString &dir) { void Jupiter::Plugin::setDirectory(const Jupiter::ReadableString &dir) {
if (plugins_directory.set(dir) != 0 && plugins_directory.get(plugins_directory.size() - 1) != directory_character) { plugins_directory = dir;
if (!plugins_directory.empty() && plugins_directory.back() != directory_character) {
plugins_directory += directory_character; plugins_directory += directory_character;
} }
} }
const Jupiter::ReadableString &Jupiter::Plugin::getDirectory() { const std::string& Jupiter::Plugin::getDirectory() {
return plugins_directory; return plugins_directory;
} }
void Jupiter::Plugin::setConfigDirectory(const Jupiter::ReadableString &dir) { void Jupiter::Plugin::setConfigDirectory(const Jupiter::ReadableString &dir) {
if (plugin_configs_directory.set(dir) != 0 && plugin_configs_directory.get(plugin_configs_directory.size() - 1) != directory_character) { plugin_configs_directory = dir;
if (!plugin_configs_directory.empty() && plugin_configs_directory.back() != directory_character) {
plugin_configs_directory += directory_character; plugin_configs_directory += directory_character;
} }
} }
const Jupiter::ReadableString &Jupiter::Plugin::getConfigDirectory() { const std::string& Jupiter::Plugin::getConfigDirectory() {
return plugin_configs_directory; return plugin_configs_directory;
} }
Jupiter::Plugin *Jupiter::Plugin::load(const Jupiter::ReadableString &pluginName) { Jupiter::Plugin *Jupiter::Plugin::load(const std::string_view& pluginName) {
std::string file = static_cast<std::string>(plugins_directory) + static_cast<std::string>(pluginName) + module_file_extension; std::string file = plugins_directory;
file += pluginName;
file += module_file_extension;
std::unique_ptr<dlib> dPlug{ std::make_unique<dlib>() }; std::unique_ptr<dlib> dPlug{ std::make_unique<dlib>() };
// Load the library // Load the library
@ -176,8 +180,11 @@ Jupiter::Plugin *Jupiter::Plugin::load(const Jupiter::ReadableString &pluginName
} }
// Initialize the plugin // Initialize the plugin
weak_plugin->name.set(pluginName); weak_plugin->name = Jupiter::ReferenceString{pluginName};
weak_plugin->config.read(plugin_configs_directory + pluginName + config_file_extension); std::string config_path = plugin_configs_directory;
config_path += pluginName;
config_path += config_file_extension;
weak_plugin->config.read(config_path);
weak_plugin->initialize(); weak_plugin->initialize();
} }
{ {
@ -236,20 +243,6 @@ bool Jupiter::Plugin::free(Jupiter::Plugin *plugin) {
return false; return false;
} }
bool Jupiter::Plugin::free(const Jupiter::ReadableString &pluginName) {
if (pluginName == nullptr) {
return false;
}
for (size_t index = 0; index != g_plugins.size(); ++index) {
if (pluginName.matchi(g_plugins[index]->getName())) {
return Jupiter::Plugin::free(index);
}
}
return false;
}
Jupiter::Plugin *Jupiter::Plugin::get(size_t index) { Jupiter::Plugin *Jupiter::Plugin::get(size_t index) {
if (index < g_plugins.size()) { if (index < g_plugins.size()) {
return g_plugins[index]; return g_plugins[index];
@ -258,16 +251,6 @@ Jupiter::Plugin *Jupiter::Plugin::get(size_t index) {
return nullptr; return nullptr;
} }
Jupiter::Plugin *Jupiter::Plugin::get(const Jupiter::ReadableString &pluginName) {
for (const auto& plugin : g_plugins) {
if (pluginName.matchi(plugin->getName())) {
return plugin;
}
}
return nullptr;
}
// Event Implementations // Event Implementations
int Jupiter::Plugin::think() { int Jupiter::Plugin::think() {

2
src/include/Jupiter/Config.h

@ -144,7 +144,7 @@ namespace Jupiter
* @param in_filename Name of the file to read from * @param in_filename Name of the file to read from
* @return True on success, false otherwise * @return True on success, false otherwise
*/ */
bool read(const Jupiter::ReadableString &in_filename); bool read(const std::string_view& in_filename);
/** /**
* @brief Writes config data to the last read file * @brief Writes config data to the last read file

3
src/include/Jupiter/Functions.h

@ -30,6 +30,9 @@
#if defined __cplusplus #if defined __cplusplus
// Just quickly using a macro because I don't want to risk having to deal with more DLL linkage warnings
#define WHITESPACE_SV std::string_view{" \t"}
#include <cstdint> #include <cstdint>
#include <cstddef> #include <cstddef>
#include <type_traits> #include <type_traits>

23
src/include/Jupiter/Plugin.h

@ -303,7 +303,7 @@ namespace Jupiter
* *
* @return String containing the directory which is prepended to module load attempts. * @return String containing the directory which is prepended to module load attempts.
*/ */
static const Jupiter::ReadableString &getDirectory(); static const std::string& getDirectory();
/** /**
* @brief Sets the directory to look in for plugin configuration files. * @brief Sets the directory to look in for plugin configuration files.
@ -317,7 +317,7 @@ namespace Jupiter
* *
* @return String containing the directory which is prepended to configuration load attempts. * @return String containing the directory which is prepended to configuration load attempts.
*/ */
static const Jupiter::ReadableString &getConfigDirectory(); static const std::string& getConfigDirectory();
/** /**
* @brief Loads a module, appending .so or .dll as appropriate for the operating system. * @brief Loads a module, appending .so or .dll as appropriate for the operating system.
@ -325,7 +325,7 @@ namespace Jupiter
* @param pluginName The name of the plugin to load. * @param pluginName The name of the plugin to load.
* @return A pointer to the plugin that was loaded on success, nullptr otherwise. * @return A pointer to the plugin that was loaded on success, nullptr otherwise.
*/ */
static Jupiter::Plugin *load(const Jupiter::ReadableString &pluginName); static Jupiter::Plugin *load(const std::string_view& pluginName);
/** /**
* @brief Unloads a module and removes it from the module list, based on its index. * @brief Unloads a module and removes it from the module list, based on its index.
@ -343,15 +343,6 @@ namespace Jupiter
*/ */
static bool free(Jupiter::Plugin *plugin); static bool free(Jupiter::Plugin *plugin);
/**
* @brief Unloads a module and removes it from the module list, based on its name.
* Note: This function accepts wildcard strings.
*
* @param pluginName Name of the module to unload.
* @return True if a module was unloaded, false otherwise.
*/
static bool free(const Jupiter::ReadableString &pluginName);
/** /**
* @brief Fetches a plugin from the list and returns it, based on its index. * @brief Fetches a plugin from the list and returns it, based on its index.
* *
@ -360,14 +351,6 @@ namespace Jupiter
*/ */
static Jupiter::Plugin *get(size_t index); static Jupiter::Plugin *get(size_t index);
/**
* @brief Fetches a plugin from the list and returns it, based on its name.
*
* @param pluginName String containing the name of the plugin.
* @return A module on success, nullptr otherwise.
*/
static Jupiter::Plugin *get(const Jupiter::ReadableString &pluginName);
protected: protected:
bool _shouldRemove = false; bool _shouldRemove = false;
Jupiter::StringS name; Jupiter::StringS name;

1
src/include/Jupiter/Reference_String.h

@ -297,6 +297,7 @@ namespace Jupiter
* @brief Copy constructor for the Reference_String class. * @brief Copy constructor for the Reference_String class.
*/ */
Reference_String(const Jupiter::Reference_String<T> &in) = default; Reference_String(const Jupiter::Reference_String<T> &in) = default;
Reference_String(const std::basic_string_view<T>&in) : std::basic_string_view<T>(in){}
// Bring in constructors from basic_string_view // Bring in constructors from basic_string_view
using std::basic_string_view<T>::basic_string_view; using std::basic_string_view<T>::basic_string_view;

1
src/include/Jupiter/String.hpp

@ -244,6 +244,7 @@ namespace Jupiter
String_Strict(const String_Strict<T> &in); String_Strict(const String_Strict<T> &in);
String_Strict(const Readable_String<T> &in); String_Strict(const Readable_String<T> &in);
String_Strict(const std::basic_string<T> &in); String_Strict(const std::basic_string<T> &in);
//String_Strict(const std::basic_string_view<T> &in) : String_Strict<T>(in.data(), in.size()){};
String_Strict(const T *in, size_t len); String_Strict(const T *in, size_t len);
String_Strict(const T *in); String_Strict(const T *in);
String_Strict(const Jupiter::DataBuffer &in); String_Strict(const Jupiter::DataBuffer &in);

1
src/include/Jupiter/String_Type.h

@ -277,6 +277,7 @@ namespace Jupiter
inline String_Type<T> &operator=(const Readable_String<T> &right) { this->set(right); return *this; }; inline String_Type<T> &operator=(const Readable_String<T> &right) { this->set(right); return *this; };
inline String_Type<T> &operator=(const String_Type<T> &right) { this->set(right); return *this; }; inline String_Type<T> &operator=(const String_Type<T> &right) { this->set(right); return *this; };
inline String_Type<T> &operator=(const std::basic_string<T> &right) { this->set(right); return *this; }; inline String_Type<T> &operator=(const std::basic_string<T> &right) { this->set(right); return *this; };
inline String_Type<T> &operator=(const std::basic_string_view<T> &right) { this->set(right.data(), right.size()); return *this; };
inline String_Type<T> &operator=(const T *right) { this->set(right); return *this; }; inline String_Type<T> &operator=(const T *right) { this->set(right); return *this; };
inline String_Type<T> &operator=(const T right) { this->set(right); return *this; }; inline String_Type<T> &operator=(const T right) { this->set(right); return *this; };

Loading…
Cancel
Save