Tulip 6.1.0
Large graphs analysis and drawing
Loading...
Searching...
No Matches
TlpTools.h
1/*
2 *
3 * This file is part of Tulip (https://tulip.labri.fr)
4 *
5 * Authors: David Auber and the Tulip development Team
6 * from LaBRI, University of Bordeaux
7 *
8 * Tulip is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation, either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * Tulip is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
17 *
18 */
19
20#ifndef _TLPTOOLS_H
21#define _TLPTOOLS_H
22
23#include <iostream>
24#include <climits>
25#include <sys/types.h>
26#include <tulip/tulipconf.h>
27#include <random>
28
29// The hash_combine function from the boost library
30// Call it repeatedly to incrementally create a hash value from several variables.
31
32// Explanation of the formula from StackOverflow
33// (http://stackoverflow.com/questions/4948780/magic-numbers-in-boosthash-combine) :
34// The magic number 0x9e3779b9 = 2^32 / ((1 + sqrt(5)) / 2) is the reciprocal of the golden ratio.
35// It is supposed to be 32 random bits, where each is equally likely to be 0 or 1, and with no
36// simple correlation between the bits.
37// So including this number "randomly" changes each bit of the seed; as you say, this means that
38// consecutive values will be far apart.
39// Including the shifted versions of the old seed makes sure that, even if hash_value() has a fairly
40// small range of values,
41// differences will soon be spread across all the bits.
42namespace std {
43template <class T>
44inline void tlp_hash_combine(std::size_t &seed, const T &v) {
45 hash<T> hasher;
46 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
47}
48} // namespace std
49
50namespace tlp {
51extern TLP_SCOPE const char PATH_DELIMITER;
52extern TLP_SCOPE std::string TulipLibDir;
53extern TLP_SCOPE std::string TulipPluginsPath;
54extern TLP_SCOPE std::string TulipBitmapDir;
55extern TLP_SCOPE std::string TulipShareDir;
56extern TLP_SCOPE bool TulipProgramExiting;
57
58/**
59 * @ingroup Plugins
60 *
61 * @brief Initializes the Tulip library.
62 * Looks for the Tulip plug-ins directory.
63 * The plug-ins directory can be defined in different ways, given by order of prevalence :
64 * 1. the TLP_DIR environment variable, if it has a value
65 * 2. the appDirPath parameter, if it is not nullptr
66 * 3. at that point, the Tulip paths will be retrieved from the path of the loaded Tulip shared
67 * library
68 * (you must dispose of a standard Tulip installation for that feature to work).
69 * 4. a fallback value of 'C:/Tulip/lib/' on windows, or '/usr/local/lib/' on Unix.
70 */
71extern TLP_SCOPE void initTulipLib(const char *appDirPath = nullptr);
72
73/**
74 * @ingroup Plugins
75 *
76 * @brief Demangles the name of a C++ class
77 * @param className The mangled name of a class
78 * @param hideTlp a flag to indicate if the 'tlp::' prefix
79 * @return string The demangled name of a Tulip C++ class.
80 */
81TLP_SCOPE std::string demangleClassName(const char *className, bool hideTlp = false);
82
83/**
84 * @ingroup Plugins
85 *
86 * @brief Demangles the name of a C++ class defined in the tlp namespace.
87 * @param className The mangled name of a class
88 * @return string The demangled name of a Tulip C++ class
89 * without the tlp:: prefix
90 */
91inline std::string demangleTlpClassName(const char *className) {
92 return demangleClassName(className, true);
93}
94
95/**
96 * @brief Returns an istream to read from a gzipped file (uses gzstream lib).
97 * The stream has to be deleted after use.
98 * @param name The name of the file to read from.
99 * @param open_mode The mode to open the file with. Defaults to std::ios::in.
100 * @return istream gzipped input stream from a file.
101 */
102TLP_SCOPE std::istream *getIgzstream(const std::string &name, int open_mode = std::ios::in);
103
104/**
105 * @brief Returns an ostream to write to a gzipped file (uses gzstream lib).
106 * The stream has to be deleted after use.
107 * @warning Don't forget to check the stream with ios::bad()!
108 * @param name The name of the file to write to.
109 * @param open_mode The mode to open the file with. Defaults to std::ios::out.
110 * @return ostream gzipped output stream to a file.
111 */
112TLP_SCOPE std::ostream *getOgzstream(const std::string &name, int open_mode = std::ios::out);
113
114/**
115 * @brief Gives the value of the seed used for further initialization
116 * of a random sequence (with further calls to rand() or random()).
117 * @param seed the value of the seed.
118 * Set seed to UINT_MAX if you need a random choice of the seed.
119 */
120TLP_SCOPE void setSeedOfRandomSequence(unsigned int seed = UINT_MAX);
121
122/**
123 * @brief Returns the value of the seed used for further initialization
124 * of a random sequence
125 */
126TLP_SCOPE unsigned int getSeedOfRandomSequence();
127
128// Mersenne Twister pseudo-random generator of 32-bit numbers
129static std::mt19937 tlpmt;
130
131/**
132 * @brief Initializes a random sequence with the seed previously set
133 * Further calls to rand() or random() will return the elements of
134 * that sequence
135 */
136TLP_SCOPE void initRandomSequence();
137
138/**
139 * @brief Returns a random integer in the range [0, max] if max is positive or in the range [max, 0]
140 * if max is negative
141 */
142TLP_SCOPE int randomInteger(int max);
143
144/**
145 * @brief Returns a random unsigned integer in the range [0, max]
146 */
147TLP_SCOPE unsigned int randomUnsignedInteger(unsigned int max);
148
149/**
150 * @brief Returns a random double in the range [0, max]
151 */
152TLP_SCOPE double randomDouble(double max = 1.0);
153
154/**
155 * @brief returns if a path exists
156 * @param pathname an utf-8 encoded string containing the path to check
157 * @return true if a file or directory according pathname can be accessed, falseif not and errno is
158 * set
159 */
160TLP_SCOPE bool pathExist(const std::string &pathname);
161
162/**
163 * @brief Cross-platform function to get an input file stream. Its purpose is to support
164 * paths on windows platform containing non-ascii characters.
165 * @param filename an utf-8 encoded string containing the path of the file to open for performing
166 * input operations
167 * @param open_mode the stream opening mode flags (bitwise combination of std::ios_base::openmode
168 * constants).
169 */
170TLP_SCOPE std::istream *getInputFileStream(const std::string &filename,
171 std::ios_base::openmode open_mode = std::ios::in);
172
173/**
174 * @brief Cross-platform function to get an output file stream. Its purpose is to support
175 * paths on windows platform containing non-ascii characters.
176 * @param filename an utf-8 encoded string containing the path of the file to open for performing
177 * output operations
178 * @param open_mode the stream opening mode flags (bitwise combination of std::ios_base::openmode
179 * constants).
180 */
181TLP_SCOPE std::ostream *getOutputFileStream(const std::string &filename,
182 std::ios_base::openmode open_mode = std::ios::out);
183
184/**
185 * @brief Returns the error message associated to errno
186 */
187TLP_SCOPE char *getStrError();
188
189///@cond DOXYGEN_HIDDEN
190// Gui test mode
191TLP_SCOPE bool inGuiTestingMode();
192TLP_SCOPE void setGuiTestingMode(bool);
193// exit handler registration
194TLP_SCOPE void registerTulipExitHandler();
195///@endcond
196} // namespace tlp
197#endif
std::string demangleTlpClassName(const char *className)
Demangles the name of a C++ class defined in the tlp namespace.
Definition TlpTools.h:91
void initTulipLib(const char *appDirPath=nullptr)
Initializes the Tulip library. Looks for the Tulip plug-ins directory. The plug-ins directory can be ...
std::string demangleClassName(const char *className, bool hideTlp=false)
Demangles the name of a C++ class.
std::istream * getIgzstream(const std::string &name, int open_mode=std::ios::in)
Returns an istream to read from a gzipped file (uses gzstream lib). The stream has to be deleted afte...
std::istream * getInputFileStream(const std::string &filename, std::ios_base::openmode open_mode=std::ios::in)
Cross-platform function to get an input file stream. Its purpose is to support paths on windows platf...
std::ostream * getOgzstream(const std::string &name, int open_mode=std::ios::out)
Returns an ostream to write to a gzipped file (uses gzstream lib). The stream has to be deleted after...
char * getStrError()
Returns the error message associated to errno.
unsigned int randomUnsignedInteger(unsigned int max)
Returns a random unsigned integer in the range [0, max].
bool pathExist(const std::string &pathname)
returns if a path exists
double randomDouble(double max=1.0)
Returns a random double in the range [0, max].
unsigned int getSeedOfRandomSequence()
Returns the value of the seed used for further initialization of a random sequence.
void initRandomSequence()
Initializes a random sequence with the seed previously set Further calls to rand() or random() will r...
void setSeedOfRandomSequence(unsigned int seed=UINT_MAX)
Gives the value of the seed used for further initialization of a random sequence (with further calls ...
std::ostream * getOutputFileStream(const std::string &filename, std::ios_base::openmode open_mode=std::ios::out)
Cross-platform function to get an output file stream. Its purpose is to support paths on windows plat...
int randomInteger(int max)
Returns a random integer in the range [0, max] if max is positive or in the range [max,...