Skip to content

Commit

Permalink
latest from "nitro"
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Jan 10, 2022
1 parent 26ba1a9 commit 571ab48
Show file tree
Hide file tree
Showing 17 changed files with 495 additions and 38 deletions.
4 changes: 4 additions & 0 deletions externals/nitro/modules/c++/nitf-c++.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<ClCompile Include="nitf\source\DownSampler.cpp" />
<ClCompile Include="nitf\source\Extensions.cpp" />
<ClCompile Include="nitf\source\Field.cpp" />
<ClCompile Include="nitf\source\FieldDescriptor.cpp" />
<ClCompile Include="nitf\source\FieldWarning.cpp" />
<ClCompile Include="nitf\source\FileHeader.cpp" />
<ClCompile Include="nitf\source\FileSecurity.cpp" />
Expand Down Expand Up @@ -62,6 +63,7 @@
<ClCompile Include="nitf\source\SegmentSource.cpp" />
<ClCompile Include="nitf\source\SegmentWriter.cpp" />
<ClCompile Include="nitf\source\SubWindow.cpp" />
<ClCompile Include="nitf\source\TestingTest.cpp" />
<ClCompile Include="nitf\source\TextSegment.cpp" />
<ClCompile Include="nitf\source\TextSubheader.cpp" />
<ClCompile Include="nitf\source\TRE.cpp" />
Expand Down Expand Up @@ -96,6 +98,7 @@
<ClInclude Include="nitf\include\nitf\exports.hpp" />
<ClInclude Include="nitf\include\nitf\Extensions.hpp" />
<ClInclude Include="nitf\include\nitf\Field.hpp" />
<ClInclude Include="nitf\include\nitf\FieldDescriptor.hpp" />
<ClInclude Include="nitf\include\nitf\FieldWarning.hpp" />
<ClInclude Include="nitf\include\nitf\FileHeader.hpp" />
<ClInclude Include="nitf\include\nitf\FileSecurity.hpp" />
Expand Down Expand Up @@ -135,6 +138,7 @@
<ClInclude Include="nitf\include\nitf\SegmentWriter.hpp" />
<ClInclude Include="nitf\include\nitf\SubWindow.hpp" />
<ClInclude Include="nitf\include\nitf\System.hpp" />
<ClInclude Include="nitf\include\nitf\TestingTest.hpp" />
<ClInclude Include="nitf\include\nitf\TextSegment.hpp" />
<ClInclude Include="nitf\include\nitf\TextSubheader.hpp" />
<ClInclude Include="nitf\include\nitf\TRE.hpp" />
Expand Down
12 changes: 12 additions & 0 deletions externals/nitro/modules/c++/nitf-c++.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@
<ClCompile Include="nitf\source\FieldWarning.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="nitf\source\FieldDescriptor.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="nitf\source\TestingTest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
Expand Down Expand Up @@ -392,5 +398,11 @@
<ClInclude Include="nitf\include\nitf\TREField.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="nitf\include\nitf\FieldDescriptor.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="nitf\include\nitf\TestingTest.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
7 changes: 6 additions & 1 deletion externals/nitro/modules/c++/nitf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ coda_add_module(
source/DownSampler.cpp
source/Extensions.cpp
source/Field.cpp
source/FieldDescriptor.cpp
source/FieldWarning.cpp
source/FileHeader.cpp
source/FileSecurity.cpp
Expand Down Expand Up @@ -61,6 +62,7 @@ coda_add_module(
source/SegmentWriter.cpp
source/SubWindow.cpp
source/TRE.cpp
source/TestingTest.cpp
source/TextSegment.cpp
source/TextSubheader.cpp
source/Utils.cpp
Expand Down Expand Up @@ -90,8 +92,11 @@ coda_add_tests(
test_image_blocker.cpp
test_image_segment_blank_nm_compression.cpp
test_image_segment_computer.cpp
test_image_writer.cpp
test_nitf_buffer_list.cpp
test_tre_mods++.cpp)
test_tre_mods++.cpp
test_tre_create++.cpp
test_tre_read.cpp)

add_executable(show_nitf++ apps/show_nitf++.cpp)
target_link_libraries(show_nitf++ PRIVATE nitf-c++)
Expand Down
3 changes: 3 additions & 0 deletions externals/nitro/modules/c++/nitf/include/nitf/Field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include <string>
#include <limits>
#include <type_traits>
#include <std/cstddef> // std::byte

#include <nitf/Field.h>

Expand Down Expand Up @@ -185,6 +187,7 @@ class NITRO_NITFCPP_API Field /*final*/ : public nitf::Object<nitf_Field> // no
Field(size_t length, FieldType type);

//! Set native object
using native_t = nitf_Field;
Field(nitf_Field * field)
{
setNative(field);
Expand Down
92 changes: 92 additions & 0 deletions externals/nitro/modules/c++/nitf/include/nitf/FieldDescriptor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* =========================================================================
* This file is part of NITRO
* =========================================================================
*
* (C) Copyright 2004 - 2014, MDA Information Systems LLC
* (C) Copyright 2022 , Maxar Technologies, Inc.
*
* NITRO is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, If not,
* see <http://www.gnu.org/licenses/>.
*
*/

#ifndef NITRO_nitf_FieldDescriptor_hpp_INCLUDED_
#define NITRO_nitf_FieldDescriptor_hpp_INCLUDED_
#pragma once

#include <string>
#include <vector>
#include <std/span>
#include <stdexcept>

#include "nitf/FieldDescriptor.h"
#include "nitf/Field.hpp"
#include "nitf/Object.hpp"

namespace nitf
{
// wrap nitf_StructFieldDescriptor
class FieldDescriptor final
{
const nitf_StructFieldDescriptor* pNative = nullptr;
public:
enum class Type
{
Field = NITF_FieldType_Field, // nitf_Field*
FileSecurity = NITF_FieldType_FileSecurity, // nitf_FileSecurity*
ComponentInfo = NITF_FieldType_ComponentInfo, // nitf_ComponentInfo*
PComponentInfo = NITF_FieldType_PComponentInfo, // nitf_ComponentInfo** aka nitf_PComponentInfo*
Extensions = NITF_FieldType_Extensions, // nitf_Extensions*
};
FieldDescriptor(const nitf_StructFieldDescriptor& native) noexcept : pNative(&native) {}
~FieldDescriptor() = default;
FieldDescriptor(const FieldDescriptor&) = default;
FieldDescriptor& operator=(const FieldDescriptor&) = default;
FieldDescriptor(FieldDescriptor&&) = default;
FieldDescriptor& operator=(FieldDescriptor&&) = default;

Type type() const noexcept
{
return static_cast<Type>(pNative->type);
}
std::string name() const
{
return pNative->name;
}
size_t offset() const noexcept
{
return pNative->offset;
}

template<typename TObject>
Field getField(const TObject& object) const
{
if (type() != Type::Field)
{
throw std::logic_error("Descriptor '" + name() + "' is not a 'Field' type.");
}
return fromNativeOffset<Field>(object, offset());
}
};

extern std::vector<FieldDescriptor> getFieldDescriptors(std::span<const nitf_StructFieldDescriptor>);
template<size_t N>
inline std::vector<FieldDescriptor> getFieldDescriptors(const nitf_StructFieldDescriptor(&descriptors)[N])
{
// constexpr auto extent = std::extent<decltype(nitf_testing_Test1b_fields)>::value;
const std::span<const nitf_StructFieldDescriptor> s(descriptors, N);
return getFieldDescriptors(s);
}
}
#endif // NITRO_nitf_FieldDescriptor_hpp_INCLUDED_
5 changes: 4 additions & 1 deletion externals/nitro/modules/c++/nitf/include/nitf/FileHeader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ DECLARE_CLASS(FileHeader)
FileHeader & operator=(const FileHeader & x);

//! Set native object
FileHeader(nitf_FileHeader * x);
using native_t = nitf_FileHeader;
FileHeader(native_t * x);

//! Constructor
FileHeader() noexcept(false);
Expand Down Expand Up @@ -195,6 +196,8 @@ DECLARE_CLASS(FileHeader)
//! Set the extendedSection
void setExtendedSection(nitf::Extensions value);

//std::vector<nitf::Field> getFields() const;

private:
mutable nitf_Error error{};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ DECLARE_CLASS(FileSecurity)
FileSecurity & operator=(const FileSecurity & x);

//! Set native object
FileSecurity(nitf_FileSecurity * x);
using native_t = nitf_FileSecurity;
FileSecurity(native_t* x);

//! Constructor
FileSecurity() noexcept(false);
Expand Down
51 changes: 49 additions & 2 deletions externals/nitro/modules/c++/nitf/include/nitf/Object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

#include <assert.h>

#include <type_traits>
#include <std/cstddef> // std::byte

#include "nitf/coda-oss.hpp"
#include "nitf/Handle.hpp"
#include "nitf/exports.hpp"
Expand Down Expand Up @@ -103,7 +106,7 @@ class NITRO_NITFCPP_API Object
}

public:
virtual ~Object() noexcept(false) { releaseHandle(); }
virtual ~Object() { releaseHandle(); }

//! Is the object valid (native object not null)?
virtual bool isValid() const noexcept
Expand All @@ -130,7 +133,7 @@ class NITRO_NITFCPP_API Object
}

