├── .clang-format ├── .gitignore ├── LICENSE ├── Makefile ├── Makefile.base ├── README.md ├── RUNME ├── build └── common.sh ├── c_plus_plus_serializer.h └── examples ├── .clang-format ├── basic_example.cpp ├── bitfields_in_class_example.cpp ├── container_example.cpp ├── custom_class_example.cpp ├── deque_example.cpp ├── hexdump.cpp ├── hexdump.h ├── main.cpp ├── map_custom_class_example.cpp ├── map_example.cpp ├── map_string_to_list_of_strings_example.cpp ├── multiset_example.cpp ├── quicklz.cpp ├── quicklz.h ├── raw_memory.cpp ├── set_example.cpp ├── template_class_example.cpp ├── unordered_map_example.cpp └── zipped_container_example.cpp /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | # BasedOnStyle: LLVM 4 | AccessModifierOffset: -2 5 | AlignAfterOpenBracket: Align 6 | AlignConsecutiveMacros: true 7 | AlignConsecutiveAssignments: true 8 | AlignConsecutiveBitFields: true 9 | AlignConsecutiveDeclarations: true 10 | AlignEscapedNewlines: Right 11 | AlignOperands: Align 12 | AlignTrailingComments: true 13 | AllowAllArgumentsOnNextLine: true 14 | AllowAllConstructorInitializersOnNextLine: true 15 | AllowAllParametersOfDeclarationOnNextLine: true 16 | AllowShortEnumsOnASingleLine: true 17 | AllowShortBlocksOnASingleLine: Never 18 | AllowShortCaseLabelsOnASingleLine: true 19 | AllowShortFunctionsOnASingleLine: All 20 | AllowShortLambdasOnASingleLine: All 21 | AllowShortIfStatementsOnASingleLine: Never 22 | AllowShortLoopsOnASingleLine: false 23 | AlwaysBreakAfterDefinitionReturnType: None 24 | AlwaysBreakAfterReturnType: None 25 | AlwaysBreakBeforeMultilineStrings: true 26 | AlwaysBreakTemplateDeclarations: MultiLine 27 | AttributeMacros: 28 | - __capability 29 | BinPackArguments: true 30 | BinPackParameters: true 31 | BraceWrapping: 32 | AfterCaseLabel: true 33 | AfterClass: true 34 | AfterControlStatement: Never 35 | AfterEnum: true 36 | AfterFunction: true 37 | AfterNamespace: true 38 | AfterObjCDeclaration: true 39 | AfterStruct: true 40 | AfterUnion: true 41 | AfterExternBlock: true 42 | BeforeCatch: true 43 | BeforeElse: true 44 | BeforeLambdaBody: true 45 | BeforeWhile: true 46 | IndentBraces: true 47 | SplitEmptyFunction: true 48 | SplitEmptyRecord: true 49 | SplitEmptyNamespace: true 50 | BreakBeforeBinaryOperators: None 51 | BreakBeforeConceptDeclarations: true 52 | BreakBeforeBraces: Linux 53 | BreakBeforeInheritanceComma: false 54 | BreakInheritanceList: BeforeColon 55 | BreakBeforeTernaryOperators: true 56 | BreakConstructorInitializersBeforeComma: false 57 | BreakConstructorInitializers: BeforeColon 58 | BreakAfterJavaFieldAnnotations: false 59 | BreakStringLiterals: true 60 | ColumnLimit: 118 61 | CommentPragmas: '^ IWYU pragma:' 62 | CompactNamespaces: false 63 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 64 | ConstructorInitializerIndentWidth: 4 65 | ContinuationIndentWidth: 4 66 | Cpp11BracedListStyle: true 67 | DeriveLineEnding: true 68 | DerivePointerAlignment: false 69 | DisableFormat: false 70 | EmptyLineBeforeAccessModifier: LogicalBlock 71 | ExperimentalAutoDetectBinPacking: false 72 | FixNamespaceComments: true 73 | ForEachMacros: 74 | - foreach 75 | - Q_FOREACH 76 | - BOOST_FOREACH 77 | StatementAttributeLikeMacros: 78 | - Q_EMIT 79 | IncludeBlocks: Preserve 80 | IncludeCategories: 81 | - Regex: '^"(llvm|llvm-c|clang|clang-c)/' 82 | Priority: 2 83 | SortPriority: 0 84 | CaseSensitive: false 85 | - Regex: '^(<|"(gtest|gmock|isl|json)/)' 86 | Priority: 3 87 | SortPriority: 0 88 | CaseSensitive: false 89 | - Regex: '.*' 90 | Priority: 1 91 | SortPriority: 0 92 | CaseSensitive: false 93 | IncludeIsMainRegex: '(Test)?$' 94 | IncludeIsMainSourceRegex: '' 95 | IndentCaseLabels: true 96 | IndentCaseBlocks: true 97 | IndentGotoLabels: true 98 | IndentPPDirectives: None 99 | IndentExternBlock: AfterExternBlock 100 | IndentRequires: false 101 | IndentWidth: 2 102 | IndentWrappedFunctionNames: true 103 | InsertTrailingCommas: None 104 | JavaScriptQuotes: Leave 105 | JavaScriptWrapImports: true 106 | KeepEmptyLinesAtTheStartOfBlocks: true 107 | MacroBlockBegin: '' 108 | MacroBlockEnd: '' 109 | MaxEmptyLinesToKeep: 1 110 | NamespaceIndentation: None 111 | ObjCBinPackProtocolList: Auto 112 | ObjCBlockIndentWidth: 2 113 | ObjCBreakBeforeNestedBlockParam: true 114 | ObjCSpaceAfterProperty: false 115 | ObjCSpaceBeforeProtocolList: true 116 | PenaltyBreakAssignment: 2 117 | PenaltyBreakBeforeFirstCallParameter: 19 118 | PenaltyBreakComment: 300 119 | PenaltyBreakFirstLessLess: 120 120 | PenaltyBreakString: 1000 121 | PenaltyBreakTemplateDeclaration: 10 122 | PenaltyExcessCharacter: 1000000 123 | PenaltyReturnTypeOnItsOwnLine: 60 124 | PenaltyIndentedWhitespace: 0 125 | PointerAlignment: Right 126 | ReflowComments: true 127 | SortIncludes: true 128 | SortJavaStaticImport: Before 129 | SortUsingDeclarations: true 130 | SpaceAfterCStyleCast: true 131 | SpaceAfterLogicalNot: true 132 | SpaceAfterTemplateKeyword: true 133 | SpaceBeforeAssignmentOperators: true 134 | SpaceBeforeCaseColon: true 135 | SpaceBeforeCpp11BracedList: true 136 | SpaceBeforeCtorInitializerColon: true 137 | SpaceBeforeInheritanceColon: true 138 | SpaceBeforeParens: ControlStatements 139 | SpaceAroundPointerQualifiers: Default 140 | SpaceBeforeRangeBasedForLoopColon: true 141 | SpaceInEmptyBlock: false 142 | SpaceInEmptyParentheses: false 143 | SpacesBeforeTrailingComments: 1 144 | SpacesInAngles: true 145 | SpacesInConditionalStatement: false 146 | SpacesInContainerLiterals: true 147 | SpacesInCStyleCastParentheses: false 148 | SpacesInParentheses: false 149 | SpacesInSquareBrackets: true 150 | SpaceBeforeSquareBrackets: false 151 | BitFieldColonSpacing: Both 152 | Standard: Latest 153 | StatementMacros: 154 | - Q_UNUSED 155 | - QT_REQUIRE_VERSION 156 | TabWidth: 2 157 | UseCRLF: false 158 | UseTab: Never 159 | WhitespaceSensitiveMacros: 160 | - STRINGIZE 161 | - PP_STRINGIZE 162 | - BOOST_PP_STRINGIZE 163 | - NS_SWIFT_NAME 164 | - CF_SWIFT_NAME 165 | ... 166 | 167 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | basic_example.bin 35 | c_plus_plus_serializer 36 | container_example.bin 37 | custom_bitset_class.bin 38 | custom_class.bin 39 | deque_of_strings.bin 40 | map_of_custom_class.bin 41 | map_of_string_to_list_of_strings.bin 42 | map_of_strings.bin 43 | multiset_of_strings.bin 44 | raw_memory_example.bin 45 | set_of_strings.bin 46 | template_class.bin 47 | unordered_map_of_strings.bin 48 | zipper_container_example.bin 49 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the build used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | COMPILER_FLAGS=-Werror -g -ggdb3 -O3 -fstack-protector-all -I . -I examples # AUTOGEN 2 | 3 | CLANG_COMPILER_WARNINGS=-Wall # AUTOGEN 4 | GCC_COMPILER_WARNINGS=-Wall # AUTOGEN 5 | GXX_COMPILER_WARNINGS=-Wall # AUTOGEN 6 | COMPILER_WARNINGS=$(GCC_COMPILER_WARNINGS) # AUTOGEN 7 | COMPILER_WARNINGS=$(GXX_COMPILER_WARNINGS) # AUTOGEN 8 | COMPILER_WARNINGS=$(CLANG_COMPILER_WARNINGS) # AUTOGEN 9 | CXX=clang++ # AUTOGEN 10 | 11 | EXE= # AUTOGEN 12 | LDLIBS= # AUTOGEN 13 | CFLAGS=$(COMPILER_FLAGS) $(COMPILER_WARNINGS) # AUTOGEN 14 | NAME=c_plus_plus_serializer 15 | OBJDIR=.o 16 | 17 | TARGET_OBJECTS=\ 18 | $(OBJDIR)/main.o \ 19 | $(OBJDIR)/basic_example.o \ 20 | $(OBJDIR)/bitfields_in_class_example.o \ 21 | $(OBJDIR)/container_example.o \ 22 | $(OBJDIR)/custom_class_example.o \ 23 | $(OBJDIR)/deque_example.o \ 24 | $(OBJDIR)/hexdump.o \ 25 | $(OBJDIR)/map_custom_class_example.o \ 26 | $(OBJDIR)/map_example.o \ 27 | $(OBJDIR)/map_string_to_list_of_strings_example.o \ 28 | $(OBJDIR)/multiset_example.o \ 29 | $(OBJDIR)/quicklz.o \ 30 | $(OBJDIR)/raw_memory.o \ 31 | $(OBJDIR)/set_example.o \ 32 | $(OBJDIR)/template_class_example.o \ 33 | $(OBJDIR)/unordered_map_example.o \ 34 | $(OBJDIR)/zipped_container_example.o \ 35 | 36 | EXTRA_CFLAGS=-std=c++11 37 | #EXTRA_CFLAGS+=-DDEBUG_C_PLUS_PLUS_SERIALIZER 38 | #EXTRA_CFLAGS+=-DUSE_SIZE_T 39 | 40 | $(OBJDIR)/%.o: examples/%.cpp 41 | $(shell echo clang-format -i $<) 42 | @echo $(CXX) $(EXTRA_CFLAGS) $(CFLAGS) -c -o $@ $< 43 | @$(CXX) $(EXTRA_CFLAGS) $(CFLAGS) -c -o $@ $< 44 | 45 | # 46 | # link 47 | # 48 | TARGET=$(NAME)$(EXE) 49 | $(TARGET): $(TARGET_OBJECTS) 50 | $(CXX) $(TARGET_OBJECTS) $(LDLIBS) -o $(TARGET) 51 | 52 | # 53 | # To force clean and avoid "up to date" warning. 54 | # 55 | .PHONY: clean 56 | .PHONY: clobber 57 | 58 | clean: 59 | rm -rf $(TARGET) $(OBJDIR)/*.o 60 | 61 | clobber: clean 62 | mkdir -p $(OBJDIR) 63 | 64 | all: $(TARGET) 65 | 66 | format: 67 | git diff -U0 HEAD^ | clang-format-diff -i -p1 68 | -------------------------------------------------------------------------------- /Makefile.base: -------------------------------------------------------------------------------- 1 | NAME=c_plus_plus_serializer 2 | OBJDIR=.o 3 | 4 | TARGET_OBJECTS=\ 5 | $(OBJDIR)/main.o \ 6 | $(OBJDIR)/basic_example.o \ 7 | $(OBJDIR)/bitfields_in_class_example.o \ 8 | $(OBJDIR)/container_example.o \ 9 | $(OBJDIR)/custom_class_example.o \ 10 | $(OBJDIR)/deque_example.o \ 11 | $(OBJDIR)/hexdump.o \ 12 | $(OBJDIR)/map_custom_class_example.o \ 13 | $(OBJDIR)/map_example.o \ 14 | $(OBJDIR)/map_string_to_list_of_strings_example.o \ 15 | $(OBJDIR)/multiset_example.o \ 16 | $(OBJDIR)/quicklz.o \ 17 | $(OBJDIR)/raw_memory.o \ 18 | $(OBJDIR)/set_example.o \ 19 | $(OBJDIR)/template_class_example.o \ 20 | $(OBJDIR)/unordered_map_example.o \ 21 | $(OBJDIR)/zipped_container_example.o \ 22 | 23 | EXTRA_CFLAGS=-std=c++11 24 | #EXTRA_CFLAGS+=-DDEBUG_C_PLUS_PLUS_SERIALIZER 25 | #EXTRA_CFLAGS+=-DUSE_SIZE_T 26 | 27 | $(OBJDIR)/%.o: examples/%.cpp 28 | $(shell echo clang-format -i $<) 29 | @echo $(CXX) $(EXTRA_CFLAGS) $(CFLAGS) -c -o $@ $< 30 | @$(CXX) $(EXTRA_CFLAGS) $(CFLAGS) -c -o $@ $< 31 | 32 | # 33 | # link 34 | # 35 | TARGET=$(NAME)$(EXE) 36 | $(TARGET): $(TARGET_OBJECTS) 37 | $(CXX) $(TARGET_OBJECTS) $(LDLIBS) -o $(TARGET) 38 | 39 | # 40 | # To force clean and avoid "up to date" warning. 41 | # 42 | .PHONY: clean 43 | .PHONY: clobber 44 | 45 | clean: 46 | rm -rf $(TARGET) $(OBJDIR)/*.o 47 | 48 | clobber: clean 49 | mkdir -p $(OBJDIR) 50 | 51 | all: $(TARGET) 52 | 53 | format: 54 | git diff -U0 HEAD^ | clang-format-diff -i -p1 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Simple C++ 11 header-only serializer 2 | ==================================== 3 | 4 | Have you ever wanted a really minimal header-only C++11 serializer? Of course 5 | you have! Having tried some other implementations I found them too hard to 6 | maintain and grok when they inevitably failed to compile under the latest 7 | g++ version. Hopefully this one is simple enough to avoid such issues. All you 8 | need to do is add 9 | 10 |
 11 | #include "c_plus_plus_serializer.h"
 12 | 
