BioDynaMo  v1.03.61-fe34ced0
behavior.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_BEHAVIOR_BEHAVIOR_H_
16 #define CORE_BEHAVIOR_BEHAVIOR_H_
17 
18 #include <limits>
19 #include "core/agent/agent.h"
21 #include "core/util/type.h"
22 
23 namespace bdm {
24 
29 class Behavior {
30  public:
32 
33  virtual ~Behavior() = default;
34 
36  virtual Behavior* New() const = 0;
37 
39  virtual Behavior* NewCopy() const = 0;
40 
47  virtual void Initialize(const NewAgentEvent& event) {
48  copy_mask_ = event.existing_behavior->copy_mask_;
49  remove_mask_ = event.existing_behavior->remove_mask_;
50  }
51 
58  virtual void Update(const NewAgentEvent& event) {}
59 
60  virtual void Run(Agent* agent) = 0;
61 
63  void AlwaysCopyToNew() {
64  copy_mask_ = std::numeric_limits<NewAgentEventUid>::max();
65  }
67  void NeverCopyToNew() { copy_mask_ = 0; }
70  void CopyToNewIf(const std::initializer_list<NewAgentEventUid>& uids) {
71  for (auto& uid : uids) {
72  copy_mask_ |= uid;
73  }
74  }
75 
79  remove_mask_ = std::numeric_limits<NewAgentEventUid>::max();
80  }
87  const std::initializer_list<NewAgentEventUid>& uids) {
88  for (auto& uid : uids) {
89  remove_mask_ |= uid;
90  }
91  }
92 
95  bool WillBeCopied(NewAgentEventUid event) const {
96  return (event & copy_mask_) != 0;
97  }
98 
101  bool WillBeRemoved(NewAgentEventUid event) const {
102  return (event & remove_mask_) != 0;
103  }
104 
105  void* operator new(size_t size) { // NOLINT
106  auto* mem_mgr = Simulation::GetActive()->GetMemoryManager();
107  if (mem_mgr) {
108  return mem_mgr->New(size);
109  } else {
110  return malloc(size);
111  }
112  }
113 
114  void operator delete(void* p) { // NOLINT
115  auto* mem_mgr = Simulation::GetActive()->GetMemoryManager();
116  if (mem_mgr) {
117  mem_mgr->Delete(p);
118  } else {
119  free(p);
120  }
121  }
122 
123  private:
127 };
128 
130 #define BDM_BEHAVIOR_HEADER(class_name, base_class, class_version_id) \
131  public: \
132  using Base = base_class; \
133  \
134  Behavior* New() const override { return new class_name(); } \
135  \
136  Behavior* NewCopy() const override { return new class_name(*this); } \
137  \
138  private: \
139  BDM_CLASS_DEF_OVERRIDE(class_name, class_version_id); \
140  \
141  public:
142 
143 } // namespace bdm
144 
145 #endif // CORE_BEHAVIOR_BEHAVIOR_H_
bdm::NewAgentEvent
Definition: new_agent_event.h:61
bdm::Behavior::AlwaysRemoveFromExisting
void AlwaysRemoveFromExisting()
Definition: behavior.h:78
bdm::Behavior::Update
virtual void Update(const NewAgentEvent &event)
Definition: behavior.h:58
bdm
Definition: agent.cc:39
bdm::Behavior
Definition: behavior.h:29
bdm::Behavior::CopyToNewIf
void CopyToNewIf(const std::initializer_list< NewAgentEventUid > &uids)
Definition: behavior.h:70
bdm::Behavior::copy_mask_
NewAgentEventUid copy_mask_
Definition: behavior.h:124
bdm::Behavior::Initialize
virtual void Initialize(const NewAgentEvent &event)
Definition: behavior.h:47
bdm::MemoryManager::Delete
void Delete(void *p)
Definition: memory_manager.cc:389
bdm::Behavior::~Behavior
virtual ~Behavior()=default
type.h
bdm::Behavior::AlwaysCopyToNew
void AlwaysCopyToNew()
Always copy this behavior to new agents.
Definition: behavior.h:63
bdm::Behavior::WillBeRemoved
bool WillBeRemoved(NewAgentEventUid event) const
Definition: behavior.h:101
bdm::Agent
Contains code required by all agents.
Definition: agent.h:79
bdm::Behavior::NewCopy
virtual Behavior * NewCopy() const =0
Create a new copy of this behavior.
new_agent_event.h
bdm::Simulation::GetMemoryManager
MemoryManager * GetMemoryManager()
Definition: simulation.h:127
bdm::Behavior::NeverCopyToNew
void NeverCopyToNew()
Never copy this behavior to new agents.
Definition: behavior.h:67
bdm::NewAgentEventUid
uint64_t NewAgentEventUid
Definition: new_agent_event.h:29
agent.h
bdm::Behavior::NeverRemoveFromExisting
void NeverRemoveFromExisting()
Definition: behavior.h:83
bdm::Behavior::RemoveFromExistingIf
void RemoveFromExistingIf(const std::initializer_list< NewAgentEventUid > &uids)
Definition: behavior.h:86
bdm::MemoryManager::New
void * New(std::size_t size)
Definition: memory_manager.cc:358
bdm::Behavior::WillBeCopied
bool WillBeCopied(NewAgentEventUid event) const
Definition: behavior.h:95
bdm::Behavior::New
virtual Behavior * New() const =0
Create a new instance of this object using the default constructor.
bdm::Behavior::Behavior
Behavior()
Definition: behavior.h:31
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:67
bdm::Behavior::Run
virtual void Run(Agent *agent)=0
bdm::Behavior::BDM_CLASS_DEF
BDM_CLASS_DEF(Behavior, 2)
bdm::Behavior::remove_mask_
NewAgentEventUid remove_mask_
Definition: behavior.h:125