Skip to content

Commit

Permalink
Created ListsDeclaration.inc and ListsBody.inc to create list of obje…
Browse files Browse the repository at this point in the history
…cts (or base types) without exposing std::vector or std::list.

Modified Configuration.h to declare class ConfigurationPtrList.
Replaced usage of Node class in ConfigManager.
For issue #102
  • Loading branch information
end2endzone committed Dec 28, 2021
1 parent e15348a commit deccf01
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 44 deletions.
9 changes: 6 additions & 3 deletions include/shellanything/ConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#ifndef SA_CONFIGMANAGER_H
#define SA_CONFIGMANAGER_H

#include "shellanything/Node.h"
#include "shellanything/Configuration.h"
#include "shellanything/Context.h"

Expand Down Expand Up @@ -58,7 +57,7 @@ namespace shellanything
/// <summary>
/// Get the list of Configuration pointers handled by the manager
/// </summary>
Configuration::ConfigurationPtrList GetConfigurations();
ConfigurationPtrList & GetConfigurations();

/// <summary>
/// Returns true if the given path is a Configuration loaded by the manager.
Expand Down Expand Up @@ -111,9 +110,13 @@ namespace shellanything
void AddSearchPath(const std::string & path);

private:
//methods
void DeleteAllConfigurations();
void DeleteConfiguration(Configuration * config);

//attributes
PathList mPaths;
Node mConfigurations;
ConfigurationPtrList mConfigurations;
};

} //namespace shellanything
Expand Down
14 changes: 9 additions & 5 deletions include/shellanything/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ namespace shellanything
class Configuration : public Node
{
public:
/// <summary>
/// A list of Configuration pointer.
/// </summary>
typedef std::vector<Configuration*> ConfigurationPtrList;

Configuration();
virtual ~Configuration();

Expand Down Expand Up @@ -137,4 +132,13 @@ namespace shellanything

} //namespace shellanything

/// <summary>
/// A list of Configuration pointer.
/// </summary>
#define SA_LISTS_CLASS_NAME ConfigurationPtrList
#define SA_LISTS_BASE_TYPE Configuration *
#include "shellanything/ListsDeclaration.inc"
#undef SA_LISTS_BASE_TYPE
#undef SA_LISTS_CLASS_NAME

#endif //SA_CONFIGURATION_H
111 changes: 111 additions & 0 deletions include/shellanything/ListsBody.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**********************************************************************************
* MIT License
*
* Copyright (c) 2018 Antoine Beauchamp
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*********************************************************************************/

namespace shellanything
{

struct SA_LISTS_CLASS_NAME::PImpl
{
std::vector<SA_LISTS_CLASS_NAME::Element> elements;
};

SA_LISTS_CLASS_NAME::SA_LISTS_CLASS_NAME()
{
mPImpl = new SA_LISTS_CLASS_NAME::PImpl();
}

SA_LISTS_CLASS_NAME::~SA_LISTS_CLASS_NAME()
{
delete mPImpl;
}

SA_LISTS_CLASS_NAME::SA_LISTS_CLASS_NAME(const SA_LISTS_CLASS_NAME& listcopy)
{
mPImpl = new SA_LISTS_CLASS_NAME::PImpl();
(*this) = listcopy;
}

SA_LISTS_CLASS_NAME& SA_LISTS_CLASS_NAME::operator=(const SA_LISTS_CLASS_NAME& listcopy)
{
mPImpl->elements = listcopy.mPImpl->elements;
return (*this);
}

size_t SA_LISTS_CLASS_NAME::GetSize() const
{
return mPImpl->elements.size();
}

void SA_LISTS_CLASS_NAME::Clear()
{
return mPImpl->elements.clear();
}

SA_LISTS_CLASS_NAME::Element & SA_LISTS_CLASS_NAME::GetElement(size_t index)
{
return mPImpl->elements[index];
}

const SA_LISTS_CLASS_NAME::Element & SA_LISTS_CLASS_NAME::GetElement(size_t index) const
{
return mPImpl->elements[index];
}

size_t SA_LISTS_CLASS_NAME::FindElement(const SA_LISTS_CLASS_NAME::Element & value) const
{
//std::vector<SA_LISTS_CLASS_NAME::Element>::iterator it;
//it = std::find(mPImpl->elements.begin(), mPImpl->elements.end(), value);
//bool found = (it != mPImpl->elements.end());
//return found;

std::vector<SA_LISTS_CLASS_NAME::Element>& v = mPImpl->elements;
for (size_t i = 0; i < v.size(); i++)
{
const SA_LISTS_CLASS_NAME::Element & element = v[i];
if (element == value)
return i;
}

return INVALID_INDEX;
}

void SA_LISTS_CLASS_NAME::AddElement(SA_LISTS_CLASS_NAME::Element instance)
{
return mPImpl->elements.push_back(instance);
}

void SA_LISTS_CLASS_NAME::RemoveElementByValue(SA_LISTS_CLASS_NAME::Element value)
{
size_t index = FindElement(value);
if (index != INVALID_INDEX)
RemoveElementByIndex(index);
}

void SA_LISTS_CLASS_NAME::RemoveElementByIndex(size_t index)
{
if (index != INVALID_INDEX)
mPImpl->elements.erase(mPImpl->elements.begin() + index);
}

} //namespace shellanything
98 changes: 98 additions & 0 deletions include/shellanything/ListsDeclaration.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**********************************************************************************
* MIT License
*
* Copyright (c) 2018 Antoine Beauchamp
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*********************************************************************************/

