Skip to content

Commit

Permalink
--set up base class and class templates for wrappers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jturner65 committed Dec 13, 2024
1 parent 7d470cf commit d4259be
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 56 deletions.
3 changes: 2 additions & 1 deletion src/esp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ set(
sensor/EquirectangularSensor.h
sensor/FisheyeSensor.cpp
sensor/FisheyeSensor.h
sensor/sensorManagers/SensorBaseManager.h
sensor/sensorManagers/SensorManager.cpp
sensor/sensorManagers/SensorManager.h
sensor/sensorWrappers/ManagedSensorBase.h
sensor/Sensor.cpp
sensor/Sensor.h
Expand Down
20 changes: 20 additions & 0 deletions src/esp/sensor/sensorManagers/SensorManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

#include "SensorManager.h"

namespace esp {
namespace sensor {

SensorManager::SensorManager()
: SensorBaseManager<esp::sensor::ManagedSensorBase>::SensorBaseManager(
"Sensor") {
this->copyConstructorMap_["ManagedAudioSensor"] =
&SensorManager::createObjCopyCtorMapEntry<
esp::sensor::ManagedAudioSensor>;

} // SensorManager ctor

} // namespace sensor
} // namespace esp
25 changes: 25 additions & 0 deletions src/esp/sensor/sensorManagers/SensorManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

#ifndef ESP_SENSOR_SENSORMANAGER_H_
#define ESP_SENSOR_SENSORMANAGER_H_

#include "SensorBaseManager.h"
#include "esp/sensor/sensorWrappers/ManagedAudioSensor.h"
#include "esp/sensor/sensorWrappers/ManagedSensorBase.h"

namespace esp {
namespace sensor {

class SensorManager : public SensorBaseManager<esp::sensor::ManagedSensorBase> {
public:
explicit SensorManager();

public:
ESP_SMART_POINTERS(SensorManager)
}; // class SensorManager

} // namespace sensor
} // namespace esp
#endif // ESP_SENSOR_SENSORMANAGER_H_
57 changes: 57 additions & 0 deletions src/esp/sensor/sensorWrappers/ManagedAudioSensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

#ifndef ESP_SENSOR_MANAGEDAUDIOSENSOR_H_
#define ESP_SENSOR_MANAGEDAUDIOSENSOR_H_

#include <Corrade/Utility/FormatStl.h>
#include <Corrade/Utility/Macros.h>

#include "esp/sensor/AudioSensor.h"
#include "esp/sensor/sensorWrappers/ManagedSensorTemplates.h"

namespace Cr = Corrade;
namespace Mn = Magnum;
namespace esp {
namespace sensor {

/**
* @brief Class for wrapper for sensor objects of all kinds to
* enable Managed Container access.
*/
class ManagedAudioSensor
: public esp::sensor::AbstractManagedSensor<AudioSensor> {
public:
explicit ManagedAudioSensor()
: AbstractManagedSensor<AudioSensor>::AbstractManagedSensor(
"ManagedAudioSensor") {}

// TODO Add appropriate audio sensor getters/setters here

protected:
/**
* @brief Retrieve a comma-separated string holding the header values for
* the info returned for this managed object, type-specific.
*/

std::string getSensorObjInfoHeaderInternal() const override {
return "Output Directory";
}
/**
* @brief Specialization-specific extension of getObjectInfo, comma
* separated info ideal for saving to csv
*/
std::string getSensorObjInfoInternal(
CORRADE_UNUSED std::shared_ptr<AudioSensor>& sp) const override {
// TODO provide info stream for sensors
return "";
}

public:
ESP_SMART_POINTERS(ManagedAudioSensor)
}; // class ManagedAudioSensor
} // namespace sensor
} // namespace esp

#endif // ESP_SENSOR_MANAGEDAUDIOSENSOR_H_
73 changes: 18 additions & 55 deletions src/esp/sensor/sensorWrappers/ManagedSensorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,89 +5,51 @@
#ifndef ESP_SENSOR_MANAGEDSENSORBASE_H_
#define ESP_SENSOR_MANAGEDSENSORBASE_H_

#include <Corrade/Containers/StringStl.h>
#include <Corrade/Utility/FormatStl.h>
#include <Corrade/Utility/Macros.h>

#include "esp/core/managedContainers/AbstractManagedObject.h"
#include "esp/sensor/Sensor.h"
#include "esp/sensor/VisualSensor.h"

namespace Cr = Corrade;
namespace Mn = Magnum;
namespace esp {
namespace sensor {

/**
* @brief Base class template for wrapper for sensor objects of all kinds to
* enable Managed Container access.
* @brief Base class for wrappers of sensor objects of all kinds to enable
* Managed Container access.
*/
template <class T>
class AbstractManagedSensor
class ManagedSensorBase
: public esp::core::managedContainers::AbstractManagedObject {
public:
static_assert(std::is_base_of<esp::sensor::Sensor, T>::value,
"AbstractManagedSensor :: Managed sensor object type must be "
"derived from esp::sensor::Sensor");

typedef std::weak_ptr<T> WeakObjRef;

explicit AbstractManagedSensor(const std::string& classKey) {
AbstractManagedSensor::setClassKey(classKey);
}

void setObjectRef(const std::shared_ptr<T>& objRef) { weakObjRef_ = objRef; }

~AbstractManagedSensor() override = default;

/**
* @brief Retrieve a comma-separated string holding the header values for the
* info returned for this managed object.
*/
std::string getObjectInfoHeader() const override {
return "Type," + getSensorObjInfoHeaderInternal();
}

/**
* @brief Retrieve a comma-separated informational string about the contents
* of this managed object.
*/
std::string getObjectInfo() const override {
if (auto sp = this->getObjectReference()) {
namespace CrUt = Cr::Utility;
return Cr::Utility::formatString("{},{},", classKey_,
getSensorObjInfoInternal(sp));
}
return Cr::Utility::formatString("Unknown classkey {},", classKey_);
explicit ManagedSensorBase(const std::string& classKey) {
ManagedSensorBase::setClassKey(classKey);
}
~ManagedSensorBase() override = default;

protected:
/**
* @brief Retrieve a comma-separated string holding the header values for
* the info returned for this managed object, type-specific.
*/

virtual std::string getSensorObjInfoHeaderInternal() const = 0;
/**
* @brief Specialization-specific extension of getObjectInfo, comma
* separated info ideal for saving to csv
*/
virtual std::string getSensorObjInfoInternal(
std::shared_ptr<T>& sp) const = 0;
void setObjectRefInternal(const std::shared_ptr<void>& objRef) {
weakObjRef_ = objRef;
}

/**
* @brief This function accesses the underlying shared pointer of this
* object's @p weakObjRef_ if it exists; if not, it provides a message.
* @return Either a shared pointer of this wrapper's object, or nullptr if
* DNE.
*/
std::shared_ptr<T> inline getObjectReference() const {
std::shared_ptr<T> sp = weakObjRef_.lock();
template <class T>
std::shared_ptr<T> inline getObjectReferenceInternal() const {
std::shared_ptr<void> sp = weakObjRef_.lock();
if (!sp) {
// TODO: Verify object is removed from manager here?
ESP_ERROR()
<< "This sensor object no longer exists. Please delete any variable "
"references.";
}
return sp;
return std::static_pointer_cast<T>(sp);
} // getObjectReference

/**
Expand All @@ -105,16 +67,17 @@ class AbstractManagedSensor
* @brief Weak ref to object. If user has copy of this wrapper but object
* has been deleted, this will be nullptr.
*/
WeakObjRef weakObjRef_{};
std::weak_ptr<void> weakObjRef_{};

/**
* @brief Name of instancing class responsible for this managed object
*/
std::string classKey_;

public:
ESP_SMART_POINTERS(AbstractManagedSensor<T>)
ESP_SMART_POINTERS(ManagedSensorBase)
}; // class AbstractManagedSensor

} // namespace sensor
} // namespace esp

Expand Down
157 changes: 157 additions & 0 deletions src/esp/sensor/sensorWrappers/ManagedSensorTemplates.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

#ifndef ESP_SENSOR_MANAGEDSENSORTEMPLATES_H_
#define ESP_SENSOR_MANAGEDSENSORTEMPLATES_H_

#include <Corrade/Utility/FormatStl.h>
#include <Corrade/Utility/Macros.h>

#include "ManagedSensorBase.h"
#include "esp/sensor/Sensor.h"
#include "esp/sensor/VisualSensor.h"

namespace Cr = Corrade;
namespace Mn = Magnum;
namespace esp {
namespace sensor {

/**
* @brief Base class template for wrapper for sensor objects of all kinds to
* enable Managed Container access.
*/
template <class T>
class AbstractManagedSensor : public ManagedSensorBase {
public:
static_assert(std::is_base_of<esp::sensor::Sensor, T>::value,
"AbstractManagedSensor :: Managed sensor object type must be "
"derived from esp::sensor::Sensor");

typedef std::weak_ptr<T> WeakObjRef;

explicit AbstractManagedSensor(const std::string& classKey)
: ManagedSensorBase(classKey) {}

void setObjectRef(const std::shared_ptr<T>& objRef) {
setObjectRefInternal(objRef);
}

/**
* @brief Return whether this is a visual sensor or not
*/
bool isVisualSensor() const {
if (auto sp = this->getObjectReference()) {
return sp->isVisualSensor();
}
return false;
}

/**
* @brief Retrieve a comma-separated string holding the header values for the
* info returned for this managed object.
*/
std::string getObjectInfoHeader() const override {
return "Type," + getSensorObjInfoHeaderInternal();
}

/**
* @brief Retrieve a comma-separated informational string about the contents
* of this managed object.
*/
std::string getObjectInfo() const override {
if (auto sp = this->getObjectReference()) {
return Cr::Utility::formatString("{},{},", classKey_,
getSensorObjInfoInternal(sp));
}
return Cr::Utility::formatString("Unknown classkey {},", classKey_);
}

protected:
/**
* @brief Retrieve a comma-separated string holding the header values for
* the info returned for this managed object, type-specific.
*/

virtual std::string getSensorObjInfoHeaderInternal() const = 0;
/**
* @brief Specialization-specific extension of getObjectInfo, comma
* separated info ideal for saving to csv
*/
virtual std::string getSensorObjInfoInternal(
std::shared_ptr<T>& sp) const = 0;

/**
* @brief This function accesses the underlying shared pointer of this
* object's @p weakObjRef_ if it exists; if not, it provides a message.
* @return Either a shared pointer of this wrapper's object, or nullptr if
* DNE.
*/
std::shared_ptr<T> inline getObjectReference() const {
return getObjectReferenceInternal<T>();
} // getObjectReference

public:
ESP_SMART_POINTERS(AbstractManagedSensor<T>)
}; // class AbstractManagedSensor

/**
* @brief Base class template for wrapper for visual sensor objects of all kinds
* that extends AbstractManagedSensorAccess
*/

template <class T>
class AbstractManagedVisualSensor
: public esp::sensor::AbstractManagedSensor<T> {
public:
static_assert(std::is_base_of<esp::sensor::VisualSensor, T>::value,
"AbstractManagedVisualSensor :: Managed visual sensor object "
"type must be derived from esp::sensor::VisualSensor");

explicit AbstractManagedVisualSensor(const std::string& classKey)
: AbstractManagedSensor<T>(classKey) {}
// TODO Add appropriate abstract visual sensor getters/setters here

protected:
/**
* @brief Retrieve a comma-separated string holding the header values for
* the info returned for this managed object, type-specific.
*/

std::string getSensorObjInfoHeaderInternal() const override {
return "" + getVisualSensorObjInfoHeaderInternal();
}
/**
* @brief Retrieve a comma-separated string holding the header values for
* the info returned for this managed visual sensor, type-specific.
*/

virtual std::string getVisualSensorObjInfoHeaderInternal() const = 0;
/**
* @brief Specialization-specific extension of getObjectInfo, comma
* separated info ideal for saving to csv
*/
std::string getSensorObjInfoInternal(
CORRADE_UNUSED std::shared_ptr<VisualSensor>& sp) const override {
// TODO provide info stream for sensors
std::string res = Cr::Utility::formatString(
"{},{}", "VisualSensor", getVisualSensorObjInfoInternal());

return res;
}

/**
* @brief Specialization-specific extension of getSensorObjInfoInternal, comma
* separated info ideal for saving to csv
*/
virtual std::string getVisualSensorObjInfoInternal(
std::shared_ptr<T>& sp) const = 0;

public:
ESP_SMART_POINTERS(AbstractManagedVisualSensor<T>)
}; // class AbstractManagedVisualSensor

} // namespace sensor
} // namespace esp

#endif // ESP_SENSOR_MANAGEDSENSORTEMPLATES_H_

0 comments on commit d4259be

Please sign in to comment.