Introduce the style::Layer factory classes

This patch introduces the initial implementation of
- A `LayerFactory` abstract class that creates `style::Layer` instances of
  a certain layer type (line, hillshade, round, ..)
- A singleton `LayerManager` class, which is responsible for initializing
  the `LayerFactory` instances and forwarding the `create()` calls to the
  corresponding factory.
This commit is contained in:
Mikhail Pozdnyakov
2018-10-28 22:33:06 +02:00
parent 278aa33703
commit cfcb53128b
23 changed files with 400 additions and 114 deletions

View File

@@ -30,9 +30,9 @@ class Filter;
* * Cloning and copying
*
* All other functionality lives in the derived classes. To instantiate a layer, create an instance of the desired
* type, passing the ID:
* type, calling `LayerManager`:
*
* auto circleLayer = std::make_unique<CircleLayer>("my-circle-layer");
* auto circleLayer = LayerManager::get()->createLayer("circle", ...);
*/
class Layer {
public:
@@ -89,5 +89,42 @@ protected:
LayerObserver* observer = nullptr;
};
/**
* @brief The LayerFactory abstract class
*
* This class is responsible for creation of the layer objects that belong to a concrete layer type.
*/
class LayerFactory {
public:
virtual ~LayerFactory() = default;
/// Returns the type of the layers, created by this factory.
virtual const char* type() const = 0;
/// Returns a new Layer instance on success call; returns `nulltptr` otherwise.
virtual std::unique_ptr<Layer> createLayer(const std::string& id, const conversion::Convertible& value) = 0;
protected:
optional<std::string> getSource(const conversion::Convertible& value) const;
bool initSourceLayerAndFilter(Layer*, const conversion::Convertible& value) const;
};
/**
* @brief A singleton class forwarding calls to the corresponding \c LayerFactory instance.
*/
class LayerManager {
public:
/**
* @brief A singleton getter.
*
* @return LayerManager*
*/
static LayerManager* get();
virtual ~LayerManager() = default;
/// Returns a new Layer instance on success call; returns `nulltptr` otherwise.
virtual std::unique_ptr<Layer> createLayer(const std::string& type, const std::string& id, const conversion::Convertible& value, conversion::Error& error) = 0;
protected:
LayerManager() = default;
};
} // namespace style
} // namespace mbgl

View File

@@ -56,5 +56,13 @@ protected:
Mutable<Layer::Impl> mutableBaseImpl() const final;
};
class BackgroundLayerFactory : public LayerFactory {
protected:
// LayerFactory overrides.
~BackgroundLayerFactory() override;
const char* type() const final;
std::unique_ptr<style::Layer> createLayer(const std::string& id, const conversion::Convertible& value) override;
};
} // namespace style
} // namespace mbgl

View File

@@ -104,5 +104,13 @@ protected:
Mutable<Layer::Impl> mutableBaseImpl() const final;
};
class CircleLayerFactory : public LayerFactory {
protected:
// LayerFactory overrides.
~CircleLayerFactory() override;
const char* type() const final;
std::unique_ptr<style::Layer> createLayer(const std::string& id, const conversion::Convertible& value) override;
};
} // namespace style
} // namespace mbgl

View File

@@ -80,5 +80,13 @@ protected:
Mutable<Layer::Impl> mutableBaseImpl() const final;
};
class FillExtrusionLayerFactory : public LayerFactory {
protected:
// LayerFactory overrides.
~FillExtrusionLayerFactory() override;
const char* type() const final;
std::unique_ptr<style::Layer> createLayer(const std::string& id, const conversion::Convertible& value) override;
};
} // namespace style
} // namespace mbgl

View File

@@ -80,5 +80,13 @@ protected:
Mutable<Layer::Impl> mutableBaseImpl() const final;
};
class FillLayerFactory : public LayerFactory {
protected:
// LayerFactory overrides.
~FillLayerFactory() override;
const char* type() const final;
std::unique_ptr<style::Layer> createLayer(const std::string& id, const conversion::Convertible& value) override;
};
} // namespace style
} // namespace mbgl

View File

@@ -69,5 +69,13 @@ protected:
Mutable<Layer::Impl> mutableBaseImpl() const final;
};
class HeatmapLayerFactory : public LayerFactory {
protected:
// LayerFactory overrides.
~HeatmapLayerFactory() override;
const char* type() const final;
std::unique_ptr<style::Layer> createLayer(const std::string& id, const conversion::Convertible& value) override;
};
} // namespace style
} // namespace mbgl

View File

@@ -74,5 +74,13 @@ protected:
Mutable<Layer::Impl> mutableBaseImpl() const final;
};
class HillshadeLayerFactory : public LayerFactory {
protected:
// LayerFactory overrides.
~HillshadeLayerFactory() override;
const char* type() const final;
std::unique_ptr<style::Layer> createLayer(const std::string& id, const conversion::Convertible& value) override;
};
} // namespace style
} // namespace mbgl

View File

@@ -72,5 +72,13 @@ protected:
Mutable<Layer::Impl> mutableBaseImpl() const final;
};
class <%- camelize(type) %>LayerFactory : public LayerFactory {
protected:
// LayerFactory overrides.
~<%- camelize(type) %>LayerFactory() override;
const char* type() const final;
std::unique_ptr<style::Layer> createLayer(const std::string& id, const conversion::Convertible& value) override;
};
} // namespace style
} // namespace mbgl

View File

@@ -125,5 +125,13 @@ protected:
Mutable<Layer::Impl> mutableBaseImpl() const final;
};
class LineLayerFactory : public LayerFactory {
protected:
// LayerFactory overrides.
~LineLayerFactory() override;
const char* type() const final;
std::unique_ptr<style::Layer> createLayer(const std::string& id, const conversion::Convertible& value) override;
};
} // namespace style
} // namespace mbgl

View File

@@ -86,5 +86,13 @@ protected:
Mutable<Layer::Impl> mutableBaseImpl() const final;
};
class RasterLayerFactory : public LayerFactory {
protected:
// LayerFactory overrides.
~RasterLayerFactory() override;
const char* type() const final;
std::unique_ptr<style::Layer> createLayer(const std::string& id, const conversion::Convertible& value) override;
};
} // namespace style
} // namespace mbgl

View File

@@ -274,5 +274,13 @@ protected:
Mutable<Layer::Impl> mutableBaseImpl() const final;
};
class SymbolLayerFactory : public LayerFactory {
protected:
// LayerFactory overrides.
~SymbolLayerFactory() override;
const char* type() const final;
std::unique_ptr<style::Layer> createLayer(const std::string& id, const conversion::Convertible& value) override;
};
} // namespace style
} // namespace mbgl