26using SampleEnvironmentSpecCache = std::unordered_map<std::string, SampleEnvironmentSpec_uptr>;
33SampleEnvironmentSpecCache &retrieveSpecCache() {
34 static SampleEnvironmentSpecCache cache;
45std::string createCacheKey(
const std::string &facility,
const std::string &instrument,
const std::string &specName) {
46 return facility +
"/" + instrument +
"/" + specName;
59 : m_finder(
std::move(specFinder)) {}
71 const std::string &instrument,
72 const std::string &specName,
73 const std::string &canName) {
75 auto &specCache = retrieveSpecCache();
76 auto cacheKey = createCacheKey(facility, instrument, specName);
79 auto iter = specCache.find(cacheKey);
80 if (iter != specCache.end()) {
81 spec = iter->second.get();
83 auto specUPtr =
m_finder->find(facility, instrument, specName);
84 spec = specUPtr.get();
85 specCache.emplace(cacheKey, std::move(specUPtr));
107 const std::string &filepath)
const {
109 std::filesystem::path fullpath = std::filesystem::path(filepath) / (filename +
".xml");
110 return m_finder->parseSpec(filename, fullpath.string());
126 : m_rootDirs(
std::move(directories)) {
128 throw std::invalid_argument(
"SampleEnvironmentSpecFileFinder() - Empty directory search list.");
140 const std::string &instrument,
141 const std::string &
name)
const {
142 using std::filesystem::path;
144 path relpath_instr = path(facility) / instrument / (
name +
m_fileext);
149 for (
const auto &rel_path : {relpath_instr, relpath_facil}) {
151 path prefix(prefixStr);
152 path fullpath = prefix / rel_path;
153 if (std::filesystem::exists(fullpath)) {
154 g_log.
debug() <<
"Found environment at \"" << fullpath <<
"\"\n";
157 g_log.
debug() <<
"Failed to find environment at \"" << fullpath <<
"\"\n";
163 std::ostringstream msg;
164 msg <<
"Unable to find sample environment file '" <<
name <<
"' for facility '" << facility <<
"' and instrument '"
165 << instrument <<
"'";
166 throw std::runtime_error(msg.str());
176 const std::string &filename)
const {
177 std::ifstream reader(filename, std::ios_base::in);
179 throw std::runtime_error(
"SampleEnvironmentSpecFileFinder() - Error accessing file '" + filename +
"'");
182 return parser.
parse(
name, filename, reader);
SampleEnvironmentSpec_uptr parseSpec(const std::string &filename, const std::string &filepath) const
Calls SampleEnvironmentSpecFileFinder::parseSpec.
void clearCache()
Clear the cache of SampleEnvironmentSpec objects.
SampleEnvironmentFactory()=default
Geometry::SampleEnvironment_uptr create(const std::string &facility, const std::string &instrument, const std::string &specName, const std::string &canName)
Create a new SampleEnvironment instance from the given specification and can.
ISampleEnvironmentSpecFinder_uptr m_finder
SampleEnvironmentSpecFileFinder(std::vector< std::string > directories)
Constructor accepting a list of directories to search.
const std::vector< std::string > m_rootDirs
SampleEnvironmentSpec_uptr parseSpec(const std::string &name, const std::string &filename) const override
Parses the specification from the given file.
SampleEnvironmentSpec_uptr find(const std::string &facility, const std::string &instrument, const std::string &name) const override
Find a named specification in a file.
const std::string m_fileext
Read an XML definition of a SampleEnvironmentSpec and produce a new SampleEnvironmentSpec object.
SampleEnvironmentSpec_uptr parse(const std::string &name, const std::string &filename, std::istream &istr)
Takes a stream that is assumed to contain a single complete SampleEnvironmentSpec definition,...
Defines the properties of a named SampleEnvironment setup.
Geometry::SampleEnvironment_uptr buildEnvironment(const std::string &canID) const
Build a new SampleEnvironment instance from a given can ID.
The Logger class is in charge of the publishing messages from the framework through various channels.
void debug(const std::string &msg)
Logs at debug level.
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::unique_ptr< SampleEnvironmentSpec > SampleEnvironmentSpec_uptr
unique_ptr to a SampleEnvironmentSpec
std::unique_ptr< ISampleEnvironmentSpecFinder > ISampleEnvironmentSpecFinder_uptr
std::unique_ptr< SampleEnvironment > SampleEnvironment_uptr