Skip to content

Commit

Permalink
--initial commit; stub manager and wrapper base classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jturner65 committed Dec 11, 2024
1 parent f3089bc commit bf0712f
Show file tree
Hide file tree
Showing 3 changed files with 294 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/esp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -458,23 +458,25 @@ set(nav_SOURCES
## sensor
set(
sensor_SOURCES
sensor/AudioSensor.cpp
sensor/AudioSensor.h
sensor/AudioSensorStubs.h
sensor/CameraSensor.cpp
sensor/CameraSensor.h
sensor/CubeMapSensorBase.cpp
sensor/CubeMapSensorBase.h
sensor/EquirectangularSensor.cpp
sensor/EquirectangularSensor.h
sensor/FisheyeSensor.cpp
sensor/FisheyeSensor.h
sensor/sensorManagers/SensorBaseManager.h
sensor/sensorWrappers/ManagedSensorBase.h
sensor/Sensor.cpp
sensor/Sensor.h
sensor/SensorFactory.cpp
sensor/SensorFactory.h
sensor/VisualSensor.cpp
sensor/VisualSensor.h
sensor/FisheyeSensor.cpp
sensor/FisheyeSensor.h
sensor/EquirectangularSensor.cpp
sensor/EquirectangularSensor.h
sensor/AudioSensor.cpp
sensor/AudioSensor.h
sensor/AudioSensorStubs.h
)

if(BUILD_WITH_CUDA)
Expand Down
167 changes: 167 additions & 0 deletions src/esp/sensor/sensorManagers/SensorBaseManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// 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_SENSORBASEMANAGER_H_
#define ESP_SENSOR_SENSORBASEMANAGER_H_

/** @file
* @brief Class Template @ref esp::sensor::SensorBaseManager
*/

#include "esp/core/managedContainers/ManagedContainer.h"
#include "esp/sensor/sensorWrappers/ManagedSensorBase.h"

namespace esp {
namespace core {
namespace managedContainers {
enum class ManagedObjectAccess;
class ManagedContainerBase;
} // namespace managedContainers
} // namespace core
namespace sensor {

/**
* @brief Class template defining responsibilities and functionality for
* managineg sensor wrappers specializing @ref
* esp::sensor::AbstractManagedSensor template.
* @tparam T the type of managed sensor object wrapper a particular
* specialization of this class works with. Must inherit from @ref
* esp::sensor::AbstractManagedSensor
*/
template <class T>
class SensorBaseManager
: public esp::core::managedContainers::ManagedContainer<
T,
core::managedContainers::ManagedObjectAccess::Copy> {
public:
typedef std::shared_ptr<T> ObjWrapperPtr;
explicit SensorBaseManager(const std::string& SensorType)
: core::managedContainers::ManagedContainer<
T,
core::managedContainers::ManagedObjectAccess::Copy>::
ManagedContainer(SensorType) {}
~SensorBaseManager() override = default;

/**
* @brief Creates an empty @ref esp::sensor::AbstractManagedSensor of
* the type managed by this manager.
*
* @param sensorTypeName Class name of the wrapper to create. This will be
* used as a key in @p managedSensorTypeConstructorMap_.
* @param registerObject whether to add this managed object to the
* library or not. If the user is going to edit this managed object, this
* should be false. Defaults to true. If specified as true, then this function
* returns a copy of the registered managed object.
* @return a reference to the desired managed object.
*/
ObjWrapperPtr createObject(
const std::string& sensorTypeName,
CORRADE_UNUSED bool registerObject = false) override;

protected:
/**
* @brief Any sensor-wrapper-specific resetting that needs to happen
* on reset.
*/
void resetFinalize() override {}

/**
* @brief Build a shared pointer to a AbstractManagedSensor wrapper around an
* appropriately cast @ref esp::sensor::Sensor.
* @tparam The type of the underlying wrapped object to create
*/
template <typename U>
ObjWrapperPtr createSensorObjectWrapper() {
return U::create();
}

/**
* @brief Used Internally. Create and configure newly-created managed object
* with any default values, before any specific values are set.
*
* @param sensorTypeName Used to determine what kind of Rigid Object wrapper
* to make (either kinematic or dynamic-library-specific)
* @param builtFromConfig Unused for wrapper objects. All wrappers are
* constructed from scratch.
* @return Newly created but unregistered ManagedObject pointer, with only
* default values set.
*/
ObjWrapperPtr initNewObjectInternal(
const std::string& sensorTypeName,
CORRADE_UNUSED bool builtFromConfig) override {
// construct a new wrapper based on the passed object
auto mgdSensorTypeCtorMapIter =
managedSensorTypeConstructorMap_.find(sensorTypeName);
if (mgdSensorTypeCtorMapIter == managedSensorTypeConstructorMap_.end()) {
ESP_ERROR(Mn::Debug::Flag::NoSpace)
<< "<" << this->sensorType_ << "> Unknown constructor type "
<< sensorTypeName << ", so initNewObject aborted.";
return nullptr;
}
auto newWrapper = (*this.*(mgdSensorTypeCtorMapIter->second))();

return newWrapper;
} // RigidObjectManager::initNewObjectInternal(

/**
* @brief Not required for this manager.
*
* This method will perform any essential updating to the managed object
* before registration is performed. If this updating fails, registration will
* also fail.
* @param object the managed object to be registered
* @param objectHandle the name to register the managed object with.
* Expected to be valid.
* @param forceRegistration Will register object even if conditional
* registration checks fail.
* @return Whether the preregistration has succeeded and what handle to use to
* register the object if it has.
*/
core::managedContainers::ManagedObjectPreregistration
preRegisterObjectFinalize(CORRADE_UNUSED ObjWrapperPtr object,
CORRADE_UNUSED const std::string& objectHandle,
CORRADE_UNUSED bool forceRegistration) override {
// No pre-registration conditioning performed
return core::managedContainers::ManagedObjectPreregistration::Success;
}

/**
* @brief Not required for this manager.
*
* This method will perform any final manager-related handling after
* successfully registering an object.
*
* See @ref esp::attributes::managers::ObjectAttributesManager for an example.
*
* @param objectID the ID of the successfully registered managed object
* @param objectHandle The name of the managed object
*/
void postRegisterObjectHandling(
CORRADE_UNUSED int objectID,
CORRADE_UNUSED const std::string& objectHandle) override {}

// ====== instance variables =====
/** @brief Define a map type referencing function pointers to @ref
* createRigidObjectWrapper() keyed by string names of classes being
* instanced
*/
typedef std::map<std::string, ObjWrapperPtr (SensorBaseManager<T>::*)()>
Map_Of_ManagedObjTypeCtors;

/** @brief Map of function pointers to instantiate a @ref
* esp::sensor::AbstractManagedSensor wrapper object, keyed by the
* wrapper's class name. A ManagedRigidObject wrapper of the appropriate type
* is instanced by accessing the constructor map with the appropriate
* classname.
*/
Map_Of_ManagedObjTypeCtors managedSensorTypeConstructorMap_;

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

} // namespace sensor
} // namespace esp

#endif // ESP_SENSOR_SENSORBASEMANAGER_H_
118 changes: 118 additions & 0 deletions src/esp/sensor/sensorWrappers/ManagedSensorBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// 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_MANAGEDSENSORBASE_H_
#define ESP_SENSOR_MANAGEDSENSORBASE_H_

#include <Corrade/Utility/Macros.h>

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

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 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 = Corrade::Utility;
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 {
std::shared_ptr<T> 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;
} // getObjectReference

/**
* @brief Set this managed object's class. Should only be set from
* constructor. Used as key in constructor function pointer maps in Managed
* Container.
* @param classKey the string handle corresponding to the
* constructors used to make copies of this object in copy constructor map.
*/
void setClassKey(const std::string& classKey) override {
classKey_ = classKey;
}

/**
* @brief Weak ref to object. If user has copy of this wrapper but object
* has been deleted, this will be nullptr.
*/
WeakObjRef weakObjRef_{};

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

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

#endif // ESP_SENSOR_MANAGEDSENSORBASE_H_

0 comments on commit bf0712f

Please sign in to comment.