Conscience Core
Command.h
Go to the documentation of this file.
1 #ifndef ConscienceEnvironmentInteractionCommand_h
2 #define ConscienceEnvironmentInteractionCommand_h
3 
4 #include "CscCommon.h"
12 #include "CscCommandsProcessor.h"
14 
15 typedef string CscWorldElementId;
16 
17 using std::fstream, std::stringstream, std::istringstream, std::ios;
18 
19 // those forward references below are very useful to optimize compilation. Please do not include these directly
20 namespace conscience_core::ai {
21 class CscEntityReflexion;
22 }
23 using namespace conscience_core::ai;
25 
27 
28 using namespace result;
29 typedef string CommandTypeId;
30 
32  std::string name;
33  std::string type;
34  std::string defaultValue;
35  bool optional;
36 };
37 
38 typedef function<const CscCommandExecutionResult *(CscEnvironmentSimulator &environmentSimulator)> FunctionCommand;
39 
41 
42 public:
43 
44  template <class TResult = CscCommandExecutionResult>
45  const TResult *execute(CscEnvironmentSimulator &environmentSimulator) {
46 
47  ptr<CscEntityReflexion> entityReflexion = getEntityReflexion();
48  if (entityReflexion != nullptr) {
49  onCommandExecutionStartOnEntity(this, entityReflexion->getSerialNumber());
50  }
51 
52  LOG_TRACE("execute command of type: " + getTypeId());
53 
54  const CscCommandExecutionResult *result = doExecute(environmentSimulator);
55  if (auto resultTyped = dynamic_cast<const TResult *>(result)) {
56  entityReflexion = getEntityReflexion();
57  if (entityReflexion != nullptr) {
58  onCommandExecutedOnEntity(this, entityReflexion->getSerialNumber(), result, environmentSimulator);
59  }
60  onCommandExecuted(this);
61  return resultTyped;
62  } else {
63  const std::type_info &requestedTypeInfo = typeid(TResult);
64  throw runtime_error("Invalid command result - requested: " + getClassName(requestedTypeInfo, true));
65  }
66  }
67 
68  const CommandTypeId &getTypeId() const;
69 
70  virtual ~CscCommand();
71 
72  typedef function<void(CscCommand *command)> CommandExecutedListener;
73  static void addCommandExecutedListener(CommandExecutedListener listener);
74 
75  void setOriginalCommandString(string command);
76 
77 protected:
79 
80  CscCommand(const CommandTypeId &typeId);
81 
82  virtual const CscCommandExecutionResult *doExecute(
83  CscEnvironmentSimulator &environmentSimulator) = 0;
84 
85  const CscCommandExecutionResultNone *resultNone();
86  const CscCommandExecutionResultSequence *resultSequence(CscSequence *sequence, const string &entitySerialNumber, bool doNotPlay = false);
87  const CscCommandExecutionResultFile *resultFile(const fs::path &resultingFilePath);
88 
89  template <class TResult>
92  }
93 
97  virtual ptr<CscEntityReflexion> getEntityReflexion() const = 0;
98 
99  const CscWorldElementId getEntityId() const;
100  optional<string> originalCommandString = {};
101 
102 private:
103  inline static vector<CommandExecutedListener> commandExecutedListeners;
104  void onCommandExecuted(CscCommand *command);
105 };
106 
110 class CSC_DLL_IMPORTEXPORT CommandExecutionException : public runtime_error {
111 public:
112  CommandExecutionException(const string &type, const string &message, const optional<int> &code = {});
113  const string type;
114  const optional<int> code;
115  string toJson() const;
116 };
117 
118 }
119 
120 #endif
conscience_core::bridging::commands::CommandSpecification::optional
bool optional
Definition: Command.h:35
conscience_core::bridging::commands::result::CscCommandExecutionResultNone
Definition: CommandExecutionResultNone.h:11
CSC_DLL_IMPORTEXPORT
#define CSC_DLL_IMPORTEXPORT
Definition: os.h:34
conscience_core::bridging::commands::CommandSpecification::type
std::string type
Definition: Command.h:33
conscience_core::ai
Definition: alerts.cpp:12
CommandExecutionResultSequence.h
conscience_core::bridging::commands::CommandSpecification::defaultValue
std::string defaultValue
Definition: Command.h:34
conscience_core::bridging::commands::result::CscCommandExecutionResultSequence
Definition: CommandExecutionResultSequence.h:15
conscience_core::bridging::commands::CommandTypeId
string CommandTypeId
Definition: Command.h:29
CommandExecutionResultNone.h
InvalidCommandException.h
conscience_core::bridging::commands::CscCommand::execute
const TResult * execute(CscEnvironmentSimulator &environmentSimulator)
Definition: Command.h:45
CscEntityReflexion.h
CscSequence.h
CommandExecutionResultFile.h
conscience_core::bridging::commands::CscCommand::typeId
const CommandTypeId typeId
Definition: Command.h:78
conscience_core::bridging::commands::CommandExecutionException::code
const optional< int > code
Definition: Command.h:114
conscience_utils::getClassName
string getClassName(const type_info &paramsClass, bool withNamespace)
Definition: conscience_util.cpp:593
conscience_core::bridging::commands::result::CscCommandExecutionResultFile
Definition: CommandExecutionResultFile.h:11
conscience_core::bridging::commands
Definition: cartographyCommands.cpp:4
conscience_core::bridging::commands::CscCommand::CommandExecutedListener
function< void(CscCommand *command)> CommandExecutedListener
Definition: Command.h:72
conscience_core::bridging::commands::CommandSpecification
Definition: Command.h:31
conscience_core::ai::CscEntityReflexion
Definition: CscEntityReflexion.h:389
conscience_core::bridging::commands::CscCommand
Definition: Command.h:40
CscWorldElementId
string CscWorldElementId
Definition: Command.h:15
CommandExecutionResult.h
nlohmann::detail::void
j template void())
Definition: json.hpp:4189
CscCommon.h
jwt::json::type
type
Generic JSON types used in JWTs.
Definition: jwt.h:1794
conscience_core::bridging::commands::result::CscCommandExecutionResult
Definition: CommandExecutionResult.h:44
CscEnvironmentSimulator
Definition: CscEnvironmentSimulator.h:31
conscience_core::bridging::commands::result::CscCommandExecutionResultObject
Definition: CommandExecutionResult.h:93
CscPhysicsType.h
conscience_core::bridging::commands::CommandExecutionException
Definition: Command.h:110
conscience_core::bridging::commands::onCommandExecutionStartOnEntity
void onCommandExecutionStartOnEntity(const CscCommand *command, const string &entitySerialNumber)
Definition: CscCommandsProcessor.cpp:49
conscience_core::bridging::commands::CommandExecutionException::type
const string type
Definition: Command.h:113
conscience_core::bridging::commands::CscCommand::resultObject
const CscCommandExecutionResultObject< TResult > * resultObject(TResult *result)
Definition: Command.h:90
conscience_core::bridging::commands::environment_objects::optional< string >
const ptr< CscObjectModel > const string const CscPoint3d const CscPoint3d optional< string >
Definition: environmentObjectsCommands.h:373
conscience_core::bridging::commands::FunctionCommand
function< const CscCommandExecutionResult *(CscEnvironmentSimulator &environmentSimulator)> FunctionCommand
Definition: Command.h:38
ptr
std::shared_ptr< T > ptr
Definition: CscCommon.h:29
CscCommandsProcessor.h
LOG_TRACE
#define LOG_TRACE(message)
Definition: conscience_log.h:188
CscSequence
Definition: CscSequence.h:51
conscience_core::bridging::commands::onCommandExecutedOnEntity
void onCommandExecutedOnEntity(const CscCommand *command, const string &entitySerialNumber, const CscCommandExecutionResult *result, CscEnvironmentSimulator &environmentSimulator)
Definition: CscCommandsProcessor.cpp:60
conscience_core::bridging::commands::CommandSpecification::name
std::string name
Definition: Command.h:32