Skip to content

Commit 9c22d62

Browse files
committed
--set up base class and class templates for wrappers.
1 parent 7d470cf commit 9c22d62

File tree

8 files changed

+323
-46
lines changed

8 files changed

+323
-46
lines changed

src/esp/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ set(
469469
sensor/EquirectangularSensor.h
470470
sensor/FisheyeSensor.cpp
471471
sensor/FisheyeSensor.h
472-
sensor/sensorManagers/SensorBaseManager.h
472+
sensor/sensorManagers/SensorManager.cpp
473+
sensor/sensorManagers/SensorManager.h
473474
sensor/sensorWrappers/ManagedSensorBase.h
474475
sensor/Sensor.cpp
475476
sensor/Sensor.h

src/esp/sensor/Sensor.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ class Sensor : public Magnum::SceneGraph::AbstractFeature3D {
120120
*/
121121
void setTransformationFromSpec();
122122

123+
/**
124+
* @brief The unique handle assigned to this sensor at creation
125+
*/
126+
std::string getSensorHandle() const { return sensorHandle_; }
127+
128+
/**
129+
* @brief The unique ID assigned to this sensor at creation
130+
*/
131+
int getSensorID() const { return sensorId_; }
132+
123133
/**
124134
* @brief Draws an observation to the frame buffer using simulator's renderer,
125135
* then reads the observation to the sensor's memory buffer
@@ -150,6 +160,11 @@ class Sensor : public Magnum::SceneGraph::AbstractFeature3D {
150160
virtual bool displayObservation(sim::Simulator& sim) = 0;
151161

152162
protected:
163+
// unique handle for sensor built when sensor is constructed.
164+
std::string sensorHandle_;
165+
166+
// unique id for sensor set when sensor is built.
167+
int sensorId_ = ID_UNDEFINED;
153168
SensorSpec::ptr spec_ = nullptr;
154169
core::Buffer::ptr buffer_ = nullptr;
155170

src/esp/sensor/sensorManagers/SensorBaseManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class SensorBaseManager
9595
managedSensorTypeConstructorMap_.find(sensorTypeName);
9696
if (mgdSensorTypeCtorMapIter == managedSensorTypeConstructorMap_.end()) {
9797
ESP_ERROR(Mn::Debug::Flag::NoSpace)
98-
<< "<" << this->sensorType_ << "> Unknown constructor type "
98+
<< "<" << this->objectType_ << "> Unknown constructor type "
9999
<< sensorTypeName << ", so initNewObject aborted.";
100100
return nullptr;
101101
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Meta Platforms, Inc. and its affiliates.
2+
// This source code is licensed under the MIT license found in the
3+
// LICENSE file in the root directory of this source tree.
4+
5+
#include "SensorManager.h"
6+
7+
namespace esp {
8+
namespace sensor {
9+
10+
SensorManager::SensorManager()
11+
: SensorBaseManager<esp::sensor::ManagedSensorBase>::SensorBaseManager(
12+
"Sensor") {
13+
this->copyConstructorMap_["ManagedAudioSensor"] =
14+
&SensorManager::createObjCopyCtorMapEntry<
15+
esp::sensor::ManagedAudioSensor>;
16+
17+
} // SensorManager ctor
18+
19+
} // namespace sensor
20+
} // namespace esp
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Meta Platforms, Inc. and its affiliates.
2+
// This source code is licensed under the MIT license found in the
3+
// LICENSE file in the root directory of this source tree.
4+
5+
#ifndef ESP_SENSOR_SENSORMANAGER_H_
6+
#define ESP_SENSOR_SENSORMANAGER_H_
7+
8+
#include "SensorBaseManager.h"
9+
#include "esp/sensor/sensorWrappers/ManagedAudioSensor.h"
10+
#include "esp/sensor/sensorWrappers/ManagedSensorBase.h"
11+
12+
namespace esp {
13+
namespace sensor {
14+
15+
class SensorManager : public SensorBaseManager<esp::sensor::ManagedSensorBase> {
16+
public:
17+
explicit SensorManager();
18+
19+
public:
20+
ESP_SMART_POINTERS(SensorManager)
21+
}; // class SensorManager
22+
23+
} // namespace sensor
24+
} // namespace esp
25+
#endif // ESP_SENSOR_SENSORMANAGER_H_
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Meta Platforms, Inc. and its affiliates.
2+
// This source code is licensed under the MIT license found in the
3+
// LICENSE file in the root directory of this source tree.
4+
5+
#ifndef ESP_SENSOR_MANAGEDAUDIOSENSOR_H_
6+
#define ESP_SENSOR_MANAGEDAUDIOSENSOR_H_
7+
8+
#include <Corrade/Utility/FormatStl.h>
9+
#include <Corrade/Utility/Macros.h>
10+
11+
#include "esp/sensor/AudioSensor.h"
12+
#include "esp/sensor/sensorWrappers/ManagedSensorTemplates.h"
13+
14+
namespace Cr = Corrade;
15+
namespace Mn = Magnum;
16+
namespace esp {
17+
namespace sensor {
18+
19+
/**
20+
* @brief Class for wrapper for sensor objects of all kinds to
21+
* enable Managed Container access.
22+
*/
23+
class ManagedAudioSensor
24+
: public esp::sensor::AbstractManagedSensor<AudioSensor> {
25+
public:
26+
explicit ManagedAudioSensor()
27+
: AbstractManagedSensor<AudioSensor>::AbstractManagedSensor(
28+
"ManagedAudioSensor") {}
29+
30+
// TODO Add appropriate audio sensor getters/setters here
31+
32+
protected:
33+
/**
34+
* @brief Retrieve a comma-separated string holding the header values for
35+
* the info returned for this managed object, type-specific.
36+
*/
37+
38+
std::string getSensorObjInfoHeaderInternal() const override {
39+
return "Output Directory";
40+
}
41+
/**
42+
* @brief Specialization-specific extension of getObjectInfo, comma
43+
* separated info ideal for saving to csv
44+
*/
45+
std::string getSensorObjInfoInternal(
46+
CORRADE_UNUSED std::shared_ptr<AudioSensor>& sp) const override {
47+
// TODO provide info stream for sensors
48+
return "";
49+
}
50+
51+
public:
52+
ESP_SMART_POINTERS(ManagedAudioSensor)
53+
}; // class ManagedAudioSensor
54+
} // namespace sensor
55+
} // namespace esp
56+
57+
#endif // ESP_SENSOR_MANAGEDAUDIOSENSOR_H_

