Katana Plug-in APIs 0.1

AttrDataCache.h

00001 // Copyright (c) 2014 The Foundry Visionmongers Ltd. All Rights Reserved.
00002 
00003 #ifndef ATTRIBUTEDATACACHE_H
00004 #define ATTRIBUTEDATACACHE_H
00005 
00006 #include <sstream>
00007 #include <boost/shared_ptr.hpp>
00008 
00009 #include <FnGeolib/util/AttributeKeyedCache.h>
00010 
00011 #include <FileReader.h>
00012 #include <FileReaderFactory.h>
00013 
00014 // This cache maps GroupAttributes containing two StringAttribute children
00015 // (named 'filename' and 'soFilename') to AttrData instances, and performs
00016 // queries (get()) in a thread-safe manner.
00017 class AttrDataCache: public FnGeolibUtil::AttributeKeyedCache<AttrData>
00018 {
00019 public:
00020     typedef boost::shared_ptr<AttrDataCache> Ptr;
00021 
00022     AttrDataCache() :
00023         FnGeolibUtil::AttributeKeyedCache<AttrData>(1000, 1000)
00024     {
00025     }
00026 
00027 private:
00028     AttrDataCache::IMPLPtr createValue(const FnAttribute::Attribute & iAttr)
00029     {
00030         // createValue() is called when there is no previously cached data
00031         // associated with the given iAttr attribute. Deconstruct the iAttr
00032         // into filename/soFilename strings, and use the FileReaderFactory
00033         // to load the attribute data from disk.
00034 
00035         FnAttribute::GroupAttribute groupAttr(iAttr);
00036         if (!groupAttr.isValid())
00037         {
00038             return AttrDataCache::IMPLPtr();
00039         }
00040 
00041         const std::string filename =
00042             FnAttribute::StringAttribute(
00043                 groupAttr.getChildByName("filename")).getValue("", false);
00044 
00045         const std::string soFilename =
00046             FnAttribute::StringAttribute(
00047                 groupAttr.getChildByName("soFilename")).getValue("", false);
00048 
00049         if (filename.empty())
00050         {
00051             return AttrDataCache::IMPLPtr();
00052         }
00053 
00054         // Parse it and add it to the cache
00055         FileReaderFactory factory;
00056         if (FileReaderPtr reader = factory.get(soFilename))
00057         {
00058             // Ask the reader plugin to parse the file and return
00059             // the attribute data
00060             const AttrData attrData = reader->read(filename);
00061             return AttrDataCache::IMPLPtr(new AttrData(attrData));
00062         }
00063         else
00064         {
00065             std::ostringstream os;
00066             os << "Error loading plug-in \"" << soFilename << "\".";
00067             throw std::runtime_error(os.str().c_str());
00068         }
00069     }
00070 };
00071 
00072 #endif
 All Classes Functions Variables Typedefs Enumerations Enumerator