13 | 14 | in your project for any files that need to do serialization. 15 | 16 | The data is saved in raw binary format, hence this is only loadable on the 17 | same architecture that it was saved in. 18 | 19 | Credits 20 | ======= 21 | 22 | This work was based on some of the ideas in [this link](https://stackoverflow.com/questions/1559254/are-there-binary-memory-streams-in-c), specifically, 23 | those by Samuel Powell. 24 | 25 | Tested types 26 | ============ 27 | 28 | - POD types (char, wchar_t, int, float, double etc...) 29 | - std::array (multidimensional works too) 30 | - std::list 31 | - std::map 32 | - std::deque 33 | - std::multiset 34 | - std::pair 35 | - std::set 36 | - std::string, std::wstring 37 | - std::unordered_map 38 | - std::vector 39 | - custom class 40 | - nested types e.g. std::map< std::string, std::list> 41 | 42 | POD serialization 43 | ================= 44 | 45 | ```C++ 46 | #include "c_plus_plus_serializer.h" 47 | 48 | static void serialize (std::ofstream out) 49 | { 50 | char a = 42; 51 | unsigned short b = 65535; 52 | int c = 123456; 53 | float d = std::numeric_limits::max(); 54 | double e = std::numeric_limits::max(); 55 | std::string f("hello"); 56 | wchar_t g = L'💩'; 57 | std::wstring h(L"wide string 💩"); 58 | 59 | out << bits(a) << bits(b) << bits(c) << bits(d); 60 | out << bits(e) << bits(f) << bits(g) << bits(h); 61 | } 62 | 63 | static void deserialize (std::ifstream in) 64 | { 65 | char a; 66 | unsigned short b; 67 | int c; 68 | float d; 69 | double e; 70 | std::string f; 71 | wchar_t g; 72 | std::wstring h; 73 | 74 | in >> bits(a) >> bits(b) >> bits(c) >> bits(d); 75 | in >> bits(e) >> bits(f) >> bits(g) >> bits(h); 76 | } 77 | ``` 78 | 79 | Container serialization 80 | ======================= 81 | 82 | ```C++ 83 | #include "c_plus_plus_serializer.h" 84 | 85 | static void serialize (std::ofstream out) 86 | { 87 | std::initializer_list< std::string > d1 = {"vec-elem1", "vec-elem2"}; 88 | std::vector< std::string > a(d1); 89 | 90 | std::initializer_list< std::string > d2 = {"list-elem1", "list-elem2"}; 91 | std::list< std::string > b(d2); 92 | 93 | std::array< std::string, 2> c = {"arr-elem1", "arr-elem2"}; 94 | 95 | // 96 | // 2d array 97 | // 98 | std::array< std::array, 3> d; 99 | d[0][0] = '0'; d[0][1] = '1'; 100 | d[1][0] = '2'; d[1][1] = '3'; 101 | d[2][0] = '4'; d[2][1] = '5'; 102 | 103 | // 104 | // 3d array 105 | // 106 | std::array< std::array< std::array, 3>, 4> ddd; 107 | ddd[0][0][0] = 'a'; ddd[0][0][1] = 'b'; 108 | ddd[0][1][0] = 'c'; ddd[0][1][1] = 'd'; 109 | ddd[0][2][0] = 'e'; ddd[0][2][1] = 'f'; 110 | ddd[1][0][0] = 'g'; ddd[1][0][1] = 'h'; 111 | ddd[1][1][0] = 'i'; ddd[1][1][1] = 'j'; 112 | ddd[1][2][0] = 'k'; ddd[1][2][1] = 'l'; 113 | ddd[2][0][0] = 'm'; ddd[2][0][1] = 'n'; 114 | ddd[2][1][0] = 'o'; ddd[2][1][1] = 'p'; 115 | ddd[2][2][0] = 'q'; ddd[2][2][1] = 'r'; 116 | ddd[3][0][0] = 's'; ddd[3][0][1] = 't'; 117 | ddd[3][1][0] = 'u'; ddd[3][1][1] = 'v'; 118 | ddd[3][2][0] = 'w'; ddd[3][2][1] = 'x'; 119 | 120 | out << bits(a) << bits(b) << bits(c) << bits(dd) << bits(ddd); 121 | } 122 | 123 | static void deserialize (std::ifstream in) 124 | { 125 | std::string f; 126 | std::vector< std::string > a; 127 | std::list< std::string > b; 128 | std::array< std::string, 2> c; 129 | std::array< std::array, 3> d; 130 | std::array< std::array< std::array, 3>, 4> ddd; 131 | 132 | in >> bits(a) >> bits(b) >> bits(c) >> bits(dd) >> bits(ddd); 133 | } 134 | ``` 135 | 136 | Map serialization 137 | ================= 138 | 139 | ```C++ 140 | static void serialize (std::ofstream out) 141 | { 142 | std::map< std::string, std::string > m; 143 | m.insert(std::make_pair(std::string("key1"), std::string("value1"))); 144 | m.insert(std::make_pair(std::string("key2"), std::string("value2"))); 145 | m.insert(std::make_pair(std::string("key3"), std::string("value3"))); 146 | m.insert(std::make_pair(std::string("key4"), std::string("value4"))); 147 | out << bits(m); 148 | } 149 | 150 | static void deserialize (std::ifstream in) 151 | { 152 | std::map< std::string, std::string > m; 153 | in >> bits(m); 154 | } 155 | ``` 156 | 157 | Unordered map serialization 158 | =========================== 159 | 160 | ```C++ 161 | static void serialize (std::ofstream out) 162 | { 163 | std::unordered_map< std::string, std::string > m; 164 | m.insert(std::make_pair(std::string("key1"), std::string("value1"))); 165 | m.insert(std::make_pair(std::string("key2"), std::string("value2"))); 166 | m.insert(std::make_pair(std::string("key3"), std::string("value3"))); 167 | m.insert(std::make_pair(std::string("key4"), std::string("value4"))); 168 | out << bits(m); 169 | } 170 | 171 | static void deserialize (std::ifstream in) 172 | { 173 | std::unordered_map< std::string, std::string > m; 174 | in >> bits(m); 175 | } 176 | ``` 177 | 178 | std::map< std::string, std::list > example 179 | ======================================== 180 | 181 | ```C++ 182 | static void serialize (std::ofstream out) 183 | { 184 | std::map< std::string, std::list< std::string > > m; 185 | 186 | std::initializer_list< std::string > L1 = {"list-elem1", "list-elem2"}; 187 | std::list< std::string > l1(L1); 188 | std::initializer_list< std::string > L2 = {"list-elem3", "list-elem4"}; 189 | std::list< std::string > l2(L2); 190 | 191 | m.insert(std::make_pair(std::string("key1"), l1)); 192 | m.insert(std::make_pair(std::string("key2"), l2)); 193 | 194 | out << bits(m); 195 | } 196 | 197 | static void deserialize (std::ifstream in) 198 | { 199 | std::map< std::string, std::list< std::string > > m; 200 | 201 | in >> bits(m); 202 | } 203 | ``` 204 | 205 | Set serialization 206 | ================= 207 | 208 | ```C++ 209 | static void serialize (std::ofstream out) 210 | { 211 | std::set< std::string > m; 212 | m.insert(std::string("key1")); 213 | m.insert(std::string("key2")); 214 | m.insert(std::string("key3")); 215 | m.insert(std::string("key4")); 216 | out << bits(m); 217 | } 218 | 219 | static void deserialize (std::ifstream in) 220 | { 221 | std::set< std::string > m; 222 | in >> bits(m); 223 | } 224 | ``` 225 | 226 | Multiset serialization 227 | ====================== 228 | 229 | ```C++ 230 | static void serialize (std::ofstream out) 231 | { 232 | std::multiset< std::string > m; 233 | m.insert(std::string("key1")); 234 | m.insert(std::string("key1")); 235 | m.insert(std::string("key1")); 236 | m.insert(std::string("key1")); 237 | m.insert(std::string("key2")); 238 | m.insert(std::string("key3")); 239 | m.insert(std::string("key4")); 240 | out << bits(m); 241 | } 242 | 243 | static void deserialize (std::ifstream in) 244 | { 245 | std::multiset< std::string > m; 246 | in >> bits(m); 247 | } 248 | ``` 249 | 250 | Deque serialization 251 | =================== 252 | 253 | ```C++ 254 | static void serialize (std::ofstream out) 255 | { 256 | std::deque< std::string > m; 257 | m.push_front(std::string("key1")); 258 | m.push_front(std::string("key2")); 259 | m.push_back(std::string("key3")); 260 | m.push_back(std::string("key4")); 261 | out << bits(m); 262 | } 263 | 264 | static void deserialize (std::ifstream in) 265 | { 266 | std::deque< std::string > m; 267 | in >> bits(m); 268 | } 269 | ``` 270 | 271 | User defined class serialization 272 | ================================ 273 | 274 | ```C++ 275 | class Custom { 276 | public: 277 | int a; 278 | std::string b; 279 | std::vector< std::string > c; 280 | 281 | friend std::ostream& operator<<(std::ostream &out, 282 | Bits my) 283 | { 284 | out << bits(my.t.a) << bits(my.t.b) << bits(my.t.c); 285 | return (out); 286 | } 287 | 288 | friend std::istream& operator>>(std::istream &in, 289 | Bits my) 290 | { 291 | in >> bits(my.t.a) >> bits(my.t.b) >> bits(my.t.c); 292 | return (in); 293 | } 294 | }; 295 | ``` 296 | 297 | Serializing a custom template class 298 | =================================== 299 | 300 | ```C++ 301 | template class MyPoint 302 | { 303 | public: 304 | T x {}; 305 | T y {}; 306 | 307 | MyPoint (void) : x(0), y(0) {}; 308 | 309 | MyPoint (T x, T y) : x(x), y(y) { } 310 | 311 | friend std::ostream& operator<<(std::ostream &out, 312 | Bits const my) 313 | { 314 | out << bits(my.t.x) << bits(my.t.y); 315 | return (out); 316 | } 317 | 318 | friend std::istream& operator>>(std::istream &in, Bits my) 319 | { 320 | in >> bits(my.t.x) >> bits(my.t.y); 321 | return (in); 322 | } 323 | 324 | friend std::ostream& operator << (std::ostream &out, const MyPoint &my) 325 | { 326 | out << "(" << my.x << ", " << my.y << ")"; 327 | return (out); 328 | } 329 | }; 330 | 331 | typedef MyPoint IntPoint; 332 | typedef MyPoint FloatPoint; 333 | typedef MyPoint DoublePoint; 334 | 335 | static void serialize (std::ofstream out) 336 | { 337 | out << bits(IntPoint(1, 2)); 338 | out << bits(FloatPoint(1.1, 2.2)); 339 | out << bits(DoublePoint(3.3, 4.4)); 340 | } 341 | 342 | static void deserialize (std::ifstream in) 343 | { 344 | IntPoint a; 345 | FloatPoint b; 346 | DoublePoint c; 347 | 348 | in >> bits(a); 349 | in >> bits(b); 350 | in >> bits(c); 351 | } 352 | ``` 353 | 354 | User defined class serialization (more complex one, a map of classes) 355 | ===================================================================== 356 | 357 | ```C++ 358 | class Custom { 359 | public: 360 | int a; 361 | std::string b; 362 | std::vector< std::string > c; 363 | 364 | friend std::ostream& operator<<(std::ostream &out, 365 | Bits my) 366 | { 367 | out << bits(my.t.a) << bits(my.t.b) << bits(my.t.c); 368 | return (out); 369 | } 370 | 371 | friend std::istream& operator>>(std::istream &in, 372 | Bits my) 373 | { 374 | in >> bits(my.t.a) >> bits(my.t.b) >> bits(my.t.c); 375 | return (in); 376 | } 377 | 378 | friend std::ostream& operator<<(std::ostream &out, 379 | class Custom &my) 380 | { 381 | out << "a:" << my.a << " b:" << my.b; 382 | 383 | out << " c:[" << my.c.size() << " elems]:"; 384 | for (auto v : my.c) { 385 | out << v << " "; 386 | } 387 | out << std::endl; 388 | 389 | return (out); 390 | } 391 | }; 392 | 393 | static void serialize (...) 394 | { 395 | std::map< std::string, class Custom > m; 396 | 397 | auto c1 = Custom(); 398 | c1.a = 1; 399 | c1.b = "hello"; 400 | std::initializer_list< std::string > L1 = {"vec-elem1", "vec-elem2"}; 401 | std::vector< std::string > l1(L1); 402 | c1.c = l1; 403 | 404 | auto c2 = Custom(); 405 | c2.a = 2; 406 | c2.b = "there"; 407 | std::initializer_list< std::string > L2 = {"vec-elem3", "vec-elem4"}; 408 | std::vector< std::string > l2(L2); 409 | c2.c = l2; 410 | 411 | m.insert(std::make_pair(std::string("key1"), c1)); 412 | m.insert(std::make_pair(std::string("key2"), c2)); 413 | 414 | out << bits(m); 415 | } 416 | 417 | static void deserialize (...) 418 | { 419 | std::cout << "read from " << filename << std::endl; 420 | std::ifstream in(filename); 421 | 422 | std::map< std::string, class Custom > m; 423 | 424 | in >> bits(m); 425 | std::cout << std::endl; 426 | 427 | std::cout << "m = " << m.size() << " list-elems { " << std::endl; 428 | for (auto i : m) { 429 | std::cout << " [" << i.first << "] = " << i.second; 430 | } 431 | std::cout << "}" << std::endl; 432 | } 433 | ``` 434 | 435 | Bitfield serialization (C and C++ style) 436 | ======================================== 437 | 438 | ```C++ 439 | class BitsetClass { 440 | public: 441 | std::bitset<1> a; 442 | std::bitset<2> b; 443 | std::bitset<3> c; 444 | 445 | unsigned int d:1; // need c++20 for default initializers for bitfields 446 | unsigned int e:2; 447 | unsigned int f:3; 448 | BitsetClass(void) { d = 0; e = 0; f = 0; } 449 | 450 | friend std::ostream& operator<<(std::ostream &out, 451 | Bits const my) 452 | { 453 | out << bits(my.t.a); 454 | out << bits(my.t.b); 455 | out << bits(my.t.c); 456 | 457 | std::bitset<6> s(my.t.d | my.t.e << 1 | my.t.f << 3); 458 | out << bits(s); 459 | 460 | return (out); 461 | } 462 | 463 | friend std::istream& operator>>(std::istream &in, 464 | Bits my) 465 | { 466 | std::bitset<1> a; 467 | in >> bits(a); 468 | my.t.a = a; 469 | 470 | in >> bits(my.t.b); 471 | in >> bits(my.t.c); 472 | std::bitset<6> s; 473 | in >> bits(s); 474 | 475 | unsigned long raw_bits = static_cast(s.to_ulong()); 476 | my.t.d = raw_bits & 0b000001; 477 | my.t.e = (raw_bits & 0b000110) >> 1; 478 | my.t.f = (raw_bits & 0b111000) >> 3; 479 | 480 | return (in); 481 | } 482 | }; 483 | ``` 484 | 485 | Raw memory 486 | ========== 487 | ```C++ 488 | #include "hexdump.h" 489 | 490 | auto elems = 128; 491 | 492 | static void serialize (std::ofstream out) 493 | { 494 | auto a = new char[elems]; 495 | for (auto i = 0; i < elems; i++) { 496 | a[i] = i; 497 | } 498 | out << bits(a); 499 | } 500 | 501 | static void deserialize (std::ifstream in) 502 | { 503 | auto a = new char[elems]; 504 | in >> bits(a); 505 | 506 | hexdump(a, elems); 507 | } 508 | ``` 509 | 510 | Output: 511 | 512 | ```C++ 513 | 128 bytes: 514 | 0000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................| 515 | 0010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f |................| 516 | 0020 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f | !"#$%&'()*+,-./| 517 | 0030 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f |0123456789:;<=>?| 518 | 0040 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f |@ABCDEFGHIJKLMNO| 519 | 0050 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f |PQRSTUVWXYZ[\]^_| 520 | 0060 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f |`abcdefghijklmno| 521 | 0070 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f |pqrstuvwxyz{|}~.| 522 | ``` 523 | 524 | Compression 525 | =========== 526 | 527 | For compression, look at the following which uses [quicklz](http://www.quicklz.com/download.html) 528 | 529 |
530 |     zipped_container_example.cpp
531 | 
532 | 533 | Building 534 | ======== 535 | 536 | Do 537 | 538 |
539 |     sh ./RUNME
540 | 
541 | 542 | Or, if that fails, manual build: 543 | 544 |
545 |     c++ -std=c++11 -Werror -O2 -Wall -c -o .o/main.o main.cpp
546 |     c++ .o/main.o -o c_plus_plus_serializer
547 | 
548 | 549 | To test: 550 | 551 |
552 |     ./c_plus_plus_serializer
553 | 
554 | 555 | To debug, uncomment this line in Makefile.base, and then rerun RUNME: 556 | 557 |
558 | #EXTRA_CFLAGS += -DDEBUG_C_PLUS_PLUS_SERIALIZER
559 | 
560 | 561 | To use 64-bit size_t when saving vectors, uncomment in Makefile.base and then 562 | rerun RUNME: 563 | 564 |
565 | #EXTRA_CFLAGS += -DUSE_SIZE_T
566 | 
567 | 568 | Note, no sanity checking is performed on the data when it is read back in, so 569 | make sure your serializer and deserializers match perfectly. 570 | -------------------------------------------------------------------------------- /RUNME: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Copyright (C) 2014 Neil McGill 4 | # 5 | # This software is free software; you can redistribute it and/or 6 | # modify it under the terms of the GNU Library General Public 7 | # License as published by the Free Software Foundation; either 8 | # version 2 of the License, or (at your option) any later version. 9 | # 10 | # This software is distributed in the hope that it will be fun, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | # Library General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU Library General Public 16 | # License along with this software; if not, write to the Free 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | # 19 | 20 | . ./build/common.sh 21 | 22 | help() 23 | { 24 | cat << %% 25 | Build options: 26 | 27 | Usage: $0 [options] 28 | 29 | [--gcc] Prefer g++ over clang++ 30 | [--clang] Prefer clang++ 31 | %% 32 | } 33 | 34 | OPT_GCC= 35 | OPT_CLANG= 36 | 37 | read_opts() 38 | { 39 | while [ "$#" -ne 0 ]; 40 | do 41 | local option=$1 42 | 43 | case $option in 44 | -*gcc) 45 | OPT_GCC="yes" 46 | export OPT_GCC 47 | shift 48 | ;; 49 | -*clang) 50 | OPT_CLANG="yes" 51 | export OPT_CLANG 52 | shift 53 | ;; 54 | *) 55 | help 56 | exit 1 57 | ;; 58 | esac 59 | done 60 | } 61 | 62 | read_opts $* 63 | 64 | rm -f Makefile 65 | cat Makefile.base | sed '/DO NOT DELETE/,$d' > Makefile.tmp 66 | mv Makefile.tmp Makefile 67 | makedepend *.cpp -p .o/ 2>/dev/null 68 | 69 | echo "COMPILER_FLAGS=-Werror -g -ggdb3 -O3 -fstack-protector-all -I . -I examples # AUTOGEN" > .Makefile 70 | echo " " >> .Makefile 71 | echo "CLANG_COMPILER_WARNINGS=-Wall # AUTOGEN" >> .Makefile 72 | echo "GCC_COMPILER_WARNINGS=-Wall # AUTOGEN" >> .Makefile 73 | echo "GXX_COMPILER_WARNINGS=-Wall # AUTOGEN" >> .Makefile 74 | 75 | if [[ $OPT_GCC = "yes" ]]; then 76 | echo "COMPILER_WARNINGS=\$(CLANG_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 77 | echo "COMPILER_WARNINGS=\$(GXX_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 78 | echo "COMPILER_WARNINGS=\$(GCC_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 79 | echo "CXX=g++ # AUTOGEN" >> .Makefile 80 | elif [[ $OPT_CLANG = "yes" ]]; then 81 | echo "COMPILER_WARNINGS=\$(CLANG_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 82 | echo "COMPILER_WARNINGS=\$(GXX_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 83 | echo "COMPILER_WARNINGS=\$(GCC_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 84 | echo "CXX=clang++ # AUTOGEN" >> .Makefile 85 | else 86 | `clang++ --version >/dev/null 2>/dev/null` 87 | if [ $? -eq 0 ] 88 | then 89 | echo "COMPILER_WARNINGS=\$(GCC_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 90 | echo "COMPILER_WARNINGS=\$(GXX_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 91 | echo "COMPILER_WARNINGS=\$(CLANG_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 92 | echo "CXX=clang++ # AUTOGEN" >> .Makefile 93 | else 94 | `gcc --version >/dev/null 2>/dev/null` 95 | if [ $? -eq 0 ] 96 | then 97 | echo "COMPILER_WARNINGS=\$(CLANG_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 98 | echo "COMPILER_WARNINGS=\$(GXX_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 99 | echo "COMPILER_WARNINGS=\$(GCC_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 100 | echo "# CXX=clang++ # AUTOGEN" >> .Makefile 101 | echo "CXX=gcc # AUTOGEN" >> .Makefile 102 | echo "# CXX=cc # AUTOGEN" >> .Makefile 103 | echo "# CXX=g++ # AUTOGEN" >> .Makefile 104 | else 105 | `g++ --version >/dev/null 2>/dev/null` 106 | if [ $? -eq 0 ] 107 | then 108 | echo "COMPILER_WARNINGS=\$(CLANG_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 109 | echo "COMPILER_WARNINGS=\$(GCC_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 110 | echo "COMPILER_WARNINGS=\$(GXX_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 111 | echo "# CXX=clang++ # AUTOGEN" >> .Makefile 112 | echo "# CXX=gcc # AUTOGEN" >> .Makefile 113 | echo "# CXX=cc # AUTOGEN" >> .Makefile 114 | echo "CXX=g++ # AUTOGEN" >> .Makefile 115 | else 116 | echo "COMPILER_WARNINGS=\$(CLANG_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 117 | echo "COMPILER_WARNINGS=\$(GXX_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 118 | echo "COMPILER_WARNINGS=\$(GCC_COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 119 | echo "# CXX=clang++ # AUTOGEN" >> .Makefile 120 | echo "# CXX=gcc # AUTOGEN" >> .Makefile 121 | echo "# CXX=cc # AUTOGEN" >> .Makefile 122 | echo "CXX=g++ # AUTOGEN" >> .Makefile 123 | fi 124 | fi 125 | fi 126 | fi 127 | 128 | echo " " >> .Makefile 129 | echo "EXE=$EXE # AUTOGEN" >> .Makefile 130 | echo "LDLIBS=$LDLIBS # AUTOGEN" >> .Makefile 131 | echo "CFLAGS=\$(COMPILER_FLAGS) \$(COMPILER_WARNINGS) # AUTOGEN" >> .Makefile 132 | 133 | cat Makefile | grep -v AUTOGEN | grep -v "^ $" >> .Makefile 134 | 135 | if [ -s .Makefile ] 136 | then 137 | mv .Makefile Makefile 138 | if [ ! -f Makefile ] 139 | then 140 | log_err "No makefile?!" 141 | exit 1 142 | fi 143 | else 144 | log_err "Makefile create fail?!" 145 | exit 1 146 | fi 147 | 148 | log_info "Cleaning" 149 | make clobber | sed 's/^/ /g' 150 | 151 | # 152 | # How many cores? 153 | # 154 | CORES="" 155 | 156 | MY_OS_NAME=$(uname) 157 | case "$MY_OS_NAME" in 158 | *Darwin*) 159 | CORES=$(/usr/sbin/system_profiler -detailLevel full SPHardwareDataType | grep Cores | sed 's/.*: //g' | awk '{print $1}') 160 | ;; 161 | *inux*) 162 | CORES=$(grep -E -c "cpu cores|processor" /proc/cpuinfo) 163 | ;; 164 | *MING*|*MSYS*) 165 | CORES=$(grep -E -c "cpu cores|processor" /proc/cpuinfo) 166 | ;; 167 | esac 168 | 169 | if [ "$CORES" != "" ] 170 | then 171 | log_info "Compiling ($CORES cpus)" 172 | 173 | CORES="-j $CORES" 174 | else 175 | log_info "Compiling" 176 | fi 177 | 178 | make $CORES all 179 | 180 | if [ $? -eq 0 ] 181 | then 182 | log_info "Run ./c_plus_plus_serializer to start" 183 | 184 | rm -f Makefile.bak 185 | else 186 | log_die "Build failed" 187 | fi 188 | -------------------------------------------------------------------------------- /build/common.sh: -------------------------------------------------------------------------------- 1 | # 2 | # common shell utilities 3 | # 4 | 5 | ESC="" 6 | 7 | DULL=0 8 | BRIGHT=1 9 | 10 | FG_BLACK=30 11 | FG_RED=31 12 | FG_GREEN=32 13 | FG_YELLOW=33 14 | FG_BLUE=34 15 | FG_MAGENTA=35 16 | FG_CYAN=36 17 | FG_WHITE=37 18 | 19 | FG_NULL=00 20 | 21 | BG_BLACK=40 22 | BG_RED=41 23 | BG_GREEN=42 24 | BG_YELLOW=43 25 | BG_BLUE=44 26 | BG_MAGENTA=45 27 | BG_CYAN=46 28 | BG_WHITE=47 29 | 30 | BG_NULL=00 31 | 32 | ESC="[" 33 | NORMAL="${ESC}m" 34 | RESET="${ESC}${DULL};${FG_WHITE};${BG_NULL}m" 35 | 36 | # DULL TEXT 37 | BLACK="${ESC}${DULL};${FG_BLACK}m" 38 | RED="${ESC}${DULL};${FG_RED}m" 39 | GREEN="${ESC}${DULL};${FG_GREEN}m" 40 | YELLOW="${ESC}${DULL};${FG_YELLOW}m" 41 | BLUE="${ESC}${DULL};${FG_BLUE}m" 42 | MAGENTA="${ESC}${DULL};${FG_MAGENTA}m" 43 | CYAN="${ESC}${DULL};${FG_CYAN}m" 44 | WHITE="${ESC}${DULL};${FG_WHITE}m" 45 | 46 | ON_BLACK="${ESC}${DULL};${BG_BLACK}m" 47 | ON_RED="${ESC}${DULL};${BG_RED}m" 48 | ON_GREEN="${ESC}${DULL};${BG_GREEN}m" 49 | ON_YELLOW="${ESC}${DULL};${BG_YELLOW}m" 50 | ON_BLUE="${ESC}${DULL};${BG_BLUE}m" 51 | ON_MAGENTA="${ESC}${DULL};${BG_MAGENTA}m" 52 | ON_CYAN="${ESC}${DULL};${BG_CYAN}m" 53 | ON_WHITE="${ESC}${DULL};${BG_WHITE}m" 54 | 55 | # BRIGHT TEXT 56 | BRIGHT_BLACK="${ESC}${BRIGHT};${FG_BLACK}m" 57 | BRIGHT_RED="${ESC}${BRIGHT};${FG_RED}m" 58 | BRIGHT_GREEN="${ESC}${BRIGHT};${FG_GREEN}m" 59 | BRIGHT_YELLOW="${ESC}${BRIGHT};${FG_YELLOW}m" 60 | BRIGHT_BLUE="${ESC}${BRIGHT};${FG_BLUE}m" 61 | BRIGHT_MAGENTA="${ESC}${BRIGHT};${FG_MAGENTA}m" 62 | BRIGHT_CYAN="${ESC}${BRIGHT};${FG_CYAN}m" 63 | BRIGHT_WHITE="${ESC}${BRIGHT};${FG_WHITE}m" 64 | 65 | setenv() { 66 | eval "$1='$2'"; export $1; 67 | } 68 | 69 | log() { 70 | echo "${MAGENTA}$*${RESET}" 71 | } 72 | 73 | log_info() { 74 | echo "${GREEN}$*${RESET}" 75 | } 76 | 77 | log_echo() { 78 | echo "$*" 79 | } 80 | 81 | log_err() { 82 | echo "${RED}ERROR: $*${RESET}" 83 | } 84 | 85 | log_warn() { 86 | echo "${CYAN}WARN: $*${RESET}" 87 | } 88 | 89 | log_die() { 90 | echo ": ${RED}FATAL ERROR: $*${RESET}" 91 | echo "Mail goblinhack@gmail.com for help" 92 | exit 1 93 | } 94 | 95 | run() { 96 | echo " $*" 97 | $* 98 | } 99 | -------------------------------------------------------------------------------- /c_plus_plus_serializer.h: -------------------------------------------------------------------------------- 1 | #ifndef C_PLUS_PLUS_SERIALIZER 2 | #define C_PLUS_PLUS_SERIALIZER 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #undef DEBUG_C_PLUS_PLUS_SERIALIZER 9 | 10 | // 11 | // 64 bits for serializing is a bit overkill for serializing, so use int 12 | // 13 | #ifdef USE_SIZE_T 14 | typedef size_t my_size_t; 15 | #else 16 | typedef int my_size_t; 17 | #endif 18 | 19 | template < typename TYPE > struct Bits { 20 | TYPE t; 21 | }; 22 | 23 | template < typename TYPE > static inline Bits< TYPE & > bits(TYPE &t) { return Bits< TYPE & > {t}; } 24 | 25 | template < typename TYPE > static inline Bits< const TYPE & > bits(const TYPE &t) { return Bits< const TYPE & > {t}; } 26 | 27 | //////////////////////////////////////////////////////////////////////////// 28 | // Read/write POD types 29 | //////////////////////////////////////////////////////////////////////////// 30 | template < typename TYPE > static inline std::istream &operator>>(std::istream &in, Bits< TYPE & > b) 31 | { 32 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 33 | std::cout << "read " << sizeof(TYPE) << " bytes" << std::endl; 34 | #endif 35 | return in.read(reinterpret_cast< char * >(&b.t), sizeof(TYPE)); 36 | } 37 | 38 | template < typename TYPE > static inline std::ostream &operator<<(std::ostream &out, Bits< TYPE & > const b) 39 | { 40 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 41 | std::cout << "write " << sizeof(TYPE) << " bytes" << std::endl; 42 | #endif 43 | // reinterpret_cast is for pointer conversion 44 | // static_cast is for compatible pointer conversion 45 | return out.write(reinterpret_cast< const char * >(&(b.t)), sizeof(TYPE)); 46 | } 47 | 48 | //////////////////////////////////////////////////////////////////////////// 49 | // Read/write std::string 50 | //////////////////////////////////////////////////////////////////////////// 51 | static inline std::istream &operator>>(std::istream &in, Bits< std::string & > v) 52 | { 53 | my_size_t sz = 0; 54 | in >> bits(sz); 55 | if (in && sz) { 56 | std::vector< char > tmp(sz); 57 | in.read(tmp.data(), sz); 58 | v.t.assign(tmp.data(), sz); 59 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 60 | std::cout << "read '" << v.t << "'" << std::endl; 61 | #endif 62 | } 63 | 64 | return in; 65 | } 66 | 67 | static inline std::ostream &operator<<(std::ostream &out, Bits< const std::string & > const v) 68 | { 69 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 70 | std::cout << "write const '" << v.t << "'" << std::endl; 71 | #endif 72 | my_size_t sz = v.t.size(); 73 | return out << bits(sz) << v.t; 74 | } 75 | 76 | static inline std::ostream &operator<<(std::ostream &out, Bits< std::string & > const v) 77 | { 78 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 79 | std::cout << "write '" << v.t << "'" << std::endl; 80 | #endif 81 | my_size_t sz = v.t.size(); 82 | return out << bits(sz) << v.t; 83 | } 84 | 85 | //////////////////////////////////////////////////////////////////////////// 86 | // Read/write std::wstring 87 | //////////////////////////////////////////////////////////////////////////// 88 | static inline std::istream &operator>>(std::istream &in, Bits< std::wstring & > v) 89 | { 90 | my_size_t sz = 0; 91 | in >> bits(sz); 92 | if (in && sz) { 93 | while (sz--) { 94 | wchar_t tmp; 95 | in >> bits(tmp); 96 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 97 | std::cout << "read '" << tmp << "'" << std::endl; 98 | #endif 99 | v.t += tmp; 100 | } 101 | } 102 | 103 | return in; 104 | } 105 | 106 | static inline std::ostream &operator<<(std::ostream &out, Bits< const std::wstring & > const v) 107 | { 108 | my_size_t sz = v.t.size(); 109 | out << bits(sz); 110 | for (auto tmp : v.t) { 111 | out << bits(tmp); 112 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 113 | std::cout << "write const '" << tmp << "'" << std::endl; 114 | #endif 115 | } 116 | return out; 117 | } 118 | 119 | static inline std::ostream &operator<<(std::ostream &out, Bits< std::wstring & > const v) 120 | { 121 | my_size_t sz = v.t.size(); 122 | out << bits(sz); 123 | for (auto tmp : v.t) { 124 | out << bits(tmp); 125 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 126 | std::cout << "write '" << tmp << "'" << std::endl; 127 | #endif 128 | } 129 | return out; 130 | } 131 | 132 | //////////////////////////////////////////////////////////////////////////// 133 | // Read/write wchar_t 134 | //////////////////////////////////////////////////////////////////////////// 135 | static inline std::istream &operator>>(std::istream &in, Bits< wchar_t & > v) 136 | { 137 | if (sizeof(wchar_t) == 4) { 138 | unsigned char _a, _b, _c, _d; 139 | in >> bits(_a); 140 | in >> bits(_b); 141 | in >> bits(_c); 142 | in >> bits(_d); 143 | v.t = (_a << 24) | (_b << 16) | (_c << 8) | _d; 144 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 145 | std::cout << "read '" << _a << "'" << std::endl; 146 | std::cout << "read '" << _b << "'" << std::endl; 147 | std::cout << "read '" << _c << "'" << std::endl; 148 | std::cout << "read '" << _d << "'" << std::endl; 149 | #endif 150 | } else if (sizeof(wchar_t) == 2) { 151 | unsigned char _a, _b; 152 | in >> bits(_a); 153 | in >> bits(_b); 154 | v.t = (_a << 8) | _b; 155 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 156 | std::cout << "read '" << _a << "'" << std::endl; 157 | std::cout << "read '" << _b << "'" << std::endl; 158 | #endif 159 | } else { 160 | static_assert(sizeof(wchar_t) <= 4, "wchar_t is greater that 32 bit"); 161 | } 162 | 163 | return in; 164 | } 165 | 166 | static inline std::ostream &operator<<(std::ostream &out, Bits< const wchar_t & > const v) 167 | { 168 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 169 | std::cout << "write const '" << v.t << "'" << std::endl; 170 | #endif 171 | if (sizeof(wchar_t) == 4) { 172 | unsigned char _a, _b, _c, _d; 173 | _a = (v.t & (0xff000000)) >> 24; 174 | out << bits(_a); 175 | _b = (v.t & (0x00ff0000)) >> 16; 176 | out << bits(_b); 177 | _c = (v.t & (0x0000ff00)) >> 8; 178 | out << bits(_c); 179 | _d = (v.t & (0x000000ff)) >> 0; 180 | out << bits(_d); 181 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 182 | std::cout << "write '" << _a << "'" << std::endl; 183 | std::cout << "write '" << _b << "'" << std::endl; 184 | std::cout << "write '" << _c << "'" << std::endl; 185 | std::cout << "write '" << _d << "'" << std::endl; 186 | #endif 187 | } else if (sizeof(wchar_t) == 2) { 188 | unsigned char _a, _b; 189 | _a = (v.t & (0xff00)) >> 8; 190 | out << bits(_a); 191 | _b = (v.t & (0x00ff)) >> 0; 192 | out << bits(_b); 193 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 194 | std::cout << "write '" << _a << "'" << std::endl; 195 | std::cout << "write '" << _b << "'" << std::endl; 196 | #endif 197 | } else { 198 | static_assert(sizeof(wchar_t) <= 4, "wchar_t is greater that 32 bit"); 199 | } 200 | return (out); 201 | } 202 | 203 | static inline std::ostream &operator<<(std::ostream &out, Bits< wchar_t & > const v) 204 | { 205 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 206 | std::cout << "write const '" << v.t << "'" << std::endl; 207 | #endif 208 | if (sizeof(wchar_t) == 4) { 209 | unsigned char _a, _b, _c, _d; 210 | _a = (v.t & (0xff000000)) >> 24; 211 | out << bits(_a); 212 | _b = (v.t & (0x00ff0000)) >> 16; 213 | out << bits(_b); 214 | _c = (v.t & (0x0000ff00)) >> 8; 215 | out << bits(_c); 216 | _d = (v.t & (0x000000ff)) >> 0; 217 | out << bits(_d); 218 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 219 | std::cout << "write '" << _a << "'" << std::endl; 220 | std::cout << "write '" << _b << "'" << std::endl; 221 | std::cout << "write '" << _c << "'" << std::endl; 222 | std::cout << "write '" << _d << "'" << std::endl; 223 | #endif 224 | } else if (sizeof(wchar_t) == 2) { 225 | unsigned char _a, _b; 226 | _a = (v.t & (0xff00)) >> 8; 227 | out << bits(_a); 228 | _b = (v.t & (0x00ff)) >> 0; 229 | out << bits(_b); 230 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 231 | std::cout << "write '" << _a << "'" << std::endl; 232 | std::cout << "write '" << _b << "'" << std::endl; 233 | #endif 234 | } else { 235 | static_assert(sizeof(wchar_t) <= 4, "wchar_t is greater that 32 bit"); 236 | } 237 | return (out); 238 | } 239 | 240 | //////////////////////////////////////////////////////////////////////////// 241 | // Read/write simple container 242 | //////////////////////////////////////////////////////////////////////////// 243 | template < class T, template < typename ELEM, typename ALLOC = std::allocator< ELEM > > class C > 244 | static inline std::ostream &operator<<(std::ostream &out, Bits< C< T > & > const v) 245 | { 246 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 247 | std::cout << "write container " << v.t.size() << " elems" << std::endl; 248 | #endif 249 | my_size_t sz = v.t.size(); 250 | out << bits(sz); 251 | for (auto i : v.t) { 252 | out << bits(i); 253 | } 254 | return (out); 255 | } 256 | 257 | template < class T, template < typename ELEM, typename ALLOC = std::allocator< ELEM > > class C > 258 | static inline std::ostream &operator<<(std::ostream &out, Bits< const C< T > & > const v) 259 | { 260 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 261 | std::cout << "write container " << v.t.size() << " elems" << std::endl; 262 | #endif 263 | my_size_t sz = v.t.size(); 264 | out << bits(sz); 265 | for (auto i : v.t) { 266 | out << bits(i); 267 | } 268 | return (out); 269 | } 270 | 271 | template < class T, template < typename ELEM, typename ALLOC = std::allocator< ELEM > > class C > 272 | static inline std::istream &operator>>(std::istream &in, Bits< C< T > & > v) 273 | { 274 | my_size_t sz = 0; 275 | in >> bits(sz); 276 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 277 | std::cout << "read container " << sz << " elems" << std::endl; 278 | #endif 279 | if (in && sz) { 280 | while (sz--) { 281 | T s; 282 | in >> bits(s); 283 | v.t.push_back(s); 284 | } 285 | } 286 | 287 | return in; 288 | } 289 | 290 | //////////////////////////////////////////////////////////////////////////// 291 | // Read/write std::array 292 | //////////////////////////////////////////////////////////////////////////// 293 | template < class T, std::size_t N, template < typename ELEM, std::size_t > class C > 294 | static inline std::ostream &operator<<(std::ostream &out, Bits< C< T, N > & > const v) 295 | { 296 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 297 | std::cout << "write array container " << v.t.size() << " elems" << std::endl; 298 | #endif 299 | my_size_t sz = v.t.size(); 300 | out << bits(sz); 301 | for (auto i : v.t) { 302 | out << bits(i); 303 | } 304 | return (out); 305 | } 306 | 307 | template < class T, std::size_t N, template < typename ELEM, std::size_t > class C > 308 | static inline std::ostream &operator<<(std::ostream &out, Bits< const C< T, N > & > const v) 309 | { 310 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 311 | std::cout << "write array container " << v.t.size() << " elems" << std::endl; 312 | #endif 313 | my_size_t sz = v.t.size(); 314 | out << bits(sz); 315 | for (auto i : v.t) { 316 | out << bits(i); 317 | } 318 | return (out); 319 | } 320 | 321 | template < class T, std::size_t N, template < typename ELEM, std::size_t > class C > 322 | static inline std::istream &operator>>(std::istream &in, Bits< C< T, N > & > v) 323 | { 324 | my_size_t sz = 0; 325 | in >> bits(sz); 326 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 327 | std::cout << "read array container " << sz << " elems" << std::endl; 328 | #endif 329 | if (in && sz) { 330 | for (auto n = 0; n < sz; n++) { 331 | T s; 332 | in >> bits(s); 333 | v.t[ n ] = s; 334 | } 335 | } 336 | 337 | return in; 338 | } 339 | 340 | //////////////////////////////////////////////////////////////////////////// 341 | // Read/write map 342 | //////////////////////////////////////////////////////////////////////////// 343 | 344 | template < template < class K, class V, class Compare = std::less< K >, 345 | class Alloc = std::allocator< std::pair< const K, V > > > 346 | class M, 347 | class K, class V > 348 | 349 | static inline std::ostream &operator<<(std::ostream &out, Bits< M< K, V > & > const m) 350 | { 351 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 352 | std::cout << "write map " << m.t.size() << " elems" << std::endl; 353 | #endif 354 | my_size_t sz = m.t.size(); 355 | out << bits(sz); 356 | for (auto i : m.t) { 357 | out << bits(i.first) << bits(i.second); 358 | } 359 | return (out); 360 | } 361 | 362 | template < template < class K, class V, class Compare = std::less< K >, 363 | class Alloc = std::allocator< std::pair< const K, V > > > 364 | class M, 365 | class K, class V > 366 | 367 | static inline std::ostream &operator<<(std::ostream &out, Bits< M< K, const V > & > const m) 368 | { 369 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 370 | std::cout << "write map " << m.t.size() << " elems" << std::endl; 371 | #endif 372 | my_size_t sz = m.t.size(); 373 | out << bits(sz); 374 | for (auto i : m.t) { 375 | out << bits(i.first) << bits(i.second); 376 | } 377 | return (out); 378 | } 379 | 380 | template < template < class K, class V, class Compare = std::less< K >, 381 | class Alloc = std::allocator< std::pair< const K, V > > > 382 | class M, 383 | class K, class V > 384 | 385 | static inline std::istream &operator>>(std::istream &in, Bits< M< K, V > & > m) 386 | { 387 | my_size_t sz = 0; 388 | in >> bits(sz); 389 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 390 | std::cout << "read map " << sz << " elems" << std::endl; 391 | #endif 392 | if (in && sz) { 393 | while (sz--) { 394 | K k; 395 | V v; 396 | in >> bits(k) >> bits(v); 397 | m.t.insert(std::make_pair(k, v)); 398 | } 399 | } 400 | 401 | return in; 402 | } 403 | 404 | //////////////////////////////////////////////////////////////////////////// 405 | // Read/write unordered_map 406 | //////////////////////////////////////////////////////////////////////////// 407 | 408 | template < template < class K, class T, class Hash = std::hash< K >, class Pred = std::equal_to< K >, 409 | class Alloc = std::allocator< std::pair< const K, T > > > 410 | class M, 411 | class K, class V > 412 | 413 | static inline std::ostream &operator<<(std::ostream &out, Bits< M< K, V > & > const m) 414 | { 415 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 416 | std::cout << "write unordered_map " << m.t.size() << " elems" << std::endl; 417 | #endif 418 | my_size_t sz = m.t.size(); 419 | out << bits(sz); 420 | for (auto i : m.t) { 421 | out << bits(i.first) << bits(i.second); 422 | } 423 | return (out); 424 | } 425 | 426 | template < template < class K, class T, class Hash = std::hash< K >, class Pred = std::equal_to< K >, 427 | class Alloc = std::allocator< std::pair< const K, T > > > 428 | class M, 429 | class K, class V > 430 | 431 | static inline std::ostream &operator<<(std::ostream &out, Bits< M< K, const V > & > const m) 432 | { 433 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 434 | std::cout << "write unordered_map " << m.t.size() << " elems" << std::endl; 435 | #endif 436 | my_size_t sz = m.t.size(); 437 | out << bits(sz); 438 | for (auto i : m.t) { 439 | out << bits(i.first) << bits(i.second); 440 | } 441 | return (out); 442 | } 443 | 444 | template < template < class K, class T, class Hash = std::hash< K >, class Pred = std::equal_to< K >, 445 | class Alloc = std::allocator< std::pair< const K, T > > > 446 | class M, 447 | class K, class V > 448 | 449 | static inline std::istream &operator>>(std::istream &in, Bits< M< K, V > & > m) 450 | { 451 | my_size_t sz = 0; 452 | in >> bits(sz); 453 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 454 | std::cout << "read unordered_map " << sz << " elems" << std::endl; 455 | #endif 456 | if (in && sz) { 457 | while (sz--) { 458 | K k; 459 | V v; 460 | in >> bits(k) >> bits(v); 461 | m.t.insert(std::make_pair(k, v)); 462 | } 463 | } 464 | 465 | return in; 466 | } 467 | 468 | //////////////////////////////////////////////////////////////////////////// 469 | // Read/write set 470 | //////////////////////////////////////////////////////////////////////////// 471 | 472 | template < 473 | template< 474 | class K, 475 | class Compare = std::less, 476 | class Allocator = std::allocator 477 | > class M, class K > 478 | 479 | static inline std::ostream &operator<<(std::ostream &out, Bits< M< K > & > const m) 480 | { 481 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 482 | std::cout << "write set " << m.t.size() << " elems" << std::endl; 483 | #endif 484 | my_size_t sz = m.t.size(); 485 | out << bits(sz); 486 | for (auto i : m.t) { 487 | out << bits(i); 488 | } 489 | return (out); 490 | } 491 | 492 | template < 493 | template< 494 | class K, 495 | class Compare = std::less, 496 | class Allocator = std::allocator 497 | > class M, class K > 498 | 499 | static inline std::ostream &operator<<(std::ostream &out, Bits< M< K > & > const m) 500 | { 501 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 502 | std::cout << "write set " << m.t.size() << " elems" << std::endl; 503 | #endif 504 | my_size_t sz = m.t.size(); 505 | out << bits(sz); 506 | for (auto i : m.t) { 507 | out << bits(i); 508 | } 509 | return (out); 510 | } 511 | 512 | template < 513 | template< 514 | class K, 515 | class Compare = std::less, 516 | class Allocator = std::allocator 517 | > class M, class K > 518 | 519 | static inline std::istream &operator>>(std::istream &in, Bits< M< K > & > m) 520 | { 521 | my_size_t sz = 0; 522 | in >> bits(sz); 523 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 524 | std::cout << "read set " << sz << " elems" << std::endl; 525 | #endif 526 | if (in && sz) { 527 | while (sz--) { 528 | K k; 529 | in >> bits(k); 530 | m.t.insert(k); 531 | } 532 | } 533 | 534 | return in; 535 | } 536 | 537 | //////////////////////////////////////////////////////////////////////////// 538 | // Read/write pair 539 | //////////////////////////////////////////////////////////////////////////// 540 | 541 | template < typename K, typename V > 542 | static inline std::ostream &operator<<(std::ostream &out, Bits< std::pair< K, V > & > const wrapped) 543 | { 544 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 545 | std::cout << "read pair" << std::endl; 546 | #endif 547 | out << bits(wrapped.t.first); 548 | out << bits(wrapped.t.second); 549 | return (out); 550 | } 551 | 552 | template < typename K, typename V > 553 | static inline std::istream &operator>>(std::istream &in, Bits< std::pair< K, V > & > wrapped) 554 | { 555 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 556 | std::cout << "write pair" << std::endl; 557 | #endif 558 | in >> bits(wrapped.t.first); 559 | in >> bits(wrapped.t.second); 560 | return in; 561 | } 562 | 563 | template < typename K, typename V > 564 | static inline std::istream &operator>>(std::istream &in, Bits< const std::pair< K, V > & > wrapped) 565 | { 566 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 567 | std::cout << "const write pair" << std::endl; 568 | #endif 569 | in >> bits(wrapped.t.first); 570 | in >> bits(wrapped.t.second); 571 | return in; 572 | } 573 | #endif /* C_PLUS_PLUS_SERIALIZER */ 574 | -------------------------------------------------------------------------------- /examples/.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | # BasedOnStyle: LLVM 4 | AccessModifierOffset: -2 5 | AlignAfterOpenBracket: Align 6 | AlignConsecutiveMacros: true 7 | AlignConsecutiveAssignments: true 8 | AlignConsecutiveBitFields: true 9 | AlignConsecutiveDeclarations: true 10 | AlignEscapedNewlines: Right 11 | AlignOperands: Align 12 | AlignTrailingComments: true 13 | AllowAllArgumentsOnNextLine: true 14 | AllowAllConstructorInitializersOnNextLine: true 15 | AllowAllParametersOfDeclarationOnNextLine: true 16 | AllowShortEnumsOnASingleLine: true 17 | AllowShortBlocksOnASingleLine: Never 18 | AllowShortCaseLabelsOnASingleLine: true 19 | AllowShortFunctionsOnASingleLine: All 20 | AllowShortLambdasOnASingleLine: All 21 | AllowShortIfStatementsOnASingleLine: Never 22 | AllowShortLoopsOnASingleLine: false 23 | AlwaysBreakAfterDefinitionReturnType: None 24 | AlwaysBreakAfterReturnType: None 25 | AlwaysBreakBeforeMultilineStrings: true 26 | AlwaysBreakTemplateDeclarations: MultiLine 27 | AttributeMacros: 28 | - __capability 29 | BinPackArguments: true 30 | BinPackParameters: true 31 | BraceWrapping: 32 | AfterCaseLabel: true 33 | AfterClass: true 34 | AfterControlStatement: Never 35 | AfterEnum: true 36 | AfterFunction: true 37 | AfterNamespace: true 38 | AfterObjCDeclaration: true 39 | AfterStruct: true 40 | AfterUnion: true 41 | AfterExternBlock: true 42 | BeforeCatch: true 43 | BeforeElse: true 44 | BeforeLambdaBody: true 45 | BeforeWhile: true 46 | IndentBraces: true 47 | SplitEmptyFunction: true 48 | SplitEmptyRecord: true 49 | SplitEmptyNamespace: true 50 | BreakBeforeBinaryOperators: None 51 | BreakBeforeConceptDeclarations: true 52 | BreakBeforeBraces: Linux 53 | BreakBeforeInheritanceComma: false 54 | BreakInheritanceList: BeforeColon 55 | BreakBeforeTernaryOperators: true 56 | BreakConstructorInitializersBeforeComma: false 57 | BreakConstructorInitializers: BeforeColon 58 | BreakAfterJavaFieldAnnotations: false 59 | BreakStringLiterals: true 60 | ColumnLimit: 118 61 | CommentPragmas: '^ IWYU pragma:' 62 | CompactNamespaces: false 63 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 64 | ConstructorInitializerIndentWidth: 4 65 | ContinuationIndentWidth: 4 66 | Cpp11BracedListStyle: true 67 | DeriveLineEnding: true 68 | DerivePointerAlignment: false 69 | DisableFormat: false 70 | EmptyLineBeforeAccessModifier: LogicalBlock 71 | ExperimentalAutoDetectBinPacking: false 72 | FixNamespaceComments: true 73 | ForEachMacros: 74 | - foreach 75 | - Q_FOREACH 76 | - BOOST_FOREACH 77 | StatementAttributeLikeMacros: 78 | - Q_EMIT 79 | IncludeBlocks: Preserve 80 | IncludeCategories: 81 | - Regex: '^"(llvm|llvm-c|clang|clang-c)/' 82 | Priority: 2 83 | SortPriority: 0 84 | CaseSensitive: false 85 | - Regex: '^(<|"(gtest|gmock|isl|json)/)' 86 | Priority: 3 87 | SortPriority: 0 88 | CaseSensitive: false 89 | - Regex: '.*' 90 | Priority: 1 91 | SortPriority: 0 92 | CaseSensitive: false 93 | IncludeIsMainRegex: '(Test)?$' 94 | IncludeIsMainSourceRegex: '' 95 | IndentCaseLabels: true 96 | IndentCaseBlocks: true 97 | IndentGotoLabels: true 98 | IndentPPDirectives: None 99 | IndentExternBlock: AfterExternBlock 100 | IndentRequires: false 101 | IndentWidth: 2 102 | IndentWrappedFunctionNames: true 103 | InsertTrailingCommas: None 104 | JavaScriptQuotes: Leave 105 | JavaScriptWrapImports: true 106 | KeepEmptyLinesAtTheStartOfBlocks: true 107 | MacroBlockBegin: '' 108 | MacroBlockEnd: '' 109 | MaxEmptyLinesToKeep: 1 110 | NamespaceIndentation: None 111 | ObjCBinPackProtocolList: Auto 112 | ObjCBlockIndentWidth: 2 113 | ObjCBreakBeforeNestedBlockParam: true 114 | ObjCSpaceAfterProperty: false 115 | ObjCSpaceBeforeProtocolList: true 116 | PenaltyBreakAssignment: 2 117 | PenaltyBreakBeforeFirstCallParameter: 19 118 | PenaltyBreakComment: 300 119 | PenaltyBreakFirstLessLess: 120 120 | PenaltyBreakString: 1000 121 | PenaltyBreakTemplateDeclaration: 10 122 | PenaltyExcessCharacter: 1000000 123 | PenaltyReturnTypeOnItsOwnLine: 60 124 | PenaltyIndentedWhitespace: 0 125 | PointerAlignment: Right 126 | ReflowComments: true 127 | SortIncludes: true 128 | SortJavaStaticImport: Before 129 | SortUsingDeclarations: true 130 | SpaceAfterCStyleCast: true 131 | SpaceAfterLogicalNot: true 132 | SpaceAfterTemplateKeyword: true 133 | SpaceBeforeAssignmentOperators: true 134 | SpaceBeforeCaseColon: true 135 | SpaceBeforeCpp11BracedList: true 136 | SpaceBeforeCtorInitializerColon: true 137 | SpaceBeforeInheritanceColon: true 138 | SpaceBeforeParens: ControlStatements 139 | SpaceAroundPointerQualifiers: Default 140 | SpaceBeforeRangeBasedForLoopColon: true 141 | SpaceInEmptyBlock: false 142 | SpaceInEmptyParentheses: false 143 | SpacesBeforeTrailingComments: 1 144 | SpacesInAngles: true 145 | SpacesInConditionalStatement: false 146 | SpacesInContainerLiterals: true 147 | SpacesInCStyleCastParentheses: false 148 | SpacesInParentheses: false 149 | SpacesInSquareBrackets: true 150 | SpaceBeforeSquareBrackets: false 151 | BitFieldColonSpacing: Both 152 | Standard: Latest 153 | StatementMacros: 154 | - Q_UNUSED 155 | - QT_REQUIRE_VERSION 156 | TabWidth: 2 157 | UseCRLF: false 158 | UseTab: Never 159 | WhitespaceSensitiveMacros: 160 | - STRINGIZE 161 | - PP_STRINGIZE 162 | - BOOST_PP_STRINGIZE 163 | - NS_SWIFT_NAME 164 | - CF_SWIFT_NAME 165 | ... 166 | 167 | -------------------------------------------------------------------------------- /examples/basic_example.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include 3 | 4 | static void save_simple(const std::string filename) 5 | { 6 | std::cout << "save to " << filename << std::endl; 7 | std::ofstream out(filename, std::ios::binary); 8 | 9 | char a = 42; 10 | unsigned short b = 65535; 11 | int c = 123456; 12 | float d = std::numeric_limits< float >::max(); 13 | double e = std::numeric_limits< double >::max(); 14 | std::string f("hello"); 15 | wchar_t g = L'💩'; 16 | std::wstring h(L"wide string 💩"); 17 | std::pair< int, std::string > p(42, "life"); 18 | 19 | std::cout << std::endl; 20 | std::cout << "write:" << std::endl; 21 | std::cout << " a: char " << a << std::endl; 22 | std::cout << " b: unsigned short " << b << std::endl; 23 | std::cout << " c: int " << c << std::endl; 24 | std::cout << " d: float " << d << std::endl; 25 | std::cout << " e: double " << e << std::endl; 26 | std::cout << " f: std::string " << f << std::endl; 27 | std::wcout << " g: wchar_t " << g << std::endl; 28 | std::wcout << " h: std::wstring " << h << std::endl; 29 | std::cout << " p: std::pair " << p.first << " " << p.second << std::endl; 30 | 31 | std::cout << std::endl; 32 | 33 | out << bits(a) << bits(b) << bits(c) << bits(d); 34 | out << bits(e) << bits(f) << bits(g) << bits(h); 35 | out << bits(p); 36 | } 37 | 38 | static void load_simple(const std::string filename) 39 | { 40 | std::cout << "read from " << filename << std::endl; 41 | std::ifstream in(filename); 42 | 43 | char a; 44 | unsigned short b; 45 | int c; 46 | float d; 47 | double e; 48 | std::string f; 49 | wchar_t g; 50 | std::wstring h; 51 | std::pair< int, std::string > p; 52 | 53 | in >> bits(a) >> bits(b) >> bits(c) >> bits(d); 54 | in >> bits(e) >> bits(f) >> bits(g) >> bits(h); 55 | in >> bits(p); 56 | 57 | std::cout << std::endl; 58 | std::cout << "read:" << std::endl; 59 | std::cout << " a: char " << a << std::endl; 60 | std::cout << " b: unsigned short " << b << std::endl; 61 | std::cout << " c: int " << c << std::endl; 62 | std::cout << " d: float " << d << std::endl; 63 | std::cout << " e: double " << e << std::endl; 64 | std::cout << " f: std::string " << f << std::endl; 65 | std::wcout << " g: wchar_t " << g << std::endl; 66 | std::wcout << " h: std::wstring " << h << std::endl; 67 | std::cout << " p: std::pair " << p.first << " " << p.second << std::endl; 68 | std::cout << std::endl; 69 | } 70 | 71 | void basic_example(void) 72 | { 73 | // 74 | // Need this to get the poop emoji printed! :) 75 | // 76 | std::locale loc(""); 77 | std::ios_base::sync_with_stdio(false); 78 | std::wcout.imbue(loc); 79 | 80 | std::cout << "basic_example" << std::endl; 81 | std::cout << "=============" << std::endl; 82 | save_simple(std::string("basic_example.bin")); 83 | load_simple(std::string("basic_example.bin")); 84 | } 85 | -------------------------------------------------------------------------------- /examples/bitfields_in_class_example.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include 3 | 4 | class BitsetClass 5 | { 6 | public: 7 | std::bitset< 1 > a; 8 | std::bitset< 2 > b; 9 | std::bitset< 3 > c; 10 | 11 | unsigned int d : 1; 12 | unsigned int e : 2; 13 | unsigned int f : 3; 14 | 15 | BitsetClass(void) 16 | { 17 | d = 0; 18 | e = 0; 19 | f = 0; 20 | } 21 | 22 | friend std::ostream &operator<<(std::ostream &out, Bits< const class BitsetClass & > const my) 23 | { 24 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 25 | std::cout << "write custom bitset class" << std::endl; 26 | #endif 27 | out << bits(my.t.a); 28 | out << bits(my.t.b); 29 | out << bits(my.t.c); 30 | 31 | std::bitset< 6 > s(my.t.d | my.t.e << 1 | my.t.f << 3); 32 | out << bits(s); 33 | 34 | return (out); 35 | } 36 | 37 | friend std::istream &operator>>(std::istream &in, Bits< class BitsetClass & > my) 38 | { 39 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 40 | std::cout << "read custom bitset class" << std::endl; 41 | #endif 42 | std::bitset< 1 > a; 43 | in >> bits(a); 44 | my.t.a = a; 45 | 46 | in >> bits(my.t.b); 47 | in >> bits(my.t.c); 48 | std::bitset< 6 > s; 49 | in >> bits(s); 50 | 51 | unsigned long raw_bits = static_cast< unsigned long >(s.to_ulong()); 52 | my.t.d = raw_bits & 0b000001; 53 | my.t.e = (raw_bits & 0b000110) >> 1; 54 | my.t.f = (raw_bits & 0b111000) >> 3; 55 | 56 | return (in); 57 | } 58 | 59 | friend std::ostream &operator<<(std::ostream &out, const class BitsetClass &my) 60 | { 61 | out << "a(1bit):" << my.a << std::endl; 62 | out << "b(2bit):" << my.b << std::endl; 63 | out << "c(3bit):" << my.c << std::endl; 64 | out << "d(1bit):" << std::bitset< 1 >(my.d) << std::endl; 65 | out << "e(2bit):" << std::bitset< 2 >(my.e) << std::endl; 66 | out << "f(3bit):" << std::bitset< 3 >(my.f) << std::endl; 67 | 68 | return (out); 69 | } 70 | }; 71 | 72 | static void save(const std::string filename, const class BitsetClass &c) 73 | { 74 | std::cout << "save to " << filename << std::endl; 75 | std::ofstream out(filename, std::ios::binary); 76 | 77 | out << bits(c); 78 | } 79 | 80 | static void load(const std::string filename, class BitsetClass &c) 81 | { 82 | std::cout << "read from " << filename << std::endl; 83 | std::ifstream in(filename); 84 | 85 | in >> bits(c); 86 | } 87 | 88 | static void save_custom_bitset_class_example(void) 89 | { 90 | auto c = BitsetClass(); 91 | 92 | c.a = 1; 93 | c.b = 3; 94 | c.c = 7; 95 | c.d = 1; 96 | c.e = 3; 97 | c.f = 7; 98 | 99 | save(std::string("custom_bitset_class.bin"), c); 100 | } 101 | 102 | static void load_custom_bitset_class_example(void) 103 | { 104 | auto c = BitsetClass(); 105 | 106 | load(std::string("custom_bitset_class.bin"), c); 107 | std::cout << std::endl; 108 | std::cout << c; 109 | std::cout << std::endl; 110 | } 111 | 112 | void custom_bitset_class_example(void) 113 | { 114 | std::cout << "custom_bitset_class_example" << std::endl; 115 | std::cout << "===========================" << std::endl; 116 | save_custom_bitset_class_example(); 117 | load_custom_bitset_class_example(); 118 | } 119 | -------------------------------------------------------------------------------- /examples/container_example.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | static void save_container(const std::string filename) 9 | { 10 | std::cout << "save to " << filename << std::endl; 11 | std::ofstream out(filename, std::ios::binary); 12 | 13 | std::initializer_list< std::string > d1 = { 14 | "vec-elem1", 15 | "vec-elem2" 16 | "vec-elem3, I may not have gone where I intended to go, but I think I have ended up where I needed to be.", 17 | }; 18 | std::vector< std::string > a(d1); 19 | 20 | std::initializer_list< std::string > d2 = {"list-elem1", "list-elem2"}; 21 | std::list< std::string > b(d2); 22 | 23 | std::array< std::string, 2 > c = {"arr-elem1", "arr-elem2"}; 24 | 25 | // 26 | // 2d array 27 | // 28 | std::array< std::array< char, 2 >, 3 > dd; 29 | dd[ 0 ][ 0 ] = '0'; 30 | dd[ 0 ][ 1 ] = '1'; 31 | dd[ 1 ][ 0 ] = '2'; 32 | dd[ 1 ][ 1 ] = '3'; 33 | dd[ 2 ][ 0 ] = '4'; 34 | dd[ 2 ][ 1 ] = '5'; 35 | 36 | // 37 | // 3d array 38 | // 39 | std::array< std::array< std::array< char, 2 >, 3 >, 4 > ddd; 40 | ddd[ 0 ][ 0 ][ 0 ] = 'a'; 41 | ddd[ 0 ][ 0 ][ 1 ] = 'b'; 42 | ddd[ 0 ][ 1 ][ 0 ] = 'c'; 43 | ddd[ 0 ][ 1 ][ 1 ] = 'd'; 44 | ddd[ 0 ][ 2 ][ 0 ] = 'e'; 45 | ddd[ 0 ][ 2 ][ 1 ] = 'f'; 46 | ddd[ 1 ][ 0 ][ 0 ] = 'g'; 47 | ddd[ 1 ][ 0 ][ 1 ] = 'h'; 48 | ddd[ 1 ][ 1 ][ 0 ] = 'i'; 49 | ddd[ 1 ][ 1 ][ 1 ] = 'j'; 50 | ddd[ 1 ][ 2 ][ 0 ] = 'k'; 51 | ddd[ 1 ][ 2 ][ 1 ] = 'l'; 52 | ddd[ 2 ][ 0 ][ 0 ] = 'm'; 53 | ddd[ 2 ][ 0 ][ 1 ] = 'n'; 54 | ddd[ 2 ][ 1 ][ 0 ] = 'o'; 55 | ddd[ 2 ][ 1 ][ 1 ] = 'p'; 56 | ddd[ 2 ][ 2 ][ 0 ] = 'q'; 57 | ddd[ 2 ][ 2 ][ 1 ] = 'r'; 58 | ddd[ 3 ][ 0 ][ 0 ] = 's'; 59 | ddd[ 3 ][ 0 ][ 1 ] = 't'; 60 | ddd[ 3 ][ 1 ][ 0 ] = 'u'; 61 | ddd[ 3 ][ 1 ][ 1 ] = 'v'; 62 | ddd[ 3 ][ 2 ][ 0 ] = 'w'; 63 | ddd[ 3 ][ 2 ][ 1 ] = 'x'; 64 | 65 | out << bits(a) << bits(b) << bits(c) << bits(dd) << bits(ddd); 66 | } 67 | 68 | static void load_container(const std::string filename) 69 | { 70 | std::cout << "read from " << filename << std::endl; 71 | std::ifstream in(filename); 72 | 73 | std::vector< std::string > a; 74 | std::list< std::string > b; 75 | std::array< std::string, 2 > c; 76 | std::array< std::array< char, 2 >, 3 > dd; 77 | std::array< std::array< std::array< char, 2 >, 3 >, 4 > ddd; 78 | 79 | in >> bits(a) >> bits(b) >> bits(c) >> bits(dd) >> bits(ddd); 80 | 81 | std::cout << std::endl; 82 | std::cout << "a: std::vector " << a.size() << " elems: "; 83 | for (auto elem : a) { 84 | std::cout << "[" << elem << "] "; 85 | } 86 | std::cout << std::endl; 87 | 88 | std::cout << "b: std::list " << b.size() << " elems: "; 89 | for (auto elem : b) { 90 | std::cout << "[" << elem << "] "; 91 | } 92 | std::cout << std::endl; 93 | 94 | std::cout << "c: std::array " << c.size() << " elems: "; 95 | for (auto elem : c) { 96 | std::cout << "[" << elem << "] "; 97 | } 98 | std::cout << std::endl; 99 | 100 | std::cout << "2d: std::array, 3> :" << std::endl; 101 | for (auto x = 0; x < 3; x++) { 102 | for (auto y = 0; y < 2; y++) { 103 | std::cout << " 2d[" << x << "][" << y << "] = " << dd[ x ][ y ] << std::endl; 104 | } 105 | } 106 | std::cout << std::endl; 107 | 108 | std::cout << "3d: std::array, 3>, 4> :" << std::endl; 109 | for (auto x = 0; x < 4; x++) { 110 | for (auto y = 0; y < 3; y++) { 111 | for (auto z = 0; z < 2; z++) { 112 | std::cout << " 3d[" << x << "][" << y << "][" << z << "] = " << ddd[ x ][ y ][ z ] << std::endl; 113 | } 114 | } 115 | } 116 | std::cout << std::endl; 117 | } 118 | 119 | void container_example(void) 120 | { 121 | std::cout << "container_example" << std::endl; 122 | std::cout << "=================" << std::endl; 123 | save_container(std::string("container_example.bin")); 124 | load_container(std::string("container_example.bin")); 125 | } 126 | -------------------------------------------------------------------------------- /examples/custom_class_example.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include 3 | #include 4 | #include 5 | 6 | class Custom 7 | { 8 | public: 9 | int a; 10 | std::string b; 11 | std::vector< std::string > c; 12 | 13 | friend std::ostream &operator<<(std::ostream &out, Bits< const class Custom & > const my) 14 | { 15 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 16 | std::cout << "write custom class" << std::endl; 17 | #endif 18 | out << bits(my.t.a) << bits(my.t.b) << bits(my.t.c); 19 | return (out); 20 | } 21 | 22 | friend std::istream &operator>>(std::istream &in, Bits< class Custom & > my) 23 | { 24 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 25 | std::cout << "read custom class" << std::endl; 26 | #endif 27 | in >> bits(my.t.a) >> bits(my.t.b) >> bits(my.t.c); 28 | return (in); 29 | } 30 | 31 | friend std::ostream &operator<<(std::ostream &out, class Custom &my) 32 | { 33 | out << "a:" << my.a << " b:" << my.b; 34 | 35 | out << " c:[" << my.c.size() << " elems]:"; 36 | for (auto v : my.c) { 37 | out << v << " "; 38 | } 39 | out << std::endl; 40 | 41 | return (out); 42 | } 43 | }; 44 | 45 | static void save(const std::string filename, const class Custom &c) 46 | { 47 | std::cout << "save to " << filename << std::endl; 48 | std::ofstream out(filename, std::ios::binary); 49 | 50 | out << bits(c); 51 | } 52 | 53 | static void load(const std::string filename, class Custom &c) 54 | { 55 | std::cout << "read from " << filename << std::endl; 56 | std::ifstream in(filename); 57 | 58 | in >> bits(c); 59 | } 60 | 61 | static void save_custom_class_example(void) 62 | { 63 | auto c = Custom(); 64 | 65 | c.a = 1; 66 | c.b = "hello"; 67 | 68 | std::initializer_list< std::string > L1 = {"vec-elem1", "vec-elem2"}; 69 | std::vector< std::string > l1(L1); 70 | c.c = l1; 71 | 72 | save(std::string("custom_class.bin"), c); 73 | } 74 | 75 | static void load_custom_class_example(void) 76 | { 77 | auto c = Custom(); 78 | 79 | load(std::string("custom_class.bin"), c); 80 | std::cout << std::endl; 81 | std::cout << c; 82 | std::cout << std::endl; 83 | } 84 | 85 | void custom_class_example(void) 86 | { 87 | std::cout << "custom_class_example" << std::endl; 88 | std::cout << "====================" << std::endl; 89 | save_custom_class_example(); 90 | load_custom_class_example(); 91 | } 92 | -------------------------------------------------------------------------------- /examples/deque_example.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include 3 | #include 4 | #include 5 | 6 | static void save_deque_key_string_value_string(const std::string filename) 7 | { 8 | std::cout << "save to " << filename << std::endl; 9 | std::ofstream out(filename, std::ios::binary); 10 | 11 | std::deque< std::string > m; 12 | 13 | m.push_front(std::string("key1")); 14 | m.push_front(std::string("key2")); 15 | m.push_back(std::string("key3")); 16 | m.push_back(std::string("key4")); 17 | 18 | out << bits(m); 19 | } 20 | 21 | static void load_deque_key_string_value_string(const std::string filename) 22 | { 23 | std::cout << "read from " << filename << std::endl; 24 | std::ifstream in(filename); 25 | 26 | std::deque< std::string > m; 27 | 28 | in >> bits(m); 29 | std::cout << std::endl; 30 | 31 | std::cout << "m = " << m.size() << " list-elems { " << std::endl; 32 | for (auto i : m) { 33 | std::cout << " [" << i << "] " << std::endl; 34 | } 35 | std::cout << "}" << std::endl; 36 | std::cout << std::endl; 37 | } 38 | 39 | void deque_example(void) 40 | { 41 | std::cout << "deque key string, value string" << std::endl; 42 | std::cout << "============================" << std::endl; 43 | save_deque_key_string_value_string(std::string("deque_of_strings.bin")); 44 | load_deque_key_string_value_string(std::string("deque_of_strings.bin")); 45 | } 46 | -------------------------------------------------------------------------------- /examples/hexdump.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define HEX_DUMP_WIDTH (16) 7 | 8 | // 9 | // e.g. 10 | // 00000000 02 00 00 00 09 00 00 00 76 65 63 2D 65 6C 65 6D |........vec-elem| 11 | // 00000010 31 09 00 00 00 76 65 63 2D 65 6C 65 6D 32 02 00 |1....vec-elem2..| 12 | // 00000020 00 00 0A 00 00 00 6C 69 73 74 2D 65 6C 65 6D 31 |......list-elem1| 13 | // 00000030 0A 00 00 00 6C 69 73 74 2D 65 6C 65 6D 32 12 61 |....list-elem2.a| 14 | // 00000040 72 72 2D 65 6C 65 6D 31 00 00 00 00 00 00 00 00 |rr-elem1........| 15 | // 00000050 00 00 00 00 00 00 12 61 72 72 2D 65 6C 65 6D 32 |.......arr-elem2| 16 | // 00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 30 31 |..............01| 17 | // 00000070 32 33 34 35 61 62 63 64 65 66 67 68 69 6A 6B 6C |2345abcdefghi 18 | // 19 | void hexdump(const char *addr, size_t len) 20 | { 21 | int skipping_blanks = false; 22 | unsigned char empty[ HEX_DUMP_WIDTH ] = {0}; 23 | unsigned char buf[ HEX_DUMP_WIDTH + 1 ]; 24 | unsigned char *pc = (__typeof__(pc)) addr; 25 | size_t i; 26 | unsigned int x; 27 | 28 | std::cout << std::dec << len << " bytes:" << std::endl; 29 | 30 | if (! len) { 31 | return; 32 | } 33 | 34 | for (i = 0, x = 0; i < len; i++, x++) { 35 | if ((i % HEX_DUMP_WIDTH) == 0) { 36 | if (! skipping_blanks) { 37 | if (i != 0) { 38 | std::cout << " |" << std::setw(HEX_DUMP_WIDTH) << buf << "|" << std::endl; 39 | } 40 | } 41 | 42 | /* 43 | * Skip blank blocks. 44 | */ 45 | if (! memcmp(pc + i, empty, sizeof(empty))) { 46 | i += HEX_DUMP_WIDTH - 1; 47 | skipping_blanks = true; 48 | buf[ 0 ] = '\0'; 49 | continue; 50 | } 51 | 52 | std::cout << " " << std::setfill('0') << std::setw(4) << std::hex << i; 53 | 54 | x = 0; 55 | } 56 | 57 | if (x && (((i % (HEX_DUMP_WIDTH / 2))) == 0)) { 58 | std::cout << " "; 59 | } 60 | 61 | skipping_blanks = false; 62 | 63 | std::cout << " " << std::setfill('0') << std::setw(2) << std::hex << (int) pc[ i ]; 64 | 65 | if ((pc[ i ] < ' ') || (pc[ i ] > '~')) { 66 | buf[ i % HEX_DUMP_WIDTH ] = '.'; 67 | } else { 68 | buf[ i % HEX_DUMP_WIDTH ] = pc[ i ]; 69 | } 70 | 71 | buf[ (i % HEX_DUMP_WIDTH) + 1 ] = '\0'; 72 | } 73 | 74 | if (! buf[ 0 ]) { 75 | if (skipping_blanks) { 76 | std::cout << " *\n"; 77 | } 78 | 79 | return; 80 | } 81 | 82 | while ((i % HEX_DUMP_WIDTH) != 0) { 83 | std::cout << " "; 84 | if (i && (((i % (HEX_DUMP_WIDTH / 2))) == 0)) { 85 | std::cout << " "; 86 | } 87 | 88 | i++; 89 | } 90 | 91 | std::cout << " |" << std::setw(-HEX_DUMP_WIDTH) << buf << "|" << std::endl; 92 | } 93 | 94 | void hexdump(std::vector< char > &v) { hexdump(v.data(), v.size()); } 95 | -------------------------------------------------------------------------------- /examples/hexdump.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void hexdump(std::vector< char > &v); 5 | void hexdump(const char *addr, size_t len); 6 | -------------------------------------------------------------------------------- /examples/main.cpp: -------------------------------------------------------------------------------- 1 | 2 | int main(int argc, char *argv[]) 3 | { 4 | extern void basic_example(void); 5 | basic_example(); 6 | 7 | extern void container_example(void); 8 | container_example(); 9 | 10 | extern void raw_memory_example(void); 11 | raw_memory_example(); 12 | 13 | extern void map_example(void); 14 | map_example(); 15 | 16 | extern void unordered_map_example(void); 17 | unordered_map_example(); 18 | 19 | extern void map_string_to_list_of_strings_example(void); 20 | map_string_to_list_of_strings_example(); 21 | 22 | extern void custom_class_example(void); 23 | custom_class_example(); 24 | 25 | extern void template_class_example(void); 26 | template_class_example(); 27 | 28 | extern void map_custom_class_example(void); 29 | map_custom_class_example(); 30 | 31 | extern void set_example(void); 32 | set_example(); 33 | 34 | extern void multiset_example(void); 35 | multiset_example(); 36 | 37 | extern void deque_example(void); 38 | deque_example(); 39 | 40 | extern void custom_bitset_class_example(); 41 | custom_bitset_class_example(); 42 | 43 | extern void zipper_container_example(); 44 | zipper_container_example(); 45 | 46 | return (0); 47 | } 48 | -------------------------------------------------------------------------------- /examples/map_custom_class_example.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include 3 | #include 4 | #include 5 | 6 | class MappedClass 7 | { 8 | public: 9 | int a; 10 | std::string b; 11 | std::vector< std::string > c; 12 | 13 | friend std::ostream &operator<<(std::ostream &out, Bits< class MappedClass & > const my) 14 | { 15 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 16 | std::cout << "write custom class" << std::endl; 17 | #endif 18 | out << bits(my.t.a) << bits(my.t.b) << bits(my.t.c); 19 | return (out); 20 | } 21 | 22 | friend std::ostream &operator<<(std::ostream &out, Bits< const class MappedClass & > const my) 23 | { 24 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 25 | std::cout << "write custom class" << std::endl; 26 | #endif 27 | out << bits(my.t.a) << bits(my.t.b) << bits(my.t.c); 28 | return (out); 29 | } 30 | 31 | friend std::istream &operator>>(std::istream &in, Bits< class MappedClass & > my) 32 | { 33 | #ifdef DEBUG_C_PLUS_PLUS_SERIALIZER 34 | std::cout << "read custom class" << std::endl; 35 | #endif 36 | in >> bits(my.t.a) >> bits(my.t.b) >> bits(my.t.c); 37 | return (in); 38 | } 39 | 40 | friend std::ostream &operator<<(std::ostream &out, class MappedClass &my) 41 | { 42 | out << "a:" << my.a << " b:" << my.b; 43 | 44 | out << " c:[" << my.c.size() << " elems]:"; 45 | for (auto v : my.c) { 46 | out << v << " "; 47 | } 48 | out << std::endl; 49 | 50 | return (out); 51 | } 52 | }; 53 | 54 | static void save_map_key_string_value_custom(const std::string filename) 55 | { 56 | std::cout << "save to " << filename << std::endl; 57 | std::ofstream out(filename, std::ios::binary); 58 | 59 | std::map< std::string, class MappedClass > m; 60 | 61 | auto c1 = MappedClass(); 62 | c1.a = 1; 63 | c1.b = "hello"; 64 | std::initializer_list< std::string > L1 = {"vec-elem1", "vec-elem2"}; 65 | std::vector< std::string > l1(L1); 66 | c1.c = l1; 67 | 68 | auto c2 = MappedClass(); 69 | c2.a = 2; 70 | c2.b = "there"; 71 | std::initializer_list< std::string > L2 = {"vec-elem3", "vec-elem4"}; 72 | std::vector< std::string > l2(L2); 73 | c2.c = l2; 74 | 75 | m.insert(std::make_pair(std::string("key1"), c1)); 76 | m.insert(std::make_pair(std::string("key2"), c2)); 77 | 78 | out << bits(m); 79 | } 80 | 81 | static void load_map_key_string_value_custom(const std::string filename) 82 | { 83 | std::cout << "read from " << filename << std::endl; 84 | std::ifstream in(filename); 85 | 86 | std::map< std::string, class MappedClass > m; 87 | 88 | in >> bits(m); 89 | std::cout << std::endl; 90 | 91 | std::cout << "m = " << m.size() << " list-elems { " << std::endl; 92 | for (auto i : m) { 93 | std::cout << " [" << i.first << "] = " << i.second; 94 | } 95 | std::cout << "}" << std::endl; 96 | std::cout << std::endl; 97 | } 98 | 99 | void map_custom_class_example(void) 100 | { 101 | std::cout << "map key string, value class" << std::endl; 102 | std::cout << "===========================" << std::endl; 103 | save_map_key_string_value_custom(std::string("map_of_custom_class.bin")); 104 | load_map_key_string_value_custom(std::string("map_of_custom_class.bin")); 105 | } 106 | -------------------------------------------------------------------------------- /examples/map_example.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include 3 | #include 4 | #include 5 | 6 | static void save_map_key_string_value_string(const std::string filename) 7 | { 8 | std::cout << "save to " << filename << std::endl; 9 | std::ofstream out(filename, std::ios::binary); 10 | 11 | std::map< std::string, std::string > m; 12 | 13 | m.insert(std::make_pair(std::string("key1"), std::string("value1"))); 14 | m.insert(std::make_pair(std::string("key2"), std::string("value2"))); 15 | m.insert(std::make_pair(std::string("key3"), std::string("value3"))); 16 | m.insert(std::make_pair(std::string("key4"), std::string("value4"))); 17 | 18 | out << bits(m); 19 | } 20 | 21 | static void load_map_key_string_value_string(const std::string filename) 22 | { 23 | std::cout << "read from " << filename << std::endl; 24 | std::ifstream in(filename); 25 | 26 | std::map< std::string, std::string > m; 27 | 28 | in >> bits(m); 29 | std::cout << std::endl; 30 | 31 | std::cout << "m = " << m.size() << " list-elems { " << std::endl; 32 | for (auto i : m) { 33 | std::cout << " [" << i.first << "] = " << i.second << std::endl; 34 | } 35 | std::cout << "}" << std::endl; 36 | std::cout << std::endl; 37 | } 38 | 39 | void map_example(void) 40 | { 41 | std::cout << "map key string, value string" << std::endl; 42 | std::cout << "============================" << std::endl; 43 | save_map_key_string_value_string(std::string("map_of_strings.bin")); 44 | load_map_key_string_value_string(std::string("map_of_strings.bin")); 45 | } 46 | -------------------------------------------------------------------------------- /examples/map_string_to_list_of_strings_example.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include 3 | #include 4 | #include 5 | 6 | static void save_map_key_string_value_list_of_strings(const std::string filename) 7 | { 8 | std::cout << "save to " << filename << std::endl; 9 | std::ofstream out(filename, std::ios::binary); 10 | 11 | std::map< std::string, std::list< std::string > > m; 12 | 13 | std::initializer_list< std::string > L1 = {"list-elem1", "list-elem2"}; 14 | std::list< std::string > l1(L1); 15 | std::initializer_list< std::string > L2 = {"list-elem3", "list-elem4"}; 16 | std::list< std::string > l2(L2); 17 | 18 | m.insert(std::make_pair(std::string("key1"), l1)); 19 | m.insert(std::make_pair(std::string("key2"), l2)); 20 | 21 | out << bits(m); 22 | } 23 | 24 | static void load_map_key_string_value_list_of_strings(const std::string filename) 25 | { 26 | std::cout << "read from " << filename << std::endl; 27 | std::ifstream in(filename); 28 | 29 | std::map< std::string, std::list< std::string > > m; 30 | 31 | in >> bits(m); 32 | std::cout << std::endl; 33 | 34 | std::cout << "m = " << m.size() << " list-elems { " << std::endl; 35 | for (auto i : m) { 36 | std::cout << " [" << i.first << "] = [ "; 37 | for (auto j : i.second) { 38 | std::cout << j << ", "; 39 | } 40 | std::cout << "]" << std::endl; 41 | } 42 | std::cout << "}" << std::endl; 43 | std::cout << std::endl; 44 | } 45 | 46 | void map_string_to_list_of_strings_example(void) 47 | { 48 | std::cout << "map key string, value list of strings" << std::endl; 49 | std::cout << "=====================================" << std::endl; 50 | save_map_key_string_value_list_of_strings(std::string("map_of_string_to_list_of_strings.bin")); 51 | load_map_key_string_value_list_of_strings(std::string("map_of_string_to_list_of_strings.bin")); 52 | } 53 | -------------------------------------------------------------------------------- /examples/multiset_example.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include 3 | #include 4 | #include 5 | 6 | static void save_multiset_key_string_value_string(const std::string filename) 7 | { 8 | std::cout << "save to " << filename << std::endl; 9 | std::ofstream out(filename, std::ios::binary); 10 | 11 | std::multiset< std::string > m; 12 | 13 | m.insert(std::string("key1")); 14 | m.insert(std::string("key1")); 15 | m.insert(std::string("key1")); 16 | m.insert(std::string("key1")); 17 | m.insert(std::string("key2")); 18 | m.insert(std::string("key3")); 19 | m.insert(std::string("key4")); 20 | 21 | out << bits(m); 22 | } 23 | 24 | static void load_multiset_key_string_value_string(const std::string filename) 25 | { 26 | std::cout << "read from " << filename << std::endl; 27 | std::ifstream in(filename); 28 | 29 | std::multiset< std::string > m; 30 | 31 | in >> bits(m); 32 | std::cout << std::endl; 33 | 34 | std::cout << "m = " << m.size() << " list-elems { " << std::endl; 35 | for (auto i : m) { 36 | std::cout << " [" << i << "] " << std::endl; 37 | } 38 | std::cout << "}" << std::endl; 39 | std::cout << std::endl; 40 | } 41 | 42 | void multiset_example(void) 43 | { 44 | std::cout << "multiset key string, value string" << std::endl; 45 | std::cout << "============================" << std::endl; 46 | save_multiset_key_string_value_string(std::string("multiset_of_strings.bin")); 47 | load_multiset_key_string_value_string(std::string("multiset_of_strings.bin")); 48 | } 49 | -------------------------------------------------------------------------------- /examples/quicklz.cpp: -------------------------------------------------------------------------------- 1 | // Fast data compression library 2 | // Copyright (C) 2006-2011 Lasse Mikkel Reinhold 3 | // lar@quicklz.com 4 | // 5 | // QuickLZ can be used for free under the GPL 1, 2 or 3 license (where anything 6 | // released into public must be open source) or under a commercial license if such 7 | // has been acquired (see http://www.quicklz.com/order.html). The commercial license 8 | // does not cover derived or ported versions created by third parties under GPL. 9 | 10 | // 1.5.0 final 11 | 12 | #include "quicklz.h" 13 | 14 | #if QLZ_VERSION_MAJOR != 1 || QLZ_VERSION_MINOR != 5 || QLZ_VERSION_REVISION != 0 15 | #error quicklz.c and quicklz.h have different versions 16 | #endif 17 | 18 | #if (defined(__X86__) || defined(__i386__) || defined(i386) || defined(_M_IX86) || defined(__386__) || \ 19 | defined(__x86_64__) || defined(_M_X64)) 20 | #define X86X64 21 | #endif 22 | 23 | #define MINOFFSET 2 24 | #define UNCONDITIONAL_MATCHLEN 6 25 | #define UNCOMPRESSED_END 4 26 | #define CWORD_LEN 4 27 | 28 | #if QLZ_COMPRESSION_LEVEL == 1 && defined QLZ_PTR_64 && QLZ_STREAMING_BUFFER == 0 29 | #define OFFSET_BASE source 30 | #define CAST (ui32)(size_t) 31 | #else 32 | #define OFFSET_BASE 0 33 | #define CAST 34 | #endif 35 | 36 | int qlz_get_setting(int setting) 37 | { 38 | switch (setting) { 39 | case 0 : return QLZ_COMPRESSION_LEVEL; 40 | case 1 : return sizeof(qlz_state_compress); 41 | case 2 : return sizeof(qlz_state_decompress); 42 | case 3 : return QLZ_STREAMING_BUFFER; 43 | #ifdef QLZ_MEMORY_SAFE 44 | case 6 : return 1; 45 | #else 46 | case 6 : return 0; 47 | #endif 48 | case 7 : return QLZ_VERSION_MAJOR; 49 | case 8 : return QLZ_VERSION_MINOR; 50 | case 9 : return QLZ_VERSION_REVISION; 51 | } 52 | return -1; 53 | } 54 | 55 | #if QLZ_COMPRESSION_LEVEL == 1 56 | static int same(const unsigned char *src, size_t n) 57 | { 58 | while (n > 0 && *(src + n) == *src) 59 | n--; 60 | return n == 0 ? 1 : 0; 61 | } 62 | #endif 63 | 64 | static void reset_table_compress(qlz_state_compress *state) 65 | { 66 | int i; 67 | for (i = 0; i < QLZ_HASH_VALUES; i++) { 68 | #if QLZ_COMPRESSION_LEVEL == 1 69 | state->hash[ i ].offset = 0; 70 | #else 71 | state->hash_counter[ i ] = 0; 72 | #endif 73 | } 74 | } 75 | 76 | static void reset_table_decompress(qlz_state_decompress *state) 77 | { 78 | int i; 79 | (void) state; 80 | (void) i; 81 | #if QLZ_COMPRESSION_LEVEL == 2 82 | for (i = 0; i < QLZ_HASH_VALUES; i++) { 83 | state->hash_counter[ i ] = 0; 84 | } 85 | #endif 86 | } 87 | 88 | static __inline ui32 hash_func(ui32 i) 89 | { 90 | #if QLZ_COMPRESSION_LEVEL == 2 91 | return ((i >> 9) ^ (i >> 13) ^ i) & (QLZ_HASH_VALUES - 1); 92 | #else 93 | return ((i >> 12) ^ i) & (QLZ_HASH_VALUES - 1); 94 | #endif 95 | } 96 | 97 | static __inline ui32 fast_read(void const *src, ui32 bytes) 98 | { 99 | #ifndef X86X64 100 | unsigned char *p = (unsigned char *) src; 101 | switch (bytes) { 102 | case 4 : return (*p | *(p + 1) << 8 | *(p + 2) << 16 | *(p + 3) << 24); 103 | case 3 : return (*p | *(p + 1) << 8 | *(p + 2) << 16); 104 | case 2 : return (*p | *(p + 1) << 8); 105 | case 1 : return (*p); 106 | } 107 | return 0; 108 | #else 109 | if (bytes >= 1 && bytes <= 4) 110 | return *((ui32 *) src); 111 | else 112 | return 0; 113 | #endif 114 | } 115 | 116 | static __inline ui32 hashat(const unsigned char *src) 117 | { 118 | ui32 fetch, hash; 119 | fetch = fast_read(src, 3); 120 | hash = hash_func(fetch); 121 | return hash; 122 | } 123 | 124 | static __inline void fast_write(ui32 f, void *dst, size_t bytes) 125 | { 126 | #ifndef X86X64 127 | unsigned char *p = (unsigned char *) dst; 128 | 129 | switch (bytes) { 130 | case 4 : 131 | *p = (unsigned char) f; 132 | *(p + 1) = (unsigned char) (f >> 8); 133 | *(p + 2) = (unsigned char) (f >> 16); 134 | *(p + 3) = (unsigned char) (f >> 24); 135 | return; 136 | case 3 : 137 | *p = (unsigned char) f; 138 | *(p + 1) = (unsigned char) (f >> 8); 139 | *(p + 2) = (unsigned char) (f >> 16); 140 | return; 141 | case 2 : 142 | *p = (unsigned char) f; 143 | *(p + 1) = (unsigned char) (f >> 8); 144 | return; 145 | case 1 : *p = (unsigned char) f; return; 146 | } 147 | #else 148 | switch (bytes) { 149 | case 4 : *((ui32 *) dst) = f; return; 150 | case 3 : *((ui32 *) dst) = f; return; 151 | case 2 : *((ui16 *) dst) = (ui16) f; return; 152 | case 1 : *((unsigned char *) dst) = (unsigned char) f; return; 153 | } 154 | #endif 155 | } 156 | 157 | size_t qlz_size_decompressed(const char *source) 158 | { 159 | ui32 n, r; 160 | n = (((*source) & 2) == 2) ? 4 : 1; 161 | r = fast_read(source + 1 + n, n); 162 | r = r & (0xffffffff >> ((4 - n) * 8)); 163 | return r; 164 | } 165 | 166 | size_t qlz_size_compressed(const char *source) 167 | { 168 | ui32 n, r; 169 | n = (((*source) & 2) == 2) ? 4 : 1; 170 | r = fast_read(source + 1, n); 171 | r = r & (0xffffffff >> ((4 - n) * 8)); 172 | return r; 173 | } 174 | 175 | size_t qlz_size_header(const char *source) 176 | { 177 | size_t n = 2 * ((((*source) & 2) == 2) ? 4 : 1) + 1; 178 | return n; 179 | } 180 | 181 | static __inline void memcpy_up(unsigned char *dst, const unsigned char *src, ui32 n) 182 | { 183 | // Caution if modifying memcpy_up! Overlap of dst and src must be special handled. 184 | #ifndef X86X64 185 | unsigned char *end = dst + n; 186 | while (dst < end) { 187 | *dst = *src; 188 | dst++; 189 | src++; 190 | } 191 | #else 192 | ui32 f = 0; 193 | do { 194 | *(ui32 *) (dst + f) = *(ui32 *) (src + f); 195 | f += MINOFFSET + 1; 196 | } while (f < n); 197 | #endif 198 | } 199 | 200 | #if QLZ_COMPRESSION_LEVEL != 3 201 | static __inline void update_hash(qlz_state_decompress *state, const unsigned char *s) 202 | { 203 | #if QLZ_COMPRESSION_LEVEL == 1 204 | ui32 hash; 205 | hash = hashat(s); 206 | state->hash[ hash ].offset = s; 207 | state->hash_counter[ hash ] = 1; 208 | #elif QLZ_COMPRESSION_LEVEL == 2 209 | ui32 hash; 210 | unsigned char c; 211 | hash = hashat(s); 212 | c = state->hash_counter[ hash ]; 213 | state->hash[ hash ].offset[ c & (QLZ_POINTERS - 1) ] = s; 214 | c++; 215 | state->hash_counter[ hash ] = c; 216 | #endif 217 | (void) state; 218 | (void) s; 219 | } 220 | #endif 221 | 222 | #if QLZ_COMPRESSION_LEVEL <= 2 223 | static void update_hash_upto(qlz_state_decompress *state, unsigned char **lh, const unsigned char *max) 224 | { 225 | while (*lh < max) { 226 | (*lh)++; 227 | update_hash(state, *lh); 228 | } 229 | } 230 | #endif 231 | 232 | static size_t qlz_compress_core(const unsigned char *source, unsigned char *destination, size_t size, 233 | qlz_state_compress *state) 234 | { 235 | const unsigned char *last_byte = source + size - 1; 236 | const unsigned char *src = source; 237 | unsigned char *cword_ptr = destination; 238 | unsigned char *dst = destination + CWORD_LEN; 239 | ui32 cword_val = 1U << 31; 240 | const unsigned char *last_matchstart = last_byte - UNCONDITIONAL_MATCHLEN - UNCOMPRESSED_END; 241 | ui32 fetch = 0; 242 | unsigned int lits = 0; 243 | 244 | (void) lits; 245 | 246 | if (src <= last_matchstart) 247 | fetch = fast_read(src, 3); 248 | 249 | while (src <= last_matchstart) { 250 | if ((cword_val & 1) == 1) { 251 | // store uncompressed if compression ratio is too low 252 | if (src > source + (size >> 1) && dst - destination > src - source - ((src - source) >> 5)) 253 | return 0; 254 | 255 | fast_write((cword_val >> 1) | (1U << 31), cword_ptr, CWORD_LEN); 256 | 257 | cword_ptr = dst; 258 | dst += CWORD_LEN; 259 | cword_val = 1U << 31; 260 | fetch = fast_read(src, 3); 261 | } 262 | #if QLZ_COMPRESSION_LEVEL == 1 263 | { 264 | const unsigned char *o; 265 | ui32 hash, cached; 266 | 267 | hash = hash_func(fetch); 268 | cached = fetch ^ state->hash[ hash ].cache; 269 | state->hash[ hash ].cache = fetch; 270 | 271 | o = state->hash[ hash ].offset + OFFSET_BASE; 272 | state->hash[ hash ].offset = CAST(src - OFFSET_BASE); 273 | 274 | #ifdef X86X64 275 | if ((cached & 0xffffff) == 0 && o != OFFSET_BASE && 276 | (src - o > MINOFFSET || (src == o + 1 && lits >= 3 && src > source + 3 && same(src - 3, 6)))) { 277 | if (cached != 0) { 278 | #else 279 | if (cached == 0 && o != OFFSET_BASE && 280 | (src - o > MINOFFSET || (src == o + 1 && lits >= 3 && src > source + 3 && same(src - 3, 6)))) { 281 | if (*(o + 3) != *(src + 3)) { 282 | #endif 283 | hash <<= 4; 284 | cword_val = (cword_val >> 1) | (1U << 31); 285 | fast_write((3 - 2) | hash, dst, 2); 286 | src += 3; 287 | dst += 2; 288 | } else { 289 | const unsigned char *old_src = src; 290 | size_t matchlen; 291 | hash <<= 4; 292 | 293 | cword_val = (cword_val >> 1) | (1U << 31); 294 | src += 4; 295 | 296 | if (*(o + (src - old_src)) == *src) { 297 | src++; 298 | if (*(o + (src - old_src)) == *src) { 299 | size_t q = last_byte - UNCOMPRESSED_END - (src - 5) + 1; 300 | size_t remaining = q > 255 ? 255 : q; 301 | src++; 302 | while (*(o + (src - old_src)) == *src && (size_t) (src - old_src) < remaining) 303 | src++; 304 | } 305 | } 306 | 307 | matchlen = src - old_src; 308 | if (matchlen < 18) { 309 | fast_write((ui32) (matchlen - 2) | hash, dst, 2); 310 | dst += 2; 311 | } else { 312 | fast_write((ui32) (matchlen << 16) | hash, dst, 3); 313 | dst += 3; 314 | } 315 | } 316 | fetch = fast_read(src, 3); 317 | lits = 0; 318 | } else { 319 | lits++; 320 | *dst = *src; 321 | src++; 322 | dst++; 323 | cword_val = (cword_val >> 1); 324 | #ifdef X86X64 325 | fetch = fast_read(src, 3); 326 | #else 327 | fetch = (fetch >> 8 & 0xffff) | (*(src + 2) << 16); 328 | #endif 329 | } 330 | } 331 | #elif QLZ_COMPRESSION_LEVEL >= 2 332 | { 333 | const unsigned char *o, *offset2; 334 | ui32 hash, matchlen, k, m, best_k = 0; 335 | unsigned char c; 336 | size_t remaining = 337 | (last_byte - UNCOMPRESSED_END - src + 1) > 255 ? 255 : (last_byte - UNCOMPRESSED_END - src + 1); 338 | (void) best_k; 339 | 340 | // hash = hashat(src); 341 | fetch = fast_read(src, 3); 342 | hash = hash_func(fetch); 343 | 344 | c = state->hash_counter[ hash ]; 345 | 346 | offset2 = state->hash[ hash ].offset[ 0 ]; 347 | if (offset2 < src - MINOFFSET && c > 0 && ((fast_read(offset2, 3) ^ fetch) & 0xffffff) == 0) { 348 | matchlen = 3; 349 | if (*(offset2 + matchlen) == *(src + matchlen)) { 350 | matchlen = 4; 351 | while (*(offset2 + matchlen) == *(src + matchlen) && matchlen < remaining) 352 | matchlen++; 353 | } 354 | } else 355 | matchlen = 0; 356 | for (k = 1; k < QLZ_POINTERS && c > k; k++) { 357 | o = state->hash[ hash ].offset[ k ]; 358 | #if QLZ_COMPRESSION_LEVEL == 3 359 | if (((fast_read(o, 3) ^ fetch) & 0xffffff) == 0 && o < src - MINOFFSET) 360 | #elif QLZ_COMPRESSION_LEVEL == 2 361 | if (*(src + matchlen) == *(o + matchlen) && ((fast_read(o, 3) ^ fetch) & 0xffffff) == 0 && 362 | o < src - MINOFFSET) 363 | #endif 364 | { 365 | m = 3; 366 | while (*(o + m) == *(src + m) && m < remaining) 367 | m++; 368 | #if QLZ_COMPRESSION_LEVEL == 3 369 | if ((m > matchlen) || (m == matchlen && o > offset2)) 370 | #elif QLZ_COMPRESSION_LEVEL == 2 371 | if (m > matchlen) 372 | #endif 373 | { 374 | offset2 = o; 375 | matchlen = m; 376 | best_k = k; 377 | } 378 | } 379 | } 380 | o = offset2; 381 | state->hash[ hash ].offset[ c & (QLZ_POINTERS - 1) ] = src; 382 | c++; 383 | state->hash_counter[ hash ] = c; 384 | 385 | #if QLZ_COMPRESSION_LEVEL == 3 386 | if (matchlen > 2 && src - o < 131071) { 387 | ui32 u; 388 | size_t offset = src - o; 389 | 390 | for (u = 1; u < matchlen; u++) { 391 | hash = hashat(src + u); 392 | c = state->hash_counter[ hash ]++; 393 | state->hash[ hash ].offset[ c & (QLZ_POINTERS - 1) ] = src + u; 394 | } 395 | 396 | cword_val = (cword_val >> 1) | (1U << 31); 397 | src += matchlen; 398 | 399 | if (matchlen == 3 && offset <= 63) { 400 | *dst = (unsigned char) (offset << 2); 401 | dst++; 402 | } else if (matchlen == 3 && offset <= 16383) { 403 | ui32 f = (ui32) ((offset << 2) | 1); 404 | fast_write(f, dst, 2); 405 | dst += 2; 406 | } else if (matchlen <= 18 && offset <= 1023) { 407 | ui32 f = ((matchlen - 3) << 2) | ((ui32) offset << 6) | 2; 408 | fast_write(f, dst, 2); 409 | dst += 2; 410 | } 411 | 412 | else if (matchlen <= 33) { 413 | ui32 f = ((matchlen - 2) << 2) | ((ui32) offset << 7) | 3; 414 | fast_write(f, dst, 3); 415 | dst += 3; 416 | } else { 417 | ui32 f = ((matchlen - 3) << 7) | ((ui32) offset << 15) | 3; 418 | fast_write(f, dst, 4); 419 | dst += 4; 420 | } 421 | } else { 422 | *dst = *src; 423 | src++; 424 | dst++; 425 | cword_val = (cword_val >> 1); 426 | } 427 | #elif QLZ_COMPRESSION_LEVEL == 2 428 | 429 | if (matchlen > 2) { 430 | cword_val = (cword_val >> 1) | (1U << 31); 431 | src += matchlen; 432 | 433 | if (matchlen < 10) { 434 | ui32 f = best_k | ((matchlen - 2) << 2) | (hash << 5); 435 | fast_write(f, dst, 2); 436 | dst += 2; 437 | } else { 438 | ui32 f = best_k | (matchlen << 16) | (hash << 5); 439 | fast_write(f, dst, 3); 440 | dst += 3; 441 | } 442 | } else { 443 | *dst = *src; 444 | src++; 445 | dst++; 446 | cword_val = (cword_val >> 1); 447 | } 448 | #endif 449 | } 450 | #endif 451 | } 452 | while (src <= last_byte) { 453 | if ((cword_val & 1) == 1) { 454 | fast_write((cword_val >> 1) | (1U << 31), cword_ptr, CWORD_LEN); 455 | cword_ptr = dst; 456 | dst += CWORD_LEN; 457 | cword_val = 1U << 31; 458 | } 459 | #if QLZ_COMPRESSION_LEVEL < 3 460 | if (src <= last_byte - 3) { 461 | #if QLZ_COMPRESSION_LEVEL == 1 462 | ui32 hash, fetch; 463 | fetch = fast_read(src, 3); 464 | hash = hash_func(fetch); 465 | state->hash[ hash ].offset = CAST(src - OFFSET_BASE); 466 | state->hash[ hash ].cache = fetch; 467 | #elif QLZ_COMPRESSION_LEVEL == 2 468 | ui32 hash; 469 | unsigned char c; 470 | hash = hashat(src); 471 | c = state->hash_counter[ hash ]; 472 | state->hash[ hash ].offset[ c & (QLZ_POINTERS - 1) ] = src; 473 | c++; 474 | state->hash_counter[ hash ] = c; 475 | #endif 476 | } 477 | #endif 478 | *dst = *src; 479 | src++; 480 | dst++; 481 | cword_val = (cword_val >> 1); 482 | } 483 | 484 | while ((cword_val & 1) != 1) 485 | cword_val = (cword_val >> 1); 486 | 487 | fast_write((cword_val >> 1) | (1U << 31), cword_ptr, CWORD_LEN); 488 | 489 | // min. size must be 9 bytes so that the qlz_size functions can take 9 bytes as argument 490 | return dst - destination < 9 ? 9 : dst - destination; 491 | } 492 | 493 | static size_t qlz_decompress_core(const unsigned char *source, unsigned char *destination, size_t size, 494 | qlz_state_decompress *state, const unsigned char *history) 495 | { 496 | const unsigned char *src = source + qlz_size_header((const char *) source); 497 | unsigned char *dst = destination; 498 | const unsigned char *last_destination_byte = destination + size - 1; 499 | ui32 cword_val = 1; 500 | const unsigned char *last_matchstart = last_destination_byte - UNCONDITIONAL_MATCHLEN - UNCOMPRESSED_END; 501 | unsigned char *last_hashed = destination - 1; 502 | const unsigned char *last_source_byte = source + qlz_size_compressed((const char *) source) - 1; 503 | static const ui32 bitlut[ 16 ] = {4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0}; 504 | 505 | (void) last_source_byte; 506 | (void) last_hashed; 507 | (void) state; 508 | (void) history; 509 | 510 | for (;;) { 511 | ui32 fetch; 512 | 513 | if (cword_val == 1) { 514 | #ifdef QLZ_MEMORY_SAFE 515 | if (src + CWORD_LEN - 1 > last_source_byte) 516 | return 0; 517 | #endif 518 | cword_val = fast_read(src, CWORD_LEN); 519 | src += CWORD_LEN; 520 | } 521 | 522 | #ifdef QLZ_MEMORY_SAFE 523 | if (src + 4 - 1 > last_source_byte) 524 | return 0; 525 | #endif 526 | 527 | fetch = fast_read(src, 4); 528 | 529 | if ((cword_val & 1) == 1) { 530 | ui32 matchlen; 531 | const unsigned char *offset2; 532 | 533 | #if QLZ_COMPRESSION_LEVEL == 1 534 | ui32 hash; 535 | cword_val = cword_val >> 1; 536 | hash = (fetch >> 4) & 0xfff; 537 | offset2 = (const unsigned char *) (size_t) state->hash[ hash ].offset; 538 | 539 | if ((fetch & 0xf) != 0) { 540 | matchlen = (fetch & 0xf) + 2; 541 | src += 2; 542 | } else { 543 | matchlen = *(src + 2); 544 | src += 3; 545 | } 546 | 547 | #elif QLZ_COMPRESSION_LEVEL == 2 548 | ui32 hash; 549 | unsigned char c; 550 | cword_val = cword_val >> 1; 551 | hash = (fetch >> 5) & 0x7ff; 552 | c = (unsigned char) (fetch & 0x3); 553 | offset2 = state->hash[ hash ].offset[ c ]; 554 | 555 | if ((fetch & (28)) != 0) { 556 | matchlen = ((fetch >> 2) & 0x7) + 2; 557 | src += 2; 558 | } else { 559 | matchlen = *(src + 2); 560 | src += 3; 561 | } 562 | 563 | #elif QLZ_COMPRESSION_LEVEL == 3 564 | ui32 offset; 565 | cword_val = cword_val >> 1; 566 | if ((fetch & 3) == 0) { 567 | offset = (fetch & 0xff) >> 2; 568 | matchlen = 3; 569 | src++; 570 | } else if ((fetch & 2) == 0) { 571 | offset = (fetch & 0xffff) >> 2; 572 | matchlen = 3; 573 | src += 2; 574 | } else if ((fetch & 1) == 0) { 575 | offset = (fetch & 0xffff) >> 6; 576 | matchlen = ((fetch >> 2) & 15) + 3; 577 | src += 2; 578 | } else if ((fetch & 127) != 3) { 579 | offset = (fetch >> 7) & 0x1ffff; 580 | matchlen = ((fetch >> 2) & 0x1f) + 2; 581 | src += 3; 582 | } else { 583 | offset = (fetch >> 15); 584 | matchlen = ((fetch >> 7) & 255) + 3; 585 | src += 4; 586 | } 587 | 588 | offset2 = dst - offset; 589 | #endif 590 | 591 | #ifdef QLZ_MEMORY_SAFE 592 | if (offset2 < history || offset2 > dst - MINOFFSET - 1) 593 | return 0; 594 | 595 | if (matchlen > (ui32) (last_destination_byte - dst - UNCOMPRESSED_END + 1)) 596 | return 0; 597 | #endif 598 | 599 | memcpy_up(dst, offset2, matchlen); 600 | dst += matchlen; 601 | 602 | #if QLZ_COMPRESSION_LEVEL <= 2 603 | update_hash_upto(state, &last_hashed, dst - matchlen); 604 | last_hashed = dst - 1; 605 | #endif 606 | } else { 607 | if (dst < last_matchstart) { 608 | unsigned int n = bitlut[ cword_val & 0xf ]; 609 | #ifdef X86X64 610 | *(ui32 *) dst = *(ui32 *) src; 611 | #else 612 | memcpy_up(dst, src, 4); 613 | #endif 614 | cword_val = cword_val >> n; 615 | dst += n; 616 | src += n; 617 | #if QLZ_COMPRESSION_LEVEL <= 2 618 | update_hash_upto(state, &last_hashed, dst - 3); 619 | #endif 620 | } else { 621 | while (dst <= last_destination_byte) { 622 | if (cword_val == 1) { 623 | src += CWORD_LEN; 624 | cword_val = 1U << 31; 625 | } 626 | #ifdef QLZ_MEMORY_SAFE 627 | if (src >= last_source_byte + 1) 628 | return 0; 629 | #endif 630 | *dst = *src; 631 | dst++; 632 | src++; 633 | cword_val = cword_val >> 1; 634 | } 635 | 636 | #if QLZ_COMPRESSION_LEVEL <= 2 637 | update_hash_upto(state, &last_hashed, last_destination_byte - 3); // todo, use constant 638 | #endif 639 | return size; 640 | } 641 | } 642 | } 643 | } 644 | 645 | size_t qlz_compress(const void *source, char *destination, size_t size, qlz_state_compress *state) 646 | { 647 | size_t r; 648 | ui32 compressed; 649 | size_t base; 650 | 651 | if (size == 0 || size > 0xffffffff - 400) 652 | return 0; 653 | 654 | if (size < 216) 655 | base = 3; 656 | else 657 | base = 9; 658 | 659 | #if QLZ_STREAMING_BUFFER > 0 660 | if (state->stream_counter + size - 1 >= QLZ_STREAMING_BUFFER) 661 | #endif 662 | { 663 | reset_table_compress(state); 664 | r = base + qlz_compress_core((const unsigned char *) source, (unsigned char *) destination + base, size, state); 665 | #if QLZ_STREAMING_BUFFER > 0 666 | reset_table_compress(state); 667 | #endif 668 | if (r == base) { 669 | memcpy(destination + base, source, size); 670 | r = size + base; 671 | compressed = 0; 672 | } else { 673 | compressed = 1; 674 | } 675 | state->stream_counter = 0; 676 | } 677 | #if QLZ_STREAMING_BUFFER > 0 678 | else { 679 | unsigned char *src = state->stream_buffer + state->stream_counter; 680 | 681 | memcpy(src, source, size); 682 | r = base + qlz_compress_core(src, (unsigned char *) destination + base, size, state); 683 | 684 | if (r == base) { 685 | memcpy(destination + base, src, size); 686 | r = size + base; 687 | compressed = 0; 688 | reset_table_compress(state); 689 | } else { 690 | compressed = 1; 691 | } 692 | state->stream_counter += size; 693 | } 694 | #endif 695 | if (base == 3) { 696 | *destination = (unsigned char) (0 | compressed); 697 | *(destination + 1) = (unsigned char) r; 698 | *(destination + 2) = (unsigned char) size; 699 | } else { 700 | *destination = (unsigned char) (2 | compressed); 701 | fast_write((ui32) r, destination + 1, 4); 702 | fast_write((ui32) size, destination + 5, 4); 703 | } 704 | 705 | *destination |= (QLZ_COMPRESSION_LEVEL << 2); 706 | *destination |= (1 << 6); 707 | *destination |= 708 | ((QLZ_STREAMING_BUFFER == 0 ? 0 709 | : (QLZ_STREAMING_BUFFER == 100000 ? 1 : (QLZ_STREAMING_BUFFER == 1000000 ? 2 : 3))) 710 | << 4); 711 | 712 | // 76543210 713 | // 01SSLLHC 714 | 715 | return r; 716 | } 717 | 718 | size_t qlz_decompress(const char *source, void *destination, qlz_state_decompress *state) 719 | { 720 | size_t dsiz = qlz_size_decompressed(source); 721 | 722 | #if QLZ_STREAMING_BUFFER > 0 723 | if (state->stream_counter + qlz_size_decompressed(source) - 1 >= QLZ_STREAMING_BUFFER) 724 | #endif 725 | { 726 | if ((*source & 1) == 1) { 727 | reset_table_decompress(state); 728 | dsiz = qlz_decompress_core((const unsigned char *) source, (unsigned char *) destination, dsiz, state, 729 | (const unsigned char *) destination); 730 | } else { 731 | memcpy(destination, source + qlz_size_header(source), dsiz); 732 | } 733 | state->stream_counter = 0; 734 | reset_table_decompress(state); 735 | } 736 | #if QLZ_STREAMING_BUFFER > 0 737 | else { 738 | unsigned char *dst = state->stream_buffer + state->stream_counter; 739 | if ((*source & 1) == 1) { 740 | dsiz = qlz_decompress_core((const unsigned char *) source, dst, dsiz, state, 741 | (const unsigned char *) state->stream_buffer); 742 | } else { 743 | memcpy(dst, source + qlz_size_header(source), dsiz); 744 | reset_table_decompress(state); 745 | } 746 | memcpy(destination, dst, dsiz); 747 | state->stream_counter += dsiz; 748 | } 749 | #endif 750 | return dsiz; 751 | } 752 | -------------------------------------------------------------------------------- /examples/quicklz.h: -------------------------------------------------------------------------------- 1 | #ifndef QLZ_HEADER 2 | #define QLZ_HEADER 3 | 4 | // Fast data compression library 5 | // Copyright (C) 2006-2011 Lasse Mikkel Reinhold 6 | // lar@quicklz.com 7 | // 8 | // QuickLZ can be used for free under the GPL 1, 2 or 3 license (where anything 9 | // released into public must be open source) or under a commercial license if such 10 | // has been acquired (see http://www.quicklz.com/order.html). The commercial license 11 | // does not cover derived or ported versions created by third parties under GPL. 12 | 13 | // You can edit following user settings. Data must be decompressed with the same 14 | // setting of QLZ_COMPRESSION_LEVEL and QLZ_STREAMING_BUFFER as it was compressed 15 | // (see manual). If QLZ_STREAMING_BUFFER > 0, scratch buffers must be initially 16 | // zeroed out (see manual). First #ifndef makes it possible to define settings from 17 | // the outside like the compiler command line. 18 | 19 | // 1.5.0 final 20 | 21 | #ifndef QLZ_COMPRESSION_LEVEL 22 | 23 | // 1 gives fastest compression speed. 3 gives fastest decompression speed and best 24 | // compression ratio. 25 | // 26 | //#define QLZ_COMPRESSION_LEVEL 1 27 | //#define QLZ_COMPRESSION_LEVEL 2 28 | #define QLZ_COMPRESSION_LEVEL 3 29 | 30 | // If > 0, zero out both states prior to first call to qlz_compress() or qlz_decompress() 31 | // and decompress packets in the same order as they were compressed 32 | #define QLZ_STREAMING_BUFFER 0 33 | //#define QLZ_STREAMING_BUFFER 100000 34 | //#define QLZ_STREAMING_BUFFER 1000000 35 | 36 | // Guarantees that decompression of corrupted data cannot crash. Decreases decompression 37 | // speed 10-20%. Compression speed not affected. 38 | //#define QLZ_MEMORY_SAFE 39 | #endif 40 | 41 | #define QLZ_VERSION_MAJOR 1 42 | #define QLZ_VERSION_MINOR 5 43 | #define QLZ_VERSION_REVISION 0 44 | 45 | // Using size_t, memset() and memcpy() 46 | #include 47 | 48 | // Verify compression level 49 | #if QLZ_COMPRESSION_LEVEL != 1 && QLZ_COMPRESSION_LEVEL != 2 && QLZ_COMPRESSION_LEVEL != 3 50 | #error QLZ_COMPRESSION_LEVEL must be 1, 2 or 3 51 | #endif 52 | 53 | typedef unsigned int ui32; 54 | typedef unsigned short int ui16; 55 | 56 | // Decrease QLZ_POINTERS for level 3 to increase compression speed. Do not touch any other values! 57 | #if QLZ_COMPRESSION_LEVEL == 1 58 | #define QLZ_POINTERS 1 59 | #define QLZ_HASH_VALUES 4096 60 | #elif QLZ_COMPRESSION_LEVEL == 2 61 | #define QLZ_POINTERS 4 62 | #define QLZ_HASH_VALUES 2048 63 | #elif QLZ_COMPRESSION_LEVEL == 3 64 | #define QLZ_POINTERS 16 65 | #define QLZ_HASH_VALUES 4096 66 | #endif 67 | 68 | // Detect if pointer size is 64-bit. It's not fatal if some 64-bit target is not detected because this is only for 69 | // adding an optional 64-bit optimization. 70 | #if defined _LP64 || defined __LP64__ || defined __64BIT__ || _ADDR64 || defined _WIN64 || defined __arch64__ || \ 71 | __WORDSIZE == 64 || (defined __sparc && defined __sparcv9) || defined __x86_64 || defined __amd64 || \ 72 | defined __x86_64__ || defined _M_X64 || defined _M_IA64 || defined __ia64 || defined __IA64__ 73 | #define QLZ_PTR_64 74 | #endif 75 | 76 | // hash entry 77 | typedef struct { 78 | #if QLZ_COMPRESSION_LEVEL == 1 79 | ui32 cache; 80 | #if defined QLZ_PTR_64 && QLZ_STREAMING_BUFFER == 0 81 | unsigned int offset; 82 | #else 83 | const unsigned char *offset; 84 | #endif 85 | #else 86 | const unsigned char *offset[ QLZ_POINTERS ]; 87 | #endif 88 | 89 | } qlz_hash_compress; 90 | 91 | typedef struct { 92 | #if QLZ_COMPRESSION_LEVEL == 1 93 | const unsigned char *offset; 94 | #else 95 | const unsigned char *offset[ QLZ_POINTERS ]; 96 | #endif 97 | } qlz_hash_decompress; 98 | 99 | // states 100 | typedef struct { 101 | #if QLZ_STREAMING_BUFFER > 0 102 | unsigned char stream_buffer[ QLZ_STREAMING_BUFFER ]; 103 | #endif 104 | size_t stream_counter; 105 | qlz_hash_compress hash[ QLZ_HASH_VALUES ]; 106 | unsigned char hash_counter[ QLZ_HASH_VALUES ]; 107 | } qlz_state_compress; 108 | 109 | #if QLZ_COMPRESSION_LEVEL == 1 || QLZ_COMPRESSION_LEVEL == 2 110 | typedef struct { 111 | #if QLZ_STREAMING_BUFFER > 0 112 | unsigned char stream_buffer[ QLZ_STREAMING_BUFFER ]; 113 | #endif 114 | qlz_hash_decompress hash[ QLZ_HASH_VALUES ]; 115 | unsigned char hash_counter[ QLZ_HASH_VALUES ]; 116 | size_t stream_counter; 117 | } qlz_state_decompress; 118 | #elif QLZ_COMPRESSION_LEVEL == 3 119 | typedef struct { 120 | #if QLZ_STREAMING_BUFFER > 0 121 | unsigned char stream_buffer[ QLZ_STREAMING_BUFFER ]; 122 | #endif 123 | #if QLZ_COMPRESSION_LEVEL <= 2 124 | qlz_hash_decompress hash[ QLZ_HASH_VALUES ]; 125 | #endif 126 | size_t stream_counter; 127 | } qlz_state_decompress; 128 | #endif 129 | 130 | #if defined(__cplusplus) 131 | extern "C" { 132 | #endif 133 | 134 | // Public functions of QuickLZ 135 | size_t qlz_size_decompressed(const char *source); 136 | size_t qlz_size_compressed(const char *source); 137 | size_t qlz_compress(const void *source, char *destination, size_t size, qlz_state_compress *state); 138 | size_t qlz_decompress(const char *source, void *destination, qlz_state_decompress *state); 139 | int qlz_get_setting(int setting); 140 | 141 | #if defined(__cplusplus) 142 | } 143 | #endif 144 | 145 | #endif 146 | -------------------------------------------------------------------------------- /examples/raw_memory.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include "hexdump.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | static void save_raw_memory(const std::string filename) 10 | { 11 | std::cout << "save to " << filename << std::endl; 12 | std::ofstream out(filename, std::ios::binary); 13 | 14 | auto elems = 128; 15 | auto a = new char[ elems ]; 16 | for (auto i = 0; i < elems; i++) { 17 | a[ i ] = i; 18 | } 19 | 20 | out << bits(a); 21 | } 22 | 23 | static void load_raw_memory(const std::string filename) 24 | { 25 | std::cout << "read from " << filename << std::endl; 26 | std::ifstream in(filename); 27 | 28 | auto elems = 128; 29 | auto a = new char[ elems ]; 30 | in >> bits(a); 31 | 32 | hexdump(a, elems); 33 | std::cout << std::endl; 34 | } 35 | 36 | void raw_memory_example(void) 37 | { 38 | std::cout << "raw_memory_example" << std::endl; 39 | std::cout << "==================" << std::endl; 40 | save_raw_memory(std::string("raw_memory_example.bin")); 41 | load_raw_memory(std::string("raw_memory_example.bin")); 42 | } 43 | -------------------------------------------------------------------------------- /examples/set_example.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include 3 | #include 4 | #include 5 | 6 | static void save_set_key_string_value_string(const std::string filename) 7 | { 8 | std::cout << "save to " << filename << std::endl; 9 | std::ofstream out(filename, std::ios::binary); 10 | 11 | std::set< std::string > m; 12 | 13 | m.insert(std::string("key1")); 14 | m.insert(std::string("key2")); 15 | m.insert(std::string("key3")); 16 | m.insert(std::string("key4")); 17 | 18 | out << bits(m); 19 | } 20 | 21 | static void load_set_key_string_value_string(const std::string filename) 22 | { 23 | std::cout << "read from " << filename << std::endl; 24 | std::ifstream in(filename); 25 | 26 | std::set< std::string > m; 27 | 28 | in >> bits(m); 29 | std::cout << std::endl; 30 | 31 | std::cout << "m = " << m.size() << " list-elems { " << std::endl; 32 | for (auto i : m) { 33 | std::cout << " [" << i << "] " << std::endl; 34 | } 35 | std::cout << "}" << std::endl; 36 | std::cout << std::endl; 37 | } 38 | 39 | void set_example(void) 40 | { 41 | std::cout << "set key string, value string" << std::endl; 42 | std::cout << "============================" << std::endl; 43 | save_set_key_string_value_string(std::string("set_of_strings.bin")); 44 | load_set_key_string_value_string(std::string("set_of_strings.bin")); 45 | } 46 | -------------------------------------------------------------------------------- /examples/template_class_example.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include 3 | #include 4 | #include 5 | 6 | template < class T > class MyPoint 7 | { 8 | public: 9 | T x {}; 10 | T y {}; 11 | 12 | MyPoint(void) : x(0), y(0) {}; 13 | 14 | MyPoint(T x, T y) : x(x), y(y) {} 15 | 16 | friend std::ostream &operator<<(std::ostream &out, Bits< const MyPoint & > const my) 17 | { 18 | out << bits(my.t.x) << bits(my.t.y); 19 | return (out); 20 | } 21 | 22 | friend std::istream &operator>>(std::istream &in, Bits< MyPoint & > my) 23 | { 24 | in >> bits(my.t.x) >> bits(my.t.y); 25 | return (in); 26 | } 27 | 28 | friend std::ostream &operator<<(std::ostream &out, const MyPoint &my) 29 | { 30 | out << "(" << my.x << ", " << my.y << ")"; 31 | return (out); 32 | } 33 | }; 34 | 35 | typedef MyPoint< int > IntPoint; 36 | typedef MyPoint< float > FloatPoint; 37 | typedef MyPoint< double > DoublePoint; 38 | 39 | static void save_template_class_example(void) 40 | { 41 | auto filename = ("template_class.bin"); 42 | std::cout << "save to " << filename << std::endl; 43 | std::ofstream out(filename, std::ios::binary); 44 | 45 | out << bits(IntPoint(1, 2)); 46 | out << bits(FloatPoint(1.1, 2.2)); 47 | out << bits(DoublePoint(3.3, 4.4)); 48 | } 49 | 50 | static void load_template_class_example(void) 51 | { 52 | auto filename = ("template_class.bin"); 53 | std::cout << "read from " << filename << std::endl; 54 | std::ifstream in(filename); 55 | 56 | IntPoint a; 57 | FloatPoint b; 58 | DoublePoint c; 59 | 60 | in >> bits(a); 61 | in >> bits(b); 62 | in >> bits(c); 63 | 64 | std::cout << std::endl; 65 | std::cout << "IntPoint: " << a << std::endl; 66 | std::cout << "FloatPoint: " << b << std::endl; 67 | std::cout << "DoublePoint: " << c << std::endl; 68 | std::cout << std::endl; 69 | } 70 | 71 | void template_class_example(void) 72 | { 73 | std::cout << "template_class_example" << std::endl; 74 | std::cout << "======================" << std::endl; 75 | save_template_class_example(); 76 | load_template_class_example(); 77 | } 78 | -------------------------------------------------------------------------------- /examples/unordered_map_example.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include 3 | #include 4 | #include 5 | 6 | static void save_unordered_map_key_string_value_string(const std::string filename) 7 | { 8 | std::cout << "save to " << filename << std::endl; 9 | std::ofstream out(filename, std::ios::binary); 10 | 11 | std::unordered_map< std::string, std::string > m; 12 | 13 | m.insert(std::make_pair(std::string("key1"), std::string("value1"))); 14 | m.insert(std::make_pair(std::string("key2"), std::string("value2"))); 15 | m.insert(std::make_pair(std::string("key3"), std::string("value3"))); 16 | m.insert(std::make_pair(std::string("key4"), std::string("value4"))); 17 | 18 | out << bits(m); 19 | } 20 | 21 | static void load_unordered_map_key_string_value_string(const std::string filename) 22 | { 23 | std::cout << "read from " << filename << std::endl; 24 | std::ifstream in(filename); 25 | 26 | std::unordered_map< std::string, std::string > m; 27 | 28 | in >> bits(m); 29 | std::cout << std::endl; 30 | 31 | std::cout << "m = " << m.size() << " list-elems { " << std::endl; 32 | for (auto i : m) { 33 | std::cout << " [" << i.first << "] = " << i.second << std::endl; 34 | } 35 | std::cout << "}" << std::endl; 36 | std::cout << std::endl; 37 | } 38 | 39 | void unordered_map_example(void) 40 | { 41 | std::cout << "unordered_map key string, value string" << std::endl; 42 | std::cout << "======================================" << std::endl; 43 | save_unordered_map_key_string_value_string(std::string("unordered_map_of_strings.bin")); 44 | load_unordered_map_key_string_value_string(std::string("unordered_map_of_strings.bin")); 45 | } 46 | -------------------------------------------------------------------------------- /examples/zipped_container_example.cpp: -------------------------------------------------------------------------------- 1 | #include "c_plus_plus_serializer.h" 2 | #include "hexdump.h" 3 | #include "quicklz.h" 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | static std::vector< char > read_binary_file(const std::string filename) 13 | { 14 | // binary mode is only for switching off newline translation 15 | std::ifstream file(filename, std::ios::binary); 16 | file.unsetf(std::ios::skipws); 17 | 18 | std::streampos file_size; 19 | file.seekg(0, std::ios::end); 20 | file_size = file.tellg(); 21 | file.seekg(0, std::ios::beg); 22 | 23 | std::vector< char > vec(file_size); 24 | vec.insert(vec.begin(), std::istream_iterator< char >(file), std::istream_iterator< char >()); 25 | return (vec); 26 | } 27 | 28 | static void save_zipper_container(const std::string filename) 29 | { 30 | std::cout << "save to " << filename << std::endl; 31 | std::stringstream out(std::ios::in | std::ios::out | std::ios::binary); 32 | 33 | std::initializer_list< std::string > d1 = {"vec-elem1", "vec-elem2"}; 34 | std::vector< std::string > a(d1); 35 | 36 | std::initializer_list< std::string > d2 = {"list-elem1", "list-elem2"}; 37 | std::list< std::string > b(d2); 38 | 39 | std::array< std::string, 2 > c = {"arr-elem1", "arr-elem2"}; 40 | 41 | // 42 | // 2d array 43 | // 44 | std::array< std::array< char, 2 >, 3 > dd; 45 | dd[ 0 ][ 0 ] = '0'; 46 | dd[ 0 ][ 1 ] = '1'; 47 | dd[ 1 ][ 0 ] = '2'; 48 | dd[ 1 ][ 1 ] = '3'; 49 | dd[ 2 ][ 0 ] = '4'; 50 | dd[ 2 ][ 1 ] = '5'; 51 | 52 | // 53 | // 3d array 54 | // 55 | std::array< std::array< std::array< char, 2 >, 3 >, 4 > ddd; 56 | ddd[ 0 ][ 0 ][ 0 ] = 'a'; 57 | ddd[ 0 ][ 0 ][ 1 ] = 'b'; 58 | ddd[ 0 ][ 1 ][ 0 ] = 'c'; 59 | ddd[ 0 ][ 1 ][ 1 ] = 'd'; 60 | ddd[ 0 ][ 2 ][ 0 ] = 'e'; 61 | ddd[ 0 ][ 2 ][ 1 ] = 'f'; 62 | ddd[ 1 ][ 0 ][ 0 ] = 'g'; 63 | ddd[ 1 ][ 0 ][ 1 ] = 'h'; 64 | ddd[ 1 ][ 1 ][ 0 ] = 'i'; 65 | ddd[ 1 ][ 1 ][ 1 ] = 'j'; 66 | ddd[ 1 ][ 2 ][ 0 ] = 'k'; 67 | ddd[ 1 ][ 2 ][ 1 ] = 'l'; 68 | ddd[ 2 ][ 0 ][ 0 ] = 'm'; 69 | ddd[ 2 ][ 0 ][ 1 ] = 'n'; 70 | ddd[ 2 ][ 1 ][ 0 ] = 'o'; 71 | ddd[ 2 ][ 1 ][ 1 ] = 'p'; 72 | ddd[ 2 ][ 2 ][ 0 ] = 'q'; 73 | ddd[ 2 ][ 2 ][ 1 ] = 'r'; 74 | ddd[ 3 ][ 0 ][ 0 ] = 's'; 75 | ddd[ 3 ][ 0 ][ 1 ] = 't'; 76 | ddd[ 3 ][ 1 ][ 0 ] = 'u'; 77 | ddd[ 3 ][ 1 ][ 1 ] = 'v'; 78 | ddd[ 3 ][ 2 ][ 0 ] = 'w'; 79 | ddd[ 3 ][ 2 ][ 1 ] = 'x'; 80 | 81 | out << bits(a) << bits(b) << bits(c) << bits(dd) << bits(ddd); 82 | 83 | // 84 | // Get the pre compress buffer 85 | // 86 | std::string tmp = out.str(); 87 | out.seekg(0, std::ios::end); 88 | int srclen = out.tellg(); 89 | out.seekg(0, std::ios::beg); 90 | 91 | std::cout << "before compression "; 92 | hexdump(tmp.c_str(), srclen); 93 | 94 | // 95 | // Compress 96 | // 97 | qlz_state_compress *state_compress = (qlz_state_compress *) new char[ sizeof(qlz_state_compress) ]; 98 | 99 | // 100 | // http://www.quicklz.com/manual.html 101 | // 102 | // The destination buffer must be at least size + 400 bytes large because 103 | // incompressible data may increase in size. 104 | // 105 | auto dst = new char[ srclen + 400 /* qlz header */ ]; 106 | auto dstlen = qlz_compress(tmp.c_str(), dst, srclen, state_compress); 107 | 108 | // 109 | // Dump the post compress buffer 110 | // 111 | std::cout << "after compression "; 112 | hexdump(dst, dstlen); 113 | 114 | // 115 | // Save the post compress buffer 116 | // 117 | auto ofile = fopen(filename.c_str(), "wb"); 118 | fwrite(dst, dstlen, 1, ofile); 119 | fclose(ofile); 120 | delete[] dst; 121 | delete[] state_compress; 122 | } 123 | 124 | static void load_zipper_container(const std::string filename) 125 | { 126 | // 127 | // Read to a vector and then copy to a C buffer for qlz to use 128 | // 129 | auto vec = read_binary_file(filename); 130 | 131 | #ifdef MAKE_COPY 132 | auto src = (char *) new char[ vec.size() ]; 133 | std::copy(vec.begin(), vec.end(), src); 134 | #else 135 | // 136 | // Avoid copying, a bit hacky, but should work 137 | // 138 | auto src = vec.data(); 139 | #endif 140 | 141 | // 142 | // Decompress 143 | // 144 | auto dstlen = qlz_size_decompressed(src); 145 | auto dst = new char[ dstlen ]; 146 | qlz_state_decompress *state_decompress = (qlz_state_decompress *) new char[ sizeof(qlz_state_decompress) ]; 147 | auto newlen = qlz_decompress(src, dst, state_decompress); 148 | 149 | std::cout << "decompressed as "; 150 | hexdump(dst, newlen); 151 | 152 | std::string s((const char *) dst, (size_t) newlen); 153 | std::istringstream in(s); 154 | 155 | std::vector< std::string > a; 156 | std::list< std::string > b; 157 | std::array< std::string, 2 > c; 158 | std::array< std::array< char, 2 >, 3 > dd; 159 | std::array< std::array< std::array< char, 2 >, 3 >, 4 > ddd; 160 | 161 | in >> bits(a) >> bits(b) >> bits(c) >> bits(dd) >> bits(ddd); 162 | 163 | std::cout << std::endl; 164 | std::cout << "a: std::vector " << a.size() << " elems: "; 165 | for (auto elem : a) { 166 | std::cout << "[" << elem << "] "; 167 | } 168 | std::cout << std::endl; 169 | 170 | std::cout << "b: std::list " << b.size() << " elems: "; 171 | for (auto elem : b) { 172 | std::cout << "[" << elem << "] "; 173 | } 174 | std::cout << std::endl; 175 | 176 | std::cout << "c: std::array " << c.size() << " elems: "; 177 | for (auto elem : c) { 178 | std::cout << "[" << elem << "] "; 179 | } 180 | std::cout << std::endl; 181 | 182 | std::cout << "2d: std::array, 3> :" << std::endl; 183 | for (auto x = 0; x < 3; x++) { 184 | for (auto y = 0; y < 2; y++) { 185 | std::cout << " 2d[" << x << "][" << y << "] = " << dd[ x ][ y ] << std::endl; 186 | } 187 | } 188 | std::cout << std::endl; 189 | 190 | std::cout << "3d: std::array, 3>, 4> :" << std::endl; 191 | for (auto x = 0; x < 4; x++) { 192 | for (auto y = 0; y < 3; y++) { 193 | for (auto z = 0; z < 2; z++) { 194 | std::cout << " 3d[" << x << "][" << y << "][" << z << "] = " << ddd[ x ][ y ][ z ] << std::endl; 195 | } 196 | } 197 | } 198 | std::cout << std::endl; 199 | 200 | delete[] state_decompress; 201 | #ifdef MAKE_COPY 202 | delete[] src; 203 | #endif 204 | delete[] dst; 205 | } 206 | 207 | void zipper_container_example(void) 208 | { 209 | std::cout << "zipper_container_example" << std::endl; 210 | std::cout << "========================" << std::endl; 211 | save_zipper_container(std::string("zipper_container_example.bin")); 212 | load_zipper_container(std::string("zipper_container_example.bin")); 213 | } 214 | --------------------------------------------------------------------------------