src/esp/sensor/sensorWrappers/ManagedSensorBase.h

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,89 +5,90 @@
55
#ifndef ESP_SENSOR_MANAGEDSENSORBASE_H_
66
#define ESP_SENSOR_MANAGEDSENSORBASE_H_
77

8-
#include <Corrade/Containers/StringStl.h>
8+
#include <Corrade/Utility/FormatStl.h>
99
#include <Corrade/Utility/Macros.h>
1010

1111
#include "esp/core/managedContainers/AbstractManagedObject.h"
1212
#include "esp/sensor/Sensor.h"
13+
#include "esp/sensor/VisualSensor.h"
1314

1415
namespace Cr = Corrade;
1516
namespace Mn = Magnum;
1617
namespace esp {
1718
namespace sensor {
1819

1920
/**
20-
* @brief Base class template for wrapper for sensor objects of all kinds to
21-
* enable Managed Container access.
21+
* @brief Base class for wrappers of sensor objects of all kinds to enable
22+
* Managed Container access.
2223
*/
23-
template <class T>
24-
class AbstractManagedSensor
24+
class ManagedSensorBase
2525
: public esp::core::managedContainers::AbstractManagedObject {
2626
public:
27-
static_assert(std::is_base_of<esp::sensor::Sensor, T>::value,
28-
"AbstractManagedSensor :: Managed sensor object type must be "
29-
"derived from esp::sensor::Sensor");
30-
31-
typedef std::weak_ptr<T> WeakObjRef;
32-
33-
explicit AbstractManagedSensor(const std::string& classKey) {
34-
AbstractManagedSensor::setClassKey(classKey);
27+
explicit ManagedSensorBase(const std::string& classKey) {
28+
ManagedSensorBase::setClassKey(classKey);
3529
}
36-
37-
void setObjectRef(const std::shared_ptr<T>& objRef) { weakObjRef_ = objRef; }
38-
39-
~AbstractManagedSensor() override = default;
30+
~ManagedSensorBase() override = default;
31+
/**
32+
* @brief Get the instancing class of the ManagedSensorBase instance. Should
33+
* only be set from implementer's constructor. Used as key in constructor
34+
* function pointer maps in @ref
35+
* esp::core::managedContainers::ManagedContainer.
36+
*/
37+
std::string getClassKey() const override { return classKey_; }
4038

4139
/**
42-
* @brief Retrieve a comma-separated string holding the header values for the
43-
* info returned for this managed object.
40+
* @brief Managed Sensor objects manage their own handles, so this is
41+
* currently unsettable.
4442
*/
45-
std::string getObjectInfoHeader() const override {
46-
return "Type," + getSensorObjInfoHeaderInternal();
47-
}
43+
void setHandle(CORRADE_UNUSED const std::string& name) override {}
4844

4945
/**
50-
* @brief Retrieve a comma-separated informational string about the contents
51-
* of this managed object.
46+
* @brief Retrieve this Managed Sensor object's unique handle.
5247
*/
53-
std::string getObjectInfo() const override {
54-
if (auto sp = this->getObjectReference()) {
55-
namespace CrUt = Cr::Utility;
56-
return Cr::Utility::formatString("{},{},", classKey_,
57-
getSensorObjInfoInternal(sp));
48+
std::string getHandle() const override {
49+
if (auto sp = getObjectReferenceInternal<Sensor>()) {
50+
return sp->getSensorHandle();
5851
}
59-
return Cr::Utility::formatString("Unknown classkey {},", classKey_);
52+
return "";
6053
}
6154

62-
protected:
6355
/**
64-
* @brief Retrieve a comma-separated string holding the header values for
65-
* the info returned for this managed object, type-specific.
56+
* @brief Managed Sensor objects manage their own IDs, so this is
57+
* unsettable.
6658
*/
59+
void setID(CORRADE_UNUSED int ID) override {}
6760

68-
virtual std::string getSensorObjInfoHeaderInternal() const = 0;
6961
/**
70-
* @brief Specialization-specific extension of getObjectInfo, comma
71-
* separated info ideal for saving to csv
62+
* @brief Retrieve this object's unique ID.
7263
*/
73-
virtual std::string getSensorObjInfoInternal(
74-
std::shared_ptr<T>& sp) const = 0;
64+
int getID() const override {
65+
if (auto sp = getObjectReferenceInternal<Sensor>()) {
66+
return sp->getSensorID();
67+
}
68+
return ID_UNDEFINED;
69+
} // getID()
70+
71+
protected:
72+
void setObjectRefInternal(const std::shared_ptr<void>& objRef) {
73+
weakObjRef_ = objRef;
74+
}
7575

7676
/**
7777
* @brief This function accesses the underlying shared pointer of this
7878
* object's @p weakObjRef_ if it exists; if not, it provides a message.
7979
* @return Either a shared pointer of this wrapper's object, or nullptr if
8080
* DNE.
8181
*/
82-
std::shared_ptr<T> inline getObjectReference() const {
83-
std::shared_ptr<T> sp = weakObjRef_.lock();
82+
template <class T>
83+
std::shared_ptr<T> inline getObjectReferenceInternal() const {
84+
std::shared_ptr<void> sp = weakObjRef_.lock();
8485
if (!sp) {
8586
// TODO: Verify object is removed from manager here?
8687
ESP_ERROR()
87-
<< "This sensor object no longer exists. Please delete any variable "
88+
<< "This sensor object no longer exists. Please delete any variable "
8889
"references.";
8990
}
90-
return sp;
91+
return std::static_pointer_cast<T>(sp);
9192
} // getObjectReference
9293

9394
/**
@@ -105,16 +106,17 @@ class AbstractManagedSensor
105106
* @brief Weak ref to object. If user has copy of this wrapper but object
106107
* has been deleted, this will be nullptr.
107108
*/
108-
WeakObjRef weakObjRef_{};
109+
std::weak_ptr<void> weakObjRef_{};
109110

110111
/**
111112
* @brief Name of instancing class responsible for this managed object
112113
*/
113114
std::string classKey_;
114115

115116
public:
116-
ESP_SMART_POINTERS(AbstractManagedSensor<T>)
117+
ESP_SMART_POINTERS(ManagedSensorBase)
117118
}; // class AbstractManagedSensor
119+
118120
} // namespace sensor
119121
} // namespace esp
120122

0 commit comments

Comments
 (0)