namespace shellanything
{

/// <summary>
/// A structure that contains a list of Configuration class pointers.
/// </summary>
class SA_LISTS_CLASS_NAME
{
public:
SA_LISTS_CLASS_NAME();
virtual ~SA_LISTS_CLASS_NAME();

static const size_t INVALID_INDEX = (size_t)-1;

SA_LISTS_CLASS_NAME(const SA_LISTS_CLASS_NAME& listcopy);
SA_LISTS_CLASS_NAME& operator=(const SA_LISTS_CLASS_NAME& listcopy);

/// <summary>
/// A custom type definition for the base type
/// </summary>
typedef SA_LISTS_BASE_TYPE Element;

/// <summary>
/// Get the number of element in the list.
/// </summary>
size_t GetSize() const;
inline size_t size() const { return GetSize(); } // for std::vector and std::list code compatibility.

/// <summary>
/// Remove all elements in the list.
/// </summary>
void Clear();

/// <summary>
/// Get an element from an index.
/// </summary>
/// <param name="index">The index of the requested instance.</param>
/// <returns>Returns a valid element if the index is valid. Returns an invalid element otherwise.</returns>
Element & GetElement(size_t index);
const Element & GetElement(size_t index) const;
inline Element & operator[](size_t index) { return GetElement(index); }
inline const Element & operator[](size_t index) const { return GetElement(index); }

/// <summary>
/// Find an element in the list.
/// </summary>
/// <param name="value">A value to find.</param>
/// <returns>Returns true when the element is found. Returns false otherwise.</returns>
size_t FindElement(const Element & value) const;

/// <summary>
/// Add a value to the list.
/// </summary>
/// <param name="value">The value to add.</param>
void AddElement(Element value);

/// <summary>
/// Remove a value from the list.
/// </summary>
/// <param name="value">The value to remove.</param>
void RemoveElementByValue(Element value);

/// <summary>
/// Remove a value from the list.
/// </summary>
/// <param name="index">The index of the value to remove.</param>
void RemoveElementByIndex(size_t index);

private:
struct PImpl;
PImpl* mPImpl;
};

} //namespace shellanything
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ set(SHELLANYTHING_HEADER_FILES ""
${CMAKE_SOURCE_DIR}/include/shellanything/Context.h
${CMAKE_SOURCE_DIR}/include/shellanything/DefaultSettings.h
${CMAKE_SOURCE_DIR}/include/shellanything/Icon.h
${CMAKE_SOURCE_DIR}/include/shellanything/ListsBody.inc
${CMAKE_SOURCE_DIR}/include/shellanything/ListsDeclaration.inc
${CMAKE_SOURCE_DIR}/include/shellanything/Menu.h
${CMAKE_SOURCE_DIR}/include/shellanything/Node.h
${CMAKE_SOURCE_DIR}/include/shellanything/Validator.h
Expand Down
45 changes: 30 additions & 15 deletions src/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace shellanything
void ConfigManager::Clear()
{
ClearSearchPath(); //remove all search path to make sure that a refresh won’t find any other configuration file
mConfigurations.RemoveChildren();
DeleteAllConfigurations();
Refresh(); //forces all loaded configurations to be unloaded
}

Expand All @@ -62,10 +62,10 @@ namespace shellanything
LOG(INFO) << __FUNCTION__ << "()";

//validate existing configurations
Configuration::ConfigurationPtrList existing = GetConfigurations();
for(size_t i=0; i<existing.size(); i++)
ConfigurationPtrList configs_copy = GetConfigurations();
for(size_t i=0; i< configs_copy.size(); i++)
{
Configuration * config = existing[i];
Configuration * config = configs_copy[i];

//compare the file's date at the load time and the current date
const std::string & file_path = config->GetFilePath();
Expand All @@ -81,7 +81,7 @@ namespace shellanything
//file is missing or current configuration is out of date
//forget about existing config
LOG(INFO) << "Configuration file '" << file_path << "' is missing or is not up to date. Deleting configuration.";
mConfigurations.RemoveChild(config);
DeleteConfiguration(config);
}
}

