Skip to content

Commit 607b758

Browse files
authored
Merge pull request #53 from diffpy/migration-nano
Migration nano
2 parents e6e11af + b3955ae commit 607b758

38 files changed

Lines changed: 174 additions & 179 deletions

‎site_scons/site_tools/cxxtest.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@
6565

6666
from SCons.Script import *
6767
from SCons.Builder import Builder
68-
from SCons.Util import PrependPath, unique, uniquer
68+
from SCons.Util import PrependPath, unique, uniquer_hashables
6969
import os
7070

7171
# A warning class to notify users of problems
72-
class ToolCxxTestWarning(SCons.Warnings.Warning):
72+
class ToolCxxTestWarning(SCons.Warnings.SConsWarning):
7373
pass
7474

7575
SCons.Warnings.enableWarningClass(ToolCxxTestWarning)
@@ -105,7 +105,7 @@ def prepend_ld_library_path(env, overrides, **kwargs):
105105
"""Prepend LD_LIBRARY_PATH with LIBPATH to run successfully programs that
106106
were linked against local shared libraries."""
107107
# make it unique but preserve order ...
108-
libpath = uniquer(Split(kwargs.get('CXXTEST_LIBPATH', [])) +
108+
libpath = uniquer_hashables(Split(kwargs.get('CXXTEST_LIBPATH', [])) +
109109
Split(env.get( 'CXXTEST_LIBPATH', [])))
110110
if len(libpath) > 0:
111111
libpath = env.arg2nodes(libpath, env.fs.Dir)

‎src/SConscript‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ if env['tool'] == 'intelc':
1919

2020
fast_linkflags = ['-s']
2121

22+
# libobjcryst is built with double precision for ObjCryst's REAL type.
23+
# Match that ABI in all consumers of ObjCryst headers.
24+
env.PrependUnique(CPPDEFINES={'REAL': 'double'})
25+
2226
# Specify minimum C++ standard. Allow later standard from sconscript.local.
2327
# In case of multiple `-std` options the last option holds.
24-
env.PrependUnique(CXXFLAGS='-std=c++11', delete_existing=1)
28+
env.PrependUnique(CXXFLAGS='-std=c++23', delete_existing=1)
2529

2630
# Platform specific intricacies.
2731
if env['PLATFORM'] == 'darwin':

‎src/diffpy/Attributes.hpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <set>
2525
#include <map>
2626
#include <stdexcept>
27-
#include <boost/shared_ptr.hpp>
27+
#include <memory>
2828

2929
namespace diffpy {
3030
namespace attributes {
@@ -109,7 +109,7 @@ class Attributes
109109

110110
// types
111111
typedef std::map<std::string,
112-
boost::shared_ptr<attributes::BaseDoubleAttribute> >
112+
std::shared_ptr<attributes::BaseDoubleAttribute> >
113113
DoubleAttributeStorage;
114114
// data
115115
DoubleAttributeStorage mdoubleattrs;

‎src/diffpy/HasClassRegistry.hpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include <stdexcept>
3333
#include <set>
3434
#include <map>
35-
#include <boost/shared_ptr.hpp>
35+
#include <memory>
3636

3737
namespace diffpy {
3838

@@ -42,7 +42,7 @@ class HasClassRegistry
4242
public:
4343

4444
// types
45-
typedef boost::shared_ptr<TBase> SharedPtr;
45+
typedef std::shared_ptr<TBase> SharedPtr;
4646

4747
// Contains virtual methods
4848
virtual ~HasClassRegistry() { }

‎src/diffpy/mathutils.hpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool eps_lt(const double& x, const double& y, double eps=SQRT_DOUBLE_EPS);
4949

5050
// binary functor for round-off aware less-than comparison
5151

52-
class EpsilonLess : public std::binary_function<double, double, bool>
52+
class EpsilonLess
5353
{
5454
public:
5555

@@ -77,7 +77,7 @@ class EpsilonLess : public std::binary_function<double, double, bool>
7777

7878
// binary functor for round-off aware equality comparison
7979

80-
class EpsilonEqual : public std::binary_function<double, double, bool>
80+
class EpsilonEqual
8181
{
8282
public:
8383

‎src/diffpy/mathutils.ipp‎

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,6 @@
1919
#ifndef MATHUTILS_IPP_INCLUDED
2020
#define MATHUTILS_IPP_INCLUDED
2121

22-
// define round and remainder when compiling with MSVC
23-
#ifdef _MSC_VER
24-
25-
inline double round(double x)
26-
{
27-
return floor(x + 0.5);
28-
}
29-
30-
31-
inline double remainder(double x, double y)
32-
{
33-
return ((x > 0 && y > 0) || (x < 0 && y < 0)) ?
34-
fmod(x, y) : (fmod(x, y) + y);
35-
}
36-
37-
38-
inline double log2(double x)
39-
{
40-
return log(x) / log(2.0);
41-
}
42-
43-
#endif // _MSC_VER
44-
4522
namespace diffpy {
4623
namespace mathutils {
4724

‎src/diffpy/runtimepath.cpp‎

Lines changed: 70 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,21 @@
1717
*****************************************************************************/
1818

1919
#include <cstdlib>
20-
#include <climits>
21-
#include <cstring>
2220
#include <cassert>
21+
#include <filesystem>
2322
#include <sstream>
2423
#include <stdexcept>
25-
#include <sys/stat.h>
24+
25+
#ifdef _WIN32
26+
#ifndef NOMINMAX
27+
#define NOMINMAX
28+
#endif
29+
#include <windows.h>
30+
#include <system_error>
31+
#include <vector>
32+
#else
2633
#include <dlfcn.h>
27-
#include <libgen.h>
34+
#endif
2835

2936
#include <diffpy/version.hpp>
3037
#include <diffpy/runtimepath.hpp>
@@ -37,6 +44,8 @@ using namespace std;
3744

3845
namespace {
3946

47+
namespace fs = std::filesystem;
48+
4049
const char* runtimerelpath =
4150
#ifdef DIFFPYRUNTIMERELPATH
4251
STRINGIFY(DIFFPYRUNTIMERELPATH)
@@ -48,75 +57,84 @@ const char* runtimerelpath =
4857
#endif
4958
;
5059

51-
bool isdir(const string& d)
60+
// Locate the module containing this code, including an embedding extension.
61+
fs::path modulepath()
5262
{
53-
struct stat sd;
54-
return (stat(d.c_str(), &sd) == 0 && S_ISDIR(sd.st_mode));
63+
static const char anchor = 0;
64+
#ifdef _WIN32
65+
HMODULE module = nullptr;
66+
if (!GetModuleHandleExW(
67+
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
68+
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
69+
reinterpret_cast<LPCWSTR>(&anchor), &module))
70+
{
71+
throw system_error(GetLastError(), system_category(),
72+
"Cannot locate the libdiffpy module");
73+
}
74+
vector<wchar_t> buffer(256);
75+
for (;;)
76+
{
77+
DWORD size = GetModuleFileNameW(module, buffer.data(),
78+
static_cast<DWORD>(buffer.size()));
79+
if (size == 0)
80+
{
81+
throw system_error(GetLastError(), system_category(),
82+
"Cannot resolve the libdiffpy module path");
83+
}
84+
if (size < buffer.size())
85+
{
86+
return fs::path(wstring(buffer.data(), size));
87+
}
88+
buffer.resize(2 * buffer.size());
89+
}
90+
#else
91+
Dl_info info = {};
92+
if (!dladdr(&anchor, &info) || !info.dli_fname)
93+
{
94+
throw runtime_error("Cannot resolve the libdiffpy module path");
95+
}
96+
return fs::path(info.dli_fname);
97+
#endif
5598
}
5699

57100

58-
/// throw runtime error if directory does not exist.
59-
void ensureIsDir(const std::string& d)
101+
// Validate an explicit directory without falling back to packaged data.
102+
string canonicalDirectory(const fs::path& directory)
60103
{
61-
if (isdir(d)) return;
62-
string emsg("Directory '@D' does not exist.");
63-
emsg = emsg.replace(emsg.find_first_of('@'), 2, d);
64-
throw runtime_error(emsg);
104+
if (!fs::is_directory(directory))
105+
{
106+
throw runtime_error("Directory '" + directory.string() +
107+
"' does not exist.");
108+
}
109+
return fs::canonical(directory).generic_string();
65110
}
66111

67112

68-
const string& diffpyruntime()
113+
string diffpyruntime()
69114
{
70-
char fpb[PATH_MAX];
71-
static string librt;
72-
static bool did_librt = false;
73-
static string envrt;
74-
static bool did_envrt = false;
75115
// check the DIFFPYRUNTIME environment variable.
76-
char* pe = getenv("DIFFPYRUNTIME");
116+
const char* pe = getenv("DIFFPYRUNTIME");
77117
if (pe && *pe != '\0')
78118
{
79-
static string dprt;
80-
did_envrt = did_envrt && (dprt == pe);
81-
if (!did_envrt)
82-
{
83-
dprt = pe;
84-
envrt = pe;
85-
size_t pt = envrt.find_last_not_of('/');
86-
if (pt == string::npos) envrt = envrt.substr(0, 1);
87-
else envrt.erase(pt + 1);
88-
ensureIsDir(envrt);
89-
envrt = realpath(envrt.c_str(), fpb);
90-
did_envrt = true;
91-
}
92-
return envrt;
119+
return canonicalDirectory(pe);
93120
}
94-
if (did_librt) return librt;
95-
// here we need to resolve path of the libdiffpy shared library
96-
Dl_info i;
97-
dladdr(reinterpret_cast<void*>(diffpyruntime), &i);
121+
static const fs::path module = fs::absolute(modulepath());
98122
// first candidate is resolved in relative data path
99-
strncpy(fpb, i.dli_fname, PATH_MAX);
100-
string d1 = string(dirname(fpb)) + "/" + runtimerelpath;
101-
if (isdir(d1))
123+
const fs::path d1 = module.parent_path() / runtimerelpath;
124+
if (fs::is_directory(d1))
102125
{
103-
librt = realpath(d1.c_str(), fpb);
104-
did_librt = true;
105-
return librt;
126+
return fs::canonical(d1).generic_string();
106127
}
107128
// second candidate is resolved with respect to physical library location
108129
// according to the source tree layout
109-
string d2 = dirname(realpath(i.dli_fname, fpb));
110-
d2 += "/../../src/runtime";
111-
if (isdir(d2))
130+
const fs::path d2 = fs::canonical(module).parent_path() /
131+
"../../src/runtime";
132+
if (fs::is_directory(d2))
112133
{
113-
librt = realpath(d2.c_str(), fpb);
114-
did_librt = true;
115-
return librt;
134+
return fs::canonical(d2).generic_string();
116135
}
117136
// nothing worked - throw exception about the first candidate path.
118-
ensureIsDir(d1);
119-
return librt;
137+
return canonicalDirectory(d1);
120138
}
121139

122140
} // namespace

‎src/diffpy/runtimepath.hpp‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ namespace runtimepath {
2929

3030
/// Return full path to a data file included with DiffPy library.
3131
///
32-
/// The paths are looked up from the DIFFPYRUNTIME environment variable.
33-
/// If DIFFPYRUNTIME does not exist, the paths are resolved as
34-
/// (1) relative path of the datadir directory with respect to the libdir
35-
/// directory at the installation time and if it does not exist as
32+
/// A nonempty DIFFPYRUNTIME environment variable overrides the data directory.
33+
/// If DIFFPYRUNTIME is unset or empty, the paths are resolved as
34+
/// (1) DIFFPYRUNTIMERELPATH relative to the module containing this code
35+
/// (the library or an embedding extension), and if it does not exist as
3636
/// (2) relative path of the runtime directory in a source tree
3737
/// at the build time.
38+
/// DIFFPYRUNTIMERELPATH defaults to ../share/diffpy/libdiffpy-MAJOR.MINOR.
3839
///
39-
/// Throw runtime_error if the base directory cannot be found.
40+
/// Throw runtime_error if the base directory cannot be found, including an
41+
/// invalid explicit override. File existence is checked by the data readers.
4042
std::string datapath(const std::string& f);
4143

4244
/// Helper class for loading text data

‎src/diffpy/serialization.ipp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* diffpy::serialization_fromstring
1919
*
2020
* Definition of macros for explicit instantiation of serialization_tostring
21-
* and serialization_fromstring for type C or type boost::shared_ptr<C>.
21+
* and serialization_fromstring for type C or type std::shared_ptr<C>.
2222
*
2323
* DIFFPY_INSTANTIATE_SERIALIZATION(C)
2424
* DIFFPY_INSTANTIATE_PTR_SERIALIZATION(C)
@@ -29,7 +29,7 @@
2929
#define SERIALIZATION_IPP_INCLUDED
3030

3131
#include <sstream>
32-
#include <boost/shared_ptr.hpp>
32+
#include <memory>
3333
#include <diffpy/serialization.hpp>
3434

3535
namespace diffpy {
@@ -70,9 +70,9 @@ void serialization_fromstring(T& tobj, const std::string& s)
7070
diffpy::serialization_fromstring<C>(C&, const std::string&); \
7171

7272
/// Insert explicit instantiations for serialization_tostring and
73-
/// serialization_fromstring for type boost::shared_ptr<C>.
73+
/// serialization_fromstring for type std::shared_ptr<C>.
7474

7575
#define DIFFPY_INSTANTIATE_PTR_SERIALIZATION(C) \
76-
DIFFPY_INSTANTIATE_SERIALIZATION(boost::shared_ptr<C>)
76+
DIFFPY_INSTANTIATE_SERIALIZATION(std::shared_ptr<C>)
7777

7878
#endif // SERIALIZATION_IPP_INCLUDED

‎src/diffpy/srreal/AtomicStructureAdapter.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ AtomicStructureAdapter::diff(StructureAdapterConstPtr other) const
170170
using std::min;
171171
StructureDifference sd = this->StructureAdapter::diff(other);
172172
if (sd.stru0 == sd.stru1) return sd;
173-
typedef boost::shared_ptr<const class AtomicStructureAdapter> APtr;
174-
APtr pother = boost::dynamic_pointer_cast<APtr::element_type>(other);
173+
typedef std::shared_ptr<const class AtomicStructureAdapter> APtr;
174+
APtr pother = std::dynamic_pointer_cast<APtr::element_type>(other);
175175
if (!pother) return sd;
176176
// try fast side-by-side comparison
177177
sd.diffmethod = StructureDifference::Method::SIDEBYSIDE;

0 commit comments

Comments
 (0)