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