Add ActorVariant class

This commit is contained in:
nsubiron 2018-10-20 16:12:52 +02:00
parent 0dd8c4e15d
commit 484fe9d949
7 changed files with 128 additions and 46 deletions

View File

@ -7,7 +7,6 @@
#include "carla/client/ActorList.h"
#include "carla/StringUtil.h"
#include "carla/client/Actor.h"
#include "carla/client/detail/ActorFactory.h"
#include <iterator>
@ -15,15 +14,6 @@
namespace carla {
namespace client {
struct GetTypeIdVisitor {
const std::string &operator()(const rpc::Actor &actor) const {
return actor.description.id;
}
const std::string &operator()(const SharedPtr<Actor> &actor) const {
return actor->GetTypeId();
}
};
ActorList::ActorList(
detail::EpisodeProxy episode,
std::vector<rpc::Actor> actors)
@ -32,25 +22,13 @@ namespace client {
ActorList ActorList::Filter(const std::string &wildcard_pattern) const {
ActorList filtered{_episode, {}};
for (auto &&item : _actors) {
const auto &id = boost::apply_visitor(GetTypeIdVisitor(), item);
if (StringUtil::Match(id, wildcard_pattern)) {
filtered._actors.push_back(item);
for (auto &&actor : _actors) {
if (StringUtil::Match(actor.GetTypeId(), wildcard_pattern)) {
filtered._actors.push_back(actor);
}
}
return filtered;
}
SharedPtr<Actor> ActorList::RetrieveActor(value_type &value) const {
if (value.which() == 0u) {
value = detail::ActorFactory::MakeActor(
_episode,
boost::get<rpc::Actor>(std::move(value)),
GarbageCollectionPolicy::Disabled);
}
DEBUG_ASSERT(value.which() == 1u);
return boost::get<SharedPtr<Actor>>(value);
}
} // namespace client
} // namespace carla

View File

@ -6,27 +6,22 @@
#pragma once
#include "carla/Memory.h"
#include "carla/client/detail/EpisodeProxy.h"
#include "carla/rpc/Actor.h"
#include "carla/client/detail/ActorVariant.h"
#include <boost/iterator/transform_iterator.hpp>
#include <boost/variant.hpp>
#include <vector>
namespace carla {
namespace client {
class Actor;
class ActorList {
class ActorList : public EnableSharedFromThis<ActorList> {
private:
template <typename It>
auto MakeIterator(It it) const {
return boost::make_transform_iterator(it, [this](auto &v) {
return RetrieveActor(v);
return v.Get(_episode);
});
}
@ -36,11 +31,11 @@ namespace client {
ActorList Filter(const std::string &wildcard_pattern) const;
SharedPtr<Actor> operator[](size_t pos) const {
return RetrieveActor(_actors[pos]);
return _actors[pos].Get(_episode);
}
SharedPtr<Actor> at(size_t pos) const {
return RetrieveActor(_actors.at(pos));
return _actors.at(pos).Get(_episode);
}
auto begin() const {
@ -65,13 +60,9 @@ namespace client {
ActorList(detail::EpisodeProxy episode, std::vector<rpc::Actor> actors);
using value_type = boost::variant<rpc::Actor, SharedPtr<Actor>>;
SharedPtr<Actor> RetrieveActor(value_type &value) const;
detail::EpisodeProxy _episode;
mutable std::vector<value_type> _actors;
std::vector<detail::ActorVariant> _actors;
};
} // namespace client

View File

@ -9,6 +9,7 @@
#include "carla/Logging.h"
#include "carla/client/Actor.h"
#include "carla/client/ActorBlueprint.h"
#include "carla/client/ActorList.h"
#include "carla/client/detail/Simulator.h"
#include <exception>
@ -40,8 +41,10 @@ namespace client {
_episode.Lock()->SetWeatherParameters(weather);
}
ActorList World::GetActors() const {
return {_episode, _episode.Lock()->GetAllTheActorsInTheEpisode()};
SharedPtr<ActorList> World::GetActors() const {
return SharedPtr<ActorList>{new ActorList{
_episode,
_episode.Lock()->GetAllTheActorsInTheEpisode()}};
}
SharedPtr<Actor> World::SpawnActor(

View File

@ -7,7 +7,6 @@
#pragma once
#include "carla/Memory.h"
#include "carla/client/ActorList.h"
#include "carla/client/detail/EpisodeProxy.h"
#include "carla/geom/Transform.h"
#include "carla/rpc/WeatherParameters.h"
@ -17,6 +16,7 @@ namespace client {
class Actor;
class ActorBlueprint;
class ActorList;
class BlueprintLibrary;
class World {
@ -42,7 +42,7 @@ namespace client {
void SetWeather(const rpc::WeatherParameters &weather);
ActorList GetActors() const;
SharedPtr<ActorList> GetActors() const;
SharedPtr<Actor> SpawnActor(
const ActorBlueprint &blueprint,

View File

@ -0,0 +1,24 @@
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
// de Barcelona (UAB).
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#include "carla/client/detail/ActorVariant.h"
#include "carla/client/detail/ActorFactory.h"
namespace carla {
namespace client {
namespace detail {
void ActorVariant::MakeActor(EpisodeProxy episode) const {
_value = detail::ActorFactory::MakeActor(
episode,
boost::get<rpc::Actor>(std::move(_value)),
GarbageCollectionPolicy::Disabled);
}
} // namespace detail
} // namespace client
} // namespace carla

View File

@ -0,0 +1,87 @@
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
// de Barcelona (UAB).
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#pragma once
#include "carla/Debug.h"
#include "carla/Memory.h"
#include "carla/client/Actor.h"
#include "carla/client/detail/EpisodeProxy.h"
#include "carla/rpc/Actor.h"
#include <boost/variant.hpp>
namespace carla {
namespace client {
namespace detail {
/// Holds an Actor, but only instantiates it when needed.
class ActorVariant {
public:
ActorVariant(rpc::Actor actor)
: _value(actor) {}
ActorVariant(SharedPtr<client::Actor> actor)
: _value(actor) {}
ActorVariant &operator=(rpc::Actor actor) {
_value = actor;
return *this;
}
ActorVariant &operator=(SharedPtr<client::Actor> actor) {
_value = actor;
return *this;
}
SharedPtr<client::Actor> Get(EpisodeProxy episode) const {
if (_value.which() == 0u) {
MakeActor(episode);
}
DEBUG_ASSERT(_value.which() == 1u);
return boost::get<SharedPtr<client::Actor>>(_value);
}
const rpc::Actor &Serialize() const {
return boost::apply_visitor(Visitor(), _value);
}
actor_id_type GetId() const {
return Serialize().id;
}
const std::string &GetTypeId() const {
return Serialize().description.id;
}
bool operator==(ActorVariant rhs) const {
return GetId() == rhs.GetId();
}
bool operator!=(ActorVariant rhs) const {
return !(*this == rhs);
}
private:
struct Visitor {
const rpc::Actor &operator()(const rpc::Actor &actor) const {
return actor;
}
const rpc::Actor &operator()(const SharedPtr<client::Actor> &actor) const {
return actor->Serialize();
}
};
void MakeActor(EpisodeProxy episode) const;
mutable boost::variant<rpc::Actor, SharedPtr<client::Actor>> _value;
};
} // namespace detail
} // namespace client
} // namespace carla

View File

@ -11,7 +11,6 @@
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
namespace carla {
namespace client {
@ -32,7 +31,7 @@ void export_world() {
namespace cc = carla::client;
namespace cg = carla::geom;
class_<cc::ActorList>("ActorList", no_init)
class_<cc::ActorList, boost::shared_ptr<cc::ActorList>>("ActorList", no_init)
.def("filter", &cc::ActorList::Filter)
.def("__getitem__", &cc::ActorList::at)
.def("__len__", &cc::ActorList::size)