Skip to content

Commit 5c32401

Browse files
authored
Pass by Value to Pass by Reference (#15)
1 parent 1a43ced commit 5c32401

File tree

10 files changed

+454
-239
lines changed

10 files changed

+454
-239
lines changed

CMakeLists.txt

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,66 +29,11 @@ add_subdirectory(tests)
2929
file(COPY ${CMAKE_SOURCE_DIR}/lattice_files/
3030
DESTINATION ${CMAKE_BINARY_DIR}/lattice_files/)
3131

32-
# if(APPLE)
33-
# # Check for package managers
34-
# execute_process(COMMAND which brew OUTPUT_VARIABLE HOMEBREW_EXISTS OUTPUT_STRIP_TRAILING_WHITESPACE)
35-
# execute_process(COMMAND which port OUTPUT_VARIABLE MACPORTS_EXISTS OUTPUT_STRIP_TRAILING_WHITESPACE)
36-
# execute_process(COMMAND which conda OUTPUT_VARIABLE CONDA_EXISTS OUTPUT_STRIP_TRAILING_WHITESPACE)
37-
38-
# # Error if both Homebrew and MacPorts exist
39-
# if(HOMEBREW_EXISTS AND MACPORTS_EXISTS)
40-
# message(FATAL_ERROR
41-
# "Both Homebrew and MacPorts detected. This can cause conflicts.\n"
42-
# "Please use only one package manager:\n"
43-
# " - Homebrew found at: ${HOMEBREW_EXISTS}\n"
44-
# " - MacPorts found at: ${MACPORTS_EXISTS}\n"
45-
# "Consider uninstalling one to avoid library conflicts."
46-
# )
47-
# endif()
48-
49-
# # Set RPATH based on which package manager is found
50-
# set(BASE_RPATH "@executable_path/../lib;@loader_path")
51-
52-
# if(HOMEBREW_EXISTS)
53-
# message(STATUS "Using Homebrew package manager")
54-
# # Check if Apple Silicon or Intel
55-
# execute_process(
56-
# COMMAND uname -m
57-
# OUTPUT_VARIABLE ARCH
58-
# OUTPUT_STRIP_TRAILING_WHITESPACE
59-
# )
60-
# if(ARCH STREQUAL "arm64")
61-
# set(CMAKE_INSTALL_RPATH "${BASE_RPATH};/opt/homebrew/lib")
62-
# list(APPEND CMAKE_PREFIX_PATH "/opt/homebrew")
63-
# else()
64-
# set(CMAKE_INSTALL_RPATH "${BASE_RPATH};/usr/local/lib")
65-
# list(APPEND CMAKE_PREFIX_PATH "/usr/local")
66-
# endif()
67-
# elseif(MACPORTS_EXISTS)
68-
# message(STATUS "Using MacPorts package manager")
69-
# set(CMAKE_INSTALL_RPATH "${BASE_RPATH};/opt/local/lib")
70-
# list(APPEND CMAKE_PREFIX_PATH "/opt/local")
71-
# elseif(CONDA_EXISTS)
72-
# message(STATUS "Using Conda package manager")
73-
# # Get conda prefix
74-
# execute_process(
75-
# COMMAND conda info --base
76-
# OUTPUT_VARIABLE CONDA_PREFIX
77-
# OUTPUT_STRIP_TRAILING_WHITESPACE
78-
# )
79-
# set(CMAKE_INSTALL_RPATH "${BASE_RPATH};${CONDA_PREFIX}/lib")
80-
# list(APPEND CMAKE_PREFIX_PATH "${CONDA_PREFIX}")
81-
# else()
82-
# message(STATUS "No package manager detected, using default paths")
83-
# set(CMAKE_INSTALL_RPATH "${BASE_RPATH};/usr/local/lib")
84-
# endif()
85-
86-
# set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
87-
# message(STATUS "RPATH set to: ${CMAKE_INSTALL_RPATH}")
88-
# endif()
89-
9032
add_library(yaml_c_wrapper SHARED src/yaml_c_wrapper.cpp)
9133
target_link_libraries(yaml_c_wrapper PUBLIC yaml-cpp)
9234

93-
add_executable(yaml_reader examples/yaml_reader.cpp)
94-
target_link_libraries(yaml_reader yaml_c_wrapper)
35+
add_executable(example_read_write examples/example_read_write.cpp)
36+
target_link_libraries(example_read_write yaml_c_wrapper yaml-cpp)
37+
38+
add_executable(get_lattices src/get_lattices.cpp)
39+
target_link_libraries(get_lattices yaml_c_wrapper yaml-cpp)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ include
66
inherits
77
repeat
88

9+
YAML::Nodes are values that act like pointers, so editing a node will cause the tree the node is
10+
contained in to reflect the changes.
11+
912
## Usage
1013
In pals-cpp, run
1114

examples/example_read_write.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
#include "../src/yaml_c_wrapper.h"
3+
#include <yaml-cpp/yaml.h>
4+
5+
int main(int argc, char* argv[]) {
6+
// reading a lattice from a yaml file
7+
YAMLNodeHandle handle = parse_file("../lattice_files/ex.pals.yaml");
8+
std::cout << "Output of example_read_write.cpp" << std::endl;
9+
// printing to terminal
10+
std::cout << yaml_to_string(handle) << std::endl << std::endl;
11+
12+
// type checking
13+
// prints "handle is of type sequence: 1", 1 meaning true
14+
std::cout << "handle is of type sequence: " << (is_sequence(handle))
15+
<< "\n";
16+
17+
// accessing sequence
18+
YAMLNodeHandle node = get_index(handle, 0);
19+
/* prints
20+
the first element is:
21+
thingB:
22+
kind: Sextupole
23+
*/
24+
std::cout << "the first element is: \n" << yaml_to_string(node) << "\n";
25+
26+
// accessing map
27+
// prints "the value at key 'thingB' is: kind: Sextupole"
28+
std::cout << "\nthe value at key 'thingB' is: "
29+
<< yaml_to_string(get_key(node, "thingB")) << "\n";
30+
31+
// creating a new node that's a map
32+
YAMLNodeHandle map = create_map();
33+
set_value_int(map, "apples", 5);
34+
35+
// creating a new node that's a sequence
36+
YAMLNodeHandle sequence = create_sequence();
37+
push_string(sequence, "magnet1");
38+
push_string(sequence, "");
39+
YAMLNodeHandle scalar = create_scalar();
40+
set_scalar_string(scalar, "magnet2");
41+
set_at_index(sequence, 1, scalar);
42+
// give sequence a name by putting it in a map:
43+
YAMLNodeHandle magnets = create_map();
44+
set_value_node(magnets, "magnets", sequence);
45+
46+
// adding new nodes to lattice
47+
push_node(handle, map);
48+
push_node(handle, magnets);
49+
50+
// getting expanded lattice
51+
struct lattices lat = get_lattices("ex.pals.yaml", "lat1");
52+
YAMLNodeHandle expanded = lat.expanded;
53+
54+
// writing modified lattice file to expand.pals.yaml
55+
write_file(handle, "../lattice_files/expand.pals.yaml");
56+
return 0;
57+
}

lattice_files/ex.pals.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
- thingB:
22
kind: Sextupole
3+
- lat1:
4+
kind: Lattice
5+
branches:
6+
- inj_line
7+
- lat2:
8+
kind: Lattice
9+
branches:
10+
- inj_line
311
- inj_line:
412
kind: BeamLine
513
multipass: true
@@ -20,4 +28,6 @@
2028
line:
2129
- a
2230
- b
23-
- c
31+
- c
32+
- use: "lat2"
33+
- use: "lat1"

lattice_files/include.pals.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
- line:
22
- first: 1
33
- second: 2
4-
- third: 3
4+
- third: 3
5+
- include: "include2.pals.yaml"

lattice_files/include2.pals.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- line:
2+
- fourth: 4
3+
- fifth: 5
4+
- sixth: 6

src/get_lattices.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include "../src/yaml_c_wrapper.h"
3+
#include <cstring>
4+
5+
int main(int argc, char* argv[]) {
6+
std::string file_name = "ex.pals.yaml";
7+
const char* lattice_name = "";
8+
if (argc >= 1) {
9+
file_name = argv[0];
10+
}
11+
if (argc >= 2) {
12+
for (int i = 1; i < argc; i++) {
13+
if (strcmp(argv[i], "-lat") == 0) {
14+
lattice_name = argv[i+1];
15+
}
16+
}
17+
}
18+
19+
struct lattices lat = get_lattices("ex.pals.yaml", lattice_name);
20+
std::cout << "Printing original lattice information: " << std::endl;
21+
std::cout << yaml_to_string(lat.original) << std::endl << "\n\n";
22+
23+
// put separating lines here
24+
std::cout << "Printing included lattice information: " << std::endl;
25+
std::cout << yaml_to_string(lat.included) << std::endl << "\n\n";
26+
27+
std::cout << "Printing expanded lattice information: " << std::endl;
28+
std::cout << yaml_to_string(lat.expanded) << std::endl;
29+
return 0;
30+
}

0 commit comments

Comments
 (0)