BioDynaMo  v1.05.119-a4ff3934
stateless_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_STATELESS_BEHAVIOR_H_
16 #define CORE_BEHAVIOR_STATELESS_BEHAVIOR_H_
17 
18 #include "core/behavior/behavior.h"
19 
20 namespace bdm {
21 
46 class StatelessBehavior : public Behavior {
48 
49  public:
50  using FPtr = void (*)(Agent*);
51  StatelessBehavior() : fptr_(nullptr) {}
52  StatelessBehavior(const FPtr fptr) : fptr_(fptr) {}
54  : Behavior(other), fptr_(other.fptr_) {}
55  virtual ~StatelessBehavior() = default;
56 
57  void Initialize(const NewAgentEvent& event) override {
58  Base::Initialize(event);
59  fptr_ = static_cast<StatelessBehavior*>(event.existing_behavior)->fptr_;
60  }
61 
62  void Run(Agent* agent) override {
63  if (!fptr_) {
64  return;
65  }
66  fptr_(agent);
67  }
68 
69  private:
71 };
72 
73 // The following custom streamer should be visible to rootcling for dictionary
74 // generation, but not to the interpreter!
75 #if (!defined(__CLING__) || defined(__ROOTCLING__)) && defined(USE_DICT)
76 
77 // The custom streamer is needed because ROOT can't stream function pointers
78 // by default.
79 inline void StatelessBehavior::Streamer(TBuffer& R__b) {
80  if (R__b.IsReading()) {
81  R__b.ReadClassBuffer(StatelessBehavior::Class(), this);
82  Long64_t l;
83  R__b.ReadLong64(l);
84  this->fptr_ = reinterpret_cast<StatelessBehavior::FPtr>(l);
85  } else {
86  R__b.WriteClassBuffer(StatelessBehavior::Class(), this);
87  Long64_t l = reinterpret_cast<Long64_t>(this->fptr_);
88  R__b.WriteLong64(l);
89  }
90 }
91 
92 #endif // !defined(__CLING__) || defined(__ROOTCLING__)
93 
94 } // namespace bdm
95 
96 #endif // CORE_BEHAVIOR_STATELESS_BEHAVIOR_H_
bdm::NewAgentEvent
Definition: new_agent_event.h:61
bdm::StatelessBehavior::Run
void Run(Agent *agent) override
Definition: stateless_behavior.h:62
bdm
Definition: agent.cc:39
bdm::StatelessBehavior::BDM_BEHAVIOR_HEADER
BDM_BEHAVIOR_HEADER(StatelessBehavior, Behavior, 1)
bdm::Behavior
Definition: behavior.h:29
bdm::Agent
Contains code required by all agents.
Definition: agent.h:79
bdm::StatelessBehavior::fptr_
FPtr fptr_
Definition: stateless_behavior.h:70
bdm::StatelessBehavior::StatelessBehavior
StatelessBehavior()
Definition: stateless_behavior.h:51
bdm::StatelessBehavior
Simplifies the creation of behaviors without attributes. Let's assume that we want to create a behavi...
Definition: stateless_behavior.h:46
bdm::StatelessBehavior::StatelessBehavior
StatelessBehavior(const FPtr fptr)
Definition: stateless_behavior.h:52
behavior.h
bdm::StatelessBehavior::Initialize
void Initialize(const NewAgentEvent &event) override
Definition: stateless_behavior.h:57
bdm::StatelessBehavior::FPtr
void(*)(Agent *) FPtr
Definition: stateless_behavior.h:50
bdm::StatelessBehavior::~StatelessBehavior
virtual ~StatelessBehavior()=default
bdm::StatelessBehavior::StatelessBehavior
StatelessBehavior(const StatelessBehavior &other)
Definition: stateless_behavior.h:53