Skip to content

Commit

Permalink
Config no longer depends on Flux/Image/Encode/Decode, breaking a circ…
Browse files Browse the repository at this point in the history
…ular

dependency.
  • Loading branch information
davidgiven committed Oct 11, 2024
1 parent c6cbae4 commit 7e80e25
Show file tree
Hide file tree
Showing 30 changed files with 241 additions and 161 deletions.
1 change: 0 additions & 1 deletion build/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ def libraryimpl(
name=f"{self.localname}_hdr",
ins=ins,
outs=outs,
deps=deps,
commands=cs,
label="CHEADERS",
)
Expand Down
108 changes: 2 additions & 106 deletions lib/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
#include "lib/proto.h"
#include "lib/logger.h"
#include "lib/core/utils.h"
#include "lib/imagewriter/imagewriter.h"
#include "lib/imagereader/imagereader.h"
#include "lib/fluxsink/fluxsink.h"
#include "lib/fluxsource/fluxsource.h"
#include "lib/encoders/encoders.h"
#include "lib/decoders/decoders.h"
#include <fstream>
#include <google/protobuf/text_format.h>

Expand Down Expand Up @@ -211,24 +205,10 @@ ConfigProto* Config::combined()

_combinedConfig.MergeFrom(_overridesConfig);

/* At this point the config is mostly valid. We're about to make calls
* that will want to call combined() reentrantly, so to prevent infinite
* loop we mark the config as valid now. */
/* At this point the config is valid, although when fluxsources or
* imagereaders are loaded it may be adjusted again. */

_configValid = true;

/* We should now be more or less done, but we still need to add in any
* config contributed by the flux source and image readers. This will
* open the files. */

if (hasFluxSource())
_combinedConfig.MergeFrom(getFluxSource()->getExtraConfig());
if (hasImageReader())
_combinedConfig.MergeFrom(getImageReader()->getExtraConfig());

/* Merge in the overrides once again. */

_combinedConfig.MergeFrom(_overridesConfig);
}
return &_combinedConfig;
}
Expand All @@ -244,11 +224,6 @@ void Config::clear()
_baseConfig.Clear();
_overridesConfig.Clear();
_combinedConfig.Clear();
_fluxSource.reset();
_verificationFluxSource.reset();
_imageReader.reset();
_encoder.reset();
_decoder.reset();
_appliedOptions.clear();
}

Expand Down Expand Up @@ -581,115 +556,36 @@ bool Config::hasFluxSource()
return (*this)->flux_source().type() != FLUXTYPE_NOT_SET;
}

std::shared_ptr<FluxSource>& Config::getFluxSource()
{
if (!_fluxSource)
{
if (!hasFluxSource())
error("no flux source configured");

_fluxSource =
std::shared_ptr(FluxSource::create((*this)->flux_source()));
}
return _fluxSource;
}

bool Config::hasVerificationFluxSource() const
{
return _verificationFluxSourceProto.type() != FLUXTYPE_NOT_SET;
}

std::shared_ptr<FluxSource>& Config::getVerificationFluxSource()
{
if (!_verificationFluxSource)
{
if (!hasVerificationFluxSource())
error("no verification flux source configured");

_verificationFluxSource =
std::shared_ptr(FluxSource::create(_verificationFluxSourceProto));
}
return _verificationFluxSource;
}

bool Config::hasImageReader()
{
return (*this)->image_reader().type() != IMAGETYPE_NOT_SET;
}

std::shared_ptr<ImageReader>& Config::getImageReader()
{
if (!_imageReader)
{
if (!hasImageReader())
error("no image reader configured");

_imageReader =
std::shared_ptr(ImageReader::create((*this)->image_reader()));
}
return _imageReader;
}

bool Config::hasFluxSink()
{
return (*this)->flux_sink().type() != FLUXTYPE_NOT_SET;
}

std::unique_ptr<FluxSink> Config::getFluxSink()
{
if (!hasFluxSink())
error("no flux sink configured");

return FluxSink::create((*this)->flux_sink());
}

