Skip to content

Commit edcfb3c

Browse files
authored
[src] Update SofaAdvancePhysicsAPI raw pointer into sharedPtr and reactivate msg handler (#25)
* [src] Update SofaAdvancePhysicsAPI raw pointer into sharedPtr * reactivate message handler
1 parent 2b2cd0e commit edcfb3c

7 files changed

Lines changed: 34 additions & 22 deletions

File tree

Source/SofaUE5/Private/Base/SofaBaseComponent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void ASofaBaseComponent::setComponentType(const FString& type)
3232
//RootComponent = CreateDefaultSubobject<USceneComponent>(*m_baseType);
3333
}
3434

35-
void ASofaBaseComponent::setSofaAPI(SofaAdvancePhysicsAPI* api)
35+
void ASofaBaseComponent::setSofaAPI(const TSharedPtr<SofaAdvancePhysicsAPI>& api)
3636
{
3737
m_sofaAPI = api;
3838
}

Source/SofaUE5/Private/DAGNode/SofaDAGNode.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void ASofaDAGNode::Tick(float DeltaTime)
6363
}
6464

6565

66-
bool ASofaDAGNode::loadComponents(SofaAdvancePhysicsAPI* _sofaAPI)
66+
bool ASofaDAGNode::loadComponents(const TSharedPtr<SofaAdvancePhysicsAPI>& _sofaAPI)
6767
{
6868
m_sofaAPI = _sofaAPI;
6969

@@ -169,7 +169,7 @@ bool ASofaDAGNode::loadComponents(SofaAdvancePhysicsAPI* _sofaAPI)
169169
}
170170

171171

172-
void ASofaDAGNode::reconnectComponents(SofaAdvancePhysicsAPI* _sofaAPI)
172+
void ASofaDAGNode::reconnectComponents(const TSharedPtr<SofaAdvancePhysicsAPI>& _sofaAPI)
173173
{
174174
m_sofaAPI = _sofaAPI;
175175

Source/SofaUE5/Private/SofaContext.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ void ASofaContext::Destroyed()
9393
if (m_log)
9494
UE_LOG(SUnreal_log, Warning, TEXT("######### ASofaContext::Destroyed(): Delete SofaAdvancePhysicsAPI: %s"), *this->GetName());
9595

96+
if (m_isMsgHandlerActivated == true)
97+
catchSofaMessages();
98+
9699
UE_LOG(SUnreal_log, Warning, TEXT("## ASofaContext::BeginDestroy: m_sofaAPI stop"));
97100
m_sofaAPI->stop();
101+
m_sofaAPI->activateMessageHandler(false);
98102
UE_LOG(SUnreal_log, Warning, TEXT("## ASofaContext::BeginDestroy: m_sofaAPI stopped"));
99-
delete m_sofaAPI;
103+
//delete m_sofaAPI;
100104
m_sofaAPI = nullptr;
101105
UE_LOG(SUnreal_log, Warning, TEXT("## ASofaContext::BeginDestroy: m_sofaAPI deleted"));
102106
}
@@ -237,19 +241,18 @@ void ASofaContext::createSofaContext()
237241
// create a new sofa context through sofaAdvancePhysicsAPI
238242
if (m_sofaAPI == nullptr)
239243
{
240-
//TSharedRef<SofaAdvancePhysicsAPI> apiRef(new SofaAdvancePhysicsAPI());
241-
//m_data.m_sofaAPI = apiRef;
242-
m_sofaAPI = new SofaAdvancePhysicsAPI();
244+
m_sofaAPI = MakeShared<SofaAdvancePhysicsAPI>();
243245
UE_LOG(SUnreal_log, Warning, TEXT("## ASofaDAGNode::loadComponents TEST 12"));
244-
// TODO restore that
245-
//m_sofaAPI->activateMessageHandler(m_isMsgHandlerActivated);
246246

247247
if (m_sofaAPI == nullptr)
248248
{
249249
UE_LOG(SUnreal_log, Error, TEXT("## ASofaContext::createSofaContext SofaAdvancePhysicsAPI creation failed."));
250250
return;
251251
}
252252

253+
// activate message handler
254+
m_sofaAPI->activateMessageHandler(m_isMsgHandlerActivated);
255+
253256
m_apiName = m_sofaAPI->APIName();
254257

255258
if (m_log)
@@ -321,8 +324,8 @@ void ASofaContext::loadSofaScene()
321324
// Start parsing scene loaded in SOFA
322325
// Create the actor of the scene:
323326

324-
//if (m_isMsgHandlerActivated == true)
325-
// catchSofaMessages();
327+
if (m_isMsgHandlerActivated == true)
328+
catchSofaMessages();
326329

327330
//m_status++;
328331
}
@@ -340,8 +343,8 @@ void ASofaContext::loadDefaultPlugin()
340343
UE_LOG(SUnreal_log, Error, TEXT("## ASofaContext::createSofaContext: loadDefaultPlugin failed, returns: %d"), resPlug);
341344
}
342345