Expand Down Expand Up @@ -119,7 +119,7 @@ namespace shellanything
else
{
//add to current list of configurations
mConfigurations.AddChild(config);
mConfigurations.AddElement(config);

//apply default properties of the configuration
config->ApplyDefaultSettings();
Expand All @@ -143,7 +143,7 @@ namespace shellanything
void ConfigManager::Update(const Context & context)
{
//for each child
Configuration::ConfigurationPtrList configurations = ConfigManager::GetConfigurations();
ConfigurationPtrList & configurations = ConfigManager::GetConfigurations();
for(size_t i=0; i<configurations.size(); i++)
{
Configuration * config = configurations[i];
Expand All @@ -154,7 +154,7 @@ namespace shellanything
Menu * ConfigManager::FindMenuByCommandId(const uint32_t & command_id)
{
//for each child
Configuration::ConfigurationPtrList configurations = ConfigManager::GetConfigurations();
ConfigurationPtrList & configurations = ConfigManager::GetConfigurations();
for(size_t i=0; i<configurations.size(); i++)
{
Configuration * config = configurations[i];
Expand All @@ -171,7 +171,7 @@ namespace shellanything
uint32_t nextCommandId = first_command_id;

//for each child
Configuration::ConfigurationPtrList configurations = ConfigManager::GetConfigurations();
ConfigurationPtrList & configurations = ConfigManager::GetConfigurations();
for(size_t i=0; i<configurations.size(); i++)
{
Configuration * config = configurations[i];
Expand All @@ -181,10 +181,9 @@ namespace shellanything
return nextCommandId;
}

Configuration::ConfigurationPtrList ConfigManager::GetConfigurations()
ConfigurationPtrList & ConfigManager::GetConfigurations()
{
Configuration::ConfigurationPtrList configurations = FilterNodes<Configuration*>(mConfigurations.FindChildren("Configuration"));
return configurations;
return mConfigurations;
}

void ConfigManager::ClearSearchPath()
Expand All @@ -199,14 +198,30 @@ namespace shellanything

bool ConfigManager::IsConfigFileLoaded(const std::string & path) const
{
for(size_t i=0; i<mConfigurations.Size(); i++)
for(size_t i=0; i<mConfigurations.size(); i++)
{
const Node * node = mConfigurations.GetChild(i);
const Configuration * config = dynamic_cast<const Configuration *>(node);
const Configuration* config = mConfigurations[i];
if (config != NULL && config->GetFilePath() == path)
return true;
}
return false;
}

void ConfigManager::DeleteAllConfigurations()
{
size_t count = mConfigurations.GetSize();
for (size_t i = 0; i < count; i++)
{
Configuration* config = mConfigurations.GetElement(i);
delete config;
}
mConfigurations.Clear();
}

void ConfigManager::DeleteConfiguration(Configuration* config)
{
mConfigurations.RemoveElementByValue(config);
delete config;
}

} //namespace shellanything
Loading

0 comments on commit deccf01

Please sign in to comment.