bool Config::hasImageWriter()
{
return (*this)->image_writer().type() != IMAGETYPE_NOT_SET;
}

std::unique_ptr<ImageWriter> Config::getImageWriter()
{
if (!hasImageWriter())
error("no image writer configured");

return ImageWriter::create((*this)->image_writer());
}

bool Config::hasEncoder()
{
return (*this)->has_encoder();
}

std::shared_ptr<Encoder>& Config::getEncoder()
{
if (!_encoder)
{
if (!hasEncoder())
error("no encoder configured");

_encoder = Encoder::create((*this)->encoder());
}
return _encoder;
}

bool Config::hasDecoder()
{
return _combinedConfig.has_decoder();
}

std::shared_ptr<Decoder>& Config::getDecoder()
{
if (!_decoder)
{
if (!hasDecoder())
error("no decoder configured");

_decoder = Decoder::create((*this)->decoder());
}
return _decoder;
}

const std::vector<FluxConstructor>& Config::getFluxFormats()
{
return fluxConstructors;
Expand Down
22 changes: 5 additions & 17 deletions lib/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,21 @@ class Config
void setImageReader(std::string value);
void setImageWriter(std::string value);

/* Fetch the sources, opening them if necessary. */
/* Query useful things about the config. */

bool hasFluxSource();
std::shared_ptr<FluxSource>& getFluxSource();
bool hasImageReader();
std::shared_ptr<ImageReader>& getImageReader();
bool hasVerificationFluxSource() const;
std::shared_ptr<FluxSource>& getVerificationFluxSource();

/* Fetch the encoder and decoder, creating them if necessary. */
const FluxSourceProto& getVerificationFluxSourceProto() const
{
return _verificationFluxSourceProto;
}

bool hasEncoder();
std::shared_ptr<Encoder>& getEncoder();
bool hasDecoder();
std::shared_ptr<Decoder>& getDecoder();

/* Create the sinks: these are not cached. */

bool hasFluxSink();
std::unique_ptr<FluxSink> getFluxSink();
bool hasImageWriter();
std::unique_ptr<ImageWriter> getImageWriter();

public:
static const std::vector<FluxConstructor>& getFluxFormats();
Expand All @@ -175,11 +168,6 @@ class Config
std::set<std::string> _appliedOptions;
bool _configValid;

std::shared_ptr<FluxSource> _fluxSource;
std::shared_ptr<ImageReader> _imageReader;
std::shared_ptr<FluxSource> _verificationFluxSource;
std::shared_ptr<Encoder> _encoder;
std::shared_ptr<Decoder> _decoder;
FluxSourceProto _verificationFluxSourceProto;
};

Expand Down
1 change: 0 additions & 1 deletion lib/core/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,3 @@ int findLowestSetBit(uint64_t value)
}
return bit;
}

8 changes: 8 additions & 0 deletions lib/decoders/decoders.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "lib/core/globals.h"
#include "lib/flags.h"
#include "lib/fluxmap.h"
#include "lib/config.h"
#include "lib/decoders/decoders.h"
#include "lib/encoders/encoders.h"
#include "arch/agat/agat.h"
Expand Down Expand Up @@ -32,6 +33,13 @@
#include "lib/layout.h"
#include <numeric>

std::unique_ptr<Decoder> Decoder::create(Config& config)
{
if (!config.hasDecoder())
error("no decoder configured");
return create(config->decoder());
}

