Skip to content

Commit 1a43ced

Browse files
authored
inherits, repeats, and name changes (#13)
1 parent 782488f commit 1a43ced

7 files changed

Lines changed: 999 additions & 905 deletions

File tree

README.md

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,10 @@
11
## Introduction
22
`yaml_c_wrapper.cpp` wraps `YAML::Node` into C objects so they can be part of a shared object library to interface with other languages
3-
<!--
4-
First install `yaml-cpp` by running
53

6-
macOS:
7-
```console
8-
brew install yaml-cpp
9-
```
10-
or
11-
```console
12-
port install yaml-cpp
13-
```
14-
15-
Linux:
16-
```console
17-
sudo apt-get install libyaml-cpp-dev
18-
```
19-
20-
Windows:
21-
```console
22-
choco install yaml-cpp
23-
```
24-
25-
Manual Install:
26-
```console
27-
git clone https://github.com/jbeder/yaml-cpp.git
28-
cd yaml-cpp/src
29-
mkdir build && cd build
30-
cmake ..
31-
cmake --build .
32-
cmake --install .
33-
```
34-
-->
4+
Implemented components of lattice expansion:
5+
include
6+
inherits
7+
repeat
358

369
## Usage
3710
In pals-cpp, run
@@ -66,6 +39,5 @@ To run a specific test, run
6639
ctest --test-dir build -R "Test Name"
6740
```
6841

69-
## Issue
70-
`yaml-cpp`'s cmake only requires cmake version 3.4, which is deprecated. Warnings must
71-
be suppressed to run properly
42+
## To Do
43+
add test cases for lattice expansion

examples/yaml_reader.cpp

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,71 @@
1-
#include "../src/yaml_c_wrapper.h"
21
#include <iostream>
2+
#include "../src/yaml_c_wrapper.h"
3+
4+
// If file name is provided as a command line argument, this will print out the
5+
// expanded contents of the file to the terminal, as well as to expand.pals.yaml.
6+
// Otherwise, it will use the example file ex.pals.yaml
37

4-
// See ex.pals.yaml for the example lattice file and expand.pals.yaml for the output of this file.
8+
int main(int argc, char* argv[]) {
9+
if (argc != 1) {
10+
char* filename = argv[1];
11+
std::string path = "../lattice_files/";
12+
path += filename;
13+
YAMLNodeHandle handle = parse_file(path.c_str());
514

6-
int main() {
15+
lattice_expand(handle);
16+
std::cout << "Printing out contents of file: " << filename << std::endl;
17+
std::cout << yaml_to_string(handle) << std::endl;
18+
write_file(handle, "../lattice_files/expand.pals.yaml");
19+
return 0;
20+
}
721
// reading a lattice from a yaml file
8-
// allow files to be read in through command line, print back out
9-
YAMLNodeHandle handle = yaml_parse_file("../lattice_files/ex.pals.yaml");
10-
// print out file name that was read in
22+
YAMLNodeHandle handle = parse_file("../lattice_files/ex.pals.yaml");
23+
std::cout << "Printing out contents of file: " << "ex.pals.yaml" << std::endl;
1124
// printing to terminal
1225
std::cout << yaml_to_string(handle) << std::endl << std::endl;
1326

1427
// type checking
1528
// prints "handle is of type sequence: 1", 1 meaning true
16-
std::cout << "handle is of type sequence: " << (yaml_is_sequence(handle)) << "\n";
29+
std::cout << "handle is of type sequence: " << (is_sequence(handle))
30+
<< "\n";
1731

1832
// accessing sequence
19-
YAMLNodeHandle node = yaml_get_index(handle, 0);
33+
YAMLNodeHandle node = get_index(handle, 0);
2034
/* prints
21-
the first element is:
35+
the first element is:
2236
thingB:
2337
kind: Sextupole
2438
*/
2539
std::cout << "the first element is: \n" << yaml_to_string(node) << "\n";
2640

2741
// accessing map
2842
// prints "the value at key 'thingB' is: kind: Sextupole"
29-
std::cout << "\nthe value at key 'thingB' is: " << yaml_to_string(yaml_get_key(node, "thingB")) << "\n";
43+
std::cout << "\nthe value at key 'thingB' is: "
44+
<< yaml_to_string(get_key(node, "thingB")) << "\n";
3045

31-
// creating a new node that's a map
32-
YAMLNodeHandle map = yaml_create_map();
33-
yaml_set_int(map, "first", 1);
46+
// creating a new node that's a map
47+
YAMLNodeHandle map = create_map();
48+
set_value_int(map, "apples", 5);
3449

3550
// creating a new node that's a sequence
36-
YAMLNodeHandle sequence = yaml_create_sequence();
37-
yaml_push_string(sequence, "magnet1");
38-
yaml_push_string(sequence, "");
39-
YAMLNodeHandle scalar = yaml_create_scalar();
40-
yaml_set_scalar_string(scalar, "magnet2");
41-
yaml_set_at_index(sequence, 1, scalar);
51+
YAMLNodeHandle sequence = create_sequence();
52+
push_string(sequence, "magnet1");
53+
push_string(sequence, "");
54+
YAMLNodeHandle scalar = create_scalar();
55+
set_scalar_string(scalar, "magnet2");
56+
set_at_index(sequence, 1, scalar);
4257
// give sequence a name by putting it in a map:
43-
YAMLNodeHandle magnets = yaml_create_map();
44-
yaml_set_node(magnets, "magnets", sequence);
58+
YAMLNodeHandle magnets = create_map();
59+
set_value_node(magnets, "magnets", sequence);
4560

4661
// adding new nodes to lattice
47-
yaml_push_node(handle, map);
48-
yaml_push_node(handle, magnets);
62+
push_node(handle, map);
63+
push_node(handle, magnets);
4964

50-
yaml_expand(handle);
65+
// performing lattice expansion
66+
lattice_expand(handle);
5167

5268
// writing modified lattice file to expand.pals.yaml
53-
yaml_write_file(handle, "../lattice_files/expand.pals.yaml");
69+
write_file(handle, "../lattice_files/expand.pals.yaml");
70+
return 0;
5471
}

lattice_files/ex.pals.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
- thingB:
22
kind: Sextupole
3-
43
- inj_line:
54
kind: BeamLine
65
multipass: true
@@ -16,3 +15,9 @@
1615
- a_subline: # Item a_subline is repeated three times
1716
repeat: 3
1817
- include: "include.pals.yaml"
18+
- a_subline:
19+
kind: BeamLine
20+
line:
21+
- a
22+
- b
23+
- c

lattice_files/include.pals.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
- line:
2-
first: 1
3-
second: 2
4-
thrid: 3
2+
- first: 1
3+
- second: 2
4+
- third: 3

0 commit comments

Comments
 (0)