Skip to content

Commit 6234947

Browse files
committed
more c++ enhancements
1 parent cd07935 commit 6234947

File tree

9 files changed

+42
-34
lines changed

9 files changed

+42
-34
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ foreach(a IN ITEMS
4646
iso_fortran_binding
4747
allocate contiguous
4848
array bool error exception
49+
iterator
4950
glibc malloc
5051
opaque pointer poly_function poly_type real struct
5152
sleep string submodule

src/allocate/my_alloc.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
extern "C" {
33
#endif
44

5-
void alloc1(float**, float**, size_t*);
6-
void alloc2(float**, float**, size_t*);
7-
void alloc3(float**, float**, size_t*);
8-
void alloc4(float**, float**, size_t*);
5+
void alloc1(float**, float**, const size_t*);
6+
void alloc2(float**, float**, const size_t*);
7+
void alloc3(float**, float**, const size_t*);
8+
void alloc4(float**, float**, const size_t*);
99

10-
void dealloc1(float**, float**, size_t*);
11-
void dealloc2(float**, float**, size_t*);
12-
void dealloc3(float**, float**, size_t*);
13-
void dealloc4(float**, float**, size_t*);
10+
void dealloc1(float**, float**, const size_t*);
11+
void dealloc2(float**, float**, const size_t*);
12+
void dealloc3(float**, float**, const size_t*);
13+
void dealloc4(float**, float**, const size_t*);
1414

15-
void falloc1(float**, size_t*);
15+
void falloc1(float**, const size_t*);
1616
void fdealloc1(float**);
1717

1818
#ifdef __cplusplus

src/contiguous/contiguous.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
extern "C" {
33
#endif
44

5-
void asub(float [], size_t*);
5+
void asub(float [], const size_t*);
66

77
#ifdef __cplusplus
88
}

test/allocate/main.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
#include <iostream>
22
#include <cstdlib>
3+
#include <vector>
34

45
#include "my_alloc.h"
56

67
int main(){
78

8-
size_t d1[1] = {1};
9-
size_t d2[2] = {1,1};
10-
size_t d3[3] = {1,1,1};
11-
size_t d4[4] = {1,1,1,1};
9+
const std::vector<size_t> d1 = {1};
10+
const std::vector<size_t> d2 = {1,1};
11+
const std::vector<size_t> d3 = {1,1,1};
12+
const std::vector<size_t> d4 = {1,1,1,1};
1213

1314
float *A1, *B1, *A2, *B2, *A3, *B3, *A4, *B4;
1415

1516

16-
alloc1(&A1, &B1, d1);
17+
alloc1(&A1, &B1, &d1.front());
1718
std::cout << "1D: A and B allocated\n";
1819

19-
dealloc1(&A1, &B1, d1);
20+
dealloc1(&A1, &B1, &d1.front());
2021
std::cout << "1D: A and B deallocated\n";
2122

22-
alloc2(&A2, &B2, d2);
23+
alloc2(&A2, &B2, &d2.front());
2324
std::cout << "2D: A and B allocated\n";
2425

25-
dealloc2(&A2, &B2, d2);
26+
dealloc2(&A2, &B2, &d2.front());
2627
std::cout << "2D: A and B deallocated\n";
2728

28-
alloc3(&A3, &B3, d3);
29+
alloc3(&A3, &B3, &d3.front());
2930
std::cout << "3D: A and B allocated\n";
3031

31-
dealloc3(&A3, &B3, d3);
32+
dealloc3(&A3, &B3, &d3.front());
3233
std::cout << "3D: A and B deallocated\n";
3334

34-
alloc4(&A4, &B4, d4);
35+
alloc4(&A4, &B4, &d4.front());
3536
std::cout << "4D: A and B allocated\n";
3637

37-
dealloc4(&A4, &B4, d4);
38+
dealloc4(&A4, &B4, &d4.front());
3839
std::cout << "4D: A and B deallocated\n";
3940

4041
std::cout << "OK: allocate test\n";

test/contiguous/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
int main(){
1212

13-
size_t dims[1] = {3};
13+
const std::vector<size_t> dims = {3};
1414

1515
std::vector<float> a(3);
1616

1717
for (size_t i = 0; i < 3; ++i)
1818
a[i] = i+1;
1919

20-
asub(&a[0], dims);
20+
asub(&a[0], &dims.front());
2121

2222
std::cout << "OK: C++ contiguous array\n";
2323

test/iterator/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
add_library(int_iterator OBJECT lib.cpp)
3-
set_property(TARGET int_iterator PROPERTY CXX_STANDARD 14)
3+
target_compile_features(int_iterator PUBLIC cxx_std_20)
44
target_include_directories(int_iterator PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
55

66
add_executable(int_iterator_fortran main.f90)
@@ -10,3 +10,7 @@ add_test(NAME IntegerIteratorFortran COMMAND int_iterator_fortran)
1010
add_executable(int_iterator_cpp main.cpp)
1111
target_link_libraries(int_iterator_cpp PRIVATE int_iterator)
1212
add_test(NAME IntegerIteratorCpp COMMAND int_iterator_cpp)
13+
14+
set_tests_properties(IntegerIteratorFortran IntegerIteratorCpp
15+
PROPERTIES DISABLED true
16+
)

test/iterator/lib.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <vector>
2+
#include <memory>
23

34
extern "C" void* initIterator_C();
45
extern "C" void incrementIterator_C(void*);
@@ -20,7 +21,7 @@ int getIteratorValue(std::vector<int>::iterator &it) {
2021

2122
void* initIterator_C(){
2223
auto it = initIterator();
23-
void* ptr = &(*it.begin());
24+
void* ptr = std::to_address(it.begin());
2425
return ptr;
2526
}
2627

test/iterator/standalone.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#include <cstdlib>
22
#include <string>
33
#include <iostream>
4+
#include <memory>
45

56

67
char* iter_ptr(char* c){
78

89
std::string s(c);
910

10-
std::string::iterator it(s.begin());
11+
auto it(s.begin());
1112

12-
return &(*it);
13+
return std::to_address(it);
1314
}
1415

1516
int main(){

test/time/strptime.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#include <iomanip>
44
#include <sstream>
55

6-
extern "C" char* strptime(const char* s,
7-
const char* f,
6+
extern "C" char* strptime(char* s,
7+
const char* format,
88
struct tm* tm) {
99
// Isn't the C++ standard lib nice? std::get_time is defined such that its
1010
// format parameters are the exact same as strptime. Of course, we have to
@@ -14,9 +14,9 @@ extern "C" char* strptime(const char* s,
1414
// of the versions in any of the C standard libraries.
1515
std::istringstream input(s);
1616
input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
17-
input >> std::get_time(tm, f);
18-
if (input.fail()) {
17+
input >> std::get_time(tm, format);
18+
if (input.fail())
1919
return nullptr;
20-
}
21-
return (char*)(s + input.tellg());
20+
21+
return s + input.tellg();
2222
}

0 commit comments

Comments
 (0)