std::unique_ptr<Decoder> Decoder::create(const DecoderProto& config)
{
static const std::map<int,
Expand Down
2 changes: 2 additions & 0 deletions lib/decoders/decoders.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class FluxMatcher;
class FluxmapReader;
class RawBits;
class DecoderProto;
class Config;

#include "lib/flux.h"

Expand Down Expand Up @@ -39,6 +40,7 @@ class Decoder

virtual ~Decoder() {}

static std::unique_ptr<Decoder> create(Config& config);
static std::unique_ptr<Decoder> create(const DecoderProto& config);

public:
Expand Down
7 changes: 7 additions & 0 deletions lib/encoders/encoders.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
#include "lib/image.h"
#include "protocol.h"

std::unique_ptr<Encoder> Encoder::create(Config& config)
{
if (!config.hasEncoder())
error("no encoder configured");
return create(config->encoder());
}

std::unique_ptr<Encoder> Encoder::create(const EncoderProto& config)
{
static const std::map<int,
Expand Down
2 changes: 2 additions & 0 deletions lib/encoders/encoders.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ class Image;
class Layout;
class Sector;
class TrackInfo;
class Config;

class Encoder
{
public:
Encoder(const EncoderProto& config) {}
virtual ~Encoder() {}

static std::unique_ptr<Encoder> create(Config& config);
static std::unique_ptr<Encoder> create(const EncoderProto& config);

public:
Expand Down
8 changes: 8 additions & 0 deletions lib/fluxsink/fluxsink.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#include "lib/core/globals.h"
#include "lib/flags.h"
#include "lib/config.h"
#include "lib/fluxsink/fluxsink.h"
#include "lib/config.pb.h"
#include "lib/proto.h"
#include "lib/core/utils.h"
#include <regex>

std::unique_ptr<FluxSink> FluxSink::create(Config& config)
{
if (!config.hasFluxSink())
error("no flux sink configured");
return create(config->flux_sink());
}

std::unique_ptr<FluxSink> FluxSink::create(const FluxSinkProto& config)
{
switch (config.type())
Expand Down
2 changes: 2 additions & 0 deletions lib/fluxsink/fluxsink.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class A2RFluxSinkProto;
class VcdFluxSinkProto;
class ScpFluxSinkProto;
class Fl2FluxSinkProto;
class Config;

class FluxSink
{
Expand All @@ -34,6 +35,7 @@ class FluxSink
static std::unique_ptr<FluxSink> createFl2FluxSink(
const std::string& filename);

static std::unique_ptr<FluxSink> create(Config& config);
static std::unique_ptr<FluxSink> create(const FluxSinkProto& config);

public:
Expand Down
8 changes: 8 additions & 0 deletions lib/fluxsource/fluxsource.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#include "lib/core/globals.h"
#include "lib/config.h"
#include "lib/flags.h"
#include "lib/fluxsource/fluxsource.h"
#include "lib/fluxmap.h"
#include "lib/config.pb.h"
#include "lib/proto.h"
#include "lib/core/utils.h"

std::unique_ptr<FluxSource> FluxSource::create(Config& config)
{
if (!config.hasFluxSource())
error("no flux source configured");
return create(config->flux_source());
}

std::unique_ptr<FluxSource> FluxSource::create(const FluxSourceProto& config)
{
switch (config.type())
Expand Down
2 changes: 2 additions & 0 deletions lib/fluxsource/fluxsource.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class KryofluxFluxSourceProto;
class ScpFluxSourceProto;
class TestPatternFluxSourceProto;
class FlxFluxSourceProto;
class Config;

class FluxSourceIterator
{
Expand Down Expand Up @@ -58,6 +59,7 @@ class FluxSource
static std::unique_ptr<FluxSource> createMemoryFluxSource(
const DiskFlux& flux);

static std::unique_ptr<FluxSource> create(Config& config);
static std::unique_ptr<FluxSource> create(const FluxSourceProto& spec);

public:
Expand Down
8 changes: 8 additions & 0 deletions lib/imagereader/imagereader.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "lib/core/globals.h"
#include "lib/config.h"
#include "lib/flags.h"
#include "lib/sector.h"
#include "lib/imagereader/imagereader.h"
Expand All @@ -11,6 +12,13 @@
#include <algorithm>
#include <ctype.h>

std::unique_ptr<ImageReader> ImageReader::create(Config& config)
{
if (!config.hasImageReader())
error("no image reader configured");
return create(config->image_reader());
}

std::unique_ptr<ImageReader> ImageReader::create(const ImageReaderProto& config)
{
switch (config.type())
Expand Down
Loading

0 comments on commit 7e80e25

Please sign in to comment.