Skip to content

Commit

Permalink
Issue #44 - Move import export helper functions out of PTN_EngineImp
Browse files Browse the repository at this point in the history
Create ActionsThreadOptionConversions to help convert
the enumeration ACTIONS_THREAD_OPTION to and from string.
  • Loading branch information
vldtecno committed May 20, 2024
1 parent 526db0a commit c238c70
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 84 deletions.
85 changes: 85 additions & 0 deletions PTN_Engine/ImportExport/ActionsThreadOptionConversions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* This file is part of PTN Engine
*
* Copyright (c) 2024 Eduardo Valgôde
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "PTN_Engine/ImportExport/ActionsThreadOptionConversions.h"
#include "PTN_Engine/PTN_Exception.h"

namespace ptne
{
using namespace std;

const string ActionsThreadOptionConversions::ACTIONS_THREAD_OPTION_SINGLE_THREAD = "SINGLE_THREAD";
const string ActionsThreadOptionConversions::ACTIONS_THREAD_OPTION_EVENT_LOOP = "EVENT_LOOP";
const string ActionsThreadOptionConversions::ACTIONS_THREAD_OPTION_DETACHED = "DETACHED";
const string ActionsThreadOptionConversions::ACTIONS_THREAD_OPTION_JOB_QUEUE = "JOB_QUEUE";

PTN_Engine::ACTIONS_THREAD_OPTION
ActionsThreadOptionConversions::toACTIONS_THREAD_OPTION(const string &actionsThreadOptionStr)
{
using enum PTN_Engine::ACTIONS_THREAD_OPTION;
if (actionsThreadOptionStr == ACTIONS_THREAD_OPTION_SINGLE_THREAD)
{
return SINGLE_THREAD;
}
else if (actionsThreadOptionStr == ACTIONS_THREAD_OPTION_EVENT_LOOP)
{
return EVENT_LOOP;
}
else if (actionsThreadOptionStr == ACTIONS_THREAD_OPTION_DETACHED)
{
return DETACHED;
}
else if (actionsThreadOptionStr == ACTIONS_THREAD_OPTION_JOB_QUEUE)
{
return JOB_QUEUE;
}
else
{
throw PTN_Exception("Could not convert " + actionsThreadOptionStr + " to ACTIONS_THREAD_OPTION");
}
}

string ActionsThreadOptionConversions::toString(PTN_Engine::ACTIONS_THREAD_OPTION actionsThreadOption)
{
using enum PTN_Engine::ACTIONS_THREAD_OPTION;
switch (actionsThreadOption)
{
default:
{
throw PTN_Exception("Could not convert to string");
}
case DETACHED:
{
return ACTIONS_THREAD_OPTION_DETACHED;
}
case SINGLE_THREAD:
{
return ACTIONS_THREAD_OPTION_SINGLE_THREAD;
}
case EVENT_LOOP:
{
return ACTIONS_THREAD_OPTION_EVENT_LOOP;
}
case JOB_QUEUE:
{
return ACTIONS_THREAD_OPTION_JOB_QUEUE;
}
}
}

} // namespace ptne
36 changes: 36 additions & 0 deletions PTN_Engine/ImportExport/ActionsThreadOptionConversions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This file is part of PTN Engine
*
* Copyright (c) 2024 Eduardo Valgôde
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "PTN_Engine/PTN_Engine.h"
namespace ptne
{

class ActionsThreadOptionConversions
{
public:
static PTN_Engine::ACTIONS_THREAD_OPTION toACTIONS_THREAD_OPTION(const std::string &actionsThreadOptionStr);
static std::string toString(PTN_Engine::ACTIONS_THREAD_OPTION actionsThreadOption);

private:
static const std::string ACTIONS_THREAD_OPTION_SINGLE_THREAD;
static const std::string ACTIONS_THREAD_OPTION_EVENT_LOOP;
static const std::string ACTIONS_THREAD_OPTION_DETACHED;
static const std::string ACTIONS_THREAD_OPTION_JOB_QUEUE;
};

} // namespace ptne
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/

#include "PTN_Engine/ImportExport/IFileExporter.h"
#include "PTN_Engine/ImportExport/ActionsThreadOptionConversions.h"
#include "PTN_Engine/PTN_Engine.h"
#include "PTN_Engine/PTN_Exception.h"

namespace ptne
Expand All @@ -30,7 +32,7 @@ void IFileExporter::_export(const PTN_Engine &ptnEngine)
throw PTN_Exception("Cannot change actions thread option while the event loop is running.");
}

exportActionsThreadOption(PTN_Engine::toString(ptnEngine.getActionsThreadOption()));
exportActionsThreadOption(ActionsThreadOptionConversions::toString(ptnEngine.getActionsThreadOption()));

for (const auto &placeProperties : ptnEngine.getPlacesProperties())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/

#include "PTN_Engine/ImportExport/IFileImporter.h"
#include "PTN_Engine/ImportExport/ActionsThreadOptionConversions.h"
#include "PTN_Engine/PTN_Engine.h"

namespace ptne
{
Expand All @@ -30,7 +32,8 @@ void IFileImporter::_import(PTN_Engine &ptnEngine) const
}

