7 #include "fwAtomsPatch/VersionsManager.hpp" 8 #include "fwAtomsPatch/exceptions/MissingInformation.hpp" 9 #include "fwAtomsPatch/exceptions/BadExtension.hpp" 10 #include "fwAtomsPatch/types.hpp" 14 #include <boost/filesystem.hpp> 15 #include <boost/property_tree/ptree.hpp> 16 #include <boost/property_tree/json_parser.hpp> 17 #include <boost/property_tree/exceptions.hpp> 19 #include <camp/class.hpp> 26 std::string getValue(const ::boost::property_tree::ptree& node,
const std::string& name,
27 const ::boost::filesystem::path& filePath )
32 value = node.get< std::string >(name);
34 catch( ::boost::property_tree::ptree_bad_path & )
37 name +
" information are missing in '"+ filePath.string() +
"'."));
43 SPTR(VersionsManager) VersionsManager::s_default =
std::make_shared<VersionsManager>();
47 VersionsManager::VersionsManager()
53 VersionsManager::~VersionsManager()
62 for (::boost::filesystem::recursive_directory_iterator end, dir(dirPath); dir != end; ++dir)
64 if( !::boost::filesystem::is_directory(*dir)
65 && ::boost::filesystem::extension(*dir) ==
".versions")
67 m_versionTable.push_back((*dir).path());
77 for ( ::boost::filesystem::recursive_directory_iterator end, dir(dirPath);
80 if( !::boost::filesystem::is_directory(*dir)
81 && ::boost::filesystem::extension(*dir) ==
".graphlink")
83 m_linkTable.push_back((*dir).path());
91 const std::string& context,
const std::string& versionName)
94 filePath.extension() !=
".versions");
96 namespace pt = ::boost::property_tree;
97 std::size_t classCount = ::camp::classCount();
100 root.put(
"context", context);
101 root.put(
"version_name", versionName);
104 for (
int i = 0; i < classCount; ++i)
106 const ::camp::Class& metaclass = ::camp::classByIndex(i);
107 const std::string& className = metaclass.name();
109 if (metaclass.hasTag(::fwAtomsPatch::s_OBJ_VERSION))
111 const ::camp::Value& value = metaclass.tag(::fwAtomsPatch::s_OBJ_VERSION);
112 versions.put(className, value.to<std::string>());
116 root.push_back( pt::ptree::value_type(
"versions", pt::ptree(versions)));
117 pt::json_parser::write_json(filePath.string(), root);
125 filePath.extension() !=
".versions");
127 namespace pt = ::boost::property_tree;
132 std::ifstream file(filePath.string().c_str());
133 std::istream input(file.rdbuf());
134 pt::json_parser::read_json(input, root);
135 const std::string& context = getValue(root,
"context", filePath);
136 const std::string& versionName = getValue(root,
"version_name", filePath);
138 for(pt::ptree::value_type &node : root.get_child(
"versions"))
140 versionids[node.first] = std::string(node.second.data().c_str());
145 return ::fwAtomsPatch::VersionDescriptor(context, versionName, versionids);
153 filePath.extension() !=
".graphlink");
155 namespace pt = ::boost::property_tree;
156 typedef std::vector< std::pair< std::string, std::string > > LinkType;
162 std::ifstream file(filePath.string().c_str());
163 std::istream input(file.rdbuf());
164 pt::json_parser::read_json(input, root);
166 const std::string& context = getValue(root,
"context", filePath);
167 const std::string& originVersion = getValue(root,
"origin_version", filePath);
168 const std::string& targetVersion = getValue(root,
"target_version", filePath);
170 const std::string& patcher = root.get(
"patcher",
"DefaultPatcher");
172 for(pt::ptree::value_type &child : root.get_child(
"links"))
174 for(pt::ptree::value_type &node : (child.second).get_child(
""))
176 link.push_back(std::make_pair(node.first, node.second.data()));
180 "A link should contain an origin version and a target version."), link.size() != 2);
182 links[link[0]] = link[1];
185 LinkType().swap(link);
190 return ::fwAtomsPatch::LinkDescriptor(context, originVersion, targetVersion, patcher, links);
195 void VersionsManager::generateVersionsGraph()
200 for(VersionsManager::ListPathType::value_type elt : m_versionTable)
204 if(m_versionsGraphMap.find(version.
getContext()) == m_versionsGraphMap.end())
206 m_versionsGraphMap[version.
getContext()] = VersionsGraph::New();
209 m_versionsGraphMap[version.
getContext()]->addNode(version);
216 for(VersionsManager::ListPathType::value_type elt : m_linkTable)
221 m_versionsGraphMap.find(link.
getContext()) != m_versionsGraphMap.end());
223 m_versionsGraphMap[link.
getContext()]->addEdge(link);
232 VersionsGraph::sptr vg;
235 if (m_versionsGraphMap.empty())
238 VersionsManager::generateVersionsGraph();
241 VersionsGraphMapType::iterator elem = m_versionsGraphMap.find(context);
242 if (elem != m_versionsGraphMap.end())
::boost::upgrade_lock< ReadWriteMutex > ReadToWriteLock
Defines an upgradable lock type for read/write mutex.
::boost::upgrade_to_unique_lock< ReadWriteMutex > UpgradeToWriteLock
Defines a write lock upgraded from ReadToWriteLock.
#define OSLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
const std::string & getContext() const
Returns context name.
Version descriptor used to identify a version.
Contains base functionalities used to transform objects from a version to another.
::boost::unique_lock< ReadWriteMutex > WriteLock
Defines a lock of write type for read/write mutex.
FWATOMSPATCH_API void buildLinkTable(const std::string &dirPath)
Builds link table.
Exception defining a wrong extension in a filename.
FWATOMSPATCH_API void buildVersionTable(const std::string &dirPath)
Builds version table.
std::map< std::string, std::string > VersionsType
VersionIDs used to link type and version.
FWATOMSPATCH_API VersionsGraph::sptr getGraph(const std::string &context)
Returns graph corresponding to given context.
Link descriptor used to identify a link between two versions.
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
const std::string & getContext() const
Returns context name.
std::map< VersionIDType, VersionIDType > LinksType
Links used to link two versions.
This file defines SpyLog macros. These macros are used to log messages to a file or to the console du...
static FWATOMSPATCH_API void generateNewFile(const ::boost::filesystem::path &filePath, const std::string &context, const std::string &versionName)
Generates a .versions file with currently known data versions.