//! Get native object
virtual T * getNativeOrThrow() const
virtual T * getNativeOrThrow() const noexcept(false)
{
T* val = getNative();
if (val)
Expand Down Expand Up @@ -231,4 +234,48 @@ void operator()(Package_##_##Name_ * nativeObject) noexcept(false) override \
#define NITRO_DECLARE_CLASS_NITF(_Name) DECLARE_CLASS_IN(_Name, nitf)
#define DECLARE_CLASS(_Name) NITRO_DECLARE_CLASS_NITF(_Name)

namespace nitf
{
// Refer to a field in a native structure without have to explicitly name it; rather
// use the offset and pointer math. Besides making it easier to refer to fields
// without naming them, it makes it easier to iterate over all of the fields in
// a struct.

// fieldOffset is offsetof(TNative, <field>), e.g., offsetof(nitf_TextSubheader, filePartType)
template<typename TReturn, typename TNative>
inline TReturn fromNativeOffset_(const TNative& native, size_t fieldOffset) noexcept
{
// This should be a C struct
static_assert(std::is_standard_layout<TNative>::value, "!std::is_standard_layout<>");

const void* const pNative_ = &native;
auto pNativeBytes = static_cast<const std::byte*>(pNative_); // for pointer math
pNativeBytes += fieldOffset;
const void* const pAddressOfField_ = pNativeBytes;

auto pAddressOfField = static_cast<const TReturn*>(pAddressOfField_);
if (pAddressOfField != nullptr) // code-analysis diagnostic
{
return *pAddressOfField;
}
return nullptr;
}
template<typename TReturn, typename TObject>
inline TReturn fromNativeOffset(const TObject& object, size_t fieldOffset) noexcept(false)
{
auto& native = *(object.getNativeOrThrow()); // e.g., nitf_testing_Test1a&

// Be sure this is one of our wrapper objects; if it is, it will have:
// using native_t = nitf_<C type>;
using native_object_t = typename TObject::native_t; // e.g., nitf_testing_Test1a
using get_native_t = typename std::remove_reference<decltype(native)>::type;
static_assert(std::is_same<native_object_t, get_native_t>::value, "!std::is_same<>");

using native_t = typename TReturn::native_t; // e.g., nitf_Field
auto const pField = fromNativeOffset_<native_t*>(native, fieldOffset);
return TReturn(pField); // e.g., nitf::Field(pNativeField)
}
#define nitf_offsetof(name) offsetof(native_t, name)
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ DECLARE_CLASS(SubWindow)
SubWindow(uint32_t rows, uint32_t cols, uint32_t* bands = nullptr, uint32_t numBands = 0);

//! Destructor
~SubWindow() noexcept(false);
~SubWindow();

uint32_t getStartRow() const;
uint32_t getNumRows() const;
Expand Down
8 changes: 4 additions & 4 deletions externals/nitro/modules/c++/nitf/include/nitf/TRE.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
*
*/

#ifndef NITF_TRE_hpp_INCLUDED_
#define NITF_TRE_hpp_INCLUDED_
#ifndef NITRO_nitf_TRE_hpp_INCLUDED_
#define NITRO_nitf_TRE_hpp_INCLUDED_
#pragma once

#include <string>
#include <cstddef>
#include <type_traits>
#include <std/type_traits>
#include <stdexcept>
#include <utility>

Expand Down Expand Up @@ -372,4 +372,4 @@ DECLARE_CLASS(TRE)
mutable nitf_Error error{};
};
}
#endif // NITF_TRE_hpp_INCLUDED_
#endif // NITRO_nitf_TRE_hpp_INCLUDED_
Loading

0 comments on commit 571ab48

Please sign in to comment.