├── examples ├── command_line │ ├── vh.dat │ ├── config.dat │ └── command_line.cc ├── signal │ └── debug_handler.cc ├── io │ ├── console.cc │ ├── abort.cc │ ├── multistream.cc │ ├── prefix.cc │ ├── shunt.cc │ ├── redact.cc │ ├── line_comment.cc │ ├── redirectstream.cc │ ├── indent.cc │ ├── fail.cc │ ├── filterstream.cc │ ├── column.cc │ ├── wrap.cc │ └── nopstream.cc ├── lazy │ └── thunk.cc ├── system │ └── terminal.cc ├── serialize │ ├── line.cc │ ├── hex.cc │ └── text.cc ├── patterns │ └── singleton.cc ├── meta │ └── indices.cc ├── container │ ├── tokenizer.cc │ ├── bijection.cc │ ├── maputil.cc │ ├── bit_vector.cc │ └── bit_array.cc ├── debug │ └── stl_print.cc ├── math │ └── online_stats.cc ├── memory │ └── interner.cc └── Makefile ├── doc └── .gitignore ├── README.md ├── include ├── serialize │ ├── range.h │ ├── line_reader.h │ ├── dec_reader.h │ ├── dec_writer.h │ ├── range_reader.h │ ├── text_style.h │ ├── hex_writer.h │ ├── hex_reader.h │ ├── span_writer.h │ ├── text_writer.h │ ├── span_reader.h │ └── text_reader.h ├── meta │ ├── is_char_pointer.h │ ├── is_stl_tuple.h │ ├── is_stl_string.h │ ├── indices.h │ ├── is_stl_pair.h │ ├── contained_type.h │ ├── is_8_bit.h │ ├── is_16_bit.h │ ├── is_64_bit.h │ ├── is_32_bit.h │ ├── is_stl_adaptor.h │ ├── is_stl_set.h │ ├── is_stl_sequence.h │ ├── is_stl_map.h │ ├── bit_width.h │ ├── is_stl_associative.h │ └── is_stl_container.h ├── patterns │ └── singleton.h ├── command_line │ ├── command_line.h │ ├── arg_group.h │ ├── heading.h │ ├── arg_registry.h │ ├── flag_arg.h │ ├── value_arg.h │ ├── file_arg.h │ ├── folder_arg.h │ └── arg.h ├── io │ ├── abort.h │ ├── shunt.h │ ├── redact.h │ ├── prefix.h │ ├── line_comment.h │ ├── indent.h │ ├── console.h │ ├── multistream.h │ ├── wrap.h │ ├── multibuf.h │ ├── filterstream.h │ ├── redirectbuf.h │ ├── redirectstream.h │ ├── column.h │ ├── fail.h │ └── filterbuf.h ├── debug │ └── stl_print.h ├── container │ ├── bit_array.h │ ├── tokenizer.h │ ├── bit_vector.h │ ├── bijection.h │ └── maputil.h ├── lazy │ └── thunk.h ├── math │ └── online_stats.h ├── system │ └── terminal.h ├── allocator │ └── aligned.h ├── memory │ └── interner.h ├── bits │ └── bit_manip.h └── signal │ └── debug_handler.h └── Makefile /examples/command_line/vh.dat: -------------------------------------------------------------------------------- 1 | { 00000001 ... 00000010 } 2 | -------------------------------------------------------------------------------- /doc/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file 4 | !.gitignore 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | cpputil 2 | ===== 3 | 4 | Miscellaneous c++ code; refactored from here and there. 5 | 6 | Most to all of the code in this repository will require a c++11 compliant compiler to build correctly. 7 | -------------------------------------------------------------------------------- /examples/signal/debug_handler.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include "include/signal/debug_handler.h" 18 | 19 | using namespace cpputil; 20 | using namespace std; 21 | 22 | int main() { 23 | DebugHandler::install_sigsegv(); 24 | 25 | int* v = 0; 26 | *v = 0; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /examples/io/console.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include "include/io/console.h" 18 | 19 | using namespace cpputil; 20 | using namespace std; 21 | 22 | int main() { 23 | Console::msg() << "Hello" << endl; 24 | Console::warn() << "World" << endl; 25 | Console::error() << "It's over" << endl; 26 | 27 | return 0; 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /examples/command_line/config.dat: -------------------------------------------------------------------------------- 1 | ##### ./command_line/command_line 2 | 3 | ##### Program-specific arguments 4 | 5 | # A 16 digit hex value, grouped 8 digits at a time. ie: 01234567 89abcdef 6 | --hex "ffffffff ffffffff" 7 | 8 | # i | 1 <= i <= 10 9 | --int 7 10 | 11 | # A set of characters, each between a and z, inclusive. May include ellipses as shorthand. ie: { a ... d } 12 | --sc "{ a ... z }" 13 | 14 | # (no description provided) 15 | --str "Hello, world!" 16 | 17 | # A sequence of integers, each between 1 and 10, inclusive 18 | --vector "{ 5 ... 10 }" 19 | 20 | # A sequence of 8 digit hex values. May include ellipses as shorthand. ie:{ 00000000 00000001 ... } 21 | --vh vh.dat 22 | 23 | ##### Help and argument utilities: 24 | 25 | # Print this message and quit 26 | # --help 27 | 28 | # Read program args from a configuration file 29 | # --config 30 | 31 | # Print program arguments and quit 32 | --debug_args 33 | 34 | # Print an example configuration file 35 | # --example_config 36 | 37 | -------------------------------------------------------------------------------- /examples/lazy/thunk.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/lazy/thunk.h" 19 | 20 | using namespace cpputil; 21 | using namespace std; 22 | 23 | int slow(int x) { 24 | sleep(x); 25 | return x; 26 | } 27 | 28 | int main() { 29 | auto t1 = make_thunk(slow, 3); 30 | cout << "Hello world!" << endl; 31 | cout << t1 << endl; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /examples/system/terminal.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include "include/system/terminal.h" 18 | 19 | using namespace cpputil; 20 | using namespace std; 21 | 22 | int main() { 23 | Terminal term; 24 | term << "sleep " << (1 + 2); 25 | 26 | cout << "Hello, world!" << endl; 27 | term << endl; 28 | 29 | term << "echo \"Goodbye, world!\"" << endl; 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /examples/io/abort.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/io/filterstream.h" 19 | #include "include/io/abort.h" 20 | 21 | using namespace cpputil; 22 | using namespace std; 23 | 24 | int main() { 25 | ofilterstream os(cout); 26 | os.filter().code(1); 27 | 28 | os << "You should see this..." << endl << "... but not this" << endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /examples/io/multistream.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include "include/io/multistream.h" 18 | 19 | using namespace cpputil; 20 | using namespace std; 21 | 22 | int main() { 23 | omultistream om; 24 | om << "You shouldn't see this" << endl; 25 | 26 | om.insert(cout); 27 | om << "You should see this" << endl; 28 | 29 | om.insert(cerr); 30 | om << "You should see this twice" << endl; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /examples/io/prefix.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/io/filterstream.h" 19 | #include "include/io/prefix.h" 20 | 21 | using namespace cpputil; 22 | using namespace std; 23 | 24 | int main() { 25 | ofilterstream os(cout); 26 | os.filter().prefix("Hello world: "); 27 | 28 | os << "This is" << endl << "a " << endl << "multi-line message" << endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /examples/io/shunt.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include "include/io/filterstream.h" 18 | #include "include/io/shunt.h" 19 | 20 | using namespace cpputil; 21 | using namespace std; 22 | 23 | int main() { 24 | ofilterstream os(cout); 25 | os << "Hello, world!" << endl; 26 | os.filter().close(); 27 | os << "Goodbye, world!" << endl; 28 | os.filter().open(); 29 | os << "Hello, world!" << endl; 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /examples/io/redact.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include "include/io/filterstream.h" 18 | #include "include/io/redact.h" 19 | 20 | using namespace cpputil; 21 | using namespace std; 22 | 23 | int main() { 24 | ofilterstream os(cout); 25 | os << "This is safe to read" << endl; 26 | os.filter().on(); 27 | os << "This isn't" << endl; 28 | os.filter().off(); 29 | os << "This is too" << endl; 30 | 31 | return 0; 32 | } 33 | 34 | 35 | -------------------------------------------------------------------------------- /include/serialize/range.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SERIALIZE_RANGE_H 16 | #define CPPUTIL_INCLUDE_SERIALIZE_RANGE_H 17 | 18 | namespace cpputil { 19 | 20 | template 21 | struct Range { 22 | typedef T value_type; 23 | 24 | static constexpr T lower() { 25 | return Lower; 26 | } 27 | 28 | static constexpr T upper() { 29 | return Upper; 30 | } 31 | }; 32 | 33 | } // namespace cpputil 34 | 35 | #endif 36 | 37 | -------------------------------------------------------------------------------- /examples/serialize/line.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include "include/serialize/line_reader.h" 20 | 21 | using namespace cpputil; 22 | using namespace std; 23 | 24 | int main() { 25 | stringstream ss; 26 | ss << "Line 1: You should see this." << endl; 27 | ss << "Line 2: You shouldn't see this." << endl; 28 | 29 | string s; 30 | LineReader<> lr; 31 | lr(ss, s); 32 | 33 | cout << s << endl; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /examples/io/line_comment.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/io/filterstream.h" 19 | #include "include/io/line_comment.h" 20 | 21 | using namespace cpputil; 22 | using namespace std; 23 | 24 | int main() { 25 | stringstream ss; 26 | ss << "#\n10 #20\n30"; 27 | 28 | int x = 7; 29 | int y = 7; 30 | ifilterstream> is(ss); 31 | is >> x >> y; 32 | 33 | cout << x << " " << y << endl; 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /include/meta/is_char_pointer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_IS_CHAR_POINTER_H 16 | #define CPPUTIL_INCLUDE_META_IS_CHAR_POINTER_H 17 | 18 | namespace cpputil { 19 | 20 | template 21 | struct is_char_pointer : public std::false_type { }; 22 | 23 | template <> 24 | struct is_char_pointer : public std::true_type { }; 25 | 26 | template <> 27 | struct is_char_pointer : public std::true_type { }; 28 | 29 | } // namespace cpputil 30 | 31 | #endif 32 | 33 | 34 | -------------------------------------------------------------------------------- /examples/io/redirectstream.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include "include/io/redirectstream.h" 20 | 21 | using namespace cpputil; 22 | using namespace std; 23 | 24 | int main() { 25 | oredirectstream ors(cout); 26 | ors << "Hello world!" << endl; 27 | 28 | istringstream iss("Hello world!"); 29 | iredirectstream irs(iss); 30 | string s1, s2; 31 | 32 | irs >> s1 >> s2; 33 | cout << s1 << " " << s2 << endl; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /include/serialize/line_reader.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SERIALIZE_LINE_READER_H 16 | #define CPPUTIL_INCLUDE_SERIALIZE_LINE_READER_H 17 | 18 | #include 19 | #include 20 | 21 | #include "include/serialize/text_style.h" 22 | 23 | namespace cpputil { 24 | 25 | template > 26 | struct LineReader { 27 | void operator()(std::istream& is, std::string& s) const { 28 | getline(is, s, Style::eol()); 29 | } 30 | }; 31 | 32 | } // namespace cpputil 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /examples/patterns/singleton.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include "include/patterns/singleton.h" 18 | 19 | using namespace cpputil; 20 | using namespace std; 21 | 22 | int main() { 23 | auto& x = Singleton::get(); 24 | const auto& y = Singleton::get(); 25 | x = 10; 26 | 27 | auto& z = Singleton::get(); 28 | z = 20; 29 | 30 | if (y == 10) { 31 | cout << "It works!" << endl; 32 | } else { 33 | cout << "It's broken!" << endl; 34 | } 35 | 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /include/meta/is_stl_tuple.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_IS_STL_TUPLE_H 16 | #define CPPUTIL_INCLUDE_META_IS_STL_TUPLE_H 17 | 18 | namespace cpputil { 19 | 20 | template 21 | struct is_stl_tuple : public std::false_type { }; 22 | 23 | template 24 | struct is_stl_tuple> : public std::true_type { }; 25 | 26 | template 27 | struct is_stl_tuple> : public std::true_type { }; 28 | 29 | } // namespace cpputil 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /include/meta/is_stl_string.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_IS_STL_STRING_H 16 | #define CPPUTIL_INCLUDE_META_IS_STL_STRING_H 17 | 18 | #include 19 | 20 | namespace cpputil { 21 | 22 | template 23 | struct is_stl_string : public std::false_type { }; 24 | 25 | template <> 26 | struct is_stl_string : public std::true_type { }; 27 | 28 | template <> 29 | struct is_stl_string : public std::true_type { }; 30 | 31 | } // namespace cpputil 32 | 33 | #endif 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /include/meta/indices.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_INDICES_H 16 | #define CPPUTIL_INCLUDE_META_INDICES_H 17 | 18 | namespace cpputil { 19 | 20 | template 21 | struct Indices { 22 | static constexpr size_t size() { 23 | return sizeof...(Is); 24 | } 25 | }; 26 | 27 | template 28 | struct MakeIndices : MakeIndices < N - 1, N - 1, Is... > { }; 29 | 30 | template 31 | struct MakeIndices<0, Is...> : Indices { }; 32 | 33 | } // namespace cpputil 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /include/meta/is_stl_pair.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_IS_STL_PAIR_H 16 | #define CPPUTIL_INCLUDE_META_IS_STL_PAIR_H 17 | 18 | namespace cpputil { 19 | 20 | template 21 | struct is_stl_pair : public std::false_type { }; 22 | 23 | template 24 | struct is_stl_pair> : public std::true_type { }; 25 | 26 | template 27 | struct is_stl_pair> : public std::true_type { }; 28 | 29 | } // namespace cpputil 30 | 31 | #endif 32 | 33 | -------------------------------------------------------------------------------- /include/patterns/singleton.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_PATTERNS_SINGLETON_H 16 | #define CPPUTIL_INCLUDE_PATTERNS_SINGLETON_H 17 | 18 | namespace cpputil { 19 | 20 | template 21 | class Singleton { 22 | public: 23 | typedef T& reference; 24 | 25 | Singleton() = delete; 26 | Singleton(const Singleton& s) = delete; 27 | Singleton& operator=(Singleton s) = delete; 28 | 29 | static reference get() { 30 | static T instance; 31 | return instance; 32 | } 33 | }; 34 | 35 | } // namespace cpputil 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /include/command_line/command_line.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_COMMAND_LINE_COMMAND_LINE_H 16 | #define CPPUTIL_INCLUDE_COMMAND_LINE_COMMAND_LINE_H 17 | 18 | #include "include/command_line/arg.h" 19 | #include "include/command_line/args.h" 20 | #include "include/command_line/command_line_config.h" 21 | #include "include/command_line/file_arg.h" 22 | #include "include/command_line/flag_arg.h" 23 | #include "include/command_line/folder_arg.h" 24 | #include "include/command_line/heading.h" 25 | #include "include/command_line/value_arg.h" 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /include/serialize/dec_reader.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SERIALIZE_DEC_READER_H 16 | #define CPPUTIL_INCLUDE_SERIALIZE_DEC_READER_H 17 | 18 | #include 19 | #include 20 | 21 | namespace cpputil { 22 | 23 | template 24 | struct DecReader; 25 | 26 | template 27 | struct DecReader ::value>::type> { 28 | void operator()(std::istream& is, T& t) const { 29 | is >> t; 30 | } 31 | }; 32 | 33 | } // namespace cpputil 34 | 35 | #endif 36 | 37 | 38 | -------------------------------------------------------------------------------- /examples/meta/indices.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/meta/indices.h" 19 | 20 | using namespace cpputil; 21 | using namespace std; 22 | 23 | template 24 | std::array make_array_helper(Indices) { 25 | return {Is...}; 26 | } 27 | 28 | template 29 | std::array make_array() { 30 | return make_array_helper(MakeIndices()); 31 | } 32 | 33 | int main() { 34 | for (auto i : make_array<10>()) { 35 | cout << i << " "; 36 | } 37 | cout << endl; 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /include/io/abort.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_ABORT_H 16 | #define CPPUTIL_INCLUDE_IO_ABORT_H 17 | 18 | #include 19 | #include 20 | 21 | namespace cpputil { 22 | 23 | class Abort { 24 | public: 25 | Abort() : code_(0) { } 26 | 27 | Abort& code(int c) { 28 | code_ = c; 29 | return *this; 30 | } 31 | 32 | void operator()(std::streambuf* sb, char c) { 33 | sb->sputc(c); 34 | if (c == '\n') { 35 | assert(false); 36 | exit(code_); 37 | } 38 | } 39 | 40 | private: 41 | int code_; 42 | }; 43 | 44 | } // namespace cpputil 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /examples/io/indent.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include "include/io/filterstream.h" 18 | #include "include/io/indent.h" 19 | 20 | using namespace cpputil; 21 | using namespace std; 22 | 23 | int main() { 24 | ofilterstream os(cout); 25 | os << "Hello, world!" << endl; 26 | os.filter().indent(); 27 | os << "Hello, world!"; 28 | os.filter().indent(); 29 | os << endl; 30 | os << "Hello, world!" << endl; 31 | 32 | for (size_t i = 0; i < 100; ++i) { 33 | os.filter().unindent(); 34 | } 35 | 36 | os << "Hello, world!" << endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /examples/container/tokenizer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/container/tokenizer.h" 19 | 20 | using namespace cpputil; 21 | using namespace std; 22 | 23 | int main() { 24 | Tokenizer t; 25 | t.tokenize("Hello"); 26 | t.tokenize("world"); 27 | 28 | for (int n = 0; n < 2; ++n) { 29 | cout << "Tokenized strings: (" << t.size() << ") [ "; 30 | for (const auto& itr : t) { 31 | cout << "(" << itr.first << " " << itr.second << ") "; 32 | } 33 | cout << "]" << endl; 34 | 35 | t.clear(); 36 | } 37 | 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /examples/debug/stl_print.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "include/debug/stl_print.h" 23 | 24 | using namespace cpputil; 25 | using namespace std; 26 | 27 | int main() { 28 | auto p = make_pair(1, 2.0); 29 | cout << p << endl; 30 | 31 | vector v { 1, 2, 3, 4, 5}; 32 | cout << v << endl; 33 | 34 | map m { {1, 'a'}, {2, 'b'} }; 35 | cout << m << endl; 36 | 37 | auto t = make_tuple(1, 2.0, "three", p, v, m); 38 | cout << t << endl; 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /include/io/shunt.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_SHUNT_H 16 | #define CPPUTIL_INCLUDE_IO_SHUNT_H 17 | 18 | #include 19 | 20 | namespace cpputil { 21 | 22 | class Shunt { 23 | public: 24 | Shunt() : 25 | open_(true) { } 26 | 27 | Shunt& open() { 28 | open_ = true; 29 | return *this; 30 | } 31 | 32 | Shunt& close() { 33 | open_ = false; 34 | return *this; 35 | } 36 | 37 | void operator()(std::streambuf* sb, char c) { 38 | if (open_) { 39 | sb->sputc(c); 40 | } 41 | } 42 | 43 | private: 44 | bool open_; 45 | }; 46 | 47 | } // namespace cpputil 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /include/io/redact.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_REDACT_H 16 | #define CPPUTIL_INCLUDE_IO_REDACT_H 17 | 18 | #include 19 | #include 20 | 21 | namespace cpputil { 22 | 23 | class Redact { 24 | public: 25 | Redact() : 26 | on_(false) { } 27 | 28 | Redact& on() { 29 | on_ = true; 30 | return *this; 31 | } 32 | 33 | Redact& off() { 34 | on_ = false; 35 | return *this; 36 | } 37 | 38 | void operator()(std::streambuf* sb, char c) { 39 | sb->sputc(on_ && isgraph(c) ? 'x' : c); 40 | } 41 | 42 | private: 43 | bool on_; 44 | }; 45 | 46 | } // namespace cpputil 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /include/serialize/dec_writer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SERIALIZE_DEC_WRITER_H 16 | #define CPPUTIL_INCLUDE_SERIALIZE_DEC_WRITER_H 17 | 18 | #include 19 | #include 20 | 21 | namespace cpputil { 22 | 23 | template 24 | struct DecWriter; 25 | 26 | template 27 | struct DecWriter ::value>::type> { 28 | void operator()(std::ostream& os, const T& t) const { 29 | const auto f = os.flags(std::ios::dec); 30 | os << t; 31 | os.setf(f); 32 | } 33 | }; 34 | 35 | } // namespace cpputil 36 | 37 | #endif 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /examples/math/online_stats.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include "include/math/online_stats.h" 18 | 19 | using namespace cpputil; 20 | using namespace std; 21 | 22 | int main() { 23 | OnlineStats os; 24 | for (size_t i = 0; i < 10; ++i) { 25 | os.push_back(i); 26 | } 27 | 28 | float mean = 0; 29 | float var = 0; 30 | for (size_t i = 0; i < 10; ++i) { 31 | mean += i; 32 | } 33 | mean /= 10; 34 | for (size_t i = 0; i < 10; ++i) { 35 | var += (i - mean) * (i - mean); 36 | } 37 | var /= 9; 38 | 39 | cout << "mean = " << os.mean() << " (should be " << mean << ")" << endl; 40 | cout << "sig2 = " << os.variance() << " (should be " << var << ")" << endl; 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /include/io/prefix.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_PREFIX_H 16 | #define CPPUTIL_INCLUDE_IO_PREFIX_H 17 | 18 | #include 19 | 20 | namespace cpputil { 21 | 22 | class Prefix { 23 | public: 24 | Prefix() : prefix_(""), pending_(true) { } 25 | 26 | Prefix& prefix(const std::string& p) { 27 | prefix_ = p; 28 | return *this; 29 | } 30 | 31 | void operator()(std::streambuf* sb, char c) { 32 | if (pending_) { 33 | for (auto p : prefix_) { 34 | sb->sputc(p); 35 | } 36 | pending_ = false; 37 | } 38 | if (c == '\n') { 39 | pending_ = true; 40 | } 41 | sb->sputc(c); 42 | } 43 | 44 | private: 45 | std::string prefix_; 46 | bool pending_; 47 | }; 48 | 49 | } // namespace cpputil 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /include/meta/contained_type.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_CONTAINED_TYPE_H 16 | #define CPPUTIL_INCLUDE_META_CONTAINED_TYPE_H 17 | 18 | #include 19 | 20 | #include "include/meta/is_stl_sequence.h" 21 | 22 | namespace cpputil { 23 | 24 | template 25 | struct contained_type { }; 26 | 27 | template 28 | struct contained_type < T, typename std::enable_if < !is_stl_sequence::value >::type { 29 | typedef T type; 30 | }; 31 | 32 | template 33 | struct contained_type::value>::type> { 34 | typedef typename contained_type::type type; 35 | }; 36 | 37 | } // namespace cpputil 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /examples/container/bijection.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/container/bijection.h" 19 | 20 | using namespace cpputil; 21 | using namespace std; 22 | 23 | int main() { 24 | Bijection b; 25 | 26 | b.insert(std::make_pair("Hello", 1)); 27 | b.insert(std::make_pair("World", 2)); 28 | 29 | cout << "[ "; 30 | for (const auto& p : b) { 31 | cout << "(" << p.first << " " << p.second << ") "; 32 | } 33 | cout << "]" << endl; 34 | 35 | const auto itr1 = b.domain_find("Hello"); 36 | cout << "(" << itr1->first << " " << itr1->second << ")" << endl; 37 | const auto itr2 = b.range_find(2); 38 | cout << "(" << itr2->first << " " << itr2->second << ")" << endl; 39 | 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /include/debug/stl_print.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_DEBUG_STL_PRINT_H 16 | #define CPPUTIL_INCLUDE_DEBUG_STL_PRINT_H 17 | 18 | #include 19 | #include 20 | 21 | #include "include/meta/is_stl_container.h" 22 | #include "include/serialize/text_reader.h" 23 | #include "include/serialize/text_writer.h" 24 | 25 | template 26 | typename std::enable_if::value, std::ostream&>::type 27 | operator<<(std::ostream& os, const T& t) { 28 | cpputil::TextWriter()(os, t); 29 | return os; 30 | } 31 | 32 | template 33 | typename std::enable_if::value, std::istream&>::type 34 | operator>>(std::istream& is, T& t) { 35 | cpputil::TextReader()(is, t); 36 | return is; 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Copyright 2013 eric schkufza 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | ##### CONSTANT DEFINITIONS 16 | 17 | DOC=doc/html 18 | 19 | ##### TOP LEVEL TARGETS 20 | 21 | all: $(DOC) 22 | 23 | ##### DOCUMENTATION TARGETS 24 | 25 | doc/html: src/doxyfile src/* src/mainpage.dox 26 | doxygen src/doxyfile 27 | 28 | src/mainpage.dox: README.txt 29 | echo "/**" > src/mainpage.dox &&\ 30 | echo \\mainpage >> src/mainpage.dox &&\ 31 | echo "\\\verbatim" >> src/mainpage.dox &&\ 32 | cat README.txt >> src/mainpage.dox &&\ 33 | echo \\endverbatim >> src/mainpage.dox &&\ 34 | echo "*/" >> src/mainpage.dox 35 | 36 | ##### ASTYLE TARGETS 37 | 38 | astyle: 39 | astyle -r include/*.h 40 | astyle -r examples/*.cc 41 | 42 | ##### CLEAN TARGETS 43 | 44 | clean: 45 | rm -rf $(DOC) src/mainpage.dox 46 | rm -f `ls -R examples/*/*.orig` 47 | rm -f `ls -R include/*/*.orig` 48 | make -C examples clean 49 | -------------------------------------------------------------------------------- /include/container/bit_array.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_CONTAINER_BIT_ARRAY_H 16 | #define CPPUTIL_INCLUDE_CONTAINER_BIT_ARRAY_H 17 | 18 | #include 19 | 20 | #include 21 | 22 | #include "include/container/bit_string.h" 23 | 24 | namespace cpputil { 25 | 26 | template 27 | class BitArray : public BitString < std::array < uint64_t, (N + 63) / 64 >> { 28 | public: 29 | /** Creates an empty bit array. */ 30 | BitArray() : BitString < std::array < uint64_t, (N + 63) / 64 >> () { 31 | this->num_bits_ = N; 32 | } 33 | 34 | /** Set all elements to zero. */ 35 | void unset() { 36 | this->contents_.fill(0); 37 | } 38 | 39 | /** Set all elements to one. */ 40 | void set() { 41 | this->contents_.fill(-1); 42 | } 43 | }; 44 | 45 | } // namespace cpputil 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /include/io/line_comment.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_LINE_COMMENT_H 16 | #define CPPUTIL_INCLUDE_IO_LINE_COMMENT_H 17 | 18 | namespace cpputil { 19 | 20 | template 21 | class LineComment { 22 | public: 23 | LineComment() : 24 | ignoring_(false) { } 25 | 26 | size_t operator()(char c, char* buffer) { 27 | switch (c) { 28 | case C: 29 | ignoring_ = true; 30 | return 0; 31 | 32 | case '\n': 33 | ignoring_ = false; 34 | buffer[0] = c; 35 | return 1; 36 | 37 | default: 38 | if (!ignoring_ || c == EOF) { 39 | buffer[0] = c; 40 | return 1; 41 | } else { 42 | return 0; 43 | } 44 | } 45 | } 46 | 47 | private: 48 | bool ignoring_; 49 | }; 50 | 51 | } // namespace cpputil 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /include/meta/is_8_bit.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_IS_8_BIT_H 16 | #define CPPUTIL_INCLUDE_META_IS_8_BIT_H 17 | 18 | #include 19 | #include 20 | 21 | namespace cpputil { 22 | 23 | template 24 | struct is_8_bit : public std::false_type { }; 25 | 26 | template <> 27 | struct is_8_bit : public std::true_type { }; 28 | 29 | template <> 30 | struct is_8_bit : public std::true_type { }; 31 | 32 | template <> 33 | struct is_8_bit : public std::true_type { }; 34 | 35 | template <> 36 | struct is_8_bit : public std::true_type { }; 37 | 38 | template <> 39 | struct is_8_bit : public std::true_type { }; 40 | 41 | template <> 42 | struct is_8_bit : public std::true_type { }; 43 | 44 | } // namespace cpputil 45 | 46 | #endif 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /examples/serialize/hex.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/serialize/hex_reader.h" 19 | #include "include/serialize/hex_writer.h" 20 | 21 | using namespace cpputil; 22 | using namespace std; 23 | 24 | template 25 | void check(T t) { 26 | T t2 = T(); 27 | 28 | stringstream ss; 29 | HexWriter()(ss, t); 30 | HexReader()(ss, t2); 31 | 32 | HexWriter()(cout, t); 33 | cout << endl; 34 | HexWriter()(cout, t2); 35 | cout << endl; 36 | } 37 | 38 | int main() { 39 | char c = 0x7b; 40 | char16_t c16 = 0x7bcd; 41 | char32_t c32 = 0x7bcdef01; 42 | uint64_t u64 = 0x7bcdef0123456789; 43 | float f = 1234.0; 44 | double d = -5678.0; 45 | 46 | check(c); 47 | check(c16); 48 | check(c32); 49 | check(u64); 50 | check(f); 51 | check(d); 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /examples/container/maputil.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/container/maputil.h" 19 | 20 | using namespace cpputil; 21 | using namespace std; 22 | 23 | int main() { 24 | CppUtilMap> m; 25 | for (int i = 0; i < 5; ++i) { 26 | m[i] = 'a' + i; 27 | } 28 | 29 | cout << "Pair iteration: [ "; 30 | for (const auto& i : m) { 31 | cout << "(" << i.first << "," << i.second << ") "; 32 | } 33 | cout << "]" << endl; 34 | 35 | cout << "Key iteration: [ "; 36 | for (auto i = m.key_begin(), ie = m.key_end(); i != ie; ++i) { 37 | cout << *i << " "; 38 | } 39 | cout << "]" << endl; 40 | 41 | cout << "Value iteration: [ "; 42 | for (auto i = m.value_begin(), ie = m.value_end(); i != ie; ++i) { 43 | cout << *i << " "; 44 | } 45 | cout << "]" << endl; 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /include/meta/is_16_bit.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_IS_16_BIT_H 16 | #define CPPUTIL_INCLUDE_META_IS_16_BIT_H 17 | 18 | #include 19 | #include 20 | 21 | namespace cpputil { 22 | 23 | template 24 | struct is_16_bit : public std::false_type { }; 25 | 26 | template <> 27 | struct is_16_bit : public std::true_type { }; 28 | 29 | template <> 30 | struct is_16_bit : public std::true_type { }; 31 | 32 | template <> 33 | struct is_16_bit : public std::true_type { }; 34 | 35 | template <> 36 | struct is_16_bit : public std::true_type { }; 37 | 38 | template <> 39 | struct is_16_bit : public std::true_type { }; 40 | 41 | template <> 42 | struct is_16_bit : public std::true_type { }; 43 | 44 | } // namespace cpputil 45 | 46 | #endif 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /include/meta/is_64_bit.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_IS_64_BIT_H 16 | #define CPPUTIL_INCLUDE_META_IS_64_BIT_H 17 | 18 | #include 19 | #include 20 | 21 | namespace cpputil { 22 | 23 | template 24 | struct is_64_bit : public std::false_type { }; 25 | 26 | template <> 27 | struct is_64_bit : public std::true_type { }; 28 | 29 | template <> 30 | struct is_64_bit : public std::true_type { }; 31 | 32 | template <> 33 | struct is_64_bit : public std::true_type { }; 34 | 35 | template <> 36 | struct is_64_bit : public std::true_type { }; 37 | 38 | template <> 39 | struct is_64_bit : public std::true_type { }; 40 | 41 | template <> 42 | struct is_64_bit : public std::true_type { }; 43 | 44 | } // namespace cpputil 45 | 46 | #endif 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /include/command_line/arg_group.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_COMMAND_LINE_ARG_GROUP_H 16 | #define CPPUTIL_INCLUDE_COMMAND_LINE_ARG_GROUP_H 17 | 18 | #include 19 | #include 20 | 21 | namespace cpputil { 22 | 23 | class Arg; 24 | 25 | class ArgGroup { 26 | friend class ArgRegistry; 27 | friend class Args; 28 | 29 | public: 30 | const std::string& heading() const { 31 | return heading_; 32 | } 33 | 34 | typedef std::vector::const_iterator arg_iterator; 35 | 36 | arg_iterator arg_begin() const { 37 | return args_.begin(); 38 | } 39 | 40 | arg_iterator arg_end() const { 41 | return args_.end(); 42 | } 43 | 44 | private: 45 | ArgGroup(const std::string& heading) : heading_ {heading} { } 46 | 47 | std::string heading_; 48 | std::vector args_; 49 | }; 50 | 51 | } // namespace cpputil 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /examples/io/fail.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include "include/io/fail.h" 18 | 19 | using namespace cpputil; 20 | using namespace std; 21 | 22 | int main() { 23 | fail(cout) << "cout is broken"; 24 | if (failed(cout)) { 25 | clog << "Should see this: " << fail_msg(cout) << endl; 26 | } 27 | if (failed(cerr)) { 28 | clog << "Shouldn't see this" << endl; 29 | } 30 | 31 | if (warned(cerr)) { 32 | clog << "Shouldn't see this" << endl; 33 | } 34 | warn(cerr) << "cerr is sort of okay"; 35 | if (warned(cout)) { 36 | clog << "Shouldn't see this" << endl; 37 | } 38 | if (warned(cerr)) { 39 | clog << "Should see this: " << warn_msg(cerr) << endl; 40 | } 41 | 42 | stringstream ss; 43 | fail(ss) << "An iostream is broken"; 44 | if (failed(ss)) { 45 | clog << "Should see this: " << fail_msg(ss) << endl; 46 | } 47 | 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /include/serialize/range_reader.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SERIALIZE_RANGE_READER_H 16 | #define CPPUTIL_INCLUDE_SERIALIZE_RANGE_READER_H 17 | 18 | #include 19 | #include 20 | 21 | #include "include/serialize/range.h" 22 | #include "include/serialize/text_reader.h" 23 | #include "include/serialize/text_style.h" 24 | 25 | namespace cpputil { 26 | 27 | template , typename Enable = void> 28 | struct RangeReader; 29 | 30 | template 31 | struct RangeReader::value>::type> { 32 | void operator()(std::istream& is, T& t) const { 33 | TextReader()(is, t); 34 | if (t < Range::lower() || t > Range::upper()) { 35 | is.setstate(std::ios::failbit); 36 | } 37 | } 38 | }; 39 | 40 | } // namespace cpputil 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /include/serialize/text_style.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SERIALIZE_TEXT_STYLE_H 16 | #define CPPUTIL_INCLUDE_SERIALIZE_TEXT_STYLE_H 17 | 18 | namespace cpputil { 19 | 20 | template 23 | struct TextStyle { 24 | static constexpr bool dec() { 25 | return Dec; 26 | } 27 | 28 | static constexpr size_t hex_group() { 29 | return HexGroup; 30 | } 31 | 32 | static constexpr char open() { 33 | return Open; 34 | } 35 | 36 | static constexpr char close() { 37 | return Close; 38 | } 39 | 40 | static constexpr char quote() { 41 | return Quote; 42 | } 43 | 44 | static constexpr char etc() { 45 | return Etc; 46 | } 47 | 48 | static constexpr char eol() { 49 | return Eol; 50 | } 51 | }; 52 | 53 | } // namespace cpputil 54 | 55 | #endif 56 | 57 | -------------------------------------------------------------------------------- /examples/memory/interner.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/memory/interner.h" 19 | 20 | using namespace cpputil; 21 | using namespace std; 22 | 23 | int main() { 24 | Interner i; 25 | 26 | string s1 = "Hello"; 27 | string s2 = "Hello"; 28 | string s3 = "world"; 29 | 30 | if (&i.intern(s1) == &i.intern(s2)) { 31 | cout << "These are the same string!" << endl; 32 | } else { 33 | cout << "Something is broken!" << endl; 34 | } 35 | 36 | if (&i.intern(s1) == &i.intern(s3)) { 37 | cout << "Something is broken!" << endl; 38 | } else { 39 | cout << "These are the same string!" << endl; 40 | } 41 | 42 | for (int n = 0; n < 2; ++n) { 43 | cout << "Interned strings: (" << i.size() << ") [ "; 44 | for (const auto& itr : i) { 45 | cout << itr << " "; 46 | } 47 | cout << "]" << endl; 48 | 49 | i.clear(); 50 | } 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /include/lazy/thunk.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include "include/meta/indices.h" 20 | 21 | namespace cpputil { 22 | 23 | template 24 | class Thunk { 25 | public: 26 | typedef typename std::result_of::type value_type; 27 | 28 | Thunk(Fxn&& fxn, Args&& ... args) : 29 | fxn_ {std::move(fxn)}, args_ {std::move(args)...} { } 30 | 31 | operator value_type() { 32 | return evaluate(MakeIndices()); 33 | } 34 | 35 | private: 36 | Fxn fxn_; 37 | std::tuple args_; 38 | 39 | template 40 | value_type evaluate(Indices) { 41 | return fxn_(std::get(args_)...); 42 | } 43 | }; 44 | 45 | template 46 | Thunk make_thunk(Fxn&& fxn, Args&& ... args) { 47 | return {fxn, std::forward(args)...}; 48 | } 49 | 50 | } // namespace cpputil 51 | -------------------------------------------------------------------------------- /include/command_line/heading.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_COMMAND_LINE_HEADING_H 16 | #define CPPUTIL_INCLUDE_COMMAND_LINE_HEADING_H 17 | 18 | #include 19 | 20 | #include "include/command_line/arg_registry.h" 21 | #include "include/patterns/singleton.h" 22 | 23 | namespace cpputil { 24 | 25 | class Heading { 26 | public: 27 | static Heading& create(const std::string& text) { 28 | return *(new Heading(text)); 29 | } 30 | 31 | private: 32 | /** Creating a singleton of this class will ensure Headings aren't leaked. */ 33 | struct HeadingCleanup { 34 | ~HeadingCleanup() { 35 | for (auto h : Singleton::get().headings_) { 36 | delete h; 37 | } 38 | } 39 | }; 40 | 41 | Heading(const std::string& text) { 42 | auto& ar = Singleton::get(); 43 | ar.insert(this, text); 44 | Singleton::get(); 45 | } 46 | }; 47 | 48 | } // namespace cpputil 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /include/command_line/arg_registry.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_COMMAND_LINE_ARG_REGISTRY_H 16 | #define CPPUTIL_INCLUDE_COMMAND_LINE_ARG_REGISTRY_H 17 | 18 | #include "include/command_line/arg_group.h" 19 | 20 | namespace cpputil { 21 | 22 | class Arg; 23 | class Heading; 24 | 25 | class ArgRegistry { 26 | friend class Arg; 27 | friend class Args; 28 | friend class Heading; 29 | 30 | private: 31 | typedef std::vector::const_iterator group_iterator; 32 | typedef std::vector::const_iterator arg_iterator; 33 | 34 | std::vector args_; 35 | std::vector headings_; 36 | std::vector groups_; 37 | 38 | void insert(Arg* arg) { 39 | groups_.back().args_.push_back(arg); 40 | args_.push_back(arg); 41 | } 42 | 43 | void insert(Heading* heading, const std::string& text) { 44 | headings_.push_back(heading); 45 | groups_.push_back(ArgGroup(text)); 46 | } 47 | }; 48 | 49 | } // namespace cpputil 50 | 51 | #endif 52 | 53 | -------------------------------------------------------------------------------- /include/serialize/hex_writer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SERIALIZE_HEX_WRITER_H 16 | #define CPPUTIL_INCLUDE_SERIALIZE_HEX_WRITER_H 17 | 18 | #include 19 | #include 20 | 21 | #include "include/meta/bit_width.h" 22 | 23 | namespace cpputil { 24 | 25 | template 26 | struct HexWriter; 27 | 28 | template 29 | struct HexWriter ::value>::type> { 30 | void operator()(std::ostream& os, const T& t) const { 31 | const auto f = os.flags(std::ios::hex); 32 | os.unsetf(std::ios::showbase); 33 | 34 | for (size_t i = bit_width::value / 4; i > 0; --i) { 35 | if (i < bit_width::value / 4 && i % Group == 0) { 36 | os << " "; 37 | } 38 | 39 | os << ((((uint8_t*) &t)[(i - 1) / 2] >> (i % 2 == 0 ? 4 : 0)) & 0x0f); 40 | } 41 | 42 | os.setf(f); 43 | } 44 | }; 45 | 46 | } // namespace cpputil 47 | 48 | #endif 49 | 50 | 51 | -------------------------------------------------------------------------------- /include/meta/is_32_bit.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_IS_32_BIT_H 16 | #define CPPUTIL_INCLUDE_META_IS_32_BIT_H 17 | 18 | #include 19 | #include 20 | 21 | namespace cpputil { 22 | 23 | template 24 | struct is_32_bit : public std::false_type { }; 25 | 26 | template <> 27 | struct is_32_bit : public std::true_type { }; 28 | 29 | template <> 30 | struct is_32_bit : public std::true_type { }; 31 | 32 | template <> 33 | struct is_32_bit : public std::true_type { }; 34 | 35 | template <> 36 | struct is_32_bit : public std::true_type { }; 37 | 38 | template <> 39 | struct is_32_bit : public std::true_type { }; 40 | 41 | template <> 42 | struct is_32_bit : public std::true_type { }; 43 | 44 | template <> 45 | struct is_32_bit : public std::true_type { }; 46 | 47 | template <> 48 | struct is_32_bit : public std::true_type { }; 49 | 50 | } // namespace cpputil 51 | 52 | #endif 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /include/math/online_stats.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_MATH_ONLINE_STATS_H 16 | #define CPPUTIL_INCLUDE_MATH_ONLINE_STATS_H 17 | 18 | #include 19 | 20 | namespace cpputil { 21 | 22 | /** Credit goes to: http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#On-line_algorithm */ 23 | template 24 | class OnlineStats; 25 | 26 | template 27 | class OnlineStats ::value>::type> { 28 | public: 29 | OnlineStats() : n_(0), mean_(0), m2_(0) { } 30 | 31 | void push_back(T t) { 32 | n_++; 33 | const auto delta = t - mean_; 34 | mean_ += delta / n_; 35 | m2_ += delta * (t - mean_); 36 | } 37 | 38 | size_t size() const { 39 | return n_; 40 | } 41 | 42 | T mean() const { 43 | return mean_; 44 | } 45 | 46 | T variance() const { 47 | return n_ < 2 ? 0 : m2_ / (n_ - 1); 48 | } 49 | 50 | private: 51 | size_t n_; 52 | T mean_; 53 | T m2_; 54 | }; 55 | 56 | } // namespace cpputil 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /include/system/terminal.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SYSTEM_TERMINAL_H 16 | #define CPPUTIL_INCLUDE_SYSTEM_TERMINAL_H 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "include/io/filterbuf.h" 25 | 26 | namespace cpputil { 27 | 28 | class Terminal : public std::ostream { 29 | private: 30 | struct Filter { 31 | Filter() : result_(-1) { } 32 | 33 | void operator()(std::streambuf* sb, char c) { 34 | if (c == '\n') { 35 | auto strbuf = (std::stringbuf*) sb; 36 | result_ = system(strbuf->str().c_str()); 37 | strbuf->str(std::string()); 38 | } else { 39 | sb->sputc(c); 40 | } 41 | } 42 | 43 | int result_; 44 | }; 45 | 46 | public: 47 | Terminal() : std::ostream(&buf_), buf_(&strbuf_) { } 48 | 49 | virtual ~Terminal() { } 50 | 51 | int result() { 52 | return buf_.filter().result_; 53 | } 54 | 55 | private: 56 | std::stringbuf strbuf_; 57 | ofilterbuf buf_; 58 | }; 59 | 60 | } // namespace cpputil 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /examples/io/filterstream.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/io/filterstream.h" 19 | 20 | using namespace cpputil; 21 | using namespace std; 22 | 23 | struct Nop { 24 | void operator()(streambuf* sb, char c) { 25 | sb->sputc(c); 26 | } 27 | }; 28 | 29 | template 30 | struct Skip { 31 | void operator()(streambuf* sb, char c) { 32 | if (c != C) { 33 | sb->sputc(c); 34 | } 35 | } 36 | }; 37 | 38 | template 39 | struct Double { 40 | size_t operator()(char c, char* buffer) { 41 | auto count = c != C ? 1 : 2; 42 | for (size_t i = 0; i < count; ++i) { 43 | buffer[i] = c; 44 | } 45 | return count; 46 | } 47 | }; 48 | 49 | int main() { 50 | ofilterstream fs(cout); 51 | ofilterstream> fs2(fs); 52 | ofilterstream> fs3(fs2); 53 | fs3 << "Hello world!" << endl; 54 | 55 | stringstream ss; 56 | ss << "1 2 3"; 57 | 58 | ifilterstream> fs4(ss); 59 | int x, y, z; 60 | fs4 >> x >> y >> z; 61 | cout << x << " " << y << " " << z << endl; 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /include/io/indent.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_INDENT_H 16 | #define CPPUTIL_INCLUDE_IO_INDENT_H 17 | 18 | #include 19 | 20 | namespace cpputil { 21 | 22 | class Indent { 23 | public: 24 | Indent() : 25 | indent_(0), width_(2), pending_(true) { } 26 | 27 | Indent& indent(size_t count = 1) { 28 | indent_ += count; 29 | return *this; 30 | } 31 | 32 | Indent& unindent(size_t count = 1) { 33 | if (count > indent_) { 34 | indent_ = 0; 35 | } else { 36 | indent_ -= count; 37 | } 38 | return *this; 39 | } 40 | 41 | Indent& width(size_t width) { 42 | width_ = width; 43 | return *this; 44 | } 45 | 46 | void operator()(std::streambuf* sb, char c) { 47 | if (pending_) { 48 | for (size_t i = 0, ie = indent_ * width_; i < ie; ++i) { 49 | sb->sputc(' '); 50 | } 51 | pending_ = false; 52 | } 53 | 54 | sb->sputc(c); 55 | if (c == '\n' || c == '\r') { 56 | pending_ = true; 57 | } 58 | } 59 | 60 | private: 61 | size_t indent_; 62 | size_t width_; 63 | bool pending_; 64 | }; 65 | 66 | } // namespace cpputil 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /include/allocator/aligned.h: -------------------------------------------------------------------------------- 1 | #ifndef CPPUTIL_INCLUDE_ALLOCATOR_ALIGNED_H 2 | #define CPPUTIL_INCLUDE_ALLOCATOR_ALIGNED_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | template 11 | class Aligned { 12 | public: 13 | typedef T value_type; 14 | typedef size_t size_type; 15 | typedef ptrdiff_t difference_type; 16 | 17 | typedef T* pointer; 18 | typedef const T* const_pointer; 19 | 20 | typedef T& reference; 21 | typedef const T& const_reference; 22 | 23 | template 24 | struct rebind { 25 | typedef Aligned other; 26 | }; 27 | 28 | Aligned() { } 29 | ~Aligned() { } 30 | Aligned(const Aligned& rhs) { } 31 | template 32 | Aligned(const Aligned& rhs) { } 33 | 34 | pointer address (reference r) { 35 | return &r; 36 | } 37 | 38 | const_pointer address (const_reference r) const { 39 | return &r; 40 | } 41 | 42 | pointer allocate(size_type n, typename std::allocator::const_pointer = 0) { 43 | void* p = nullptr; 44 | 45 | const auto res = posix_memalign(&p, N, n*sizeof(value_type)); 46 | assert(res == 0); 47 | 48 | return (pointer)p; 49 | } 50 | 51 | void deallocate(pointer p, size_type) { 52 | free(p); 53 | } 54 | 55 | size_type max_size() const { 56 | return std::numeric_limits::max() / sizeof(T); 57 | } 58 | 59 | void construct(pointer p, const value_type& t) { 60 | new(p) value_type(t); 61 | } 62 | void destroy(pointer p) { 63 | p->~value_type (); 64 | } 65 | 66 | bool operator==(const Aligned& rhs) const { 67 | return true; 68 | } 69 | bool operator!=(const Aligned& rhs) const { 70 | return !(*this == rhs); 71 | } 72 | }; 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /include/io/console.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_CONSOLE_H 16 | #define CPPUTIL_INCLUDE_IO_CONSOLE_H 17 | 18 | #include 19 | 20 | #include "include/io/abort.h" 21 | #include "include/io/filterstream.h" 22 | #include "include/io/prefix.h" 23 | 24 | namespace cpputil { 25 | 26 | class Console { 27 | public: 28 | typedef ofilterstream message_type; 29 | typedef ofilterstream error_type; 30 | 31 | /** Messages are routed through cout. */ 32 | static message_type& msg() { 33 | static message_type m(std::cout); 34 | return m; 35 | } 36 | /** Warnings are assigned a prefix and routed through cerr. */ 37 | static message_type& warn() { 38 | static message_type w(std::cerr); 39 | w.filter().prefix("WARNING: "); 40 | return w; 41 | } 42 | /** Errors are assigned a prefix, routed through cerr, and invoke shutdown. */ 43 | static error_type& error(int code = 1) { 44 | static message_type m(std::cerr); 45 | m.filter().prefix("FATAL ERROR: "); 46 | 47 | static error_type e(m); 48 | e.filter().code(code); 49 | 50 | return e; 51 | } 52 | }; 53 | 54 | } // namespace cpputil 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /include/io/multistream.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_MULTISTREAM_H 16 | #define CPPUTIL_INCLUDE_IO_MULTISTREAM_H 17 | 18 | #include 19 | #include 20 | 21 | #include "include/io/multibuf.h" 22 | 23 | namespace cpputil { 24 | 25 | template 26 | class basic_omultistream : public std::basic_ostream { 27 | public: 28 | explicit basic_omultistream() 29 | : std::basic_ostream(&buf_), buf_() { } 30 | 31 | explicit basic_omultistream(std::basic_ostream& os) 32 | : std::basic_ostream(&buf_), buf_(os.rdbuf()) { } 33 | 34 | explicit basic_omultistream(std::basic_streambuf& sb) 35 | : std::basic_ostream(&buf_), buf_(sb) { } 36 | 37 | virtual ~basic_omultistream() { } 38 | 39 | void insert(std::basic_ostream& os) { 40 | buf_.insert(os.rdbuf()); 41 | } 42 | 43 | void clear() { 44 | buf_.clear(); 45 | } 46 | 47 | private: 48 | basic_multibuf buf_; 49 | }; 50 | 51 | typedef basic_omultistream> omultistream; 52 | typedef basic_omultistream> womultistream; 53 | 54 | } // namespace cpputil 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /include/meta/is_stl_adaptor.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_IS_STL_ADAPTOR_H 16 | #define CPPUTIL_INCLUDE_META_IS_STL_ADAPTOR_H 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | namespace cpputil { 23 | 24 | template 25 | struct is_stl_adaptor : public std::false_type { }; 26 | 27 | template 28 | struct is_stl_adaptor> : public std::true_type { }; 29 | 30 | template 31 | struct is_stl_adaptor> : public std::true_type { }; 32 | 33 | template 34 | struct is_stl_adaptor> : public std::true_type { }; 35 | 36 | template 37 | struct is_stl_adaptor> : public std::true_type { }; 38 | 39 | template 40 | struct is_stl_adaptor> : public std::true_type { }; 41 | 42 | template 43 | struct is_stl_adaptor> : public std::true_type { }; 44 | 45 | } // namespace cpputil 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /examples/io/column.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/io/filterstream.h" 19 | #include "include/io/column.h" 20 | 21 | using namespace cpputil; 22 | using namespace std; 23 | 24 | struct Double { 25 | string x; 26 | string y; 27 | }; 28 | 29 | struct Triple { 30 | int x; 31 | int y; 32 | int z; 33 | }; 34 | 35 | struct Sext { 36 | Triple t1; 37 | Triple t2; 38 | }; 39 | 40 | ostream& operator<<(ostream& os, const Double& d) { 41 | os << d.x << endl; 42 | os << d.y; 43 | return os; 44 | } 45 | 46 | ostream& operator<<(ostream& os, const Triple& t) { 47 | os << t.x << endl; 48 | os << t.y << endl; 49 | os << t.z; 50 | return os; 51 | } 52 | 53 | ostream& operator<<(ostream& os, const Sext& s) { 54 | os << s.t1 << endl; 55 | os << s.t2; 56 | return os; 57 | } 58 | 59 | int main() { 60 | ofilterstream os(cout); 61 | os.filter().padding(3); 62 | 63 | Double d {"Hello", "World!!!"}; 64 | Triple t {1, 2, 3}; 65 | Sext s {{1, 2, 3}, {4, 5, 6}}; 66 | 67 | os << "Col 1" << endl << d; 68 | os.filter().next(); 69 | os << "Col 2" << endl << t; 70 | os.filter().next(); 71 | os << "Col 3" << endl << s; 72 | os.filter().done(); 73 | 74 | cout << endl; 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /examples/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright 2013 eric schkufza 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | ##### CONSTANT DEFINITIONS 16 | 17 | GCC = ccache g++ -std=c++11 -mavx -mavx2 -mbmi -mbmi2 -mpopcnt 18 | OPT = -Werror -Wextra -pedantic -O3 19 | INC = -I../ 20 | LIB = 21 | EX = command_line/command_line \ 22 | container/bijection \ 23 | container/bit_array \ 24 | container/bit_vector \ 25 | container/maputil \ 26 | container/tokenizer \ 27 | debug/stl_print \ 28 | io/abort \ 29 | io/column \ 30 | io/console \ 31 | io/fail \ 32 | io/filterstream \ 33 | io/indent \ 34 | io/line_comment \ 35 | io/multistream \ 36 | io/prefix \ 37 | io/redact \ 38 | io/redirectstream \ 39 | io/shunt \ 40 | io/nopstream \ 41 | io/wrap \ 42 | lazy/thunk \ 43 | math/online_stats \ 44 | memory/interner \ 45 | meta/indices \ 46 | patterns/singleton \ 47 | serialize/hex \ 48 | serialize/line \ 49 | serialize/text \ 50 | signal/debug_handler \ 51 | system/terminal 52 | 53 | ##### TOP LEVEL TARGETS 54 | 55 | all: $(EX) 56 | 57 | ##### BUILD TARGETS 58 | 59 | %: %.cc 60 | $(GCC) $(OPT) $< -o $@ $(INC) $(LIB) 61 | 62 | ##### CLEAN TARGETS 63 | 64 | clean: 65 | rm -f $(EX) 66 | rm -rf command_line/*.dSYM 67 | rm -rf container/*.dSYM 68 | rm -rf io/*.dSYM 69 | rm -rf memory/*.dSYM 70 | rm -rf patterns/*.dSYM 71 | rm -rf serialize/*.dSYM 72 | rm -rf signal/*.dSYM 73 | -------------------------------------------------------------------------------- /include/io/wrap.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_WRAP_H 16 | #define CPPUTIL_INCLUDE_IO_WRAP_H 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | namespace cpputil { 23 | 24 | class Wrap { 25 | public: 26 | Wrap() : 27 | limit_(80), current_(0) { } 28 | 29 | Wrap& limit(size_t p) { 30 | limit_ = p; 31 | return *this; 32 | } 33 | 34 | void operator()(std::streambuf* sb, char c) { 35 | if (isgraph(c)) { 36 | word_ += c; 37 | } else { 38 | const auto next = current_ + word_.length(); 39 | const auto space = isblank(c); 40 | 41 | if (next < limit_) { 42 | write(sb, word_); 43 | sb->sputc(c); 44 | current_ = space ? current_ + word_.length() + 1 : 0; 45 | } else if (next == limit_) { 46 | write(sb, word_); 47 | sb->sputc('\n'); 48 | current_ = 0; 49 | } else { 50 | sb->sputc('\n'); 51 | write(sb, word_); 52 | sb->sputc(c); 53 | current_ = word_.length() + (space ? 1 : 0); 54 | } 55 | word_ = ""; 56 | } 57 | } 58 | 59 | private: 60 | size_t limit_; 61 | size_t current_; 62 | std::string word_; 63 | 64 | void write(std::streambuf* sb, const std::string& word) { 65 | for (auto l : word) { 66 | sb->sputc(l); 67 | } 68 | } 69 | }; 70 | 71 | } // namespace cpputil 72 | 73 | #endif 74 | 75 | -------------------------------------------------------------------------------- /include/serialize/hex_reader.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SERIALIZE_HEX_READER_H 16 | #define CPPUTIL_INCLUDE_SERIALIZE_HEX_READER_H 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "include/meta/bit_width.h" 24 | 25 | namespace cpputil { 26 | 27 | #define die_if(cond) \ 28 | if (cond) { \ 29 | is.setstate(std::ios::failbit); \ 30 | return; \ 31 | } 32 | 33 | template 34 | struct HexReader; 35 | 36 | template 37 | struct HexReader ::value>::type> { 38 | void operator()(std::istream& is, T& t) const { 39 | std::array < uint8_t, bit_width::value / 8 > buffer; 40 | buffer.fill(0); 41 | 42 | for (size_t i = bit_width::value / 4; i > 0; --i) { 43 | if (i < bit_width::value / 4 && i % Group == 0) { 44 | die_if(is.get() != ' '); 45 | } 46 | const auto c = is.get(); 47 | die_if(!isxdigit(c)); 48 | 49 | const uint8_t temp = (c >= '0' && c <= '9') ? (c - '0') : ( 50 | (c >= 'A' && c <= 'F') ? (c - 'A' + 10) : (c - 'a' + 10) 51 | ); 52 | buffer[(i - 1) / 2] |= (temp << (i % 2 == 0 ? 4 : 0)); 53 | } 54 | 55 | t = *((T*) buffer.data()); 56 | } 57 | }; 58 | 59 | #undef die_unless 60 | 61 | } // namespace cpputil 62 | 63 | #endif 64 | 65 | -------------------------------------------------------------------------------- /include/memory/interner.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_MEMORY_INTERNER_H 16 | #define CPPUTIL_INCLUDE_MEMORY_INTERNER_H 17 | 18 | #include 19 | #include 20 | 21 | namespace cpputil { 22 | 23 | template > 24 | class Interner { 25 | public: 26 | typedef T value_type; 27 | typedef const T& const_reference; 28 | typedef typename Set::size_type size_type; 29 | typedef typename Set::const_iterator const_iterator; 30 | 31 | const_reference intern(const_reference t) { 32 | const auto res = vals_.insert(t); 33 | return *(res.first); 34 | } 35 | 36 | const_iterator begin() const { 37 | return vals_.begin(); 38 | } 39 | 40 | const_iterator cbegin() const { 41 | return begin(); 42 | } 43 | 44 | const_iterator end() const { 45 | return vals_.end(); 46 | } 47 | 48 | const_iterator cend() const { 49 | return end(); 50 | } 51 | 52 | bool empty() const { 53 | return vals_.empty(); 54 | } 55 | 56 | size_type size() const { 57 | return vals_.size(); 58 | } 59 | 60 | void clear() { 61 | vals_.clear(); 62 | } 63 | 64 | void swap(Interner& rhs) { 65 | vals_.swap(rhs.vals_); 66 | } 67 | 68 | private: 69 | Set vals_; 70 | }; 71 | 72 | template 73 | void swap(Interner& i1, Interner& i2) { 74 | i1.swap(i2); 75 | } 76 | 77 | } // namespace cpputil 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /examples/io/wrap.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include "include/io/filterstream.h" 18 | #include "include/io/column.h" 19 | #include "include/io/wrap.h" 20 | 21 | using namespace cpputil; 22 | using namespace std; 23 | 24 | const char* s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " 25 | "sed do eiusmod tempor incididunt ut labore et dolore magna " 26 | "aliqua. Ut enim ad minim veniam, quis nostrud exercitation " 27 | "ullamco laboris nisi ut aliquip ex ea commodo consequat. " 28 | "Duis aute irure dolor in reprehenderit in voluptate velit " 29 | "esse cillum dolore eu fugiat nulla pariatur. Excepteur sint " 30 | "occaecat cupidatat non proident, sunt in culpa qui officia " 31 | "deserunt mollit anim id est laborum."; 32 | 33 | int main() { 34 | ofilterstream col(cout); 35 | col.filter().padding(5); 36 | 37 | ofilterstream wrap(col); 38 | 39 | wrap.filter().limit(20); 40 | wrap << "Width = 20" << endl; 41 | wrap << endl; 42 | wrap << s << endl; 43 | col.filter().next(); 44 | 45 | wrap.filter().limit(40); 46 | wrap << "Width = 40" << endl; 47 | wrap << endl; 48 | wrap << s << endl; 49 | col.filter().next(); 50 | 51 | wrap.filter().limit(80); 52 | wrap << "Width = 80" << endl; 53 | wrap << endl; 54 | wrap << s << endl; 55 | col.filter().done(); 56 | 57 | return 0; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /include/meta/is_stl_set.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_META_IS_STL_SET_H 16 | #define CPPUTIL_META_IS_STL_SET_H 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | namespace cpputil { 23 | 24 | template 25 | struct is_stl_set : public std::false_type { }; 26 | 27 | template 28 | struct is_stl_set> : public std::true_type { }; 29 | 30 | template 31 | struct is_stl_set> : public std::true_type { }; 32 | 33 | template 34 | struct is_stl_set> : public std::true_type { }; 35 | 36 | template 37 | struct is_stl_set> : public std::true_type { }; 38 | 39 | template 40 | struct is_stl_set> : public std::true_type { }; 41 | 42 | template 43 | struct is_stl_set> : public std::true_type { }; 44 | 45 | template 46 | struct is_stl_set> : public std::true_type { }; 47 | 48 | template 49 | struct is_stl_set> : public std::true_type { }; 50 | 51 | } // namespace cpputil 52 | 53 | #endif 54 | 55 | -------------------------------------------------------------------------------- /examples/container/bit_vector.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/container/bit_vector.h" 19 | 20 | using namespace cpputil; 21 | using namespace std; 22 | 23 | int main() { 24 | BitVector b1(63 * 8); 25 | BitVector b2(63 * 8); 26 | 27 | cout << "Alignment on 32-bit boundary: " << ((uint64_t)(&b1.get_fixed_byte(0)) % 32) << endl; 28 | 29 | for (auto i = 0; i < 63; ++i) { 30 | b1.get_fixed_byte(i) = 0xff; 31 | } 32 | 33 | for (auto i = b1.fixed_byte_begin(), ie = b1.fixed_byte_end(); i != ie; ++i) { 34 | cout << hex << setw(2) << setfill(' ') << (int) *i << " "; 35 | } 36 | cout << endl; 37 | for (auto i = b2.fixed_byte_begin(), ie = b2.fixed_byte_end(); i != ie; ++i) { 38 | cout << hex << setw(2) << setfill(' ') << (int) *i << " "; 39 | } 40 | cout << endl; 41 | 42 | b2 |= b1; 43 | 44 | for (auto i = b1.fixed_byte_begin(), ie = b1.fixed_byte_end(); i != ie; ++i) { 45 | cout << hex << setw(2) << setfill(' ') << (int) *i << " "; 46 | } 47 | cout << endl; 48 | for (auto i = b2.fixed_byte_begin(), ie = b2.fixed_byte_end(); i != ie; ++i) { 49 | cout << hex << setw(2) << setfill(' ') << (int) *i << " "; 50 | } 51 | cout << endl; 52 | 53 | auto b3 = ~b2; 54 | for (auto i = b3.fixed_byte_begin(), ie = b3.fixed_byte_end(); i != ie; ++i) { 55 | cout << hex << setw(2) << setfill(' ') << (int) *i << " "; 56 | } 57 | cout << endl; 58 | 59 | b3.copy(b2); 60 | for (auto i = b3.fixed_byte_begin(), ie = b3.fixed_byte_end(); i != ie; ++i) { 61 | cout << hex << setw(2) << setfill(' ') << (int) *i << " "; 62 | } 63 | cout << endl; 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /include/meta/is_stl_sequence.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_IS_STL_SEQUENCE_H 16 | #define CPPUTIL_INCLUDE_META_IS_STL_SEQUENCE_H 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | namespace cpputil { 26 | 27 | template 28 | struct is_stl_sequence : public std::false_type { }; 29 | 30 | template 31 | struct is_stl_sequence> : public std::true_type { }; 32 | 33 | template 34 | struct is_stl_sequence> : public std::true_type { }; 35 | 36 | template 37 | struct is_stl_sequence> : public std::true_type { }; 38 | 39 | template 40 | struct is_stl_sequence> : public std::true_type { }; 41 | 42 | template 43 | struct is_stl_sequence> : public std::true_type { }; 44 | 45 | template 46 | struct is_stl_sequence> : public std::true_type { }; 47 | 48 | template 49 | struct is_stl_sequence> : public std::true_type { }; 50 | 51 | template 52 | struct is_stl_sequence> : public std::true_type { }; 53 | 54 | template 55 | struct is_stl_sequence> : public std::true_type { }; 56 | 57 | template 58 | struct is_stl_sequence> : public std::true_type { }; 59 | 60 | } // namespace cpputil 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /include/meta/is_stl_map.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_IS_STL_MAP_H 16 | #define CPPUTIL_INCLUDE_META_IS_STL_MAP_H 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | namespace cpputil { 23 | 24 | template 25 | struct is_stl_map : public std::false_type { }; 26 | 27 | template 28 | struct is_stl_map> : public std::true_type { }; 29 | 30 | template 31 | struct is_stl_map> : public std::true_type { }; 32 | 33 | template 34 | struct is_stl_map> : public std::true_type { }; 35 | 36 | template 37 | struct is_stl_map> : public std::true_type { }; 38 | 39 | template 40 | struct is_stl_map> : public std::true_type { }; 41 | 42 | template 43 | struct is_stl_map> : public std::true_type { }; 44 | 45 | template 46 | struct is_stl_map> : public std::true_type { }; 47 | 48 | template 49 | struct is_stl_map> : public 50 | std::true_type { }; 51 | 52 | } // namespace cpputil 53 | 54 | #endif 55 | 56 | -------------------------------------------------------------------------------- /include/command_line/flag_arg.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_COMMAND_LINE_FLAG_ARG_H 16 | #define CPPUTIL_INCLUDE_COMMAND_LINE_FLAG_ARG_H 17 | 18 | #include 19 | 20 | #include "include/command_line/arg.h" 21 | 22 | namespace cpputil { 23 | 24 | class FlagArg : public Arg { 25 | public: 26 | virtual ~FlagArg() = default; 27 | 28 | /** Creates and registers a new flag */ 29 | static FlagArg& create(const std::string& opt) { 30 | return *(new FlagArg(opt)); 31 | } 32 | 33 | /** FlagArgs don't consume anything */ 34 | virtual std::pair read(int argc, char** argv) { 35 | val_ = !get_appearances(argc, argv).empty(); 36 | return std::make_pair(0, 0); 37 | } 38 | 39 | /** Create a new arg alias (dashes implicit; chars get 1, strings 2) */ 40 | FlagArg& alternate(const std::string& a) { 41 | Arg::alternate(a); 42 | return *this; 43 | } 44 | 45 | /** Reset arg usage */ 46 | FlagArg& usage(const std::string& u) { 47 | Arg::usage(u); 48 | return *this; 49 | } 50 | 51 | /** Reset arg description */ 52 | FlagArg& description(const std::string& d) { 53 | Arg::description(d); 54 | return *this; 55 | } 56 | 57 | /** Implicit conversion to bool */ 58 | operator bool& () { 59 | return val_; 60 | } 61 | 62 | /** Explicit conversion to bool */ 63 | bool& value() { 64 | return val_; 65 | } 66 | 67 | /** Prints true or false */ 68 | virtual void debug(std::ostream& os) const { 69 | os << (val_ ? "true" : "false"); 70 | } 71 | 72 | private: 73 | /** Did this arg appear on the command line? */ 74 | bool val_; 75 | 76 | /** Flag args are false by default */ 77 | FlagArg(const std::string& opt) : Arg {opt}, val_ {false} { } 78 | }; 79 | 80 | } // namespace cpputil 81 | 82 | #endif 83 | 84 | -------------------------------------------------------------------------------- /include/io/multibuf.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_MULTIBUF_H 16 | #define CPPUTIL_INCLUDE_IO_MULTIBUF_H 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | namespace cpputil { 23 | 24 | template 25 | class basic_multibuf : public std::basic_streambuf { 26 | public: 27 | typedef typename std::basic_streambuf::char_type char_type; 28 | typedef typename std::basic_streambuf::int_type int_type; 29 | typedef typename std::basic_streambuf::off_type off_type; 30 | typedef typename std::basic_streambuf::pos_type pos_type; 31 | typedef typename std::basic_streambuf::traits_type traits_type; 32 | 33 | explicit basic_multibuf() { } 34 | 35 | explicit basic_multibuf(std::basic_streambuf* buf) { 36 | insert(buf); 37 | } 38 | 39 | virtual ~basic_multibuf() { } 40 | 41 | void insert(std::basic_streambuf* buf) { 42 | bufs_.insert(buf); 43 | } 44 | 45 | void clear() { 46 | bufs_.clear(); 47 | } 48 | 49 | protected: 50 | virtual int_type sync() { 51 | for (auto buf : bufs_) 52 | if (buf->pubsync() != 0) { 53 | return -1; 54 | } 55 | 56 | return 0; 57 | } 58 | 59 | virtual int_type overflow(int_type c = traits_type::eof()) { 60 | const auto ch = traits_type::to_char_type(c); 61 | for (auto buf : bufs_) 62 | if (traits_type::eq_int_type(buf->sputc(ch), traits_type::eof())) { 63 | return traits_type::eof(); 64 | } 65 | 66 | return c; 67 | } 68 | 69 | private: 70 | std::unordered_set*> bufs_; 71 | }; 72 | 73 | typedef basic_multibuf> multibuf; 74 | typedef basic_multibuf> wmultibuf; 75 | 76 | } // namespace cpputil 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /include/bits/bit_manip.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_BITS_BIT_MANIP_H 16 | #define CPPUTIL_INCLUDE_BITS_BIT_MANIP_H 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | namespace cpputil { 23 | 24 | template 25 | class BitManip; 26 | 27 | template <> 28 | class BitManip { 29 | public: 30 | static size_t ntz(uint64_t x) { 31 | #ifdef __BMI__ 32 | return __builtin_ctzll(x); 33 | #else 34 | // See https://graphics.stanford.edu/~seander/bithacks.html 35 | uint64_t res = 64; 36 | x &= -((int64_t)x); 37 | if (x) res--; 38 | if (x & 0x00000000ffffffff) res -= 32; 39 | if (x & 0x0000ffff0000ffff) res -= 16; 40 | if (x & 0x00ff00ff00ff00ff) res -= 8; 41 | if (x & 0x0f0f0f0f0f0f0f0f) res -= 4; 42 | if (x & 0x3333333333333333) res -= 2; 43 | if (x & 0x5555555555555555) res -= 1; 44 | return res; 45 | #endif 46 | } 47 | 48 | static size_t pop_count(uint64_t x) { 49 | #ifdef __POPCNT__ 50 | return __builtin_popcountll(x); 51 | #else 52 | // See https://graphics.stanford.edu/~seander/bithacks.html 53 | uint64_t res = x - ((x >> 1) & 0x5555555555555555); 54 | res = ((res >> 2) & 0x3333333333333333) + (res & 0x3333333333333333); 55 | res = ((res >> 4) + res) & 0x0f0f0f0f0f0f0f0f; 56 | res = ((res >> 8) + res) & 0x00ff00ff00ff00ff; 57 | res = ((res >> 16) + res) & 0x0000ffff0000ffff; 58 | res = ((res >> 32) + res) & 0x00000000ffffffff; 59 | return res; 60 | #endif 61 | } 62 | 63 | static uint64_t& unset_rightmost(uint64_t& x) { 64 | #ifdef __BMI__ 65 | return (x = _blsr_u64(x)); 66 | #else 67 | return (x &= (x - 1)); 68 | #endif 69 | } 70 | 71 | static uint64_t& unset_rightmost(uint64_t& x, size_t n) { 72 | assert(n <= 64); 73 | if (n == 64) { 74 | return (x = 0); 75 | } else { 76 | return (x &= ~((0x1ul << n) - 1)); 77 | } 78 | } 79 | }; 80 | 81 | } // namespace cpputil 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /examples/io/nopstream.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include "include/io/nopstream.h" 18 | 19 | using namespace cpputil; 20 | using namespace std; 21 | 22 | int foo() { 23 | int x = 0; 24 | for (; x < 1e9; ++x); 25 | return x; 26 | } 27 | 28 | struct C { 29 | int x_; 30 | int y_; 31 | int z_; 32 | }; 33 | 34 | istream& operator>>(istream& is, C& c) { 35 | is >> c.x_ >> c.y_ >> c.z_; 36 | return is; 37 | } 38 | 39 | ostream& operator<<(ostream& os, const C& c) { 40 | os << c.x_ << c.y_ << c.z_ << endl; 41 | return os; 42 | } 43 | 44 | int main() { 45 | bool b; 46 | short s; 47 | unsigned short us; 48 | int i; 49 | unsigned int ui; 50 | long l; 51 | unsigned long ul; 52 | float f; 53 | double d; 54 | long double ld; 55 | void* v; 56 | char c; 57 | signed char sc; 58 | unsigned char uc; 59 | char* ch; 60 | signed char* scp; 61 | unsigned char* ucp; 62 | C udt; 63 | 64 | inopstream ins(cin); 65 | ins >> b; 66 | ins >> s; 67 | ins >> us; 68 | ins >> i; 69 | ins >> ui; 70 | ins >> l; 71 | ins >> ul; 72 | ins >> f; 73 | ins >> d; 74 | ins >> ld; 75 | ins >> v; 76 | ins >> cin.rdbuf(); 77 | ins >> cin; 78 | ins >> c; 79 | ins >> sc; 80 | ins >> uc; 81 | ins >> ch; 82 | ins >> scp; 83 | ins >> ucp; 84 | ins >> udt; 85 | 86 | onopstream ons(cout); 87 | ons << b; 88 | ons << s; 89 | ons << us; 90 | ons << i; 91 | ons << ui; 92 | ons << l; 93 | ons << ul; 94 | ons << f; 95 | ons << d; 96 | ons << ld; 97 | ons << v; 98 | ons << cout.rdbuf(); 99 | ons << cout; 100 | ons << c; 101 | ons << sc; 102 | ons << uc; 103 | ons << ch; 104 | ons << scp; 105 | ons << ucp; 106 | ons << udt; 107 | 108 | ons << foo(); 109 | 110 | cout << "If you didn't type anything or see anything, this works!" << endl; 111 | 112 | return 0; 113 | } 114 | 115 | -------------------------------------------------------------------------------- /examples/serialize/text.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "include/debug/stl_print.h" 24 | #include "include/serialize/text_reader.h" 25 | #include "include/serialize/text_writer.h" 26 | 27 | using namespace cpputil; 28 | using namespace std; 29 | 30 | struct C { 31 | int x; 32 | char c; 33 | double d; 34 | string s; 35 | }; 36 | 37 | namespace cpputil { 38 | 39 | template 40 | struct TextWriter { 41 | void operator()(ostream& os, const C& c) const { 42 | os << Style::open(); 43 | TextWriter()(os, c.x); 44 | os << " "; 45 | TextWriter()(os, c.c); 46 | os << " "; 47 | TextWriter()(os, c.d); 48 | os << " "; 49 | TextWriter()(os, c.s); 50 | os << Style::close(); 51 | } 52 | }; 53 | 54 | template 55 | struct TextReader { 56 | void operator()(istream& is, C& c) const { 57 | is.get(); 58 | TextReader()(is, c.x); 59 | is.get(); 60 | TextReader()(is, c.c); 61 | is.get(); 62 | TextReader()(is, c.d); 63 | is.get(); 64 | TextReader()(is, c.s); 65 | is.get(); 66 | } 67 | }; 68 | 69 | } // namespace cpputil 70 | 71 | int main() { 72 | C c {1, 'c', 2.0, "Hello"}; 73 | 74 | typedef vector VI; 75 | VI vi {1, 2}; 76 | 77 | typedef vector VC; 78 | VC vc {c}; 79 | 80 | typedef list L; 81 | L l {vi, vi}; 82 | 83 | typedef map M; 84 | M m {{l, vc}}; 85 | 86 | typedef pair P; 87 | P p1 {m, 3}; 88 | 89 | stringstream ss; 90 | ss << p1; 91 | 92 | P p2; 93 | ss >> p2; 94 | 95 | cout << p1 << endl; 96 | cout << p2 << endl; 97 | 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /include/io/filterstream.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_FILTER_STREAM_H 16 | #define CPPUTIL_INCLUDE_IO_FILTER_STREAM_H 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include "include/io/filterbuf.h" 23 | 24 | namespace cpputil { 25 | 26 | template 27 | class basic_ifilterstream : public std::basic_istream { 28 | public: 29 | explicit basic_ifilterstream(std::basic_istream& is) 30 | : std::basic_istream(&buf_), buf_(is.rdbuf()) { } 31 | 32 | explicit basic_ifilterstream(std::basic_streambuf& sb) 33 | : std::basic_istream(&buf_), buf_(sb) { } 34 | 35 | virtual ~basic_ifilterstream() { } 36 | 37 | F& filter() { 38 | return buf_.filter(); 39 | } 40 | 41 | void reserve(size_t bytes) { 42 | buf_.reserve(bytes); 43 | } 44 | 45 | private: 46 | basic_ifilterbuf buf_; 47 | }; 48 | 49 | template 50 | using ifilterstream = basic_ifilterstream>; 51 | template 52 | using wifilterstream = basic_ifilterstream>; 53 | 54 | template 55 | class basic_ofilterstream : public std::basic_ostream { 56 | public: 57 | explicit basic_ofilterstream(std::basic_ostream& os) 58 | : std::basic_ostream(&buf_), buf_(os.rdbuf()) { } 59 | 60 | explicit basic_ofilterstream(std::basic_streambuf& sb) 61 | : std::basic_ostream(&buf_), buf_(sb) { } 62 | 63 | virtual ~basic_ofilterstream() { } 64 | 65 | F& filter() { 66 | return buf_.filter(); 67 | } 68 | 69 | private: 70 | basic_ofilterbuf buf_; 71 | }; 72 | 73 | template 74 | using ofilterstream = basic_ofilterstream>; 75 | template 76 | using wofilterstream = basic_ofilterstream>; 77 | 78 | } // namespace cpputil 79 | 80 | #endif 81 | 82 | -------------------------------------------------------------------------------- /include/container/tokenizer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_CONTAINER_TOKENIZER_H 16 | #define CPPUTIL_INCLUDE_CONTAINER_TOKENIZER_H 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include "include/container/bijection.h" 23 | 24 | namespace cpputil { 25 | 26 | template , 28 | typename TokenMap = std::unordered_map> 29 | class Tokenizer { 30 | public: 31 | typedef T value_type; 32 | typedef const T& const_reference; 33 | typedef Token token_type; 34 | typedef typename Bijection::size_type size_type; 35 | typedef typename Bijection::const_iterator const_iterator; 36 | 37 | Tokenizer() : next_token_ {Token()} { } 38 | 39 | const_iterator tokenize(const_reference t) { 40 | const auto itr = contents_.domain_find(t); 41 | if (itr == contents_.end()) { 42 | return contents_.insert(std::make_pair(t, next_token_++)).first; 43 | } else { 44 | return itr; 45 | } 46 | } 47 | 48 | const_iterator untokenize(token_type token) const { 49 | return contents_.range_find(token); 50 | } 51 | 52 | const_iterator begin() const { 53 | return contents_.begin(); 54 | } 55 | 56 | const_iterator cbegin() const { 57 | return begin(); 58 | } 59 | 60 | const_iterator end() const { 61 | return contents_.end(); 62 | } 63 | 64 | const_iterator cend() const { 65 | return end(); 66 | } 67 | 68 | bool empty() const { 69 | return contents_.empty(); 70 | } 71 | 72 | size_type size() const { 73 | return contents_.size(); 74 | } 75 | 76 | void clear() { 77 | contents_.clear(); 78 | next_token_ = Token(); 79 | } 80 | 81 | void swap(Tokenizer& rhs) { 82 | contents_.swap(rhs.contents_); 83 | std::swap(next_token_, rhs.next_token); 84 | } 85 | 86 | private: 87 | Bijection contents_; 88 | Token next_token_; 89 | }; 90 | 91 | } // namespace cpputil 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /examples/container/bit_array.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/container/bit_array.h" 19 | 20 | using namespace cpputil; 21 | using namespace std; 22 | 23 | int main() { 24 | BitArray<9 * 8> b1; 25 | BitArray<9 * 8> b2; 26 | 27 | cout << "Alignment on 32-bit boundary: " << ((uint64_t)(&b1.get_fixed_byte(0)) % 32) << endl; 28 | 29 | for (auto i = 0; i < 9; ++i) { 30 | b1.get_fixed_byte(i) = 0xff; 31 | } 32 | 33 | for (auto i = b1.fixed_byte_begin(), ie = b1.fixed_byte_end(); i != ie; ++i) { 34 | cout << hex << setw(2) << setfill(' ') << (int) *i << " "; 35 | } 36 | cout << endl; 37 | for (auto i = b2.fixed_byte_begin(), ie = b2.fixed_byte_end(); i != ie; ++i) { 38 | cout << hex << setw(2) << setfill(' ') << (int) *i << " "; 39 | } 40 | cout << endl; 41 | 42 | b2 |= b1; 43 | 44 | for (auto i = b1.fixed_byte_begin(), ie = b1.fixed_byte_end(); i != ie; ++i) { 45 | cout << hex << setw(2) << setfill(' ') << (int) *i << " "; 46 | } 47 | cout << endl; 48 | for (auto i = b2.fixed_byte_begin(), ie = b2.fixed_byte_end(); i != ie; ++i) { 49 | cout << hex << setw(2) << setfill(' ') << (int) *i << " "; 50 | } 51 | cout << endl; 52 | 53 | auto b3 = ~b2; 54 | for (auto i = b3.fixed_byte_begin(), ie = b3.fixed_byte_end(); i != ie; ++i) { 55 | cout << hex << setw(2) << setfill(' ') << (int) *i << " "; 56 | } 57 | cout << endl; 58 | 59 | b3.get_bit(0) = false; 60 | b3.get_bit(2) = false; 61 | b3.get_bit(4) = true; 62 | b3.get_bit(20) = true; 63 | b3.get_bit(70) = true; 64 | 65 | cout << "Bit indices: "; 66 | for (auto i = b3.set_bit_index_begin(); i != b3.set_bit_index_end(); ++i) { 67 | cout << dec << *i << " "; 68 | } 69 | cout << endl; 70 | 71 | cout << "Word indices: "; 72 | for (auto i = b3.set_word_index_begin(); i != b3.set_word_index_end(); ++i) { 73 | cout << dec << *i << " "; 74 | } 75 | cout << endl; 76 | 77 | cout << "Quad indices: "; 78 | for (auto i = b3.set_quad_index_begin(); i != b3.set_quad_index_end(); ++i) { 79 | cout << dec << *i << " "; 80 | } 81 | cout << endl; 82 | 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /include/io/redirectbuf.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_REDIRECTBUF_H 16 | #define CPPUTIL_INCLUDE_IO_REDIRECTBUF_H 17 | 18 | #include 19 | #include 20 | 21 | namespace cpputil { 22 | 23 | template 24 | class basic_redirectbuf : public std::basic_streambuf { 25 | public: 26 | typedef typename std::basic_streambuf::char_type char_type; 27 | typedef typename std::basic_streambuf::int_type int_type; 28 | typedef typename std::basic_streambuf::off_type off_type; 29 | typedef typename std::basic_streambuf::pos_type pos_type; 30 | typedef typename std::basic_streambuf::traits_type traits_type; 31 | 32 | explicit basic_redirectbuf(std::basic_streambuf* buf) 33 | : std::basic_streambuf(), buf_(buf) { } 34 | 35 | virtual ~basic_redirectbuf() { } 36 | 37 | protected: 38 | virtual void imbue(const std::locale& loc) { 39 | buf_->pubimbue(loc); 40 | } 41 | 42 | virtual std::basic_streambuf* setbuf(char_type* s, std::streamsize n) { 43 | return buf_->pubsetbuf(s, n); 44 | } 45 | 46 | virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir, 47 | std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) { 48 | return buf_->pubseekoff(off, dir, which); 49 | } 50 | 51 | virtual pos_type seekpos(pos_type pos, 52 | std::ios_base::openmode which = std::ios_base::in | std::ios_base::out) { 53 | return buf_->pubseekpos(pos, which); 54 | } 55 | 56 | virtual int sync() { 57 | return buf_->pubsync(); 58 | } 59 | 60 | virtual int_type overflow(int_type c = traits_type::eof()) { 61 | return buf_->sputc(traits_type::to_char_type(c)); 62 | } 63 | 64 | virtual int_type underflow() { 65 | return buf_->sgetc(); 66 | } 67 | 68 | virtual int_type uflow() { 69 | return buf_->sbumpc(); 70 | } 71 | 72 | private: 73 | std::basic_streambuf* buf_; 74 | }; 75 | 76 | typedef basic_redirectbuf> redirectbuf; 77 | typedef basic_redirectbuf> wredirectbuf; 78 | 79 | } // namespace cpputil 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /examples/command_line/command_line.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "include/command_line/command_line.h" 19 | #include "include/serialize/hex_reader.h" 20 | #include "include/serialize/hex_writer.h" 21 | #include "include/serialize/range_reader.h" 22 | #include "include/serialize/span_reader.h" 23 | #include "include/serialize/text_style.h" 24 | 25 | using namespace cpputil; 26 | using namespace std; 27 | 28 | auto& heading = Heading::create("Program-specific arguments:"); 29 | 30 | auto& i = ValueArg>>::create("i") 31 | .alternate("int") 32 | .usage("") 33 | .description("i | 1 <= i <= 10") 34 | .required(); 35 | 36 | auto& h = ValueArg, HexWriter>::create("j") 37 | .alternate("hex") 38 | .usage("") 39 | .description("A 16 digit hex value, grouped 8 digits at a time. ie: 01234567 89abcdef") 40 | .default_val(0); 41 | 42 | auto& s = ValueArg::create("s") 43 | .alternate("str") 44 | .usage("\"...\"") 45 | .default_val("Hello, world"); 46 | 47 | auto& v = ValueArg, SpanReader, Range>>::create("v") 48 | .alternate("vector") 49 | .usage("{ 1 2 3 }") 50 | .description("A sequence of integers, each between 1 and 10, inclusive") 51 | .default_val({1, 2, 3}); 52 | 53 | auto& sc = ValueArg, SpanReader, Range>>::create("sc") 54 | .usage("{ a b c }") 55 | .description("A set of characters, each between a and z, inclusive. May include ellipses as shorthand. ie: { a ... d }") 56 | .default_val({'a', 'b', 'c'}); 57 | 58 | auto& vh = FileArg, 59 | SpanReader, Range, TextStyle>, 60 | TextWriter, TextStyle>>::create("vh") 61 | .usage("/path/to/file.dat") 62 | .description("A sequence of 8 digit hex values. May include ellipses as shorthand. ie:{ 00000000 00000001 ... }") 63 | .default_val({0, 1, 2}); 64 | 65 | int main(int argc, char** argv) { 66 | CommandLineConfig::strict_with_convenience(argc, argv); 67 | return 0; 68 | } 69 | 70 | -------------------------------------------------------------------------------- /include/container/bit_vector.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_CONTAINER_BIT_VECTOR_H 16 | #define CPPUTIL_INCLUDE_CONTAINER_BIT_VECTOR_H 17 | 18 | #include 19 | 20 | #include 21 | 22 | #include "include/allocator/aligned.h" 23 | #include "include/container/bit_string.h" 24 | 25 | namespace cpputil { 26 | 27 | class BitVector : public BitString>> { 28 | public: 29 | /** Creates an empty bit vector. */ 30 | BitVector() : BitString>>() { } 31 | /** Creates a bit vector to hold n bits. */ 32 | BitVector(size_t n) : BitString>>() { 33 | contents_.resize((n + 63) / 64); 34 | num_bits_ = n; 35 | } 36 | 37 | /** Resizes a BitVector to contain n bits. */ 38 | void resize_for_bits(size_t n) { 39 | contents_.resize((n + 63) / 64); 40 | num_bits_ = n; 41 | } 42 | /** Resizes a BitVector to contain n fixed bytes. */ 43 | void resize_for_fixed_bytes(size_t n) { 44 | contents_.resize(n + 7 / 8); 45 | num_bits_ = 8 * n; 46 | } 47 | /** Resizes a BitVector to contain n fixed words. */ 48 | void resize_for_fixed_words(size_t n) { 49 | contents_.resize(n + 3 / 4); 50 | num_bits_ = 16 * n; 51 | } 52 | /** Resizes a BitVector to contain n fixed doubles. */ 53 | void resize_for_fixed_doubles(size_t n) { 54 | contents_.resize(n + 1 / 2); 55 | num_bits_ = 32 * n; 56 | } 57 | /** Resizes a BitVector to contain n fixed quads. */ 58 | void resize_for_fixed_quads(size_t n) { 59 | contents_.resize(n); 60 | num_bits_ = 64 * n; 61 | } 62 | /** Resizes a BitVector to contain n float singles. */ 63 | void resize_for_float_singles(size_t n) { 64 | contents_.resize(n + 1 / 2); 65 | num_bits_ = 32 * n; 66 | } 67 | /** Resizes a BitVector to contain n float doubles. */ 68 | void resize_for_float_doubles(size_t n) { 69 | contents_.resize(n); 70 | num_bits_ = 64 * n; 71 | } 72 | 73 | /** Set all elements to zero. */ 74 | void reset() { 75 | contents_.assign(contents_.size(), 0); 76 | } 77 | 78 | /** Set all elements to one. */ 79 | void set() { 80 | contents_.assign(contents_.size(), -1); 81 | } 82 | }; 83 | 84 | } // namespace cpputil 85 | 86 | #endif 87 | -------------------------------------------------------------------------------- /include/io/redirectstream.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_REDIRECTSTREAM_H 16 | #define CPPUTIL_INCLUDE_IO_REDIRECTSTREAM_H 17 | 18 | #include 19 | #include 20 | 21 | #include "include/io/redirectbuf.h" 22 | 23 | namespace cpputil { 24 | 25 | template 26 | class basic_iredirectstream : public std::basic_istream { 27 | public: 28 | explicit basic_iredirectstream(std::basic_istream& is) 29 | : std::basic_istream(&buf_), buf_(is.rdbuf()) { } 30 | 31 | explicit basic_iredirectstream(std::basic_streambuf* sb) 32 | : std::basic_istream(&buf_), buf_(sb) { } 33 | 34 | virtual ~basic_iredirectstream() { } 35 | 36 | private: 37 | basic_redirectbuf buf_; 38 | }; 39 | 40 | typedef basic_iredirectstream> iredirectstream; 41 | typedef basic_iredirectstream> wiredirectstream; 42 | 43 | template 44 | class basic_oredirectstream : public std::basic_ostream { 45 | public: 46 | explicit basic_oredirectstream(std::basic_ostream& os) 47 | : std::basic_ostream(&buf_), buf_(os.rdbuf()) { } 48 | 49 | explicit basic_oredirectstream(std::basic_streambuf* sb) 50 | : std::basic_ostream(&buf_), buf_(sb) { } 51 | 52 | virtual ~basic_oredirectstream() { } 53 | 54 | private: 55 | basic_redirectbuf buf_; 56 | }; 57 | 58 | typedef basic_oredirectstream> oredirectstream; 59 | typedef basic_oredirectstream> woredirectstream; 60 | 61 | template 62 | class basic_redirectstream : public std::basic_iostream { 63 | public: 64 | explicit basic_redirectstream(std::basic_iostream& ios) 65 | : std::basic_iostream(&buf_), buf_(ios.rdbuf()) { } 66 | 67 | explicit basic_redirectstream(std::basic_streambuf* sb) 68 | : std::basic_iostream(&buf_), buf_(sb) { } 69 | 70 | virtual ~basic_redirectstream() { } 71 | 72 | private: 73 | basic_redirectbuf buf_; 74 | }; 75 | 76 | typedef basic_redirectstream> redirectstream; 77 | typedef basic_redirectstream> wredirectstream; 78 | 79 | } // namespace cpputil 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /include/io/column.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_COLUMN_H 16 | #define CPPUTIL_INCLUDE_IO_COLUMN_H 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | namespace cpputil { 24 | 25 | class Column { 26 | public: 27 | Column() : 28 | padding_(1), sb_(0) { } 29 | 30 | Column& padding(size_t p) { 31 | padding_ = p; 32 | return *this; 33 | } 34 | 35 | void operator()(std::streambuf* sb, char c) { 36 | sb_ = sb; 37 | 38 | if (text_.empty()) { 39 | text_.resize(1); 40 | } 41 | auto& col = text_.back(); 42 | if (col.empty()) { 43 | col.resize(1); 44 | } 45 | 46 | if (c == '\n') { 47 | col.resize(col.size() + 1); 48 | } else { 49 | col.back().push_back(c); 50 | } 51 | } 52 | 53 | Column& next() { 54 | text_.resize(text_.size() + 1); 55 | return *this; 56 | } 57 | 58 | Column& done() { 59 | if (sb_ == 0) { 60 | return *this; 61 | } 62 | 63 | size_t height = 0; 64 | std::vector width; 65 | 66 | for (const auto& col : text_) { 67 | if (col.size() > height) { 68 | height = col.size(); 69 | } 70 | 71 | width.push_back(0); 72 | for (const auto& line : col) 73 | if (line.size() > width.back()) { 74 | width.back() = line.size(); 75 | } 76 | } 77 | 78 | for (size_t i = 0; i < height; ++i) { 79 | for (size_t c = 0, ce = text_.size(); c < ce; ++c) { 80 | const auto& col = text_[c]; 81 | if (col.size() <= i) { 82 | for (size_t j = 0; j < width[c]; ++j) { 83 | sb_->sputc(' '); 84 | } 85 | } else { 86 | const auto& line = col[i]; 87 | size_t j = 0; 88 | for (size_t je = line.size(); j < je; ++j) { 89 | sb_->sputc(line[j]); 90 | } 91 | for (; j < width[c]; ++j) { 92 | sb_->sputc(' '); 93 | } 94 | } 95 | for (size_t j = 0; j < padding_; ++j) { 96 | sb_->sputc(' '); 97 | } 98 | } 99 | 100 | if ( i+1 < height ) { 101 | sb_->sputc('\n'); 102 | } 103 | } 104 | 105 | sb_->pubsync(); 106 | text_.clear(); 107 | 108 | return *this; 109 | } 110 | 111 | private: 112 | size_t padding_; 113 | std::vector>> text_; 114 | std::streambuf* sb_; 115 | }; 116 | 117 | } // namespace cpputil 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /include/io/fail.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_FAIL_H 16 | #define CPPUTIL_INCLUDE_IO_FAIL_H 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | namespace cpputil { 23 | 24 | inline int __fail_msg_idx() { 25 | static const int idx = std::ios::xalloc(); 26 | return idx; 27 | } 28 | 29 | inline int __warn_bit_idx() { 30 | static const int idx = std::ios::xalloc(); 31 | return idx; 32 | } 33 | 34 | inline int __warn_msg_idx() { 35 | static const int idx = std::ios::xalloc(); 36 | return idx; 37 | } 38 | 39 | template 40 | inline bool failed(S& is) { 41 | return is.fail(); 42 | } 43 | 44 | template 45 | inline bool warned(S& is) { 46 | return is.iword(__warn_bit_idx()) != 0; 47 | } 48 | 49 | template 50 | inline std::ostringstream& fail(S& is) { 51 | auto& p = is.pword(__fail_msg_idx()); 52 | if (p == nullptr) { 53 | p = (void*) new std::ostringstream(); 54 | } 55 | auto ss = static_cast(p); 56 | 57 | is.setstate(std::ios::failbit); 58 | return *ss; 59 | } 60 | 61 | template 62 | inline std::ostringstream& warn(S& is) { 63 | auto& p = is.pword(__warn_msg_idx()); 64 | if (p == nullptr) { 65 | p = (void*) new std::ostringstream(); 66 | } 67 | auto ss = static_cast(p); 68 | 69 | is.iword(__warn_bit_idx()) = 1; 70 | return *ss; 71 | } 72 | 73 | template 74 | inline std::string fail_msg(S& is) { 75 | auto& p = is.pword(__fail_msg_idx()); 76 | if (p == nullptr) { 77 | p = (void*) new std::ostringstream(); 78 | } 79 | const auto& ss = static_cast(p); 80 | 81 | return ss->str(); 82 | } 83 | 84 | template 85 | inline std::string warn_msg(S& is) { 86 | auto& p = is.pword(__warn_msg_idx()); 87 | if (p == nullptr) { 88 | p = (void*) new std::ostringstream(); 89 | } 90 | const auto& ss = static_cast(p); 91 | 92 | return ss->str(); 93 | } 94 | 95 | template 96 | inline void fail_if_not(S& is, char c) { 97 | if(is.fail()) // already failed, don't show bad error message 98 | return; 99 | char d = is.get(); 100 | if(c != d) { 101 | if(isprint(c) && isprint(d)) 102 | fail(is) << "Expected character '" << c << "' but got '" << d << "'." << std::endl; 103 | else 104 | fail(is) << "Expected character " << std::showbase << std::hex 105 | << (int)c << " but got " << (int)d << "." << std::endl; 106 | } 107 | } 108 | 109 | 110 | 111 | } // namespace cpputil 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /include/serialize/span_writer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SERIALIZE_SPAN_WRITER_H 16 | #define CPPUTIL_INCLUDE_SERIALIZE_SPAN_WRITER_H 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | namespace cpputil { 23 | 24 | template , typename Enable = void> 25 | struct SpanWriter; 26 | 27 | template 28 | struct SpanWriter::value>::type> { 29 | public: 30 | void operator()(std::ostream& os, const T& t) const { 31 | std::vector v(t.size()); 32 | std::copy(t.begin(), t.end(), v.begin()); 33 | std::sort(v.begin(), v.end()); 34 | 35 | os << Style::open(); 36 | 37 | typename T::value_type last = v[0]; 38 | typename T::value_type last_run_start = v[0]; 39 | bool use_open_range = false; // using open ranges seems to make the result less readable 40 | int run = -1; 41 | for (auto cur : v) { 42 | if (cur == last + 1) { 43 | // we are in a run 44 | ++run; 45 | } else { 46 | if (run > 2) { 47 | // end current run 48 | if (last_run_start != Range::lower() || !use_open_range) { 49 | os << " " << last_run_start; 50 | } 51 | os << " ... " << last; 52 | } else if (run == 2) { 53 | os << " " << last_run_start << " " << last; 54 | } else if (run >= 0) { 55 | os << " " << last; 56 | } 57 | run = 1; 58 | last_run_start = cur; 59 | } 60 | last = cur; 61 | } 62 | 63 | if (run > 0) { 64 | if (run > 2) { 65 | // end current run 66 | if (last_run_start != Range::lower() || !use_open_range) { 67 | os << " " << last_run_start; 68 | } 69 | os << " ..."; 70 | if (last != Range::upper() || !use_open_range) { 71 | os << " " << last; 72 | } 73 | } else if (run == 2) { 74 | os << " " << last_run_start << " " << last; 75 | } else { 76 | os << " " << last; 77 | } 78 | } 79 | 80 | os << " " << Style::close(); 81 | } 82 | }; 83 | 84 | template 85 | struct SpanWriter::value>::type> { 86 | public: 87 | void operator()(std::ostream& os, const T& t) const { 88 | std::vector v(t.size()); 89 | std::copy(t.begin(), t.end(), v.begin()); 90 | SpanWriter, Range, Style> w; 91 | w(os, v); 92 | } 93 | }; 94 | 95 | } // namespace cpputil 96 | 97 | #endif -------------------------------------------------------------------------------- /include/io/filterbuf.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_IO_FILTERBUF_H 16 | #define CPPUTIL_INCLUDE_IO_FILTERBUF_H 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | namespace cpputil { 23 | 24 | template 25 | class basic_ifilterbuf : public std::basic_streambuf { 26 | public: 27 | typedef typename std::basic_streambuf::char_type char_type; 28 | typedef typename std::basic_streambuf::int_type int_type; 29 | typedef typename std::basic_streambuf::off_type off_type; 30 | typedef typename std::basic_streambuf::pos_type pos_type; 31 | typedef typename std::basic_streambuf::traits_type traits_type; 32 | 33 | basic_ifilterbuf(std::basic_streambuf* buf) 34 | : buf_(buf), next_(16) { } 35 | 36 | virtual ~basic_ifilterbuf() { } 37 | 38 | F& filter() { 39 | return filter_; 40 | } 41 | 42 | void reserve(size_t bytes) { 43 | next_.reserve(bytes); 44 | } 45 | 46 | protected: 47 | virtual int_type underflow() { 48 | if (std::basic_streambuf::gptr() == std::basic_streambuf::egptr()) { 49 | size_t count = 0; 50 | do { 51 | const auto c = buf_->sbumpc(); 52 | count = filter_(c, next_.data()); 53 | } while (count == 0); 54 | std::basic_streambuf::setg(next_.data(), next_.data(), next_.data() + count); 55 | } 56 | return *std::basic_streambuf::gptr(); 57 | } 58 | 59 | virtual int_type sync() { 60 | return buf_->pubsync(); 61 | } 62 | 63 | private: 64 | std::basic_streambuf* buf_; 65 | std::vector next_; 66 | F filter_; 67 | }; 68 | 69 | template 70 | using ifilterbuf = basic_ifilterbuf>; 71 | template 72 | using wifilterbuf = basic_ifilterbuf>; 73 | 74 | template 75 | class basic_ofilterbuf : public std::basic_streambuf { 76 | public: 77 | basic_ofilterbuf(std::basic_streambuf* buf) 78 | : buf_(buf) { } 79 | 80 | virtual ~basic_ofilterbuf() { } 81 | 82 | F& filter() { 83 | return filter_; 84 | } 85 | 86 | protected: 87 | virtual int sync() { 88 | return buf_->pubsync(); 89 | } 90 | 91 | virtual int overflow(int c = EOF) { 92 | if (c == EOF) { 93 | return EOF; 94 | } 95 | 96 | filter_(buf_, c); 97 | return 1; 98 | } 99 | 100 | private: 101 | std::basic_streambuf* buf_; 102 | F filter_; 103 | }; 104 | 105 | template 106 | using ofilterbuf = basic_ofilterbuf>; 107 | template 108 | using wofilterbuf = basic_ofilterbuf>; 109 | 110 | } // namespace cpputil 111 | 112 | #endif 113 | 114 | -------------------------------------------------------------------------------- /include/meta/bit_width.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_BIT_WIDTH_H 16 | #define CPPUTIL_INCLUDE_META_BIT_WIDTH_H 17 | 18 | #include 19 | #include 20 | 21 | namespace cpputil { 22 | 23 | template 24 | struct bit_width : public std::integral_constant < int, -1 > { }; 25 | 26 | template <> 27 | struct bit_width : public std::integral_constant { }; 28 | 29 | template <> 30 | struct bit_width : public std::integral_constant { }; 31 | 32 | template <> 33 | struct bit_width : public std::integral_constant { }; 34 | 35 | template <> 36 | struct bit_width : public std::integral_constant { }; 37 | 38 | template <> 39 | struct bit_width : public std::integral_constant { }; 40 | 41 | template <> 42 | struct bit_width : public std::integral_constant { }; 43 | 44 | template <> 45 | struct bit_width : public std::integral_constant { }; 46 | 47 | template <> 48 | struct bit_width : public std::integral_constant { }; 49 | 50 | template <> 51 | struct bit_width : public std::integral_constant { }; 52 | 53 | template <> 54 | struct bit_width : public std::integral_constant { }; 55 | 56 | template <> 57 | struct bit_width : public std::integral_constant { }; 58 | 59 | template <> 60 | struct bit_width : public std::integral_constant { }; 61 | 62 | template <> 63 | struct bit_width : public std::integral_constant { }; 64 | 65 | template <> 66 | struct bit_width : public std::integral_constant { }; 67 | 68 | template <> 69 | struct bit_width : public std::integral_constant { }; 70 | 71 | template <> 72 | struct bit_width : public std::integral_constant { }; 73 | 74 | template <> 75 | struct bit_width : public std::integral_constant { }; 76 | 77 | template <> 78 | struct bit_width : public std::integral_constant { }; 79 | 80 | template <> 81 | struct bit_width : public std::integral_constant { }; 82 | 83 | template <> 84 | struct bit_width : public std::integral_constant { }; 85 | 86 | template <> 87 | struct bit_width : public std::integral_constant { }; 88 | 89 | template <> 90 | struct bit_width : public std::integral_constant { }; 91 | 92 | template <> 93 | struct bit_width : public std::integral_constant { }; 94 | 95 | template <> 96 | struct bit_width : public std::integral_constant { }; 97 | 98 | template <> 99 | struct bit_width : public std::integral_constant { }; 100 | 101 | template <> 102 | struct bit_width : public std::integral_constant { }; 103 | 104 | } // namespace cpputil 105 | 106 | #endif 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /include/meta/is_stl_associative.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_META_IS_STL_ASSOCIATIVE_H 16 | #define CPPUTIL_META_IS_STL_ASSOCIATIVE_H 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | namespace cpputil { 25 | 26 | template 27 | struct is_stl_associative : public std::false_type { }; 28 | 29 | template 30 | struct is_stl_associative> : public std::true_type { }; 31 | 32 | template 33 | struct is_stl_associative> : public std::true_type { }; 34 | 35 | template 36 | struct is_stl_associative> : public std::true_type { }; 37 | 38 | template 39 | struct is_stl_associative> : public std::true_type { }; 40 | 41 | template 42 | struct is_stl_associative> : public std::true_type { }; 43 | 44 | template 45 | struct is_stl_associative> : public 46 | std::true_type { }; 47 | 48 | template 49 | struct is_stl_associative> : public 50 | std::true_type { }; 51 | template 52 | struct is_stl_associative> : public 53 | std::true_type { }; 54 | 55 | template 56 | struct is_stl_associative> : public std::true_type { }; 57 | 58 | template 59 | struct is_stl_associative> : public std::true_type { }; 60 | 61 | template 62 | struct is_stl_associative> : public std::true_type { }; 63 | 64 | template 65 | struct is_stl_associative> : public std::true_type { }; 66 | 67 | template 68 | struct is_stl_associative> : public std::true_type { }; 69 | 70 | template 71 | struct is_stl_associative> : public 72 | std::true_type { }; 73 | 74 | template 75 | struct is_stl_associative> : public 76 | std::true_type { }; 77 | 78 | template 79 | struct is_stl_associative> : public 80 | std::true_type { }; 81 | 82 | } // namespace cpputil 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /include/serialize/text_writer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SERIALIZE_TEXT_WRITER_H 16 | #define CPPUTIL_INCLUDE_SERIALIZE_TEXT_WRITER_H 17 | 18 | #include 19 | #include 20 | 21 | #include "include/meta/is_char_pointer.h" 22 | #include "include/meta/is_stl_associative.h" 23 | #include "include/meta/is_stl_sequence.h" 24 | #include "include/meta/is_stl_pair.h" 25 | #include "include/meta/is_stl_string.h" 26 | #include "include/meta/is_stl_tuple.h" 27 | #include "include/serialize/dec_writer.h" 28 | #include "include/serialize/hex_writer.h" 29 | #include "include/serialize/text_style.h" 30 | 31 | namespace cpputil { 32 | 33 | template , typename Enable = void> 34 | struct TextWriter; 35 | 36 | template 37 | struct TextWriter ::value>::type> { 38 | void operator()(std::ostream& os, const T& t) const { 39 | if (Style::dec()) { 40 | DecWriter()(os, t); 41 | } else { 42 | HexWriter()(os, t); 43 | } 44 | } 45 | }; 46 | 47 | template 48 | struct TextWriter < T, Style, 49 | typename std::enable_if < is_char_pointer::value || is_stl_string::value >::type > { 50 | void operator()(std::ostream& os, const T& t) const { 51 | os << Style::quote() << t << Style::quote(); 52 | } 53 | }; 54 | 55 | template 56 | struct TextWriter::value>::type> { 57 | void operator()(std::ostream& os, const T& t) const { 58 | os << Style::open() << " "; 59 | 60 | TextWriter()(os, t.first); 61 | os << " "; 62 | TextWriter()(os, t.second); 63 | 64 | os << " " << Style::close(); 65 | } 66 | }; 67 | 68 | template 69 | struct TextWriter < T, Style, 70 | typename std::enable_if < is_stl_sequence::value || is_stl_associative::value >::type > { 71 | void operator()(std::ostream& os, const T& t) const { 72 | os << Style::open(); 73 | 74 | for (auto& elem : t) { 75 | os << " "; 76 | TextWriter()(os, elem); 77 | } 78 | 79 | os << " " << Style::close(); 80 | } 81 | }; 82 | 83 | template 84 | class TextWriter ::value>::type> { 85 | public: 86 | void operator()(std::ostream& os, const T& t) const { 87 | os << Style::open(); 88 | Helper::value>()(os, t); 89 | os << " " << Style::close(); 90 | } 91 | 92 | private: 93 | template 94 | struct Helper { 95 | void operator()(std::ostream& os, const Tuple& t) { 96 | os << " "; 97 | TextWriter::type, Style>()(os, std::get(t)); 98 | Helper < Tuple, Begin + 1, End > ()(os, t); 99 | } 100 | }; 101 | 102 | template 103 | struct Helper { 104 | void operator()(std::ostream& os, const Tuple& t) { } 105 | }; 106 | }; 107 | 108 | } // namespace cpputil 109 | 110 | #endif 111 | 112 | -------------------------------------------------------------------------------- /include/command_line/value_arg.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_COMMAND_LINE_VALUE_ARG_H 16 | #define CPPUTIL_INCLUDE_COMMAND_LINE_VALUE_ARG_H 17 | 18 | #include 19 | 20 | #include "include/command_line/arg.h" 21 | #include "include/io/fail.h" 22 | #include "include/serialize/text_reader.h" 23 | #include "include/serialize/text_writer.h" 24 | 25 | namespace cpputil { 26 | 27 | template , typename W = TextWriter> 28 | class ValueArg : public Arg { 29 | public: 30 | virtual ~ValueArg() = default; 31 | 32 | /** Creates and registers a new flag */ 33 | static ValueArg& create(const std::string& opt) { 34 | return *(new ValueArg(opt)); 35 | } 36 | 37 | /** Consumes indices from first alias to next - */ 38 | virtual std::pair read(int argc, char** argv) { 39 | for (const int i : get_appearances(argc, argv)) { 40 | if (i == (argc - 1) || argv[i + 1][0] == '-') { 41 | error("No argument provided!"); 42 | return std::make_pair(i, i); 43 | } 44 | 45 | std::istringstream iss(argv[i + 1]); 46 | T temp = T(); 47 | R()(iss, temp); 48 | 49 | if (failed(iss)) { 50 | const auto msg = fail_msg(iss); 51 | error(msg == "" ? parse_error_ : msg); 52 | } else { 53 | val_ = temp; 54 | } 55 | set_provided(); 56 | return std::make_pair(i, i + 1); 57 | } 58 | 59 | return std::make_pair(0, 0); 60 | } 61 | 62 | /** Create a new arg alias (hashes implicit; chars get 1, strings 2) */ 63 | ValueArg& alternate(const std::string& a) { 64 | Arg::alternate(a); 65 | return *this; 66 | } 67 | 68 | /** Reset arg usage */ 69 | ValueArg& usage(const std::string& u) { 70 | Arg::usage(u); 71 | return *this; 72 | } 73 | 74 | /** Reset arg description */ 75 | ValueArg& description(const std::string& d) { 76 | Arg::description(d); 77 | return *this; 78 | } 79 | 80 | /** Resets arg default value */ 81 | ValueArg& default_val(const T& t) { 82 | set_has_default(); 83 | val_ = t; 84 | return *this; 85 | } 86 | 87 | /** Resets the parse error message */ 88 | ValueArg& parse_error(const std::string& pe) { 89 | parse_error_ = pe; 90 | return *this; 91 | } 92 | 93 | /** Resets the required value. */ 94 | ValueArg& required(const bool val = true) { 95 | Arg::required(val); 96 | return *this; 97 | } 98 | 99 | /** Implicit conversion to underlying type */ 100 | operator T& () { 101 | return val_; 102 | } 103 | 104 | /** Explicit conversion to underlying type */ 105 | T& value() { 106 | return val_; 107 | } 108 | 109 | /** Prints underlying value using writer */ 110 | virtual void debug(std::ostream& os) const { 111 | W()(os, val_); 112 | } 113 | 114 | private: 115 | /** Underlying value, optionally specified on command line */ 116 | T val_; 117 | /** String to emit if an error occurs during read() */ 118 | std::string parse_error_; 119 | 120 | /** ValueArgs are assigned default constructor values by default */ 121 | ValueArg(const std::string& opt) : 122 | Arg {opt} { 123 | usage(""); 124 | parse_error("Unspecified parse error!"); 125 | } 126 | }; 127 | 128 | } // namespace cpputil 129 | 130 | #endif 131 | 132 | -------------------------------------------------------------------------------- /include/command_line/file_arg.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_COMMAND_LINE_FILE_ARG_H 16 | #define CPPUTIL_INCLUDE_COMMAND_LINE_FILE_ARG_H 17 | 18 | #include 19 | 20 | #include "include/command_line/arg.h" 21 | #include "include/io/fail.h" 22 | #include "include/serialize/text_reader.h" 23 | #include "include/serialize/text_writer.h" 24 | 25 | namespace cpputil { 26 | 27 | template , typename W = TextWriter> 28 | class FileArg : public Arg { 29 | public: 30 | virtual ~FileArg() = default; 31 | 32 | /** Creates and registers a new flag */ 33 | static FileArg& create(const std::string& opt) { 34 | return *(new FileArg(opt)); 35 | } 36 | 37 | /** Consumes indices from first alias to next - */ 38 | virtual std::pair read(int argc, char** argv) { 39 | for (const int i : get_appearances(argc, argv)) { 40 | if (i == (argc - 1) || argv[i + 1][0] == '-') { 41 | error("No argument provided!"); 42 | return std::make_pair(i, i); 43 | } 44 | 45 | std::ifstream ifs(argv[i + 1]); 46 | if (!ifs.is_open()) { 47 | error(file_error_); 48 | } else { 49 | T temp = T(); 50 | R()(ifs, temp); 51 | 52 | if (failed(ifs)) { 53 | const auto msg = fail_msg(ifs); 54 | error(msg == "" ? parse_error_ : msg); 55 | } else { 56 | val_ = temp; 57 | } 58 | } 59 | 60 | set_provided(); 61 | return std::make_pair(i, i + 1); 62 | } 63 | 64 | return std::make_pair(0, 0); 65 | } 66 | 67 | /** Create a new arg alias (hashes implicit; chars get 1, strings 2) */ 68 | FileArg& alternate(const std::string& a) { 69 | Arg::alternate(a); 70 | return *this; 71 | } 72 | 73 | /** Reset arg usage */ 74 | FileArg& usage(const std::string& u) { 75 | Arg::usage(u); 76 | return *this; 77 | } 78 | 79 | /** Reset arg description */ 80 | FileArg& description(const std::string& d) { 81 | Arg::description(d); 82 | return *this; 83 | } 84 | 85 | /** Resets arg default value */ 86 | FileArg& default_val(const T& t) { 87 | set_has_default(); 88 | val_ = t; 89 | return *this; 90 | } 91 | 92 | /** Resets the required value. */ 93 | FileArg& required(const bool val = true) { 94 | Arg::required(val); 95 | return *this; 96 | } 97 | 98 | /** Resets the parse error message */ 99 | FileArg& parse_error(const std::string& pe) { 100 | parse_error_ = pe; 101 | return *this; 102 | } 103 | 104 | /** Resets file error message */ 105 | FileArg& file_error(const std::string& fe) { 106 | file_error_ = fe; 107 | return *this; 108 | } 109 | 110 | /** Implicit conversion to underlying type */ 111 | operator T& () { 112 | return val_; 113 | } 114 | 115 | /** Explicit conversion to underlying type */ 116 | T& value() { 117 | return val_; 118 | } 119 | 120 | /** Prints underlying value using writer */ 121 | virtual void debug(std::ostream& os) const { 122 | W()(os, val_); 123 | } 124 | 125 | private: 126 | /** Underlying value, optionally specified on command line */ 127 | T val_; 128 | /** String to emit if an error occurs during read() */ 129 | std::string parse_error_; 130 | /** String to emit if unable to open source file during read() */ 131 | std::string file_error_; 132 | 133 | /** FileArgs are assigned default constructor values by default */ 134 | FileArg(const std::string& opt) : 135 | Arg {opt} { 136 | usage(""); 137 | parse_error("Unspecified parse error!"); 138 | file_error("Unable to open source file!"); 139 | } 140 | }; 141 | 142 | } // namespace cpputil 143 | 144 | #endif 145 | 146 | 147 | -------------------------------------------------------------------------------- /include/container/bijection.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_CONTAINER_BIJECTION_H 16 | #define CPPUTIL_INCLUDE_CONTAINER_BIJECTION_H 17 | 18 | #include 19 | #include 20 | 21 | namespace cpputil { 22 | 23 | template , 24 | typename RMap = std::map> 25 | class Bijection { 26 | public: 27 | typedef D domain_type; 28 | typedef const domain_type& const_domain_reference; 29 | typedef R range_type; 30 | typedef const range_type& const_range_reference; 31 | typedef typename DMap::const_iterator const_iterator; 32 | typedef typename DMap::value_type value_type; 33 | typedef const value_type& const_reference; 34 | typedef typename DMap::size_type size_type; 35 | 36 | const_iterator begin() const { 37 | return d2r_.begin(); 38 | } 39 | 40 | const_iterator cbegin() const { 41 | return d2r_.cbegin(); 42 | } 43 | 44 | const_iterator end() const { 45 | return d2r_.end(); 46 | } 47 | 48 | const_iterator cend() const { 49 | return d2r_.cend(); 50 | } 51 | 52 | bool empty() const { 53 | return d2r_.empty(); 54 | } 55 | 56 | size_type size() const { 57 | return d2r_.size(); 58 | } 59 | 60 | void clear() { 61 | d2r_.clear(); 62 | r2d_.clear(); 63 | } 64 | 65 | std::pair insert(const value_type& val) { 66 | if (d2r_.find(val.first) != d2r_.end() || r2d_.find(val.second) != r2d_.end()) { 67 | return std::make_pair(end(), false); 68 | } else { 69 | r2d_.insert(std::make_pair(val.second, val.first)); 70 | return d2r_.insert(val); 71 | } 72 | } 73 | 74 | template 75 | void insert(InputIterator first, InputIterator last) { 76 | for (; first != last; ++first) { 77 | insert(*first); 78 | } 79 | } 80 | 81 | void insert(std::initializer_list il) { 82 | for (const auto& i : il) { 83 | insert(*i); 84 | } 85 | } 86 | 87 | const_iterator erase(const_iterator position) { 88 | r2d_.erase(position->second); 89 | return d2r_.erase(position); 90 | } 91 | 92 | size_type domain_erase(const_domain_reference val) { 93 | const auto itr = d2r_.find(val); 94 | if (itr != d2r_.end()) { 95 | r2d_.erase(itr->second); 96 | return d2r_.erase(val); 97 | } else { 98 | return 0; 99 | } 100 | } 101 | 102 | size_type range_erase(const_range_reference val) { 103 | const auto itr = r2d_.find(val); 104 | if (itr != r2d_.end()) { 105 | const auto ret = d2r_.erase(itr->second); 106 | r2d_.erase(itr->second); 107 | return ret; 108 | } else { 109 | return 0; 110 | } 111 | } 112 | 113 | const_iterator erase(const_iterator first, const_iterator last) { 114 | for (auto temp = first; temp != last; ++temp) { 115 | r2d_.erase(temp->second); 116 | } 117 | return d2r_.erase(first, last); 118 | } 119 | 120 | const_iterator domain_find(const_domain_reference d) const { 121 | return d2r_.find(d); 122 | } 123 | 124 | const_iterator range_find(const_range_reference r) const { 125 | const auto itr = r2d_.find(r); 126 | return itr != r2d_.end() ? domain_find(itr->second) : end(); 127 | } 128 | 129 | const_reference domain_at(const_domain_reference d) const { 130 | return d2r_.at(d); 131 | } 132 | 133 | const_reference range_at(const_range_reference r) const { 134 | const auto& val = r2d_.at(r); 135 | return d2r_.at(val.second); 136 | } 137 | 138 | void swap(Bijection& rhs) { 139 | d2r_.swap(rhs.d2r_); 140 | r2d_.swap(rhs.r2d_); 141 | } 142 | 143 | private: 144 | DMap d2r_; 145 | RMap r2d_; 146 | }; 147 | 148 | } // namespace cpputil 149 | 150 | #endif 151 | -------------------------------------------------------------------------------- /include/serialize/span_reader.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SERIALIZE_SPAN_READER_H 16 | #define CPPUTIL_INCLUDE_SERIALIZE_SPAN_READER_H 17 | 18 | #include 19 | #include 20 | 21 | #include "include/meta/is_stl_sequence.h" 22 | #include "include/meta/is_stl_set.h" 23 | #include "include/serialize/range.h" 24 | #include "include/serialize/text_reader.h" 25 | #include "include/serialize/text_style.h" 26 | 27 | namespace cpputil { 28 | 29 | #define die_unless(c) \ 30 | if (is.get() != c) { \ 31 | is.setstate(std::ios::failbit); \ 32 | return; \ 33 | } 34 | 35 | #define die_outside(v, l,u) \ 36 | if (v < l || v > u) { \ 37 | is.setstate(std::ios::failbit); \ 38 | return; \ 39 | } 40 | 41 | template , typename Enable = void> 42 | struct SpanReader; 43 | 44 | template 45 | class SpanReader::value>::type> { 46 | public: 47 | void operator()(std::istream& is, T& t) const { 48 | die_unless(Style::open()); 49 | die_unless(' '); 50 | 51 | t.clear(); 52 | auto range = false; 53 | 54 | while (is.peek() != Style::close()) { 55 | if (is.peek() == Style::etc()) { 56 | range = true; 57 | while (is.peek() == Style::etc()) { 58 | is.get(); 59 | } 60 | } else { 61 | typename T::value_type v; 62 | TextReader()(is, v); 63 | die_outside(v, Range::lower(), Range::upper()); 64 | 65 | if (range) { 66 | range = false; 67 | fill_until(t, v); 68 | } 69 | t.emplace_back(v); 70 | } 71 | 72 | die_unless(' '); 73 | } 74 | die_unless(Style::close()); 75 | 76 | if (range) { 77 | fill_until(t, Range::upper()); 78 | t.emplace_back(Range::upper()); 79 | } 80 | } 81 | 82 | private: 83 | void fill_until(T& t, const typename T::value_type& v) const { 84 | if (t.empty()) { 85 | t.emplace_back(Range::lower()); 86 | } 87 | auto last = t.back(); 88 | for (++last; last < v; ++last) { 89 | t.emplace_back(last); 90 | } 91 | } 92 | }; 93 | 94 | template 95 | struct SpanReader::value>::type> { 96 | public: 97 | void operator()(std::istream& is, T& t) const { 98 | die_unless(Style::open()); 99 | die_unless(' '); 100 | 101 | t.clear(); 102 | auto range = false; 103 | auto last = Range::lower(); 104 | 105 | while (is.peek() != Style::close()) { 106 | if (is.peek() == Style::etc()) { 107 | range = true; 108 | while (is.peek() == Style::etc()) { 109 | is.get(); 110 | } 111 | } else { 112 | typename T::value_type v; 113 | TextReader()(is, v); 114 | die_outside(v, Range::lower(), Range::upper()); 115 | 116 | if (range) { 117 | range = false; 118 | fill_until(t, last, v); 119 | } 120 | t.emplace(v); 121 | last = v; 122 | } 123 | 124 | die_unless(' '); 125 | } 126 | die_unless(Style::close()); 127 | 128 | if (range) { 129 | fill_until(t, last, Range::upper()); 130 | t.emplace(Range::upper()); 131 | } 132 | } 133 | 134 | private: 135 | void fill_until(T& t, typename T::value_type last, const typename T::value_type& v) const { 136 | if (t.empty()) { 137 | t.emplace(Range::lower()); 138 | } 139 | for (++last; last < v; ++last) { 140 | t.emplace(last); 141 | } 142 | } 143 | }; 144 | 145 | #undef die_unless 146 | #undef die_outside 147 | 148 | } // namespace cpputil 149 | 150 | #endif 151 | 152 | -------------------------------------------------------------------------------- /include/container/maputil.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_CONTAINER_MAP_UTIL_H 16 | #define CPPUTIL_INCLUDE_CONTAINER_MAP_UTIL_H 17 | 18 | #include 19 | 20 | namespace cpputil { 21 | 22 | template 23 | class CppUtilMap : public Map { 24 | public: 25 | typedef Map map_type; 26 | 27 | class const_key_iterator { 28 | public: 29 | const_key_iterator() = delete; 30 | const_key_iterator(const typename map_type::const_iterator& itr) 31 | : itr_ {itr} { } 32 | 33 | const_key_iterator& operator++() { 34 | itr_++; 35 | return *this; 36 | } 37 | 38 | const typename map_type::key_type& operator*() const { 39 | return itr_->first; 40 | } 41 | 42 | const typename map_type::key_type* operator->() const { 43 | return &(itr_->first); 44 | } 45 | 46 | bool operator==(const const_key_iterator& rhs) const { 47 | return itr_ == rhs.itr_; 48 | } 49 | 50 | bool operator!=(const const_key_iterator& rhs) const { 51 | return itr_ != rhs.itr_; 52 | } 53 | 54 | private: 55 | typename map_type::const_iterator itr_; 56 | }; 57 | 58 | class value_iterator { 59 | public: 60 | value_iterator() = delete; 61 | value_iterator(const typename map_type::iterator& itr) 62 | : itr_ {itr} { } 63 | 64 | value_iterator& operator++() { 65 | itr_++; 66 | return *this; 67 | } 68 | 69 | typename map_type::mapped_type& operator*() const { 70 | return itr_->second; 71 | } 72 | 73 | typename map_type::mapped_type* operator->() const { 74 | return &(itr_->second); 75 | } 76 | 77 | bool operator==(const value_iterator& rhs) const { 78 | return itr_ == rhs.itr_; 79 | } 80 | 81 | bool operator!=(const value_iterator& rhs) const { 82 | return itr_ != rhs.itr_; 83 | } 84 | 85 | private: 86 | typename map_type::iterator itr_; 87 | }; 88 | 89 | class const_value_iterator { 90 | public: 91 | const_value_iterator() = delete; 92 | const_value_iterator(const typename map_type::const_iterator& itr) 93 | : itr_ {itr} { } 94 | 95 | const_value_iterator& operator++() { 96 | itr_++; 97 | return *this; 98 | } 99 | 100 | const typename map_type::mapped_type& operator*() const { 101 | return itr_->second; 102 | } 103 | 104 | const typename map_type::mapped_type* operator->() const { 105 | return &(itr_->second); 106 | } 107 | 108 | bool operator==(const const_value_iterator& rhs) const { 109 | return itr_ == rhs.itr_; 110 | } 111 | 112 | bool operator!=(const const_value_iterator& rhs) const { 113 | return itr_ != rhs.itr_; 114 | } 115 | 116 | private: 117 | typename map_type::const_iterator itr_; 118 | }; 119 | 120 | const_key_iterator key_begin() const { 121 | return const_key_iterator(Map::begin()); 122 | } 123 | 124 | const_key_iterator key_cbegin() const { 125 | return const_key_iterator(Map::cbegin()); 126 | } 127 | 128 | const_key_iterator key_end() const { 129 | return const_key_iterator(Map::end()); 130 | } 131 | 132 | const_key_iterator key_cend() const { 133 | return const_key_iterator(Map::cend()); 134 | } 135 | 136 | value_iterator value_begin() { 137 | return value_iterator(Map::begin()); 138 | } 139 | 140 | const_value_iterator value_begin() const { 141 | return const_value_iterator(Map::begin()); 142 | } 143 | 144 | const_value_iterator value_cbegin() const { 145 | return const_value_iterator(Map::cbegin()); 146 | } 147 | 148 | value_iterator value_end() { 149 | return value_iterator(Map::end()); 150 | } 151 | 152 | const_value_iterator value_end() const { 153 | return const_value_iterator(Map::end()); 154 | } 155 | 156 | const_value_iterator value_cend() const { 157 | return const_value_iterator(Map::cend()); 158 | } 159 | 160 | typename map_type::mapped_type& assert_at(const typename map_type::key_type& k) { 161 | assert(Map::find(k) != Map::end() && "Unrecognized key!"); 162 | return Map::at(k); 163 | } 164 | 165 | const typename map_type::mapped_type& assert_at(const typename map_type::key_type& k) const { 166 | assert(Map::find(k) != Map::end() && "Unrecognized key!"); 167 | return Map::at(k); 168 | } 169 | 170 | typename map_type::size_type assert_erase(const typename map_type::key_type& k) { 171 | assert(Map::find(k) != Map::end() && "Unrecognized key!"); 172 | return Map::erase(k); 173 | } 174 | }; 175 | 176 | } // namespace cpputil 177 | 178 | #endif 179 | -------------------------------------------------------------------------------- /include/serialize/text_reader.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SERIALIZE_TEXT_READER_H 16 | #define CPPUTIL_INCLUDE_SERIALIZE_TEXT_READER_H 17 | 18 | #include 19 | #include 20 | 21 | #include "include/meta/is_stl_map.h" 22 | #include "include/meta/is_stl_pair.h" 23 | #include "include/meta/is_stl_sequence.h" 24 | #include "include/meta/is_stl_set.h" 25 | #include "include/meta/is_stl_string.h" 26 | #include "include/meta/is_stl_tuple.h" 27 | #include "include/serialize/dec_reader.h" 28 | #include "include/serialize/hex_reader.h" 29 | #include "include/serialize/text_style.h" 30 | 31 | namespace cpputil { 32 | 33 | #define die_unless(c) \ 34 | if (is.get() != c) { \ 35 | is.setstate(std::ios::failbit); \ 36 | return; \ 37 | } 38 | 39 | template , typename Enable = void> 40 | struct TextReader; 41 | 42 | template 43 | struct TextReader ::value>::type> { 44 | void operator()(std::istream& is, T& t) const { 45 | if (Style::dec()) { 46 | DecReader()(is, t); 47 | } else { 48 | HexReader()(is, t); 49 | } 50 | } 51 | }; 52 | 53 | template 54 | struct TextReader::value>::type> { 55 | void operator()(std::istream& is, T& t) const { 56 | if (is.peek() == Style::quote()) { 57 | is.get(); 58 | std::getline(is, t, Style::quote()); 59 | } else { 60 | is >> t; 61 | } 62 | } 63 | }; 64 | 65 | template 66 | struct TextReader::value>::type> { 67 | void operator()(std::istream& is, T& t) const { 68 | die_unless(Style::open()); 69 | die_unless(' '); 70 | 71 | TextReader()(is, t.first); 72 | die_unless(' '); 73 | TextReader()(is, t.second); 74 | 75 | die_unless(' '); 76 | die_unless(Style::close()); 77 | } 78 | }; 79 | 80 | template 81 | struct TextReader::value>::type> { 82 | void operator()(std::istream& is, T& t) const { 83 | die_unless(Style::open()); 84 | die_unless(' '); 85 | 86 | t.clear(); 87 | while (is.peek() != Style::close()) { 88 | typename T::value_type v; 89 | TextReader()(is, v); 90 | t.emplace_back(v); 91 | 92 | die_unless(' '); 93 | } 94 | die_unless(Style::close()); 95 | } 96 | }; 97 | 98 | template 99 | struct TextReader::value>::type> { 100 | void operator()(std::istream& is, T& t) const { 101 | die_unless(Style::open()); 102 | die_unless(' '); 103 | 104 | t.clear(); 105 | while (is.peek() != Style::close()) { 106 | typename T::value_type v; 107 | TextReader()(is, v); 108 | t.emplace(v); 109 | 110 | die_unless(' '); 111 | } 112 | die_unless(Style::close()); 113 | } 114 | }; 115 | 116 | template 117 | struct TextReader::value>::type> { 118 | void operator()(std::istream& is, T& t) const { 119 | die_unless(Style::open()); 120 | die_unless(' '); 121 | 122 | t.clear(); 123 | while (is.peek() != Style::close()) { 124 | std::pair::type, typename T::mapped_type> v; 125 | TextReader()(is, v); 126 | t.emplace(v); 127 | 128 | die_unless(' '); 129 | } 130 | die_unless(Style::close()); 131 | } 132 | }; 133 | 134 | template 135 | class TextReader ::value>::type> { 136 | public: 137 | void operator()(std::istream& is, const T& t) const { 138 | die_unless(Style::open()); 139 | Helper::value>()(is, t); 140 | die_unless(' '); 141 | die_unless(Style::close()); 142 | } 143 | 144 | private: 145 | template 146 | struct Helper { 147 | void operator()(std::istream& is, const Tuple& t) { 148 | die_unless(' '); 149 | TextReader::type, Style>()(is, std::get(t)); 150 | Helper < Tuple, Begin + 1, End > ()(is, t); 151 | } 152 | }; 153 | 154 | template 155 | struct Helper { 156 | void operator()(std::istream& is, const Tuple& t) { } 157 | }; 158 | }; 159 | 160 | #undef die_unless 161 | 162 | } // namespace cpputil 163 | 164 | #endif 165 | -------------------------------------------------------------------------------- /include/command_line/folder_arg.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_COMMAND_LINE_FOLDER_ARG_H 16 | #define CPPUTIL_INCLUDE_COMMAND_LINE_FOLDER_ARG_H 17 | 18 | #include 19 | #include 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | #include "include/command_line/arg.h" 29 | #include "include/io/fail.h" 30 | #include "include/serialize/text_reader.h" 31 | #include "include/serialize/text_writer.h" 32 | 33 | namespace cpputil { 34 | 35 | template , typename W = TextWriter> 36 | class FolderArg : public Arg { 37 | public: 38 | virtual ~FolderArg() = default; 39 | 40 | /** Creates and registers a new flag */ 41 | static FolderArg& create(const std::string& opt) { 42 | return *(new FolderArg(opt)); 43 | } 44 | 45 | /** Consumes indices from first alias to next - */ 46 | virtual std::pair read(int argc, char** argv) { 47 | for (const int i : get_appearances(argc, argv)) { 48 | if (i == (argc - 1) || argv[i + 1][0] == '-') { 49 | error("No argument provided!"); 50 | return std::make_pair(i, i); 51 | } 52 | 53 | // some code borrowed from 54 | // http://www.cplusplus.com/forum/beginner/10292/ 55 | DIR *dp = opendir(argv[i+1]); 56 | if(dp == NULL) { 57 | error(folder_error_); 58 | return std::make_pair(i, i+1); 59 | } 60 | 61 | struct dirent *dirp; 62 | while((dirp = readdir(dp))) { 63 | std::string filepath = std::string(argv[i+1]) + "/" + dirp->d_name; 64 | 65 | struct stat filestat; 66 | if (stat(filepath.c_str(), &filestat)) continue; 67 | if (S_ISDIR(filestat.st_mode)) continue; 68 | 69 | std::ifstream ifs(filepath); 70 | if (!ifs.is_open()) { 71 | error(file_error_); 72 | } else { 73 | T temp = T(); 74 | R()(ifs, temp); 75 | 76 | if (failed(ifs)) { 77 | const auto msg = fail_msg(ifs); 78 | error(std::string(dirp->d_name) + " -- " + (msg == "" ? parse_error_ : msg)); 79 | return std::make_pair(i, i+1); 80 | } else { 81 | val_.push_back(temp); 82 | } 83 | } 84 | } 85 | closedir(dp); 86 | 87 | set_provided(); 88 | return std::make_pair(i, i + 1); 89 | } 90 | 91 | return std::make_pair(0, 0); 92 | } 93 | 94 | /** Create a new arg alias (hashes implicit; chars get 1, strings 2) */ 95 | FolderArg& alternate(const std::string& a) { 96 | Arg::alternate(a); 97 | return *this; 98 | } 99 | 100 | /** Reset arg usage */ 101 | FolderArg& usage(const std::string& u) { 102 | Arg::usage(u); 103 | return *this; 104 | } 105 | 106 | /** Reset arg description */ 107 | FolderArg& description(const std::string& d) { 108 | Arg::description(d); 109 | return *this; 110 | } 111 | 112 | /** Resets arg default value */ 113 | FolderArg& default_val(const std::vector& t) { 114 | set_has_default(); 115 | val_ = t; 116 | return *this; 117 | } 118 | 119 | /** Resets the required value. */ 120 | FolderArg& required(const bool val = true) { 121 | Arg::required(val); 122 | return *this; 123 | } 124 | 125 | /** Resets parse error message */ 126 | FolderArg& parse_error(const std::string& pe) { 127 | parse_error_ = pe; 128 | return *this; 129 | } 130 | 131 | /** Resets file error message */ 132 | FolderArg& file_error(const std::string& fe) { 133 | file_error_ = fe; 134 | return *this; 135 | } 136 | 137 | /** Resets folder error message */ 138 | FolderArg & folder_error(const std::string& fe) { 139 | folder_error_ = fe; 140 | return *this; 141 | } 142 | 143 | /** Implicit conversion to underlying type */ 144 | operator std::vector& () { 145 | return val_; 146 | } 147 | 148 | /** Explicit conversion to underlying type */ 149 | std::vector& value() { 150 | return val_; 151 | } 152 | 153 | /** Prints underlying value using writer */ 154 | virtual void debug(std::ostream& os) const { 155 | os << "[" << std::endl; 156 | for (auto& it : val_) { 157 | W()(os, it); 158 | os << std::endl; 159 | } 160 | os << "]"; 161 | } 162 | 163 | private: 164 | /** Underlying value, optionally specified on command line */ 165 | std::vector val_; 166 | /** String to emit if an error occurs during read() */ 167 | std::string parse_error_; 168 | /** String to emit if unable to open source file during read() */ 169 | std::string file_error_; 170 | /** String to emit if unable to open directory */ 171 | std::string folder_error_; 172 | 173 | /** FolderArgs are assigned default constructor values by default */ 174 | FolderArg(const std::string& opt) : 175 | Arg {opt} { 176 | usage(""); 177 | parse_error("Unspecified parse error!"); 178 | file_error("Unable to open one of the files!"); 179 | folder_error("Unable to open drectory!"); 180 | } 181 | }; 182 | 183 | } // namespace cpputil 184 | 185 | #endif 186 | 187 | 188 | -------------------------------------------------------------------------------- /include/meta/is_stl_container.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_META_IS_STL_CONTAINER_H 16 | #define CPPUTIL_INCLUDE_META_IS_STL_CONTAINER_H 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | namespace cpputil { 31 | 32 | template 33 | struct is_stl_container : public std::false_type { }; 34 | 35 | template 36 | struct is_stl_container> : public std::true_type { }; 37 | 38 | template 39 | struct is_stl_container> : public std::true_type { }; 40 | 41 | template 42 | struct is_stl_container> : public std::true_type { }; 43 | 44 | template 45 | struct is_stl_container> : public std::true_type { }; 46 | 47 | template 48 | struct is_stl_container> : public std::true_type { }; 49 | 50 | template 51 | struct is_stl_container> : public std::true_type { }; 52 | 53 | template 54 | struct is_stl_container> : public std::true_type { }; 55 | 56 | template 57 | struct is_stl_container> : public std::true_type { }; 58 | 59 | template 60 | struct is_stl_container> : public std::true_type { }; 61 | 62 | template 63 | struct is_stl_container> : public std::true_type { }; 64 | 65 | template 66 | struct is_stl_container> : public std::true_type { }; 67 | 68 | template 69 | struct is_stl_container> : public std::true_type { }; 70 | 71 | template 72 | struct is_stl_container> : public std::true_type { }; 73 | 74 | template 75 | struct is_stl_container> : public std::true_type { }; 76 | 77 | template 78 | struct is_stl_container> : public std::true_type { }; 79 | 80 | template 81 | struct is_stl_container> : public std::true_type { }; 82 | 83 | template 84 | struct is_stl_container> : public std::true_type { }; 85 | 86 | template 87 | struct is_stl_container> : public std::true_type { }; 88 | 89 | template 90 | struct is_stl_container> : public std::true_type { }; 91 | 92 | template 93 | struct is_stl_container> : public std::true_type { }; 94 | 95 | template 96 | struct is_stl_container> : public std::true_type { }; 97 | 98 | template 99 | struct is_stl_container> : public 100 | std::true_type { }; 101 | 102 | template 103 | struct is_stl_container> : public std::true_type { }; 104 | 105 | template 106 | struct is_stl_container> : public std::true_type { }; 107 | 108 | template 109 | struct is_stl_container> : public 110 | std::true_type { }; 111 | 112 | template 113 | struct is_stl_container> : public 114 | std::true_type { }; 115 | 116 | template 117 | struct is_stl_container> : public std::true_type { }; 118 | 119 | template 120 | struct is_stl_container> : public 121 | std::true_type { }; 122 | 123 | template 124 | struct is_stl_container> : public std::true_type { }; 125 | 126 | template 127 | struct is_stl_container> : public std::true_type { }; 128 | 129 | } // namespace cpputil 130 | 131 | #endif 132 | -------------------------------------------------------------------------------- /include/signal/debug_handler.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_SIGNAL_DEBUG_HANDLER_H 16 | #define CPPUTIL_INCLUDE_SIGNAL_DEBUG_HANDLER_H 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "include/serialize/hex_writer.h" 28 | 29 | namespace cpputil { 30 | 31 | class DebugHandler { 32 | public: 33 | static void install_sigsegv() { 34 | #ifdef DEBUG_SIGNAL_HANDLER 35 | install(SIGSEGV, sigsegv); 36 | #endif 37 | } 38 | 39 | static void install_sigill() { 40 | #ifdef DEBUG_SIGNAL_HANDLER 41 | install(SIGILL, sigill); 42 | #endif 43 | } 44 | 45 | private: 46 | typedef void (*handler_t)(int, siginfo_t*, void*); 47 | 48 | static void install(int signum, handler_t h) { 49 | struct sigaction sa; 50 | memset(&sa, '\0', sizeof(sa)); 51 | sigfillset(&sa.sa_mask); 52 | sa.sa_sigaction = h; 53 | sa.sa_flags = SA_ONSTACK; 54 | 55 | assert(sigaction(signum, &sa, 0) >= 0); 56 | } 57 | 58 | static void write_reg(std::ostream& os, const std::string& reg, uint64_t val) { 59 | os << reg << " = "; 60 | HexWriter()(os, val); 61 | os << std::endl; 62 | } 63 | 64 | static void write_mem(std::ostream& os, unsigned char* addr) { 65 | auto p = (uint64_t*)addr; 66 | 67 | HexWriter()(os, (uint64_t)p); 68 | os << ": "; 69 | 70 | HexWriter()(os, *p); 71 | os << std::endl; 72 | } 73 | 74 | static void write_cpu(std::ostream& os, void* context) { 75 | os << "Register Contents:" << std::endl; 76 | os << std::endl; 77 | 78 | const auto regs = ((ucontext_t*)context)->uc_mcontext.gregs; 79 | 80 | write_reg(os, "%rip", regs[REG_RIP]); 81 | write_reg(os, "%rfl", regs[REG_EFL]); 82 | os << std::endl; 83 | 84 | write_reg(os, "%rax", regs[REG_RAX]); 85 | write_reg(os, "%rdx", regs[REG_RDX]); 86 | write_reg(os, "%rcx", regs[REG_RCX]); 87 | write_reg(os, "%rbx", regs[REG_RBX]); 88 | write_reg(os, "%rdi", regs[REG_RDI]); 89 | write_reg(os, "%rsi", regs[REG_RSI]); 90 | write_reg(os, "%rbp", regs[REG_RBP]); 91 | write_reg(os, "%rsp", regs[REG_RSP]); 92 | write_reg(os, "%r8 ", regs[REG_R8]); 93 | write_reg(os, "%r9 ", regs[REG_R9]); 94 | write_reg(os, "%r10", regs[REG_R10]); 95 | write_reg(os, "%r11", regs[REG_R11]); 96 | write_reg(os, "%r12", regs[REG_R12]); 97 | write_reg(os, "%r13", regs[REG_R13]); 98 | write_reg(os, "%r14", regs[REG_R14]); 99 | write_reg(os, "%r15", regs[REG_R15]); 100 | os << std::endl; 101 | 102 | const auto ip = (unsigned char*)regs[REG_RIP]; 103 | if (ip == 0) { 104 | os << "Unable to print memory in vicinity of %rip" << std::endl; 105 | exit(1); 106 | } 107 | 108 | os << "Preceeding 64 bytes of %rip" << std::endl; 109 | os << std::endl; 110 | 111 | for (unsigned char* i = ip - 64; i < ip; i += 8) { 112 | write_mem(os, i); 113 | } 114 | os << std::endl; 115 | 116 | os << "Next 128 bytes of %rip" << std::endl; 117 | os << std::endl; 118 | 119 | for (unsigned char* i = ip; i < ip + 128; i += 8) { 120 | write_mem(os, i); 121 | } 122 | os << std::endl; 123 | } 124 | 125 | static void sigsegv(int sig, siginfo_t* siginfo, void* context) { 126 | std::cerr << "SIGNAL: SIGSEGV" << std::endl; 127 | std::cerr << std::endl; 128 | 129 | std::cerr << "Address: "; 130 | HexWriter()(std::cerr, (uint64_t) siginfo->si_addr); 131 | std::cerr << std::endl; 132 | 133 | std::cerr << "Cause: "; 134 | if (siginfo->si_code == SEGV_MAPERR) { 135 | std::cerr << "Address not mapped" << std::endl; 136 | } else { 137 | std::cerr << "Bad permissions" << std::endl; 138 | } 139 | std::cerr << std::endl; 140 | 141 | write_cpu(std::cerr, context); 142 | exit(1); 143 | } 144 | 145 | static void sigill(int sig, siginfo_t* siginfo, void* context) { 146 | std::cerr << "SIGNAL: SIGILL" << std::endl; 147 | std::cerr << std::endl; 148 | 149 | std::cerr << "Caused by: "; 150 | switch (siginfo->si_code) { 151 | case ILL_ILLOPC: 152 | std::cerr << "Illegal opcode" << std::endl; 153 | break; 154 | case ILL_ILLOPN: 155 | std::cerr << "Illegal operand" << std::endl; 156 | break; 157 | case ILL_ILLADR: 158 | std::cerr << "Illegal addressing mode" << std::endl; 159 | break; 160 | case ILL_PRVOPC: 161 | std::cerr << "Privileged opcode" << std::endl; 162 | break; 163 | case ILL_PRVREG: 164 | std::cerr << "Privileged register" << std::endl; 165 | break; 166 | case ILL_COPROC: 167 | std::cerr << "Coprocessor error" << std::endl; 168 | break; 169 | case ILL_BADSTK: 170 | std::cerr << "Bad stack" << std::endl; 171 | break; 172 | default: 173 | std::cerr << "" << std::endl; 174 | } 175 | std::cerr << std::endl; 176 | 177 | write_cpu(std::cerr, context); 178 | exit(1); 179 | } 180 | }; 181 | 182 | } // namespace cpputil 183 | 184 | #endif 185 | -------------------------------------------------------------------------------- /include/command_line/arg.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 eric schkufza 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef CPPUTIL_INCLUDE_COMMAND_LINE_ARG_H 16 | #define CPPUTIL_INCLUDE_COMMAND_LINE_ARG_H 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include "include/command_line/arg_registry.h" 27 | #include "include/patterns/singleton.h" 28 | 29 | namespace cpputil { 30 | 31 | class Arg { 32 | public: 33 | virtual ~Arg() = default; 34 | 35 | /** Read an arg, returns range of indices consumed or (0,0) for none */ 36 | virtual std::pair read(int argc, char** argv) = 0; 37 | 38 | /** Returns true if an arg was not provided or read() without error */ 39 | bool good() const { 40 | return error_ == ""; 41 | } 42 | 43 | /** Alias iterator */ 44 | typedef std::set::const_iterator alias_iterator; 45 | 46 | /** First alias */ 47 | alias_iterator alias_begin() const { 48 | return opts_.begin(); 49 | } 50 | 51 | /** Last alias */ 52 | alias_iterator alias_end() const { 53 | return opts_.end(); 54 | } 55 | 56 | /** Appearance index iterator */ 57 | typedef std::vector::const_iterator appearance_iterator; 58 | 59 | /** First appearance in argv */ 60 | appearance_iterator appearance_begin() const { 61 | return appearances_.begin(); 62 | } 63 | 64 | /** Last appearance in argv */ 65 | appearance_iterator appearance_end() const { 66 | return appearances_.end(); 67 | } 68 | 69 | /** Does this arg appear more than once? */ 70 | bool duplicated() const { 71 | return appearances_.size() > 1; 72 | } 73 | 74 | /** Prints arg usage */ 75 | void usage(std::ostream& os) const { 76 | os << usage_; 77 | } 78 | 79 | /** Prints arg description */ 80 | void description(std::ostream& os) const { 81 | os << description_; 82 | } 83 | 84 | /** Prints the cause of an error; valid iff good() == false */ 85 | void error(std::ostream& os) const { 86 | os << error_; 87 | } 88 | 89 | /** Is this argument required? */ 90 | bool is_required() const { 91 | return required_; 92 | } 93 | 94 | /** Has this argument been set (or does it have a default value)? */ 95 | bool has_been_provided() const { 96 | return is_provided_; 97 | } 98 | 99 | /** Does this argument have a default? */ 100 | bool has_default() const { 101 | return has_default_; 102 | } 103 | 104 | /** Indicate that this argument has been set now. */ 105 | void set_provided() { 106 | is_provided_ = true; 107 | } 108 | 109 | /** Prints the value of an arg */ 110 | virtual void debug(std::ostream& os) const = 0; 111 | 112 | protected: 113 | /** Creating a singleton of this class will ensure Args aren't leaked. */ 114 | struct ArgCleanup { 115 | ~ArgCleanup() { 116 | for (auto a : Singleton::get().args_) { 117 | delete a; 118 | } 119 | } 120 | }; 121 | 122 | /** An arg must be assigned at least one alias */ 123 | Arg(const std::string& opt) : is_provided_(false), has_default_(false) { 124 | alternate(opt); 125 | usage(""); 126 | description("(no description provided)"); 127 | error(""); 128 | required(false); 129 | 130 | auto& ar = Singleton::get(); 131 | ar.insert(this); 132 | Singleton::get(); 133 | } 134 | 135 | /** Record indices in argv where arg aliases occur */ 136 | std::vector& get_appearances(int argc, char** argv) { 137 | appearances_.clear(); 138 | for (auto i = 1; i < argc; ++i) { 139 | for (const auto& o : opts_) { 140 | if (o == argv[i]) { 141 | appearances_.push_back(i); 142 | } 143 | } 144 | } 145 | 146 | return appearances_; 147 | } 148 | 149 | /** Create a new arg alias (dashes implicit; chars get 1, strings 2) */ 150 | Arg& alternate(const std::string& a) { 151 | std::string alt = ""; 152 | 153 | assert(a.length() != 0 && "Cannot register arg with no name!"); 154 | assert((a.length() != 1 || a != "-") && "Cannot register arg with no name!"); 155 | assert((a.length() != 2 || a != "--") && "Cannot register arg with no name!"); 156 | 157 | if (a.length() == 1) { 158 | alt = std::string("-") + a; 159 | } else { 160 | alt = std::string("--") + a; 161 | } 162 | 163 | auto& ar = Singleton::get(); 164 | for (auto i = ar.args_.begin(), ie = ar.args_.end(); i != ie; ++i) { 165 | if ((*i)->opts_.find(alt) != (*i)->opts_.end()) { 166 | assert(false && "Cannot register arg with pre-existing name!"); 167 | } 168 | } 169 | 170 | opts_.insert(alt); 171 | 172 | return *this; 173 | } 174 | 175 | /** Reset arg usage */ 176 | void usage(const std::string& u) { 177 | usage_ = u; 178 | } 179 | 180 | /** Reset arg description */ 181 | void description(const std::string& d) { 182 | description_ = d; 183 | } 184 | 185 | /** Places a value in error_ to indicate trouble of some kind */ 186 | void error(const std::string& error) { 187 | error_ = error; 188 | } 189 | 190 | /** Reset the required argument. */ 191 | void required(const bool val = true) { 192 | required_ = val; 193 | } 194 | 195 | /** Reset whether this argument has a default. */ 196 | void set_has_default(const bool val = true) { 197 | has_default_ = val; 198 | } 199 | 200 | private: 201 | /** Aliases for this arg ,ie: "-h --help" */ 202 | std::set opts_; 203 | /** Indices of alias appearances in argv */ 204 | std::vector appearances_; 205 | 206 | /** Example usage text, ie: "" */ 207 | std::string usage_; 208 | /** What does this arg do, ie: "Annealing constant" */ 209 | std::string description_; 210 | /** A non-empty value here indicates that a survivable error occurred */ 211 | std::string error_; 212 | /** Is this argument mandatory? */ 213 | bool required_; 214 | /** Has this argument been provided in some way? */ 215 | bool is_provided_; 216 | /** Does this argument have a default? */ 217 | bool has_default_; 218 | }; 219 | 220 | } // namespace cpputil 221 | 222 | #endif 223 | 224 | --------------------------------------------------------------------------------