343-
//if (m_isMsgHandlerActivated == true)
344-
// catchSofaMessages();
346+
if (m_isMsgHandlerActivated == true)
347+
catchSofaMessages();
345348
}
346349

347350

@@ -498,6 +501,9 @@ void ASofaContext::reconnectNodeGraph()
498501
dagNode->reconnectComponents(m_sofaAPI);
499502
}
500503
}
504+
505+
if (m_isMsgHandlerActivated == true)
506+
catchSofaMessages();
501507
}
502508

503509

@@ -523,12 +529,17 @@ void ASofaContext::clearNodeGraph()
523529
void ASofaContext::catchSofaMessages()
524530
{
525531
int nbrMsgs = m_sofaAPI->getNbMessages();
532+
UE_LOG(SUnreal_log, Warning, TEXT("## ASofaContext::catchSofaMessages: nbr message: %d"), nbrMsgs);
533+
526534
int* type = new int[1];
527535
type[0] = -1;
536+
std::string rawMsg;
528537
for (int i = 0; i < nbrMsgs; ++i)
529538
{
530-
const char* rawMsg = m_sofaAPI->getMessage(i, *type).c_str();
531-
FString FMessage(rawMsg);
539+
540+
//const char* rawMsg = m_sofaAPI->getMessage(i, *type).c_str();
541+
m_sofaAPI->getMessage_out(i, *type, rawMsg);
542+
FString FMessage(rawMsg.c_str());
532543

533544
if (type[0] == -1) {
534545
continue;

Source/SofaUE5/Public/Base/SofaBaseComponent.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ class SOFAUE5_API ASofaBaseComponent : public AActor
3737
return m_uniqueNameID;
3838
}
3939

40-
void setSofaAPI(SofaAdvancePhysicsAPI* api);
40+
void setSofaAPI(const TSharedPtr<SofaAdvancePhysicsAPI>& api);
4141

4242
virtual void computeComponent() {}
4343

4444
protected:
45-
SofaAdvancePhysicsAPI* m_sofaAPI = nullptr;
45+
TSharedPtr <SofaAdvancePhysicsAPI> m_sofaAPI = nullptr;
4646

4747
UPROPERTY(SaveGame, VisibleAnywhere)
4848
FString m_uniqueNameID;

Source/SofaUE5/Public/DAGNode/SofaDAGNode.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class SOFAUE5_API ASofaDAGNode : public AActor
3737
void setUniqueNameID(const FString& uniqueNameID) { m_uniqueNameID = uniqueNameID; }
3838
void setParentName(const FString& parentName) { m_parentName = parentName; }
3939

40-
bool loadComponents(SofaAdvancePhysicsAPI* _sofaAPI);
40+
bool loadComponents(const TSharedPtr<SofaAdvancePhysicsAPI>& _sofaAPI);
4141
void clearComponents();
4242

43-
void reconnectComponents(SofaAdvancePhysicsAPI* _sofaAPI);
43+
void reconnectComponents(const TSharedPtr<SofaAdvancePhysicsAPI>& _sofaAPI);
4444

4545

4646
const FString& getParentName() {
@@ -52,7 +52,7 @@ class SOFAUE5_API ASofaDAGNode : public AActor
5252
}
5353

5454
private:
55-
SofaAdvancePhysicsAPI* m_sofaAPI = nullptr;
55+
TSharedPtr<SofaAdvancePhysicsAPI> m_sofaAPI = nullptr;
5656

5757
UPROPERTY(SaveGame, VisibleAnywhere)
5858
FString m_uniqueNameID;

Source/SofaUE5/Public/SofaContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ class SOFAUE5_API ASofaContext : public AActor
107107
bool m_isInit;
108108

109109
//UPROPERTY(SaveGame)
110-
SofaAdvancePhysicsAPI* m_sofaAPI = nullptr;
111-
//TSharedPtr<SofaAdvancePhysicsAPI> m_sofaAPI;
110+
TSharedPtr <SofaAdvancePhysicsAPI> m_sofaAPI = nullptr;
111+
112112
UPROPERTY(SaveGame)
113113
int m_status;
114114

Source/ThirdParty/SofaUE5Library/Public/SofaUE5Library/SofaAdvancePhysicsAPI.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class SOFA_VERSE_API SofaAdvancePhysicsAPI
139139
int activateMessageHandler(bool value);
140140
int getNbMessages();
141141
std::string getMessage(int messageId, int& msgType);
142+
void getMessage_out(int messageId, int& msgType, std::string& outName);
142143
int clearMessages();
143144

144145
////////////////////////////////////////////

0 commit comments

Comments
 (0)