string actionsThreadOptionStr = importActionsThreadOption();
ptnEngine.setActionsThreadOption(std::move(PTN_Engine::toACTIONS_THREAD_OPTION(actionsThreadOptionStr)));
ptnEngine.setActionsThreadOption(
std::move(ActionsThreadOptionConversions::toACTIONS_THREAD_OPTION(actionsThreadOptionStr)));

for (const auto &placeProperties : importPlaces())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

#pragma once

#include "PTN_Engine/PTN_Engine.h"
#include "PTN_Engine/Utilities/Explicit.h"
#include <string>

namespace ptne
{

class PTN_Engine;
class PlaceProperties;
class TransitionProperties;

//!
//! \brief The IFileExporter class is an interface for all PTN_Engine file exporters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@

#pragma once

#include "PTN_Engine/PTN_Engine.h"
#include "PTN_Engine/Utilities/Explicit.h"
#include <string>
#include <vector>

namespace ptne
{

class ArcProperties;
class PTN_Engine;
class PlaceProperties;
class TransitionProperties;

//!
//! \brief The IFileImporter class is an interface class for all PTN_Engine file importers.
Expand Down
62 changes: 0 additions & 62 deletions PTN_Engine/PTN_Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,66 +125,4 @@ void PTN_Engine::stop()
m_impProxy->stop();
}

namespace
{
const string ACTIONS_THREAD_OPTION_SINGLE_THREAD{ "SINGLE_THREAD" };
const string ACTIONS_THREAD_OPTION_EVENT_LOOP{ "EVENT_LOOP" };
const string ACTIONS_THREAD_OPTION_DETACHED{ "DETACHED" };
const string ACTIONS_THREAD_OPTION_JOB_QUEUE{ "JOB_QUEUE" };
}

PTN_Engine::ACTIONS_THREAD_OPTION
PTN_Engine::toACTIONS_THREAD_OPTION(const string &actionsThreadOptionStr)
{
using enum ACTIONS_THREAD_OPTION;
if (actionsThreadOptionStr == ACTIONS_THREAD_OPTION_SINGLE_THREAD)
{
return SINGLE_THREAD;
}
else if (actionsThreadOptionStr == ACTIONS_THREAD_OPTION_EVENT_LOOP)
{
return EVENT_LOOP;
}
else if (actionsThreadOptionStr == ACTIONS_THREAD_OPTION_DETACHED)
{
return DETACHED;
}
else if (actionsThreadOptionStr == ACTIONS_THREAD_OPTION_JOB_QUEUE)
{
return JOB_QUEUE;
}
else
{
throw PTN_Exception("Could not convert " + actionsThreadOptionStr + " to ACTIONS_THREAD_OPTION");
}
}

string PTN_Engine::toString(ACTIONS_THREAD_OPTION actionsThreadOption)
{
using enum ACTIONS_THREAD_OPTION;
switch (actionsThreadOption)
{
default:
{
throw PTN_Exception("Could not convert to string");
}
case DETACHED:
{
return ACTIONS_THREAD_OPTION_DETACHED;
}
case SINGLE_THREAD:
{
return ACTIONS_THREAD_OPTION_SINGLE_THREAD;
}
case EVENT_LOOP:
{
return ACTIONS_THREAD_OPTION_EVENT_LOOP;
}
case JOB_QUEUE:
{
return ACTIONS_THREAD_OPTION_JOB_QUEUE;
}
}
}

} // namespace ptne
23 changes: 5 additions & 18 deletions PTN_Engine/include/PTN_Engine/PTN_Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ using ActionFunction = std::function<void(void)>;
*/
struct DLL_PUBLIC ArcProperties final
{
enum class Type {
enum class Type
{
ACTIVATION,
DESTINATION,
BIDIRECTIONAL,
INHIBITOR,
//RESET,
// RESET,
};

/*!
Expand Down Expand Up @@ -164,20 +165,6 @@ class DLL_PUBLIC PTN_Engine
JOB_QUEUE
};

/*!
* \brief toACTIONS_THREAD_OPTION
* \param actionsThreadOptionStr
* \return
*/
static ACTIONS_THREAD_OPTION toACTIONS_THREAD_OPTION(const std::string &actionsThreadOptionStr);

/*!
* \brief toString
* \param actionsThreadOption
* \return
*/
static std::string toString(ACTIONS_THREAD_OPTION actionsThreadOption);

using EventLoopSleepDuration = std::chrono::duration<long, std::ratio<1, 1000>>;

virtual ~PTN_Engine();
Expand Down Expand Up @@ -276,13 +263,13 @@ class DLL_PUBLIC PTN_Engine
* \brief addArc
* \param arcProperties
*/
void addArc(const ArcProperties& arcProperties);
void addArc(const ArcProperties &arcProperties);

/*!
* \brief addArc
* \param arcProperties
*/
void removeArc(const ArcProperties& arcProperties);
void removeArc(const ArcProperties &arcProperties);

/*!
* \brief clearNet
Expand Down

0 comments on commit c238c70

Please sign in to comment.