28using SampleEnvironmentSpecCache = std::unordered_map<std::string, SampleEnvironmentSpec_uptr>;
35SampleEnvironmentSpecCache &retrieveSpecCache() {
36 static SampleEnvironmentSpecCache cache;
47std::string createCacheKey(
const std::string &facility,
const std::string &instrument,
const std::string &specName) {
48 return facility +
"/" + instrument +
"/" + specName;
61 : m_finder(
std::move(specFinder)) {}
73 const std::string &instrument,
74 const std::string &specName,
75 const std::string &canName) {
77 auto &specCache = retrieveSpecCache();
78 auto cacheKey = createCacheKey(facility, instrument, specName);
81 auto iter = specCache.find(cacheKey);
82 if (iter != specCache.end()) {
83 spec = iter->second.get();
85 auto specUPtr =
m_finder->find(facility, instrument, specName);
86 spec = specUPtr.get();
87 specCache.emplace(cacheKey, std::move(specUPtr));
109 const std::string &filepath)
const {
111 Poco::File fullpath(Poco::Path(filepath, filename +
".xml"));
112 return m_finder->parseSpec(filename, fullpath.path());
128 : m_rootDirs(
std::move(directories)) {
130 throw std::invalid_argument(
"SampleEnvironmentSpecFileFinder() - Empty directory search list.");
142 const std::string &instrument,
143 const std::string &name)
const {
147 Path relpath_instr(facility);
148 relpath_instr.append(instrument).append(name +
m_fileext);
150 Path relpath_facil(facility);
154 for (
const auto &rel_path : {relpath_instr, relpath_facil}) {
156 Path prefix(prefixStr);
158 prefix.makeDirectory();
159 File fullpath(Poco::Path(prefix, rel_path));
160 if (fullpath.exists()) {
161 g_log.
debug() <<
"Found environment at \"" << fullpath.path() <<
"\"\n";
164 g_log.
debug() <<
"Failed to find environment at \"" << fullpath.path() <<
"\"\n";
170 std::ostringstream msg;
171 msg <<
"Unable to find sample environment file '" << name <<
"' for facility '" << facility <<
"' and instrument '"
172 << instrument <<
"'";
173 throw std::runtime_error(msg.str());
183 const std::string &filename)
const {
184 std::ifstream reader(filename, std::ios_base::in);
186 throw std::runtime_error(
"SampleEnvironmentSpecFileFinder() - Error accessing file '" + filename +
"'");
189 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